-
Notifications
You must be signed in to change notification settings - Fork 7
/
hoverable.js
410 lines (332 loc) · 12 KB
/
hoverable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Hoverable: declarative UI components | 2K, 0 dependency, IE8 compatible
*
* latest version, README and documentation available on Github:
* https://github.com/louisremi/Activable
*
* Copyright 2012 @louis_remi
* Licensed under the MIT license.
*
* Want to show some love?
* Send me music http://www.amazon.co.uk/wishlist/HNTU0468LQON
*
*/
(function(window,document,Math,undefined) {
var delayed = {},
currentTarget = {},
isStandard = "addEventListener" in window,
_addEventListener = isStandard ? "addEventListener" : "attachEvent",
prefix = isStandard ? "" : "on",
_ransition = "ransition",
html = document.documentElement,
prfx,
prefixes = {
"WebkitT": "webkitTransitionEnd",
"MozT": "transitionend",
// transitions are unprefixed in IE10
//"msT": "msTransitionEnd",
// Previous Opera had really strange behaviors
//"OT": "oTransitionEnd",
"t": "transitionend"
},
transition,
transitionend;
// transition feature detection
for ( prfx in prefixes ) {
if ( ( transition = prfx + _ransition ) in html.style ) {
transitionend = prefixes[ prfx ];
break;
}
transition = undefined;
}
transition && document[ _addEventListener ]( transitionend, transitionendHandler, false );
// event delegater
document[ _addEventListener ]( prefix + "mouseover", intentHandler, false );
document[ _addEventListener ]( prefix + "mouseout", intentHandler, false );
function Hoverable( elem ) {
this[0] = typeof elem == "string" ? exa( document, elem )[0] : elem;
}
// Hoverable API
Hoverable.prototype = {
enter: function( index ) {
hoverHandler({ target: this[0], type: "add" });
},
leave: function() {
hoverHandler({ target: this[0], type: "remove" });
},
on: function( type, handler ) {
var expando = this[0].hoverExpando || {};
if ( type == "enter" || type == "both" ) {
( expando.enterHandlers || ( expando.enterHandlers = [] ) ).push( handler );
}
if ( type == "leave" || type == "both" ) {
( expando.leaveHandlers || ( expando.leaveHandlers = [] ) ).push( handler );
}
this[0].hoverExpando = expando;
},
off: function( type, handler ) {
var expando;
if ( ( expando = this[0].hoverExpando ) ) {
if ( type == "enter" || type == "both" ) {
off( expando.enterHandlers, handler );
}
if ( type == "leave" || type == "both" ) {
off( expando.leaveHandlers, handler );
}
}
},
toggle: function( enterHandler, leaveHandler ) {
if ( enterHandler && leaveHandler ) {
Hoverable.on( this[0], "enter", enterHandler );
Hoverable.on( this[0], "leave", leaveHandler );
} else {
hoverHandler({ target: this[0], type: "toggle" });
}
}
};
window.Hoverable = window.H = function( elem ) {
return new Hoverable( elem );
};
function intentHandler( event ) {
!event && ( event = window.event );
!event.target && ( event.target = event.srcElement );
!event.relatedTarget && ( event.relatedTarget = event.toElement );
!event.type && ( event.type = event.type );
// cancel mouseover as long as events are captured
clearTimeout( delayed.mouseover );
if ( event.type == "mouseover" ) {
currentTarget.mouseover = event.target;
// if mouseover is captured before mouseout, block subsequent mouseout
if ( !delayed.mouseout ) {
delayed.mouseout = 1;
}
// type == "mouseout"
} else {
currentTarget.mouseover = undefined;
if ( !delayed.mouseout ) {
currentTarget.mouseout = event.target;
}
}
// reject all mouseout events as long as a mouseout handler is in the queue
if ( event.type == "mouseover" || !delayed.mouseout ) {
// delay the handler
delayed[ event.type ] = setTimeout(function() {
hoverHandler( event, currentTarget[ event.type == "mouseover" ? "mouseout" : "mouseover" ] );
// reset .mouseout to make sure it's not stuck at "1"
currentTarget.mouseout = delayed.mouseout = undefined;
}, event.type == "mouseover" ? 150 : 1 );
}
}
function hoverHandler( event, related ) {
var target = event.target,
descendants = [target],
dataHoverable,
dataDelegate,
dataTrigger,
delegater,
trigger,
internalTarget,
isHover,
verb;
while ( target && target.ownerDocument && ( dataHoverable = target.getAttribute( "data-hoverable" ) ) == undefined ) {
descendants.unshift( target = target.parentNode );
}
if ( dataHoverable == undefined ) { return; }
dataDelegate = target.getAttribute("data-delegate");
dataTrigger = target.getAttribute("data-trigger");
target = [ target, 0 ];
// if the element is an ul, delegation is being used;
if ( dataDelegate || dataDelegate != "" && target[0].nodeName == "UL" ) {
delegater = target[0];
target = dataDelegate ?
queryList( descendants, dataDelegate ) :
[ descendants[1], 1 ];
}
// refresh the list of descendants
descendants.splice( 0, target[1] + 1 );
target = target[0];
while ( related && related != target && related.ownerDocument ) {
related = related.parentNode;
}
if ( related == target ) { return; }
// make sure we have a target and the trigger was hovered if it exists
if ( target && ( !dataTrigger || ( trigger = queryList( descendants, dataTrigger )[0] ) ) ) {
isHover = c( target, "has", "hover" );
// also ignore programatically triggered actions when they don't change the current state
if ( ( isHover && event.type != "add" ) || ( !isHover && event.type != "remove" ) ) {
verb = ( event.type == "toggle" && !ishover ) || event.type == "mouseover" || event.type == "add" ?
"add":
"remove";
// activate or deactivate the target and the internal target
make( [ target, delegater, trigger || target ], verb, verb == "add" ? "enter" : "leave", "hover", event );
preventDefault( event );
}
}
}
function queryList( nodeList, selector ) {
var i = nodeList.length;
// walk the list in reverse order, to find the deepest matching element
while ( i-- ) {
if( matches( nodeList[i], selector ) ) {
return [ nodeList[i], i ];
}
}
return [];
}
function findInternalTarget( elem, internalTarget ) {
return ( internalTarget = elem.getAttribute( "href" ) ) &&
/^#./.test( internalTarget ) &&
exa( document, internalTarget )[0];
}
function preventDefault( event ) {
event.type == "click" && isStandard ?
event.preventDefault() :
event.returnValue = false;
}
function off( handlers, handler ) {
if( handler ) {
i = handlers.length;
while ( i-- ) {
if ( handlers[i] == handler ) {
handlers.splice( i, 1 );
}
}
} else {
handlers.length = 0;
}
}
function make( targets, verb, type, action, event ) {
var target = targets[0],
attributeHolder = targets[1] || target,
dataAuto, handlers;
targets[2] = findInternalTarget( targets[2] );
// content rendering
renderContent( target, attributeHolder, verb, action );
// change class names, after starting a transition if necessary
if ( transition && ( dataAuto = attributeHolder.getAttribute("data-auto") ) ) {
if ( targets[2] ) {
autoDim( targets[2], dataAuto, verb, action );
}
autoDim( target, dataAuto, verb, action );
} else {
if ( targets[2] ) {
c( targets[2], verb, action );
}
c( target, verb, action );
}
// bubble the event up in the tree
while ( target && target.ownerDocument ) {
handlers = ( target[ action + "Expando" ] || {} )[ type + "Handlers" ] || [];
i = handlers.length;
while ( i-- ) {
if ( !handlers[i].call(
targets[0], event, type, targets[2]
) ) {
return;
}
}
target = target.parentNode;
}
}
function renderContent( target, attributeHolder, verb, action ) {
var dataTemplate, dataContent, dataPlacement, title,
tmp, targetOffset,
rendered;
// We'll try to display a content
if ( ( verb == "add" ) && ( dataContent = target.getAttribute("data-content") || ( title = target.title ) ) ) {
dataPlacement = attributeHolder.getAttribute("data-placement") || "right";
// content is an internal link
if ( /^#/.test( dataContent ) ) {
rendered = exa( document, dataContent )[0];
// content is JSON and must be rendered
} else if ( ( window.Mustache || H ).render && ( dataTemplate = attributeHolder.getAttribute("data-template") ) ) {
// parse the content (create a simple {title:...} object if not jsonable)
try {
dataContent = JSON.parse( dataContent );
} catch (e) {
dataContent = {
title: dataContent
}
// if the title contains a ":", add "header" and "body" properties
if ( ( tmp = dataContent.title.split(":") ).length > 1 ) {
dataContent.header = tmp.shift();
dataContent.body = tmp.join(":");
}
}
// render the template
rendered = ( window.Mustache || H ).render( exa( document, dataTemplate )[0].innerHTML, dataContent );
tmp = document.createElement("div");
tmp.innerHTML = rendered;
// add a reference to the expando
( target[ action + "Expando" ] || ( target[ action + "Expando" ] = {} ) ).rendered =
rendered = exa( tmp, "*" )[0];
document.body.appendChild( rendered );
c( rendered, "add", dataPlacement );
}
// place the content
if ( rendered ) {
// remove target title
if ( title ) {
target.setAttribute( "data-content", title );
target.title = null;
}
rendered.style.position = "absolute";
rendered.style.display = "block";
rendered.style.opacity = 0;
targetOffset = getOffset( target );
if ( dataPlacement == "top" || dataPlacement == "bottom" ) {
// center the render content and the target on the same vertical axe
rendered.style.left = targetOffset.left + ( target.offsetWidth - rendered.offsetWidth )/2 + "px";
rendered.style.top = targetOffset.top +
( dataPlacement == "top" ? -rendered.offsetHeight : target.offsetHeight ) + "px";
// dataPlacement == "left" || dataPlacement == "right"
} else {
// center the render content and the target on the same horizontal axe
rendered.style.top = targetOffset.top + ( target.offsetHeight - rendered.offsetHeight )/2 + "px";
rendered.style.left = targetOffset.left +
( dataPlacement == "left" ? -rendered.offsetWidth : target.offsetWidth ) + "px";
}
rendered.style.opacity = "";
}
// We'll hide the rendered content
} else if ( rendered = ( target[ action + "Expando" ] || {} ).rendered ) {
document.body.removeChild( rendered );
}
}
function autoDim( elem, dimension, type, action ) {
var from, to;
from = getComputedStyle( elem )[ dimension ];
// transitions should be temporarily disabled for Chrome
elem.style[ transition ] = "none";
c( elem, typeToVerb[ type ], action );
// make sure the inline style is empty (not the case during a transition)
elem.style[ dimension ] = "";
to = getComputedStyle( elem )[ dimension ];
elem.style[ dimension ] = from;
// computed value has to be accessed to make sure the browser took it into account
getComputedStyle( elem )[ dimension ];
elem.style[ transition ] = "";
elem.style[ dimension ] = to;
}
// remove inline style on transition end
function transitionendHandler( event ) {
var propertyName;
if ( ( propertyName = event.target.getAttribute("data-auto") ) == event.propertyName ) {
// Chrome is having this bug again
event.target.style[ transition ] = "none";
// This line is enough to get the job done in Firefox
event.target.style[ propertyName ] = "";
// the following 2 lines are part of workaround for Chrome as well
getComputedStyle( event.target )[ transition ];
event.target.style[ transition ] = "";
}
}
// c, an expressive className manipulation library
function c(e,v,n,c,r){r=e[c="className"].replace(RegExp(" *\\b"+n+"\\b","g"),"");return"has"==v?r!=e[c]:e[c]={add:1,toggle:r==e[c]}[v]?r+" "+n:r}
// matches, check if an element matches a CSS selector | IE8 compatible
function matches(a,d,b,c){for(b=exa(a.parentNode||document,d),c=0;d=b[c++];)if(d==a)return!0;return!1}
// enhanced querySelecorAll, understand queries starting with ">"
function exa(a,b,c,d){b.indexOf(">")||(b=((c=a.parentNode)?"#"+(a.id||(a.id=d="a"+(1E6*Math.random()|0))):"html")+b);c=(c||a).querySelectorAll(b);d&&(a.id="");return c}
// return offset of a given element
function getOffset(e){var r=e.getBoundingClientRect(),b=document.body,d=document.documentElement;return{top:r.top+(window.pageYOffset||d.scrollTop)-(d.clientTop||b.clientTop||0),left:r.left+(window.pageXOffset||d.scrollLeft)-(d.clientLeft||b.clientLeft||0)}}
})(window,document,Math);