-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emily.min.js
30 lines (28 loc) · 9.49 KB
/
Emily.min.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
/*
Emily 1.8.1 http://flams.github.com/emily
The MIT License (MIT)
Copyright (c) 2012-2013 Olivier Scherrer <[email protected]>
*/
define("Tools",[],function(){function f(a,b,c){var d,e;if(b)return b.forEach(function(b,g){var h=c(b,a);if(h>=0&&(typeof e=="undefined"||h<e))e=h,d=g}),d}return{getGlobal:function(){return function(){return this}.call(null)},mixin:function(a,b,c){this.loop(a,function(d,e){if(!b[e]||!c)b[e]=a[e]});return b},count:function(a){var b=0;this.loop(a,function(){b++});return b},compareObjects:function(a,b){return Object.getOwnPropertyNames(a).sort().join("")==Object.getOwnPropertyNames(b).sort().join("")},
compareNumbers:function(a,b){return a>b?1:a<b?-1:0},toArray:function(a){return[].slice.call(a)},loop:function(a,b,c){var d;if(a instanceof Object&&b instanceof Function){if(a instanceof Array)for(d=0;d<a.length;d++)b.call(c,a[d],d,a);else for(d in a)a.hasOwnProperty(d)&&b.call(c,a[d],d,a);return true}else return false},objectsDiffs:function(a,b){if(a instanceof Object&&b instanceof Object){var c=[],d=[],e=[],i=[];this.loop(b,function(b,h){typeof a[h]=="undefined"?i.push(h):b!==a[h]?d.push(h):b===
a[h]&&c.push(h)});this.loop(a,function(a,h){typeof b[h]=="undefined"&&e.push(h)});return{updated:d,unchanged:c,added:i,deleted:e}}else return false},jsonify:function(a){return a instanceof Object?JSON.parse(JSON.stringify(a)):false},clone:function(a){return a instanceof Array?a.slice(0):typeof a=="object"&&a!==null&&!(a instanceof RegExp)?this.mixin(a,{}):false},getNestedProperty:function(a,b){return a&&a instanceof Object?typeof b=="string"&&b!==""?b.split(".").reduce(function(a,b){return a&&a[b]},
a):typeof b=="number"?a[b]:a:a},setNestedProperty:function(a,b,c){if(a&&a instanceof Object)if(typeof b=="string"&&b!==""){var d=b.split(".");return d.reduce(function(a,b,g){a[b]=a[b]||{};d.length==g+1&&(a[b]=c);return a[b]},a)}else return typeof b=="number"?(a[b]=c,a[b]):a;else return a},closest:function(a,b){return f(a,b,function(a,b){return Math.abs(a-b)})},closestGreater:function(a,b){return f(a,b,function(a,b){return a-b})},closestLower:function(a,b){return f(a,b,function(a,b){return b-a})}}});
define("Observable",["Tools"],function(f){return function(){var a={};this.watch=function(b,c,d){if(typeof c=="function"){var e=a[b]=a[b]||[],c=[c,d];e.push(c);return[b,e.indexOf(c)]}else return false};this.once=function(a,c,d){var e=this.watch(a,function(){c.apply(d,arguments);this.unwatch(e)},this);return e};this.unwatch=function(b){var c=b[0],b=b[1];return a[c]&&a[c][b]?(delete a[c][b],a[c].some(function(a){return!!a})||delete a[c],true):false};this.notify=function(b){var c=a[b],d=f.toArray(arguments).slice(1);
return c?(f.loop(c,function(a){try{a&&a[0].apply(a[1]||null,d)}catch(b){}}),true):false};this.hasObserver=function(b){return!(!b||!a[b[0]]||!a[b[0]][b[1]])};this.hasTopic=function(b){return!!a[b]};this.unwatchAll=function(b){a[b]?delete a[b]:a={};return true}}});
define("StateMachine",["Tools"],function(f){function a(){var a={};this.add=function(c,d,e,i){var g=[];if(a[c])return false;return typeof c=="string"&&typeof d=="function"?(g[0]=d,typeof e=="object"&&(g[1]=e),typeof e=="string"&&(g[2]=e),typeof i=="string"&&(g[2]=i),a[c]=g,true):false};this.has=function(c){return!!a[c]};this.get=function(c){return a[c]||false};this.event=function(c){var d=a[c];return d?(d[0].apply(d[1],f.toArray(arguments).slice(1)),d[2]):false}}return function(b,c){var d={},e="";
this.init=function(a){return d[a]?(e=a,true):false};this.add=function(b){return d[b]?d[b]:d[b]=new a};this.get=function(a){return d[a]};this.getCurrent=function(){return e};this.has=function(a){return d.hasOwnProperty(a)};this.advance=function(a){return this.has(a)?(e=a,true):false};this.event=function(a){var b;b=d[e].event.apply(d[e].event,f.toArray(arguments));return b===false?false:(b&&(d[e].event("exit"),e=b,d[e].event("entry")),true)};f.loop(c,function(a,b){var h=this.add(b);a.forEach(function(a){h.add.apply(null,
a)})},this);this.init(b)}});
define("Promise",["Observable","StateMachine"],function(f,a){return function c(){var d=null,e=null,i=new f,g={Pending:[["fulfill",function(a){d=a;i.notify("fulfill",a)},"Fulfilled"],["reject",function(a){e=a;i.notify("reject",a)},"Rejected"],["toFulfill",function(a){i.watch("fulfill",a)}],["toReject",function(a){i.watch("reject",a)}]],Fulfilled:[["toFulfill",function(a){setTimeout(function(){a(d)},0)}]],Rejected:[["toReject",function(a){setTimeout(function(){a(e)},0)}]]},h=new a("Pending",g);this.fulfill=
function(a){h.event("fulfill",a);return this};this.reject=function(a){h.event("reject",a);return this};this.then=function(a,j,g,i){var f=new c;a instanceof Function?j instanceof Function?h.event("toFulfill",this.makeResolver(f,a)):h.event("toFulfill",this.makeResolver(f,a,j)):h.event("toFulfill",this.makeResolver(f,function(){f.fulfill(d)}));j instanceof Function&&h.event("toReject",this.makeResolver(f,j,g));g instanceof Function&&h.event("toReject",this.makeResolver(f,g,i));!(j instanceof Function)&&
!(g instanceof Function)&&h.event("toReject",this.makeResolver(f,function(){f.reject(e)}));return f};this.sync=function(a){return a instanceof Object&&a.then?(a.then(function(a){this.fulfill(a)}.bind(this),function(a){this.reject(a)}.bind(this)),true):false};this.makeResolver=function(a,h,c){return function(d){var e;try{e=h.call(c,d),a.sync(e)||a.fulfill(e)}catch(g){a.reject(g)}}};this.getReason=function(){return e};this.getValue=function(){return d};this.getObservable=function(){return i};this.getStateMachine=
function(){return h};this.getStates=function(){return g}}});
define("Store",["Observable","Tools"],function(f,a){return function(b){var c=a.clone(b)||{},d=new f,e=new f,i=[],g=function(b){var g=a.objectsDiffs(b,c);["updated","deleted","added"].forEach(function(a){g[a].forEach(function(b){d.notify(a,b,c[b]);e.notify(b,c[b],a)})})};this.count=this.getNbItems=function(){return c instanceof Array?c.length:a.count(c)};this.get=function(a){return c[a]};this.has=function(a){return c.hasOwnProperty(a)};this.set=function(a,b){var g,f;return typeof a!="undefined"?(g=
this.has(a),f=this.get(a),c[a]=b,g=g?"updated":"added",d.notify(g,a,c[a],f),e.notify(a,c[a],g,f),true):false};this.update=function(b,c,g){var f;return this.has(b)?(f=this.get(b),a.setNestedProperty(f,c,g),d.notify("updated",c,g),e.notify(b,f,"updated"),true):false};this.del=function(a){return this.has(a)?(this.alter("splice",a,1)||(delete c[a],d.notify("deleted",a),e.notify(a,c[a],"deleted")),true):false};this.delAll=function(b){return b instanceof Array?(b.sort(a.compareNumbers).reverse().forEach(this.del,
this),true):false};this.alter=function(b){var e,f;return c[b]?(f=a.clone(c),e=this.proxy.apply(this,arguments),g(f),d.notify("altered",c,f),e):false};this.proxy=function(a){return c[a]?c[a].apply(c,Array.prototype.slice.call(arguments,1)):false};this.watch=function(a,b,c){return d.watch(a,b,c)};this.unwatch=function(a){return d.unwatch(a)};this.getStoreObservable=function(){return d};this.watchValue=function(a,b,c){return e.watch(a,b,c)};this.unwatchValue=function(a){return e.unwatch(a)};this.getValueObservable=
function(){return e};this.loop=function(b,d){a.loop(c,b,d)};this.reset=function(b){if(b instanceof Object){var e=a.clone(c);c=a.clone(b)||{};g(e);d.notify("resetted",c,e);return true}else return false};this.compute=function(b,c,d,e){return typeof b=="string"&&typeof c=="object"&&typeof d=="function"&&!this.isCompute(b)?(i[b]=[],a.loop(c,function(a){i[b].push(this.watchValue(a,function(){this.set(b,d.call(e))},this))},this),this.set(b,d.call(e)),true):false};this.removeCompute=function(b){return this.isCompute(b)?
(a.loop(i[b],function(a){this.unwatchValue(a)},this),this.del(b),true):false};this.isCompute=function(a){return!!i[a]};this.toJSON=function(){return JSON.stringify(c)};this.dump=function(){return c}}});
define("Transport",[],function(){return function(f){var a=null;this.setReqHandlers=function(b){return b instanceof Object?(a=b,true):false};this.getReqHandlers=function(){return a};this.request=function(b,c,d,e){return a.has(b)&&typeof c!="undefined"?(a.get(b)(c,function(){d&&d.apply(e,arguments)}),true):false};this.listen=function(b,c,d,e){if(a.has(b)&&typeof c!="undefined"&&typeof d=="function"){var f=function(){d.apply(e,arguments)},g;g=a.get(b)(c,f,f);return function(){typeof g=="function"?g():
typeof g=="object"&&typeof g.func=="function"&&g.func.call(g.scope)}}else return false};this.setReqHandlers(f)}});
define("Router",["Observable","Store","Tools"],function(f,a,b){return function(){var c=new f,d=new f,e=new a([]),i=-1,g=10;this.getRoutesObservable=function(){return c};this.getEventsObservable=function(){return d};this.setMaxHistory=function(a){return a>=0?(g=a,true):false};this.getMaxHistory=function(){return g};this.set=function(){return c.watch.apply(c,arguments)};this.unset=function(a){return c.unwatch(a)};this.navigate=function(a,c){return this.load.apply(this,arguments)?(e.proxy("splice",i+
1,e.count()),e.proxy("push",b.toArray(arguments)),this.ensureMaxHistory(e),i=e.count()-1,true):false};this.ensureMaxHistory=function(a){var b=a.count(),c=this.getMaxHistory();b-=c;b>0&&a.proxy("splice",0,b)};this.load=function(){var a=b.toArray(arguments);return c.notify.apply(c,a)?(a.unshift("route"),d.notify.apply(d,a),true):false};this.watch=function(a,b){return d.watch("route",a,b)};this.unwatch=function(a){return d.unwatch(a)};this.getHistoryStore=function(){return e};this.getHistoryCount=function(){return e.count()};
this.clearHistory=function(){e.reset([])};this.go=function(a){var b=e.get(i+a);return b?(i+=a,this.load.apply(this,b),true):false};this.back=function(){return this.go(-1)};this.forward=function(){return this.go(1)}}});