-
Notifications
You must be signed in to change notification settings - Fork 4
/
nimbly.min.js
1 lines (1 loc) · 17.3 KB
/
nimbly.min.js
1
var Nimbly=function(u,o,e,t,r,n,a){if(void 0===u)throw new Error("Nimbly requires jQuery 1.9+.");if(void 0===o)throw new Error("Nimbly requires ObservableSlim 0.1.0+.");var l=0,i=[],s=[],d=function(e){-1===i.indexOf(e.instanceId)&&(s.push(e.instanceId),i.push(e.refresh));var t=i.length;setTimeout(function(){if(t==i.length){for(var e=i.length;e--;)i.pop()();s.length=0}},10)},h=[];new e(function(e){for(var t=h.length;t--;)null!==h[t].jqDom&&a.body.contains(h[t].jqDom[0])&&(h[t]._afterInDocument(),h.splice(t,1))}).observe(a,{attributes:!1,childList:!0,characterData:!1,subtree:!0});var c=function(e){var t=a.getElementById(e);if(!t)throw new Error("Nimbly::_getTemplate() could not find the template with element ID: '"+e+"'.");return t.innerHTML.trim()},f=function(e){return"function"==typeof e.render&&"function"==typeof e.init&&"function"==typeof e.isReady},m=function(e,t,n,i){var h=this;if(void 0===n)n={};if(void 0===i)i={};var r={};if(u.extend(!0,r,t.data,n),u.extend(!0,n,r),this.options=u.extend(!0,{},t,i,{data:n}),this._data=n,this.className=e,this._baseClassInstance=l++,this.jqDom=null,this.domNode=null,this.initialized=!1,this.initList=this.options.initList||[],this._pendingInit=!1,this._pendingFetchCount=0,this._initRendered=!1,this._delayRefresh=!1,this._renderRunning=!1,this._registeredDuringRender=!1,this._insertedIntoParent=!1,this._renderjQuery=this.options.renderjQuery||!1,this._cleanUpChildren=null,this.childComponents={default:[]},this._tagName=null,this._refreshList=[],this.templates={},this.options.templates instanceof Array){if(0==this.options.templates.length)throw new Error("Nimbly::constructor cannot continue -- no templates provided.");for(var s=0;s<this.options.templates.length;s++)this.templates[this.options.templates[s]]=c(this.options.templates[s])}else this.templates=this.options.templates;if("string"==typeof this.options.loadingTemplate){if(0===this.options.loadingTemplate.length)throw new Error("Nimbly::constructor cannot continue -- the loadingTemplate cannot be an empty string.");a.getElementById(this.options.loadingTemplate)?this.loadingTemplate=c(this.options.loadingTemplate):this.loadingTemplate=this.options.loadingTemplate}else this.loadingTemplate='<div class="default_nimbly_loading_template"></div>';this._uiBindings=this.options.uiBindings||{},this._dataBindings=this.options.dataBindings||{},this.data=o.create(this._data,!1,function(e){if(!0===h._renderRunning)throw new Error(h.className+"._render() is attempting to modify this.data. Mutating this.data while rendering is disallowed because it's likely to generate infinite loops via uiBindings.");if(1==h.initialized){for(var t=!1,n={},i=e.length;i--;){for(var r in h._uiBindings){var s=!1;if("/"===r.charAt(0)&&"/"===r.charAt(r.length-1))s=new RegExp(r.substring(1,r.length-1)).test(e[i].currentPath);!0===h._refreshList||r!=e[i].currentPath&&!s||(!0===h._uiBindings[r]?h._refreshList=!0:h._refreshList=h._refreshList.concat(h._uiBindings[r]))}for(var o in h._dataBindings){s=!1;if("/"===o.charAt(0)&&"/"===o.charAt(o.length-1))s=new RegExp(o.substring(1,o.length-1)).test(e[i].currentPath);if(o==e[i].currentPath||s){1==h._dataBindings[o].delayRefresh&&(t=!0);for(var a=h._dataBindings[o].methods.length;a--;)void 0===n[h._dataBindings[o].methods[a]]&&(n[h._dataBindings[o].methods[a]]=[]),n[h._dataBindings[o].methods[a]].push(e[i])}}}h._fetch(t,n),(!0===h._refreshList||0<h._refreshList.length)&&(h.pendingRefresh=!0,d({instanceId:h._baseClassInstance,refresh:function(){h._refresh()}}))}}),0==(this.options.delayInit||!1)&&this.init()};m.prototype.observe=function(e){return o.create(this._data,!0,e)},m.prototype.init=function(){var i=this;for(var e in this._dataBindings)for(var t=0;t<this._dataBindings[e].methods.length;t++)if("function"!=typeof this[this._dataBindings[e].methods[t]])throw new Error("Nimbly::init cannot continue. Please review the dataBindings on class "+i.className+". The method "+this._dataBindings[e].methods[t]+" does not exist or is not a function.");for(var n=[],r=[],s=0;s<this.initList.length;s++)if("function"!=typeof this.initList[s].condition||0!=this.initList[s].condition.call(i)){var o=new Promise(function(n){return function(e,t){i[i.initList[n].method](e,t)}}(s)).catch(function(t){return function(e){throw console.error(e),new Error("Nimbly::init cannot continue, "+i.className+"."+i.initList[t].method+"() failed to resolve. Error: "+e)}}(s));1==this.initList[s].preventRender?n.push(o):r.push(o)}if(0<n.length){this._pendingFetchCount++,this._pendingInit=!0;var a=Promise.all(n).then(function(){a.done=!0,i.initialized=!0,i._pendingInit=!1,i._refreshList=!0,i.pendingRefresh=!0,d({instanceId:i._baseClassInstance,refresh:function(){i._refresh()}}),"function"==typeof i._init&&i._init(),i._pendingFetchCount--}).catch(function(e){a.done=!0,console.error(e)});setTimeout(function(){!0!==a.done&&console.warn(i.className+" initList methods have not resolved after 15 seconds. Please ensure that your initList methods properly resolve or reject.")},15e3)}else this.initialized=!0,"function"==typeof i._init&&i._init();if(0<r.length){this._pendingFetchCount++;var h=Promise.all(r).then(function(){h.done=!0,i._pendingFetchCount--}).catch(function(e){h.done=!0,console.error(e)});setTimeout(function(){!0!==h.done&&console.warn(i.className+" initList methods have not resolved after 15 seconds. Please ensure that your initList methods properly resolve or reject.")},15e3)}},m.prototype._fetch=function(e,i){var r=this,t=!1;if(1==e&&0<Object.keys(i).length&&(this._delayRefresh=e,"function"==typeof this.showLoadMask&&this.showLoadMask(),t=!0),0<Object.keys(i).length){this._pendingFetchCount++;var n=[];for(var s in i){var o=new Promise(function(n){return function(e,t){if("function"!=typeof r[n])throw new Error("Nimbly::_fetch cannot continue, the method "+r.className+"."+n+"() does not exist or is not a function.");r[n](e,t,i[n])}}(s)).catch(function(t){return function(e){throw console.error(e),new Error("An error occured in the "+r.className+"."+t+"() method.")}}(s));n.push(o)}var a=Promise.all(n).then(function(){a.done=!0,r._delayRefresh=!1,1==t&&"function"==typeof r.hideLoadMask&&r.hideLoadMask(),(1==r._refreshList||0<r._refreshList.length)&&d({instanceId:r._baseClassInstance,refresh:function(){r._refresh()}}),r._pendingFetchCount--}).catch(function(e){a.done=!0,console.error(e)});setTimeout(function(){!0!==a.done&&console.warn(r.className+" dataBinding methods have not resolved after 15 seconds. Please ensure that your dataBinding methods properly resolve or reject.")},15e3)}},m.prototype.render=function(){if(0==this.initialized&&0==this._pendingInit&&this.init(),0==this.initialized&&1==this._pendingInit)var e=this._renderLoadingFromComp();else if(this._refreshList instanceof Array&&0==this._refreshList.length&&1==this._initRendered)e=this.jqDom;else{e=this._renderFromComp();for(var t=this._insertChildren(e),n=t.length;n--;)t[n].comp.jqDom=t[n].elmt,t[n].comp.domNode=t[n].elmt[0];"function"==typeof this._renderFinalize&&this._renderFinalize(e),"function"==typeof this._afterRender&&this._afterRender(e),!1===this._initRendered&&this._monitorInsertion(this),this._initRendered=!0}return this._refreshList=[],this.jqDom=e,this.domNode=e[0],!1===this._renderjQuery?this.domNode:this.jqDom},m.prototype._renderWithChildren=function(){if(0==this.initialized&&0==this._pendingInit&&this.init(),0==this.initialized&&1==this._pendingInit)var e=this._renderLoadingFromComp();else{e=this._renderFromComp();var t=this._insertChildren(e);"function"==typeof this._renderFinalize&&this._renderFinalize(e),"function"==typeof this._afterRender&&this._afterRender(e)}return{elmt:e,insertedChildren:t}},m.prototype._renderFromComp=function(){this._renderRunning=!0;var e=this._render();return this._renderRunning=!1,this._validateRender(e),this._cleanOrphans(),u(e)},m.prototype._renderLoadingFromComp=function(){if("function"==typeof this._renderLoading){var e=this._renderLoading();this._validateRender(e)}else e=u(this.loadingTemplate);return u(e)},m.prototype._monitorInsertion=function(e){var t=this;"function"!=typeof e._afterInDocument||-1!==h.indexOf(e)||null!==e.jqDom&&a.body.contains(e.jqDom[0])||h.push(e),e.eachChildComponent(function(e){t._monitorInsertion(e)},!0)},m.prototype._validateRender=function(e){if(!(!0!==this._renderjQuery||e instanceof u))throw new Error(this.className+".render() cannot continue. "+this.className+"._render() did not return a jQuery-encapsulated HTMLElement but your component settings indicate that it should have (options.renderjQuery === true).");if(!1===this._renderjQuery&&(e instanceof u||!(e instanceof t)))throw new Error(this.className+".render() cannot continue. "+this.className+"._render() did not return an HTMLElement. Use this.parseHTML to render your component. Otherwise, if you intend to render a jQuery-encapsulated HTMLElement, then remember to set options.renderjQuery to true.");if(1!==u(e).length)throw new Error(this.className+".render() cannot continue. "+this.className+"._render() must return a single HTMLElement or jQuery-referenced DOM element -- instead it returned "+e.length+" elements. Ensure that your component template is wrapped (encapsulated) by a single element.")},m.prototype._render=function(){return u(this.templates[Object.keys(this.templates)[0]])};var p=new n;return m.prototype.parseHTML=function(e){return p.parseFromString(e,"text/html").body.firstChild},m.prototype.getTemplateComponents=function(e){if("string"!=typeof e||0==e.length)throw new Error("Nimbly::getTemplateComponents() cannot continue. templateName must be a string.");if(void 0===this.templates[e])throw new Error("Nimbly::getTemplateComponents() cannot continue. Template '"+e+"' does not exist.");var t=this.parseHTML(this.templates[e]),i=[],n=function(e,t){for(t(e),e=e.firstChild;e;)n(e,t),e=e.nextSibling};return n(t,function(e){if(void 0!==e.tagName&&a.createElement(e.tagName.replace(/\W/g,""))instanceof r||void 0!==e.attributes&&"object"==typeof e.attributes.is){if("object"==typeof e.attributes.is)var t={tagName:e.attributes.is.value.toUpperCase()};else t={tagName:e.tagName};t.attributes={};for(var n=0;n<e.attributes.length;n++)t.attributes[e.attributes[n].name]=e.attributes[n].value;i.push(t)}}),i},m.prototype.addTemplate=function(e,t){if("string"!=typeof t)throw new Error("Nimbly::addTemplate() cannot continue. templateHtml must be a string.");if("string"!=typeof e||0==e.length)throw new Error("Nimbly::addTemplate() cannot continue. templateName must be a string.");void 0!==this.templates[e]&&console.warn("Nimbly::addTemplate() is about to overwrite template '"+e+"'."),this.templates[e]=t},m.prototype._insertChildren=function(e){var t=[];for(var n in this.childComponents)if("default"!==n){var i=[],r=e.find(n);if(0===r.length&&(r=e.find("table[is='"+n+"'], tbody[is='"+n+"'], select[is='"+n+"'], ul[is='"+n+"'], ol[is='"+n+"']")),1===r.length){for(var s=this.childComponents[n],o=0;o<s.length;o++){for(var a=r.clone(),h=s[o],l=0;l<h.length;l++){var d=h[l];if(!0!==d._registeredDuringRender||!0!==d._insertedIntoParent){var c=a.find(d._tagName);if(0===c.length)a.find("tbody, tr, td, li, option").each(function(e,t){var n=u(t);if(n.attr("is")===d._tagName)return(c=n).removeAttr("is"),!1});if(1===c.length){var f=d._renderWithChildren();t.push({comp:d,elmt:f.elmt}),this._monitorInsertion(d),t.push.apply(t,f.insertedChildren),c.replaceWith(f.elmt),d._insertedIntoParent=!0}else if(1<c.length)throw new Error("Nimbly::_insertChildren() cannot continue. Found multiple elements matching the '"+d._tagName+"' selector. A repeatable section must not contain duplicate child tags.")}}i.push(a.contents())}r.attr("is")===n?(r.html(""),r.removeAttr("is"),r.append(i)):r.replaceWith(i)}else if(1<r.length)throw new Error("Nimbly::_insertChildren() cannot continue. Found multiple elements matching the '"+n+"' selector. A repeatable section must have one insertion target (i.e., one matching custom tag).")}for(var m=this.childComponents.default.length;m--;)if(!0!==this.childComponents.default[m]._registeredDuringRender||!0!==this.childComponents.default[m]._insertedIntoParent){var p=e.find(this.childComponents.default[m]._tagName);if(1===p.length){f=this.childComponents.default[m]._renderWithChildren();t.push({comp:this.childComponents.default[m],elmt:f.elmt}),t.push.apply(t,f.insertedChildren),p.replaceWith(f.elmt),this.childComponents.default[m]._insertedIntoParent=!0}else if(1<p.length)throw new Error("Nimbly::_insertChildren() cannot continue. Found multiple elements matching the '"+this.childComponents.default[m]._tagName+"' selector. A child component must match exactly one tag in the parent component. If you require instances of the same child component, use a repeatable section or provide each instance a unique tag name via the options.")}return t},m.prototype.refresh=function(){var e=this;this.pendingRefresh=!0,this._refreshList=!0,d({instanceId:e._baseClassInstance,refresh:function(){e._refresh()}})},m.prototype._refresh=function(){var n=this;if(1==this.initialized&&0==this._delayRefresh)if(null===this.jqDom)this.render();else if("object"==typeof this._refreshList&&0<this._refreshList.length){if(this._refreshList=this._refreshList.filter(function(e,t){return n._refreshList.indexOf(e)==t}),1<this._refreshList.length)for(var e=this._refreshList.length;e--;){var t=this.jqDom.find(this._refreshList[e]);if(0!==t.length){for(var i=t[0],r=this._refreshList.length;r--;)if(e!==r){var s=this.jqDom.find(this._refreshList[r]);if(0===s.length)continue;if(s[0].contains(i)){this._refreshList.splice(e,1);break}}}else console.warn("Did not find a match for the selector '"+this._refreshList[e]+"'. Please review the uiBindings for class '"+this.className+"'.")}for(var o=this._renderWithChildren(),a=o.elmt,h=o.insertedChildren,l=this._refreshList.length;l--;){var d=this.jqDom.find(this._refreshList[l]),c=a.find(this._refreshList[l]);if(1<d.length||1<c.length)throw new Error("Nimbly::refresh() cannot continue. Refresh selector has multiple targets.");d.replaceWith(c)}for(l=h.length;l--;)this.jqDom[0].contains(h[l].elmt[0])&&(h[l].comp.jqDom=h[l].elmt,h[l].comp.domNode=h[l].elmt[0]);this._refreshList=[]}else if(1==this._refreshList){var f=this.jqDom;a=this.render();f.replaceWith(a),this._refreshList=[],"function"==typeof this._postRefresh&&this._postRefresh(!0,f),"function"==typeof this._afterRefresh&&this._afterRefresh(!0,f),n._monitorInsertion(this)}},m.prototype.eachChildComponent=function(e,t){var n=this;for(var i in this.childComponents)for(var r=this.childComponents[i].length;r--;)if("default"===i)!0===t&&this.childComponents[i][r].eachChildComponent(e,t),e(this.childComponents[i][r],i,function(e){!0!==e&&void 0!==e||n.childComponents[i][r].destroy(),n.childComponents[i].splice(r,1)});else for(var s=this.childComponents[i][r].length;s--;)!0===t&&this.childComponents[i][r][s].eachChildComponent(e,t),e(this.childComponents[i][r][s],i,function(e){!0!==e&&void 0!==e||n.childComponents[i][r][s].destroy(),n.childComponents[i][r].splice(s,1),0===n.childComponents[i][r].length&&n.childComponents[i].splice(r,1),0===n.childComponents[i].length&&delete n.childComponents[i]})},m.prototype.registerChild=function(e,t){if(void 0===t)throw new Error("Nimbly cannot register this component -- targetName must be specified.");var n=null;if(e instanceof Array)for(var i=e.length;i--;){if(void 0===e[i].comp)throw new Error("Nimbly cannot register this set of components -- 'comp' attribute must be specified.");if(void 0===e[i].tagName)throw new Error("Nimbly cannot register this set of components -- 'tagName' attribute must be specified.");if(!1===f(e[i].comp))throw new Error("Nimbly cannot register this set of components. Encountered one or more non-Nimbly components.")}else if(!1===f(e))throw new Error("Nimbly cannot register this component. It is not a valid Nimbly component.");if(n=e instanceof Array?t:"default",void 0===this.childComponents[n]&&(this.childComponents[n]=[]),!0===this._renderRunning)if(e instanceof Array)for(i=e.length;i--;)e[i].comp._registeredDuringRender=!0;else e._registeredDuringRender=!0;if(e instanceof Array){var r=[];for(i=0;i<e.length;i++)e[i].comp._tagName=e[i].tagName,r.push(e[i].comp);this.childComponents[n].push(r)}else e._tagName=t,-1===this.childComponents[n].indexOf(e)&&this.childComponents[n].push(e)},m.prototype.destroy=function(){var e=this;"function"==typeof this._destroy&&this._destroy(),this.eachChildComponent(function(e,t){e.destroy()});var t=h.indexOf(this);-1<t&&h.splice(t,1),o.remove(this.data),null!==this.jqDom&&(this.jqDom.remove(),this.jqDom=null,this.domNode=null);var n=20,i=setInterval(function(){n--,(e.isReady()||0===n)&&(e.childComponents=null,e.data=null,e._data=null,e.templates=null,e._uiBindings=null,e._dataBindings=null,e.initList=null,e.showLoadMask=null,e.hideLoadMask=null,e._refreshList=null,clearInterval(i))},1e3)},m.prototype._cleanOrphans=function(){var i=this;this._cleanUpChildren=Math.floor(1e9*Math.random());var e=this._cleanUpChildren;setTimeout(function(){e==i._cleanUpChildren&&i.eachChildComponent(function(e,t,n){!0!==e._registeredDuringRender||null!==e.jqDom&&i.jqDom[0].contains(e.jqDom[0])||n()})},100)},m.prototype.isReady=function(){var n=!0;this.eachChildComponent(function(e,t){!1===e.isReady()&&(n=!1)});var e=null===this._refreshList||0===this._refreshList.length,t=null===this._pendingFetchCount||0===this._pendingFetchCount,i=!1===this._pendingInit;return!0===n&&e&&t&&i},m};"undefined"==typeof module?window.Nimbly=Nimbly($,ObservableSlim,MutationObserver,HTMLElement,HTMLUnknownElement,DOMParser,document):module.exports=function(e,t,n,i,r,s,o){return Nimbly(e,t,n,i,r,s,o)};