forked from darcyclarke/Watch.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.min.js
15 lines (14 loc) · 1.83 KB
/
watch.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Watch Event Listener
*
* @author Darcy Clarke
*
* Copyright (c) 2013 Darcy Clarke
* Dual licensed under the MIT and GPL licenses.
*
* Usage:
* watch(element, 'width height', function(){
* console.log(this.style.width, this.style.height);
* });
*/
window.watch=function(){(function(e,t,n,r){function f(e){var t=[];for(var n=e.length>>>0;n--;){t[n]=e[n]}return t}var i=document.createElement("div");var s=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;var o=window.CustomEvent||function(){return arguments||{}};var u=function(e,t){e="on"+e;var n=e in t;if(!n){t.setAttribute(e,"return;");n=typeof t[e]=="function"}return n};var a=function(e,t,n){if(e.addEventListener){e.addEventListener(t,n,false)}else if(e.attachEvent){e["e"+t+n]=n;e[t+n]=function(){e["e"+t+n](window.event)};e.attachEvent("on"+t,e[t+n])}};var l=function(e){var t=this;for(var n=0;n<t.watching.length;n++){var r=t.watching[n];var i=false;var s;for(var u=0;u<r.props.length;u++){s=e.attributes[r.props[u]]||e.style[r.props[u]];if(r.vals[u]!=s){r.vals[u]=s;i=true;break}}if(i&&r.callback)r.callback.call(t[0],new o("AttrChange"))}};if(!e||typeof e!=="object")return;e=f(e);if(typeof n=="function"){r=n;n={}}if(typeof r!="function")r=function(){};n.throttle=n.throttle||10;for(var c=0;c<e.length;c++){var h=this;var p=e[c];var d={props:t.split(" "),vals:[],changed:[],callback:r};for(var v=0;v<d.props.length;v++){d.vals[v]=p.attributes[d.props[v]]||p.style[d.props[v]];d.changed[v]=false}if(!h.watching)h.watching=[];h.watching.push(d);if(s){var m=new s(function(e){e.forEach(function(e){r.call(p,e)})});m.observe(p,{subtree:false,attributes:true})}else if(u("DOMAttrModified",i)){a(p,"DOMAttrModified",r)}else if(u("propertychange",i)){a(p,"propertychange",r)}else{setInterval(function(){l.call(h,p)},n.throttle)}}}).apply(arguments[0],arguments)};