ractive
Version:
Next-generation DOM manipulation
2 lines • 35.8 kB
JavaScript
(function(t){"use strict";var e,i;e=function(t){var n,r;if(t)for(r in t)t.hasOwnProperty(r)&&(this[r]=t[r]);n={preserveWhitespace:!1,append:!1,twoway:!0,formatters:{},modifyArrays:!0};for(r in n)void 0===this[r]&&(this[r]=n[r]);if(void 0!==this.el&&(this.el=i(this.el)),this._subs={},void 0===this.viewmodel&&(this.viewmodel=new e.ViewModel),this.viewmodel.dependents.push(this),this.data&&this.viewmodel.set(this.data),this.partials)for(r in this.partials)if(this.partials.hasOwnProperty(r)&&"string"==typeof this.partials[r]){if(!e.compile)throw Error('Missing Anglebars.compile - cannot compile partial "'+r+'". Either precompile or use the version that includes the compiler');this.partials[r]=e.compile(this.partials[r],this)}if("string"==typeof this.template){if(!e.compile)throw Error("Missing Anglebars.compile - cannot compile template. Either precompile or use the version that includes the compiler");this.template=e.compile(this.template,this)}this.el&&this.render({el:this.el,callback:this.callback,append:this.append})},e.prototype={render:function(t){var n=t.el?i(t.el):this.el;if(!n)throw Error("You must specify a DOM element to render to");t.append||(n.innerHTML=""),t.callback&&(this.callback=t.callback),this.rendered=new e.DomFragment({model:this.template,anglebars:this,parentNode:n})},teardown:function(){this.rendered.teardown()},set:function(){return this.el.style.display,this.viewmodel.set.apply(this.viewmodel,arguments),this},get:function(){return this.viewmodel.get.apply(this.viewmodel,arguments)},update:function(){return this.viewmodel.update.apply(this.viewmodel,arguments),this},_format:function(t,i){var n,r,s,o,a,h;if(!i)return t;for(r=i.length,n=0;r>n;n+=1)s=i[n],o=s.name,a=s.args||[],h=this.formatters[o]||e.formatters[o],h&&(t=h.apply(this,[t].concat(a)));return t}},i=function(t){var e;if(!t)throw Error("No container element specified");if(t.tagName)return t;if("string"==typeof t&&(e=document.getElementById(t),!e&&document.querySelector&&(e=document.querySelector(t)),e.tagName))return e;if(t[0]&&t[0].tagName)return t[0];throw Error("Could not find container element")},e.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)},e.isObject=function(t){return"[object Object]"===Object.prototype.toString.call(t)&&"function"!=typeof t},e.types={TEXT:1,INTERPOLATOR:2,TRIPLE:3,SECTION:4,INVERTED:5,CLOSING:6,ELEMENT:7,PARTIAL:8,COMMENT:9,DELIMCHANGE:10,MUSTACHE:11,TAG:12,ATTR_VALUE_TOKEN:13},function(t){t.on=function(t,e){var i=this;return this._subs[t]?this._subs[t].push(e):this._subs[t]=[e],{cancel:function(){i.off(t,e)}}},t.off=function(t,e){var i,n;e||(t?this._subs[t]=[]:this._subs={}),i=this._subs[t],i&&(n=i.indexOf(e),-1!==n&&i.splice(n,1))},t.fire=function(t){var e,i,n,r=this._subs[t];if(r)for(e=Array.prototype.slice.call(arguments,1),i=0,n=r.length;n>i;i+=1)r[i].apply(this,e)}}(e.prototype);var e=e||{};(function(t){var e,i,n,r,s,o,a,h,l,u,d,c,f,p;t.compile=function(e,n){var r,s;return n=n||{},t.delimiters=n.delimiters||["{{","}}"],t.tripleDelimiters=n.tripleDelimiters||["{{{","}}}"],r=t.tokenize(e),s=i(r),s.toJson()},u=t.types,d=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],c=["a","abbr","acronym","address","applet","area","b","base","basefont","bdo","big","blockquote","body","br","button","caption","center","cite","code","col","colgroup","dd","del","dfn","dir","div","dl","dt","em","fieldset","font","form","frame","frameset","h1","h2","h3","h4","h5","h6","head","hr","html","i","iframe","img","input","ins","isindex","kbd","label","legend","li","link","map","menu","meta","noframes","noscript","object","ol","optgroup","option","p","param","pre","q","s","samp","script","select","small","span","strike","strong","style","sub","sup","table","tbody","td","textarea","tfoot","th","thead","title","tr","tt","u","ul","var","article","aside","audio","bdi","canvas","command","data","datagrid","datalist","details","embed","eventsource","figcaption","figure","footer","header","hgroup","keygen","mark","meter","nav","output","progress","ruby","rp","rt","section","source","summary","time","track","video","wbr"],f=["li","dd","rt","rp","optgroup","option","tbody","tfoot","tr","td","th"],p={li:["li"],dt:["dt","dd"],dd:["dt","dd"],p:["address","article","aside","blockquote","dir","div","dl","fieldset","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","menu","nav","ol","p","pre","section","table","ul"],rt:["rt","rp"],rp:["rp","rt"],optgroup:["optgroup"],option:["option","optgroup"],thead:["tbody","tfoot"],tbody:["tbody","tfoot"],tr:["tr"],td:["td","th"],th:["td","th"]},l=function(t){var e,i,n,r;if(r=t.indexOf("["),-1!==r){e=t.substr(0,r),i=t.substring(r+1,t.length-1);try{n=JSON.parse(i)}catch(s){throw"Could not parse arguments ("+i+") using JSON.parse"}return{name:e,args:n}}return{name:t}},h={quot:34,amp:38,apos:39,lt:60,gt:62,nbsp:160,iexcl:161,cent:162,pound:163,curren:164,yen:165,brvbar:166,sect:167,uml:168,copy:169,ordf:170,laquo:171,not:172,shy:173,reg:174,macr:175,deg:176,plusmn:177,sup2:178,sup3:179,acute:180,micro:181,para:182,middot:183,cedil:184,sup1:185,ordm:186,raquo:187,frac14:188,frac12:189,frac34:190,iquest:191,Agrave:192,Aacute:193,Acirc:194,Atilde:195,Auml:196,Aring:197,AElig:198,Ccedil:199,Egrave:200,Eacute:201,Ecirc:202,Euml:203,Igrave:204,Iacute:205,Icirc:206,Iuml:207,ETH:208,Ntilde:209,Ograve:210,Oacute:211,Ocirc:212,Otilde:213,Ouml:214,times:215,Oslash:216,Ugrave:217,Uacute:218,Ucirc:219,Uuml:220,Yacute:221,THORN:222,szlig:223,agrave:224,aacute:225,acirc:226,atilde:227,auml:228,aring:229,aelig:230,ccedil:231,egrave:232,eacute:233,ecirc:234,euml:235,igrave:236,iacute:237,icirc:238,iuml:239,eth:240,ntilde:241,ograve:242,oacute:243,ocirc:244,otilde:245,ouml:246,divide:247,oslash:248,ugrave:249,uacute:250,ucirc:251,uuml:252,yacute:253,thorn:254,yuml:255,OElig:338,oelig:339,Scaron:352,scaron:353,Yuml:376,fnof:402,circ:710,tilde:732,Alpha:913,Beta:914,Gamma:915,Delta:916,Epsilon:917,Zeta:918,Eta:919,Theta:920,Iota:921,Kappa:922,Lambda:923,Mu:924,Nu:925,Xi:926,Omicron:927,Pi:928,Rho:929,Sigma:931,Tau:932,Upsilon:933,Phi:934,Chi:935,Psi:936,Omega:937,alpha:945,beta:946,gamma:947,delta:948,epsilon:949,zeta:950,eta:951,theta:952,iota:953,kappa:954,lambda:955,mu:956,nu:957,xi:958,omicron:959,pi:960,rho:961,sigmaf:962,sigma:963,tau:964,upsilon:965,phi:966,chi:967,psi:968,omega:969,thetasym:977,upsih:978,piv:982,ensp:8194,emsp:8195,thinsp:8201,zwnj:8204,zwj:8205,lrm:8206,rlm:8207,ndash:8211,mdash:8212,lsquo:8216,rsquo:8217,sbquo:8218,ldquo:8220,rdquo:8221,bdquo:8222,dagger:8224,Dagger:8225,bull:8226,hellip:8230,permil:8240,prime:8242,Prime:8243,lsaquo:8249,rsaquo:8250,oline:8254,frasl:8260,euro:8364,image:8465,weierp:8472,real:8476,trade:8482,alefsym:8501,larr:8592,uarr:8593,rarr:8594,darr:8595,harr:8596,crarr:8629,lArr:8656,uArr:8657,rArr:8658,dArr:8659,hArr:8660,forall:8704,part:8706,exist:8707,empty:8709,nabla:8711,isin:8712,notin:8713,ni:8715,prod:8719,sum:8721,minus:8722,lowast:8727,radic:8730,prop:8733,infin:8734,ang:8736,and:8743,or:8744,cap:8745,cup:8746,"int":8747,there4:8756,sim:8764,cong:8773,asymp:8776,ne:8800,equiv:8801,le:8804,ge:8805,sub:8834,sup:8835,nsub:8836,sube:8838,supe:8839,oplus:8853,otimes:8855,perp:8869,sdot:8901,lceil:8968,rceil:8969,lfloor:8970,rfloor:8971,lang:9001,rang:9002,loz:9674,spades:9824,clubs:9827,hearts:9829,diams:9830},a=function(t){var e;return e=t.replace(/&([a-zA-Z]+);/,function(t,e){return h[e]?String.fromCharCode(h[e]):t}),e=e.replace(/&#x([0-9]+);/,function(t,e){return String.fromCharCode(parseInt(e,16))}),e=e.replace(/&#([0-9]+);/,function(t,e){return String.fromCharCode(e)})},n=function(t){this.text=t.value},n.prototype={toJson:function(){return this.decoded||(this.decoded=a(this.text))},toString:function(){return this.text},decodeCharacterReferences:function(){}},r=function(t,n){var r,s,o,a;if(this.type=u.ELEMENT,this.tag=t.tag,this.parentFragment=n,this.parentElement=n.parentElement,r=t.attributes.items,o=r.length){for(s=[],a=0;o>a;a+=1)s[a]={name:r[a].name.value,value:i(r[a].value.tokens,this.parentFragment.priority+1)};this.attributes=s}t.isSelfClosingTag||-1!==d.indexOf(t.tag.toLowerCase())||(this.fragment=new e(this,n.priority+1))},r.prototype={read:function(t){return this.fragment&&this.fragment.read(t)},toJson:function(t){var e,i,n,r,s;if(e={type:u.ELEMENT,tag:this.tag},this.attributes)for(e.attrs={},s=0;this.attributes.length>s;s+=1)i=this.attributes[s].name,r=""+this.attributes[s].value,n=r!==!1?r:this.attributes[s].value.toJson(),e.attrs[i]=n;return this.fragment&&this.fragment.items.length&&(e.frag=this.fragment.toJson(t)),e},toString:function(){var t,e,i,n,r,s,o;if(-1===c.indexOf(this.tag.toLowerCase()))return!1;if(s=this.fragment?""+this.fragment:"",s===!1)return!1;if(o=-1!==d.indexOf(this.tag.toLowerCase()),t="<"+this.tag,this.attributes)for(e=0,i=this.attributes.length;i>e;e+=1){if(-1!==this.attributes[e].name.indexOf(":"))return!1;if(n=" "+this.attributes[e].name,r=""+this.attributes[e].value,r===!1)return!1;""!==r&&(n+="=",n+=/[\s"'=<>`]/.test(r)?'"'+r.replace(/"/g,""")+'"':r),t+=n}return this.isSelfClosing&&!o?t+="/>":(t+=">",o?t:(t+=s,t+="</"+this.tag+">"))}},s=function(t,i){this.type=u.SECTION,this.parentFragment=i,this.ref=t.ref,this.inverted=t.type===u.INVERTED,this.formatters=t.formatters,this.i=t.i,this.fragment=new e(this,i.priority+1)},s.prototype={read:function(t){return this.fragment.read(t)},toJson:function(t){var e;return e={type:u.SECTION,ref:this.ref,frag:this.fragment.toJson(t)},this.formatters&&this.formatters.length&&(e.fmtrs=this.formatters.map(l)),this.inverted&&(e.inv=!0),this.priority&&(e.p=this.parentFragment.priority),this.i&&(e.i=this.i),e},toString:function(){return!1}},o=function(t,e){this.type=t.type,this.priority=e,this.ref=t.ref,this.formatters=t.formatters},o.prototype={toJson:function(){var t={type:this.type,ref:this.ref};return this.formatters&&(t.fmtrs=this.formatters.map(l)),this.priority&&(t.p=this.priority),t},toString:function(){return!1}},e=function(t,e){this.owner=t,this.items=[],t&&(this.parentElement=t.type===u.ELEMENT?t:t.parentElement),this.priority=e},e.prototype={read:function(t){if(this.sealed)return!1;if(this.isImplicitlyClosedBy(t))return this.seal(),!1;if(this.currentChild){if(this.currentChild.read(t))return!0;this.currentChild=null}return this.isExplicitlyClosedBy(t)?(this.seal(),!0):t.type===u.CLOSING||t.type===u.DELIMCHANGE||t.type===u.COMMENT?!1:t.type===u.SECTION||t.type===u.INVERTED?(this.currentChild=new s(t,this),this.items[this.items.length]=this.currentChild,!0):t.type===u.TAG?(this.currentChild=new r(t,this),this.items[this.items.length]=this.currentChild,!0):t.type===u.TEXT||t.type===u.ATTR_VALUE_TOKEN?(this.items[this.items.length]=new n(t),!0):(this.items[this.items.length]=new o(t,this.priority),!0)},isClosedBy:function(t){return this.isImplicitlyClosedBy(t)||this.isExplicitlyClosedBy(t)},isImplicitlyClosedBy:function(t){var e,i,n,r,s;return t.tag&&this.owner&&this.owner.type===u.ELEMENT?(r=this.owner.tag.toLowerCase(),s=t.tag.toLowerCase(),i=this.owner,n=i.parentElement||null,e=p[r],e&&!t.isClosingTag&&-1!==e.indexOf(s)?!0:-1!==f.indexOf(r)&&n&&n.fragment.isClosedBy(t)?!0:"p"===r&&n&&"a"===n.tag.toLowerCase()&&n.fragment.isClosedBy(t)?!0:void 0):!1},isExplicitlyClosedBy:function(t){return this.owner?this.owner.type===u.SECTION&&t.type===u.CLOSING&&t.ref===this.owner.ref?!0:this.owner.type===u.ELEMENT&&this.owner&&t.isClosingTag&&t.tag.toLowerCase()===this.owner.tag.toLowerCase()?!0:void 0:!1},toJson:function(t){var e,i,n,r=[];if(!t&&(n=""+this,n!==!1))return n;for(e=0,i=this.items.length;i>e;e+=1)r[e]=this.items[e].toJson(t);return r},toString:function(){var t,e,i,n="";for(t=0,e=this.items.length;e>t;t+=1){if(i=""+this.items[t],i===!1)return!1;n+=i}return n},seal:function(){this.sealed=!0}},i=function(t,i){for(var n,r=new e(null,i||0);t.length;)n=t.shift(),r.read(n);return r}})(e),function(t){var e,i,n,r,s,o,a,h,l,u,d,c,f,p,g,m,v,y,b;t.tokenize=function(t){var e=r.fromString(n(t));return e.tokens},r=function(){this.tokens=[],this.buffer=new s},r.prototype={read:function(t){var i,n;if(this.currentToken&&this.currentToken.type===e.TAG&&this.currentToken.read(t))return!0;if(this.buffer.read(t))return!0;if(i=this.buffer.convert())return this.currentToken&&this.currentToken.seal(),this.currentToken=this.tokens[this.tokens.length]=i,!0;if(n=this.buffer.release(),this.currentToken){for(;n.length;){for(;n.length&&this.currentToken.read(n.charAt(0));)n=n.substring(1);n.length&&("<"===n.charAt(0)?(this.currentToken=new l,this.currentToken.read("<")):(this.currentToken=new o,this.currentToken.read(n.charAt(0))),this.tokens[this.tokens.length]=this.currentToken,n=n.substring(1))}return!0}return this.currentToken="<"===t?new l:new o,this.currentToken.read(t),this.tokens[this.tokens.length]=this.currentToken,!0},end:function(){this.buffer.isEmpty()||(this.tokens[this.tokens.length]=this.buffer.convert())}},r.fromString=function(t){var e,i,n;for(e=new r,i=0,n=t.length;n>i;)e.read(t.charAt(i)),i+=1;return e.end(),e},s=function(){this.value=""},s.prototype={read:function(e){var i;return this.value+=e,i=this.isPartialMatchOf(t.delimiters[0])||this.isPartialMatchOf(t.tripleDelimiters[0])},convert:function(){var e,i,n,r,s,o;if(i=t.delimiters[0],n=t.tripleDelimiters[0],e=this.value,s=function(){return 0===e.indexOf(n)?new h:void 0},o=function(){return 0===e.indexOf(i)?new a:void 0},r=n.length>i.length?s()||o():o()||s()){for(;this.value.length;)r.read(this.value.charAt(0)),this.value=this.value.substring(1);return r}return!1},release:function(){var t=this.value;return this.value="",t},isEmpty:function(){return!this.value.length},isPartialMatchOf:function(t){return 0===t.indexOf(this.value)}},o=function(){this.type=e.TEXT,this.value=""},o.prototype={read:function(t){return this.sealed?!1:"<"===t?!1:(this.value+=t,!0)},seal:function(){this.sealed=!0}},a=function(){this.value="",this.openingDelimiter=t.delimiters[0],this.closingDelimiter=t.delimiters[1]},h=function(){this.value="",this.openingDelimiter=t.tripleDelimiters[0],this.closingDelimiter=t.tripleDelimiters[1],this.type=e.TRIPLE},a.prototype=h.prototype={read:function(t){return this.sealed?!1:(this.value+=t,this.value.substr(-this.closingDelimiter.length)===this.closingDelimiter&&this.seal(),!0)},seal:function(){var t,i,n,r,s;this.sealed||(t=this.value.replace(this.openingDelimiter,"").replace(this.closingDelimiter,"").trim(),"="===t.charAt(0)&&(this.changeDelimiters(t),this.type=e.DELIMCHANGE),this.type||(i=t.charAt(0),d[i]?(this.type=d[i],t=t.substring(1).trim()):this.type=e.INTERPOLATOR),this.type===e.SECTION&&(r=/:\s*([a-zA-Z_$][a-zA-Z0-9_$]*)$/,s=r.exec(t),s&&(this.i=s[1],t=t.substr(0,t.length-s[0].length))),n=t.split("|"),this.ref=n.shift().trim(),n.length&&(this.formatters=n.map(function(t){return t.trim()})),this.sealed=!0)},changeDelimiters:function(i){var n,r;r=/\=([^\s=]+)\s+([^\s=]+)=/.exec(i),n=this.type===e.TRIPLE?t.tripleDelimiters:t.delimiters,n[0]=r[1],n[1]=r[2]}},l=function(){this.type=e.TAG,this.openingBracket=new c,this.closingTagSolidus=new g,this.tagName=new f,this.attributes=new p,this.selfClosingSolidus=new g,this.closingBracket=new m},l.prototype={read:function(t){var e;return this.sealed?!1:(e=this.openingBracket.read(t)||this.closingTagSolidus.read(t)||this.tagName.read(t)||this.attributes.read(t)||this.selfClosingSolidus.read(t)||this.closingBracket.read(t))?(this.closingBracket.sealed&&this.seal(),!0):(this.seal(),!1)},seal:function(){this.tag=this.tagName.value,this.closingTagSolidus.value&&(this.isClosingTag=!0),this.selfClosingSolidus.value&&(this.isSelfClosingTag=!0),this.sealed=!0}},c=function(){},c.prototype={read:function(t){if(this.sealed)return!1;if("<"===t)return this.value="<",this.seal(),!0;throw'Expected "<", saw "'+t+'"'},seal:function(){this.sealed=!0}},f=function(){},f.prototype={read:function(t){return this.sealed?!1:!this.value&&/[a-zA-Z]/.test(t)?(this.value=t,!0):/[a-zA-Z0-9\-]/.test(t)?(this.value+=t,!0):(this.seal(),!1)},seal:function(){this.sealed=!0}},p=function(){this.items=[]},p.prototype={read:function(t){return this.sealed?!1:this.nextItem&&this.nextItem.read(t)?!0:i.test(t)?!0:(this.nextItem=new v,this.nextItem.read(t)?(this.items[this.items.length]=this.nextItem,!0):(this.seal(),!1))},seal:function(){this.sealed=!0}},v=function(){this.name=new y,this.value=new b},v.prototype={read:function(t){return this.sealed?!1:this.name.read(t)?!0:this.name.value?this.value.read(t)?!0:(this.seal(),!1):(this.seal(),!1)},seal:function(){this.sealed=!0}},y=function(){},y.prototype={read:function(t){return this.sealed?!1:this.value?/[_:a-zA-Z0-9\.\-]/.test(t)?(this.value+=t,!0):(this.seal(),!1):/[a-zA-Z_:]/.test(t)?(this.value=t,!0):(this.seal(),!1)},seal:function(){this.sealed=!0}},b=function(){this.tokens=[],this.buffer=new s,this.expected=!1},b.prototype={read:function(t){var e,n;if(this.sealed)return!1;if(!this.expected)return i.test(t)?!0:"="===t?(this.expected=!0,!0):!1;if(!this.tokens.length&&!this.quoteMark){if(i.test(t))return!0;if('"'===t||"'"===t)return this.quoteMark=t,!0}if(this.buffer.read(t))return!0;if(e=this.buffer.convert())return this.currentToken&&this.currentToken.seal(),this.currentToken=this.tokens[this.tokens.length]=e,!0;if(n=this.buffer.release(),this.currentToken){for(;n.length;){for(;n.length&&n.charAt(0)!==this.quoteMark&&this.currentToken.read(n.charAt(0));)n=n.substring(1);if(n.length&&n.charAt(0)!==this.quoteMark&&(this.currentToken=new u(this.quoteMark),this.currentToken.read(n.charAt(0)),this.tokens[this.tokens.length]=this.currentToken,n=n.substring(1)),n.charAt(0)===this.quoteMark)return this.currentToken.seal(),this.seal(),!0}return!0}return this.currentToken=new u(this.quoteMark),this.currentToken.read(t),this.tokens[this.tokens.length]=this.currentToken,this.currentToken.sealed&&this.seal(),!0},seal:function(){this.sealed=!0}},u=function(t){this.type=e.ATTR_VALUE_TOKEN,this.quoteMark=t||"",this.value=""},u.prototype={read:function(t){return this.sealed?!1:t===this.quoteMark?(this.seal(),!0):this.quoteMark?(this.value+=t,!0):/[\s"'=<>`]/.test(t)?(this.seal(),!1):(this.value+=t,!0)},seal:function(){this.sealed=!0}},g=function(){},g.prototype={read:function(t){return this.sealed?!1:"/"===t?(this.value="/",this.seal(),!0):(this.seal(),!1)},seal:function(){this.sealed=!0}},m=function(){},m.prototype={read:function(t){if(this.sealed)return!1;if(">"===t)return this.value=">",this.seal(),!0;throw'Expected ">", received "'+t+'"'},seal:function(){this.sealed=!0}},n=function(t){var e,i,n;for(n="";t.length;){if(e=t.indexOf("<!--"),i=t.indexOf("-->"),-1===e&&-1===i){n+=t;break}if(-1!==e&&-1===i)throw"Illegal HTML - expected closing comment sequence ('-->')";if(-1!==i&&-1===e||e>i)throw"Illegal HTML - unexpected closing comment sequence ('-->')";n+=t.substr(0,e),t=t.substring(i+3)}return n},e=t.types,i=/\s/,d={"#":e.SECTION,"^":e.INVERTED,"/":e.CLOSING,">":e.PARTIAL,"!":e.COMMENT,"&":e.INTERPOLATOR}}(e),e.formatters={equals:function(t,e){return t===e},greaterThan:function(t,e){return t>e},greaterThanEquals:function(t,e){return t>=e},lessThan:function(t,e){return e>t},lessThanEquals:function(t,e){return e>=t}},function(t){var e,i;t.ViewModel=function(t){this.data=t||{},this.pendingResolution=[],this.observers={},this.dependents=[]},t.ViewModel.prototype={set:function(t,i){var n,r,s,o,a,h,l;if("object"!=typeof t){for(this.dependents.forEach(function(e){e.setting||(e.setting=!0,e.fire("set",t,i),e.fire("set:"+t,i),e.setting=!1)}),r=e(t),o=this.data;r.length>1;)s=r.shift(),o=o[s]||{};for(s=r[0],o[s]=i,this._notifyObservers(t,i),a=this.pendingResolution.length;a--;)h=this.pendingResolution.splice(a,1)[0],l=this.getFullKeypath(h.view.model.ref,h.view.contextStack),void 0!==l?h.callback(l):this.registerUnresolvedKeypath(h)}else for(n in t)t.hasOwnProperty(n)&&this.set(n,t[n])},get:function(t){var i,n;if(!t)return void 0;for(i=e(t),n=this.data;i.length;)if(n&&(n=n[i.shift()]),void 0===n)return n;return n},update:function(t){if(t)this._notifyObservers(t,this.get(t));else for(t in this.data)this.data.hasOwnProperty(t)&&this._notifyObservers(t,this.get(t))},registerView:function(t){var e,i,n,r,s=this;return t.parentFragment&&t.model.ref in t.parentFragment.indexRefs?(r=t.parentFragment.indexRefs[t.model.ref],n=t.model.fmtrs?t.anglebars._format(r,t.model.fmtrs):r,t.update(n),void 0):(i=function(e){t.keypath=e,t.observerRefs=s.observe({keypath:e,priority:t.model.p||0,view:t}),n=s.get(e),t.model.fmtrs&&(n=t.anglebars._format(n,t.model.fmtrs)),t.update(n)},e=this.getFullKeypath(t.model.ref,t.contextStack),void 0===e?this.registerUnresolvedKeypath({view:t,callback:i}):i(e),void 0)},getFullKeypath:function(t,e){var i;if("."===t)return e[e.length-1];for(e=e.concat();e.length;)if(i=e.pop(),void 0!==this.get(i+"."+t))return i+"."+t;return void 0!==this.get(t)?t:void 0},registerUnresolvedKeypath:function(t){this.pendingResolution[this.pendingResolution.length]=t},_notifyObservers:function(t,e){var i,n,r,s,o,a=this,h=this.observers[t]||[];for(i=0;h.length>i;i+=1)if(r=h[i])for(n=0;r.length>n;n+=1)s=r[n],o=t!==s.originalAddress?a.get(s.originalAddress):e,s.view&&(s.view.model.fmtrs&&(o=s.view.anglebars._format(o,s.view.model.fmtrs)),s.view.update(o)),s.callback&&s.callback(o)},observe:function(t){var e,i,n=this,r=t.keypath,s=t.priority,o=[];if(2===arguments.length&&"string"==typeof arguments[0]&&"function"==typeof arguments[1])return this.observe({keypath:arguments[0],callback:arguments[1],priority:0});if(!t.keypath)return void 0;for(i=function(e){var i,a;i=n.observers[e]=n.observers[e]||[],i=i[s]=i[s]||[],a={originalAddress:r},t.view&&(a.view=t.view),t.callback&&(a.callback=t.callback),i[i.length]=a,o[o.length]={keypath:e,priority:s,observer:a}},e=t.keypath;-1!==e.lastIndexOf(".");)i(e),e=e.substr(0,e.lastIndexOf("."));return i(e),o},unobserve:function(t){var e,i,n;if(e=this.observers[t.keypath],e&&(i=e[t.priority])){if(i.indexOf)n=i.indexOf(t.observer);else for(var r=0,s=i.length;s>r;r+=1)if(i[r]===t.observer){n=r;break}-1!==n&&(i.splice(n,1),0===i.length&&delete e[t.priority],0===e.length&&delete this.observers[t.keypath])}},unobserveAll:function(t){for(;t.length;)this.unobserve(t.shift())}},t.ViewModel.prototype.cancelKeypathResolution=Array.prototype.filter?function(t){this.pendingResolution=this.pendingResolution.filter(function(e){return e.view!==t})}:function(t){var e,i=[];for(e=0;this.pendingResolution.length>e;e+=1)this.pendingResolution[e].view!==t&&(i[i.length]=this.pendingResolution[e]);this.pendingResolution=i},i=/\[\s*([0-9]+)\s*\]/g,e=function(t){var e;return e=t.replace(i,".$1"),e.split(".")}}(e),function(t,e){var i,n,r,s,o,a,h,l,u,d,c,f,p,g;n=t.types,s=t.isArray,o=t.isObject,a=function(t,e){return t.contains?t.contains(e):!0},r=function(t,i,n){var r,s,o,a=[];for(n=n||null,r=e.createElement("div"),r.innerHTML=t,o=r.childNodes.length,s=0;o>s;s+=1)a[s]=r.childNodes[s];for(s=0;o>s;s+=1)i.insertBefore(a[s],n);return a},c=function(t){this.model=t.model,this.anglebars=t.anglebars,this.viewmodel=t.anglebars.viewmodel,this.parentNode=t.parentNode,this.parentFragment=t.parentFragment,this.contextStack=t.contextStack||[],this.anchor=t.anchor,this.index=t.index,this.type=t.model.type,this.initialize(),this.viewmodel.registerView(this),!this.keypath&&this.model.inv&&this.update(!1)},i=function(t){if("string"==typeof t.model)return new h(t);switch(t.model.type){case n.INTERPOLATOR:return new f(t);case n.SECTION:return new g(t);case n.TRIPLE:return new p(t);case n.ELEMENT:return new l(t);case n.PARTIAL:return new u(t);default:throw"WTF? not sure what happened here..."}},t.DomFragment=function(t){var e,n,s,o,a;if("string"==typeof t.model)return this.nodes=r(t.model,t.parentNode,t.anchor),void 0;if(this.owner=t.owner,this.index=t.index,this.indexRefs={},this.owner&&this.owner.parentFragment){o=this.owner.parentFragment.indexRefs;for(a in o)o.hasOwnProperty(a)&&(this.indexRefs[a]=o[a])}for(t.indexRef&&(this.indexRefs[t.indexRef]=t.index),s={anglebars:t.anglebars,parentNode:t.parentNode,contextStack:t.contextStack,anchor:t.anchor,parentFragment:this},this.items=[],this.queue=[],e=t.model.length,n=0;e>n;n+=1)s.model=t.model[n],s.index=n,this.items[n]=i(s)},t.DomFragment.prototype={teardown:function(){var t;if(this.nodes)for(;this.nodes.length;)t=this.nodes.pop(),t.parentNode.removeChild(t);else for(;this.items.length;)this.items.pop().teardown()},firstNode:function(){return this.items[0]?this.items[0].firstNode():this.parentSection?this.parentSection.findNextNode(this):null},findNextNode:function(t){var e;return e=t.index,this.items[e+1]?this.items[e+1].firstNode():this.parentSection?this.parentSection.findNextNode(this):null}},u=function(e){this.fragment=new t.DomFragment({model:e.anglebars.partials[e.model.ref]||[],anglebars:e.anglebars,parentNode:e.parentNode,contextStack:e.contextStack,anchor:e.anchor,owner:this})},u.prototype={teardown:function(){this.fragment.teardown()}},h=function(t){this.node=e.createTextNode(t.model),this.index=t.index,this.anglebars=t.anglebars,this.parentNode=t.parentNode,this.parentNode.insertBefore(this.node,t.anchor||null)},h.prototype={teardown:function(){a(this.anglebars.el,this.node)&&this.parentNode.removeChild(this.node)},firstNode:function(){return this.node}},l=function(i){var n,r,s,o,a,h;if(r=this.model=i.model,this.anglebars=i.anglebars,this.viewmodel=i.anglebars.viewmodel,this.parentFragment=i.parentFragment,this.parentNode=i.parentNode,this.index=i.index,r.attrs&&r.attrs.xmlns){if(s=r.attrs.xmlns,"string"!=typeof s)throw"Namespace attribute cannot contain mustaches"}else s=this.parentNode.namespaceURI;this.node=e.createElementNS(s,r.tag),this.attributes=[];for(a in r.attrs)r.attrs.hasOwnProperty(a)&&(h=r.attrs[a],o=new d({owner:this,name:a,value:h,anglebars:i.anglebars,parentNode:this.node,contextStack:i.contextStack}),"value"!==a||!this.anglebars.twoway||"input"!==r.tag.toLowerCase()&&"textarea"!==r.tag.toLowerCase()||(n=o),this.attributes[this.attributes.length]=o);n&&this.bind(n,i.anglebars.lazy),r.frag&&("string"==typeof r.frag?this.node.innerHTML=r.frag:this.children=new t.DomFragment({model:r.frag,anglebars:i.anglebars,parentNode:this.node,contextStack:i.contextStack,anchor:null,owner:this})),this.parentNode.insertBefore(this.node,i.anchor||null)},l.prototype={bind:function(e,i){var n,r,s,o,a=this.viewmodel,h=this.node;if(r=!0,!e.fragment||1!==e.fragment.items.length||e.fragment.items[0].type!==t.types.INTERPOLATOR||e.fragment.items[0].model.formatters&&e.fragment.items[0].model.formatters.length)throw"Not a valid two-way data binding candidate - must be a single interpolator with no formatters";s=e.fragment.items[0],o=s.keypath||s.model.partialKeypath,n=function(){var t=h.value;"0"===t?t=0:""!==t&&(t=+t||t),a.set(o,t)},n(),h.addEventListener("change",n),i||h.addEventListener("keyup",n)},teardown:function(){for(a(this.anglebars.el,this.node)&&this.parentNode.removeChild(this.node),this.children&&this.children.teardown();this.attributes.length;)this.attributes.pop().teardown()},firstNode:function(){return this.node}},d=function(e){var i,n,r,s,o,a;if(i=e.name,n=e.value,this.owner=e.owner,r=i.indexOf(":"),-1!==r){if(s=i.substr(0,r),"xmlns"===s)o=null;else for(a=e.parentNode;a&&!o;)o=a.getAttribute("xmlns:"+s),a=a.parentNode||e.owner.parentFragment.owner.node||e.owner.parentFragment.owner.parentNode;o&&(this.namespace=o)}return"string"==typeof n?(o?e.parentNode.setAttributeNS(o,i.replace(s+":",""),n):e.parentNode.setAttribute(i,n),void 0):(this.parentNode=e.parentNode,this.name=i,this.children=[],this.parentFragment=this.owner.parentFragment,this.fragment=new t.TextFragment({model:n,anglebars:e.anglebars,owner:this,contextStack:e.contextStack}),this.ready=!0,this.update(),void 0)},d.prototype={teardown:function(){if(this.children)for(;this.children.length;)this.children.pop().teardown()},bubble:function(){this.update()},update:function(){var t=this.value;this.ready&&(this.value=""+this.fragment,this.value!==t&&(this.namespace?this.parentNode.setAttributeNS(this.namespace,this.name,this.value):this.parentNode.setAttribute(this.name,this.value)))}},f=function(t){c.call(this,t)},f.prototype={initialize:function(){this.node=e.createTextNode(""),this.parentNode.insertBefore(this.node,this.anchor||null)},teardown:function(){this.observerRefs?this.viewmodel.unobserveAll(this.observerRefs):this.viewmodel.cancelKeypathResolution(this),a(this.anglebars.el,this.node)&&this.parentNode.removeChild(this.node)},update:function(t){t!==this.text&&(this.text=t,this.node.data=t)},firstNode:function(){return this.node}},p=function(t){c.call(this,t)},p.prototype={initialize:function(){this.nodes=[]},teardown:function(){if(a(this.anglebars.el,this.parentNode))for(;this.nodes.length;)this.parentNode.removeChild(this.nodes.pop());this.observerRefs?this.viewmodel.unobserveAll(this.observerRefs):this.viewmodel.cancelKeypathResolution(this)},firstNode:function(){return this.nodes[0]?this.nodes[0]:this.parentFragment.findNextNode(this)},update:function(t){var e;if(t!==this.html){for(this.html=t;this.nodes.length;)this.parentNode.removeChild(this.nodes.pop());e=this.anchor||this.parentFragment.findNextNode(this),this.nodes=r(t,this.parentNode,e)}}},g=function(t){c.call(this,t)},g.prototype={initialize:function(){this.views=[],this.length=0},teardown:function(){this.unrender(),this.observerRefs?this.viewmodel.unobserveAll(this.observerRefs):this.viewmodel.cancelKeypathResolution(this)},firstNode:function(){return this.views[0]?this.views[0].firstNode():this.parentFragment.findNextNode(this)},findNextNode:function(t){return this.views[t.index+1]?this.views[t.index+1].firstNode():this.parentFragment.findNextNode(this)},unrender:function(){for(;this.views.length;)this.views.shift().teardown()},update:function(e){var i,n,r,a,h,l;if(h={model:this.model.frag,anglebars:this.anglebars,parentNode:this.parentNode,anchor:this.parentFragment.findNextNode(this),owner:this},l=s(e),l&&this.anglebars.modifyArrays&&t.modifyArray(e,this.keypath,this.anglebars.viewmodel),l&&0===e.length&&(i=!0),this.model.inv){if(e&&!i){if(this.length)return this.unrender(),this.length=0,void 0}else if(!this.length)return a=this.parentFragment.findNextNode(this),h.contextStack=this.contextStack,h.index=0,this.views[0]=new t.DomFragment(h),this.length=1,void 0}else if(l){if(e.length<this.length)for(r=this.views.splice(e.length,this.length-e.length);r.length;)r.pop().teardown();else if(e.length>this.length)for(n=this.length;e.length>n;n+=1)h.contextStack=this.contextStack.concat(this.keypath+"."+n),h.index=n,this.model.i&&(h.indexRef=this.model.i),this.views[n]=new t.DomFragment(h);this.length=e.length}else o(e)?this.length||(h.contextStack=this.contextStack.concat(this.keypath),h.index=0,this.views[0]=new t.DomFragment(h),this.length=1):e&&!i?this.length||(h.contextStack=this.contextStack,h.index=0,this.views[0]=new t.DomFragment(h),this.length=1):this.length&&(this.unrender(),this.length=0)}}}(e,document),function(t){var e,i,n,r,s,o,a,h,l,u;i=t.types,n=[],n[i.TEXT]="Text",n[i.INTERPOLATOR]="Interpolator",n[i.TRIPLE]="Triple",n[i.SECTION]="Section",r=t.isArray,s=t.isObject,a=function(t){this.model=t.model,this.anglebars=t.anglebars,this.viewmodel=t.anglebars.viewmodel,this.parent=t.parent,this.parentFragment=t.parentFragment,this.contextStack=t.contextStack||[],this.type=t.model.type,this.initialize&&this.initialize(),this.viewmodel.registerView(this),!this.keypath&&this.model.inv&&this.update(!1)},e=function(t){if("string"==typeof t.model)return new o(t.model);switch(t.model.type){case i.INTERPOLATOR:return new h(t);case i.TRIPLE:return new l(t);case i.SECTION:return new u(t);default:throw"Something went wrong in a rather interesting way"}},t.TextFragment=function(t){var i,n,r,s,o;if(this.owner=t.owner,this.index=t.index,this.items=[],this.indexRefs={},this.owner&&this.owner.parentFragment){s=this.owner.parentFragment.indexRefs;for(o in s)s.hasOwnProperty(o)&&(this.indexRefs[o]=s[o])}for(t.indexRef&&(this.indexRefs[t.indexRef]=t.index),r={anglebars:t.anglebars,parentFragment:this,parent:this.owner,contextStack:t.contextStack},i=t.model?t.model.length:0,n=0;i>n;n+=1)r.model=t.model[n],this.items[this.items.length]=e(r);
this.value=this.items.join("")},t.TextFragment.prototype={bubble:function(){this.value=this.items.join(""),this.parent.bubble()},teardown:function(){var t,e;for(t=this.items.length,e=0;t>e;e+=1)this.items[e].teardown()},toString:function(){return void 0===this.value?"":this.value}},o=function(t){this.text=t},o.prototype={toString:function(){return this.text},teardown:function(){}},h=function(t){a.call(this,t)},h.prototype={update:function(t){this.value=t,this.parent.bubble()},bubble:function(){this.parent.bubble()},teardown:function(){this.observerRefs?this.viewmodel.unobserveAll(this.observerRefs):this.viewmodel.cancelKeypathResolution(this)},toString:function(){return void 0===this.value?"":this.value}},l=h,u=function(t){a.call(this,t)},u.prototype={initialize:function(){this.children=[],this.length=0},teardown:function(){this.unrender(),this.observerRefs?this.viewmodel.unobserveAll(this.observerRefs):this.viewmodel.cancelKeypathResolution(this)},unrender:function(){for(;this.children.length;)this.children.shift().teardown();this.length=0},bubble:function(){this.value=this.children.join(""),this.parent.bubble()},update:function(e){var i,n,s,o;if(o=r(e),o&&this.anglebars.modifyArrays&&t.modifyArray(e,this.keypath,this.anglebars.viewmodel),o&&0===e.length&&(i=!0),this.model.inv)return e&&!i?this.length&&(this.unrender(),this.length=0):this.length||(this.children[0]=new t.TextFragment(this.model.frag,this.anglebars,this,this.contextStack),this.length=1),this.value=this.children.join(""),this.parent.bubble(),void 0;if("object"==typeof e)if(o){if(e.length<this.length)for(s=this.children.splice(e.length,this.length-e.length);s.length;)s.shift().teardown();else{for(n=0;this.length>n;n+=1)this.viewmodel.update(this.keypath+"."+n);if(e.length>this.length)for(n=this.length;e.length>n;n+=1)this.children[n]=new t.TextFragment(this.model.frag,this.anglebars,this,this.contextStack.concat(this.keypath+"."+n))}this.length=e.length}else this.length||(this.children[0]=new t.TextFragment(this.model.frag,this.anglebars,this,this.contextStack.concat(this.keypath)),this.length=1);else e&&!i?this.length||(this.children[0]=new t.TextFragment(this.model.frag,this.anglebars,this,this.contextStack),this.length=1):this.length&&(this.unrender(),this.length=0);this.value=this.children.join(""),this.parent.bubble()},toString:function(){return void 0===this.value?"":this.value}}}(e),function(t){t.extend=function(e){var i,n,r;i=this,n=function(){t.apply(this,arguments),this.init&&this.init.apply(this,arguments)};for(r in i.prototype)i.prototype.hasOwnProperty(r)&&(n.prototype[r]=i.prototype[r]);for(r in e)if(e.hasOwnProperty(r)){if(t.prototype.hasOwnProperty(r))throw Error('Cannot override "'+r+'" method or property of Anglebars prototype');n.prototype[r]=e[r]}return n.extend=i.extend,n}}(e),function(t){var e;t.modifyArray=function(t,i,n){var r,s,o,a;t._anglebars?(r=t._anglebars.viewmodels,s=t._anglebars.keypathsByIndex,o=r.indexOf(n),-1===o&&(o=r.length,r[o]=n),a=s[o],-1===a.indexOf(i)&&(a[a.length]=i)):(t._anglebars={viewmodels:[n],keypathsByIndex:[[i]]},e(t))},e=function(t){var e=function(t){var e,i;e=t._anglebars.viewmodels,i=t._anglebars.keypathsByIndex,e.forEach(function(e,n){var r=i[n];r.forEach(function(i){e.set(i,t)})})};["pop","push","reverse","shift","sort","splice","unshift"].forEach(function(i){t[i]=function(){var n=Array.prototype[i].apply(this,arguments);return e(t),n}})}}(e),"undefined"!=typeof module&&module.exports?module.exports=e:"function"==typeof define&&define.amd?define(function(){return e}):t.Anglebars=e})(this);