| 1 |
var insertionQ = (function () { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
var sequence = 100, |
| 5 |
isAnimationSupported = false, |
| 6 |
animation_string = 'animationName', |
| 7 |
keyframeprefix = '', |
| 8 |
domPrefixes = 'Webkit Moz O ms Khtml'.split(' '), |
| 9 |
pfx = '', |
| 10 |
elm = document.createElement('div'), |
| 11 |
options = { |
| 12 |
strictlyNew: true, |
| 13 |
timeout: 20 |
| 14 |
}; |
| 15 |
|
| 16 |
if (elm.style.animationName) { |
| 17 |
isAnimationSupported = true; |
| 18 |
} |
| 19 |
|
| 20 |
if (isAnimationSupported === false) { |
| 21 |
for (var i = 0; i < domPrefixes.length; i++) { |
| 22 |
if (elm.style[domPrefixes[i] + 'AnimationName'] !== undefined) { |
| 23 |
pfx = domPrefixes[i]; |
| 24 |
animation_string = pfx + 'AnimationName'; |
| 25 |
keyframeprefix = '-' + pfx.toLowerCase() + '-'; |
| 26 |
isAnimationSupported = true; |
| 27 |
break; |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
function listen(selector, callback) { |
| 34 |
var styleAnimation, animationName = 'insQ_' + (sequence++); |
| 35 |
|
| 36 |
var eventHandler = function (event) { |
| 37 |
if (event.animationName === animationName || event[animation_string] === animationName) { |
| 38 |
if (!isTagged(event.target)) { |
| 39 |
callback(event.target); |
| 40 |
} |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
styleAnimation = document.createElement('style'); |
| 45 |
styleAnimation.innerHTML = '@' + keyframeprefix + 'keyframes ' + animationName + ' { from { outline: 1px solid transparent } to { outline: 0px solid transparent } }' + |
| 46 |
"\n" + selector + ' { animation-duration: 0.001s; animation-name: ' + animationName + '; ' + |
| 47 |
keyframeprefix + 'animation-duration: 0.001s; ' + keyframeprefix + 'animation-name: ' + animationName + '; ' + |
| 48 |
' } '; |
| 49 |
|
| 50 |
document.head.appendChild(styleAnimation); |
| 51 |
|
| 52 |
var bindAnimationLater = setTimeout(function () { |
| 53 |
document.addEventListener('animationstart', eventHandler, false); |
| 54 |
document.addEventListener('MSAnimationStart', eventHandler, false); |
| 55 |
document.addEventListener('webkitAnimationStart', eventHandler, false); |
| 56 |
|
| 57 |
}, options.timeout); |
| 58 |
|
| 59 |
return { |
| 60 |
destroy: function () { |
| 61 |
clearTimeout(bindAnimationLater); |
| 62 |
if (styleAnimation) { |
| 63 |
document.head.removeChild(styleAnimation); |
| 64 |
styleAnimation = null; |
| 65 |
} |
| 66 |
document.removeEventListener('animationstart', eventHandler); |
| 67 |
document.removeEventListener('MSAnimationStart', eventHandler); |
| 68 |
document.removeEventListener('webkitAnimationStart', eventHandler); |
| 69 |
} |
| 70 |
}; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
function tag(el) { |
| 75 |
el.QinsQ = true; |
| 76 |
} |
| 77 |
|
| 78 |
function isTagged(el) { |
| 79 |
return (options.strictlyNew && (el.QinsQ === true)); |
| 80 |
} |
| 81 |
|
| 82 |
function topmostUntaggedParent(el) { |
| 83 |
if (isTagged(el.parentNode)) { |
| 84 |
return el; |
| 85 |
} else { |
| 86 |
return topmostUntaggedParent(el.parentNode); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
function tagAll(e) { |
| 91 |
tag(e); |
| 92 |
e = e.firstChild; |
| 93 |
for (; e; e = e.nextSibling) { |
| 94 |
if (e !== undefined && e.nodeType === 1) { |
| 95 |
tagAll(e); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function catchInsertions(selector, callback) { |
| 102 |
var insertions = []; |
| 103 |
|
| 104 |
var sumUp = (function () { |
| 105 |
var to; |
| 106 |
return function () { |
| 107 |
clearTimeout(to); |
| 108 |
to = setTimeout(function () { |
| 109 |
insertions.forEach(tagAll); |
| 110 |
callback(insertions); |
| 111 |
insertions = []; |
| 112 |
}, 10); |
| 113 |
}; |
| 114 |
})(); |
| 115 |
|
| 116 |
return listen(selector, function (el) { |
| 117 |
if (isTagged(el)) { |
| 118 |
return; |
| 119 |
} |
| 120 |
tag(el); |
| 121 |
var myparent = topmostUntaggedParent(el); |
| 122 |
if (insertions.indexOf(myparent) < 0) { |
| 123 |
insertions.push(myparent); |
| 124 |
} |
| 125 |
sumUp(); |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
var exports = function (selector) { |
| 131 |
if (isAnimationSupported && selector.match(/[^{}]/)) { |
| 132 |
|
| 133 |
if (options.strictlyNew) { |
| 134 |
tagAll(document.body); |
| 135 |
} |
| 136 |
return { |
| 137 |
every: function (callback) { |
| 138 |
return listen(selector, callback); |
| 139 |
}, |
| 140 |
summary: function (callback) { |
| 141 |
return catchInsertions(selector, callback); |
| 142 |
} |
| 143 |
}; |
| 144 |
} else { |
| 145 |
return false; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
exports.config = function (opt) { |
| 151 |
for (var o in opt) { |
| 152 |
if (opt.hasOwnProperty(o)) { |
| 153 |
options[o] = opt[o]; |
| 154 |
} |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
return exports; |
| 159 |
})(); |
| 160 |
|
| 161 |
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
| 162 |
module.exports = insertionQ; |
| 163 |
} |