t3js
Version:
18 lines (15 loc) • 8.23 kB
JavaScript
/*! t3 v2.7.0 */
/*!
Copyright 2016 Box, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
!function(e){var t={};if(t.EventTarget=function(){"use strict";function e(){this._handlers={}}return e.prototype={constructor:e,on:function(e,t){var n,r,i=this._handlers[e];for("undefined"==typeof i&&(i=this._handlers[e]=[]),n=0,r=i.length;r>n;n++)if(i[n]===t)return;i.push(t)},fire:function(e,t){var n,r,i,o={type:e,data:t};if(n=this._handlers[o.type],n instanceof Array)for(n=n.concat(),r=0,i=n.length;i>r;r++)n[r].call(this,o)},off:function(e,t){var n,r,i=this._handlers[e];if(i instanceof Array)for(n=0,r=i.length;r>n;n++)if(i[n]===t){i.splice(n,1);break}}},e}(),t.NativeDOM=function(){"use strict";return{type:"native",query:function(e,t){return e.querySelector(t)},queryAll:function(e,t){return e.querySelectorAll(t)},on:function(e,t,n){e.addEventListener(t,n,!1)},off:function(e,t,n){e.removeEventListener(t,n,!1)}}}(),t.DOM=t.NativeDOM,t.DOMEventDelegate=function(){"use strict";function e(e){return e&&e.hasAttribute("data-module")}function n(e){return e&&e.hasAttribute("data-type")}function r(t){for(var r=!1,i=!1;!r&&t&&t.parentNode&&!i;)r=n(t),i=e(t),r||(t=t.parentNode);return r?t:null}function i(e,t,n,r){var i,o;for(i=0;i<e.length;i++)o=e[i],t["on"+o]&&n.call(r,o)}function o(e,t,n){this.element=e,this._handler=t,this._eventTypes=n||a,this._boundHandler={},this._attached=!1}var a=["click","mouseover","mouseout","mousedown","mouseup","mouseenter","mouseleave","mousemove","keydown","keyup","submit","change","contextmenu","dblclick","input","focusin","focusout"];return o.prototype={constructor:o,_handleEvent:function(e){var t=r(e.target),n=t?t.getAttribute("data-type"):"";this._handler["on"+e.type](e,t,n)},attachEvents:function(){this._attached||(i(this._eventTypes,this._handler,function(e){function n(){r._handleEvent.apply(r,arguments)}var r=this;t.DOM.on(this.element,e,n),this._boundHandler[e]=n},this),this._attached=!0)},detachEvents:function(){i(this._eventTypes,this._handler,function(e){t.DOM.off(this.element,e,this._boundHandler[e])},this)}},o}(),t.Context=function(){"use strict";function e(e,t){this.application=e,this.element=t}return e.prototype={constructor:e,broadcast:function(e,t){this.application.broadcast(e,t)},getService:function(e){return this.application.getService(e)},hasService:function(e){return this.application.hasService(e)},getConfig:function(e){return this.application.getModuleConfig(this.element,e)},getGlobal:function(e){return this.application.getGlobal(e)},getGlobalConfig:function(e){return this.application.getGlobalConfig(e)},reportError:function(e){this.application.reportError(e)},getElement:function(){return this.element}},e}(),t.Application=function(){"use strict";function n(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}function r(e,t){for(var n=0,r=e.length;r>n;n++)if(e[n]===t)return n;return-1}function i(){y={},b={},E={},w=[],_={},D={},A=!1}function o(e){for(var t=0,n=w.length;n>t;t++)if(w[t]===e)return!0;return!1}function a(e){if("function"==typeof M)return void M(e);if(y.debug)throw e;O.fire("error",{exception:e})}function u(e,t){var n,r;for(n in e)r=e[n],"function"==typeof r&&(e[n]=function(e,n){return function(){var r=t+"."+e+"() - ";try{return n.apply(this,arguments)}catch(i){i.methodName=e,i.objectName=t,i.name=r+i.name,i.message=r+i.message,a(i)}}}(n,r))}function s(e){var t=e.getAttribute("data-module");return t?t.split(" ")[0]:""}function c(e,t){"function"==typeof e[t]&&e[t].apply(e,Array.prototype.slice.call(arguments,2))}function f(e){var t=E[e];if(t){if(!t.instance){if(o(e))return a(new ReferenceError("Circular service dependency: "+w.join(" -> ")+" -> "+e)),null;w.push(e),t.instance=t.creator(O),w.pop()}return t.instance}return a(new Error('Service "'+e+'" not found')),null}function l(e){var t,n,r,i,o,u=[],s={};for(n=e.instance.behaviors||[],t=0;t<n.length;t++)o=n[t],"behaviorInstances"in e||(e.behaviorInstances={}),i=e.behaviorInstances,r=_[o],o in s?a(new Error('Behavior "'+o+'" cannot be specified twice in a module.')):r?(i[o]||(i[o]=r.creator(e.context)),u.push(i[o])):a(new Error('Behavior "'+o+'" not found')),s[o]=!0;return u}function h(e,n,r){var i=new t.DOMEventDelegate(n,r,y.eventTypes);e.push(i),i.attachEvents()}function d(e){var t=e.eventDelegates,n=l(e);h(t,e.element,e.instance);for(var r=0;r<n.length;r++)h(t,e.element,n[r])}function p(e){for(var t=e.eventDelegates,n=0;n<t.length;n++)t[n].detachEvents();e.eventDelegates=[]}function v(e){return D[e.id]}function g(e,t,n){null!==e.onmessage&&"object"==typeof e.onmessage&&e.onmessage.hasOwnProperty(t)?e.onmessage[t].call(e,n):-1!==r(e.messages||[],t)&&e.onmessage.call(e,t,n)}var m="[data-module]",y={},b={},w=[],E={},_={},D={},A=!1,M=null,O=new t.EventTarget;return n(O,{init:function(e){return n(y,e||{}),this.startAll(document.documentElement),this.fire("init"),A=!0,this},destroy:function(){return this.stopAll(document.documentElement),i(),this},isStarted:function(e){var t=v(e);return"object"==typeof t},start:function(e){var n,r,i,o=s(e),f=b[o];if(!f)return a(new Error('Module type "'+o+'" is not defined.')),this;if(!this.isStarted(e)){e.id||(e.id="mod-"+o+"-"+f.counter),f.counter++,r=new t.Context(this,e),i=f.creator(r),y.debug||u(i,o),n={moduleName:o,instance:i,context:r,element:e,eventDelegates:[]},D[e.id]=n;for(var h,p=l(n),v=0,g=p.length;g>v;v++)h=p[v],c(h,"init");c(n.instance,"init"),d(n)}return this},stop:function(e){var t=v(e);if(t){p(t);for(var n,r=l(t),i=r.length-1;i>=0;i--)n=r[i],c(n,"destroy");c(t.instance,"destroy"),delete D[e.id]}else if(y.debug)return a(new Error("Unable to stop module associated with element: "+e.id)),this;return this},startAll:function(e){for(var n=t.DOM.queryAll(e,m),r=0,i=n.length;i>r;r++)this.start(n[r]);return this},stopAll:function(e){for(var n=t.DOM.queryAll(e,m),r=0,i=n.length;i>r;r++)this.stop(n[r]);return this},addModule:function(e,t){return"undefined"!=typeof b[e]?(a(new Error("Module "+e+" has already been added.")),this):(b[e]={creator:t,counter:1},this)},getModuleConfig:function(e,n){var r=v(e),i=null;if(r&&r.config)i=r.config;else{var o=t.DOM.query(e,'script[type="text/x-config"]');if(o)try{i=JSON.parse(o.text)}catch(u){a(new Error("Module with id "+e.id+" has a malformed config."))}r&&(r.config=i)}return i?"undefined"==typeof n?i:n in i?i[n]:null:null},addService:function(e,t){return"undefined"!=typeof E[e]?(a(new Error("Service "+e+" has already been added.")),this):(E[e]={creator:t,instance:null},this)},getService:f,hasService:function(e){return E.hasOwnProperty(e)},addBehavior:function(e,t){return"undefined"!=typeof _[e]?(a(new Error("Behavior "+e+" has already been added.")),this):(_[e]={creator:t,instance:null},this)},broadcast:function(e,t){var n,r,i,o,a;for(r in D)if(D.hasOwnProperty(r))for(i=D[r],g(i.instance,e,t),a=l(i),n=0;n<a.length;n++)o=a[n],g(o,e,t);return this.fire("message",{message:e,messageData:t}),this},getGlobal:function(t){return t in e?e[t]:null},getGlobalConfig:function(e){return"undefined"==typeof e?y:e in y?y[e]:null},setGlobalConfig:function(e){return A?(a(new Error("Cannot set global configuration after application initialization")),this):(n(y,e),this)},setErrorHandler:function(e){M=e},reportError:a,reportWarning:function(e){if(y.debug){var t=this.getGlobal("console");t&&t.warn&&t.warn(e)}else O.fire("warning",e)},reportInfo:function(e){if(y.debug){var t=this.getGlobal("console");t&&t.info&&t.info(e)}}})}(),"function"==typeof define&&define.amd)define("t3",[],function(){return t});else if("object"==typeof module&&"object"==typeof module.exports)module.exports=t;else{e.Box=e.Box||{};for(var n in t)t.hasOwnProperty(n)&&(e.Box[n]=t[n])}}("undefined"!=typeof window?window:this);
//# sourceMappingURL=t3.min.js.map