// Generated by CoffeeScript 1.3.3 /* knockback.js 0.15.4 (c) 2011, 2012 Kevin Malakoff. Knockback.js is freely distributable under the MIT license. See the following for full license details: https://github.com/kmalakoff/knockback/blob/master/LICENSE Dependencies: Knockout.js, Backbone.js, and Underscore.js. Optional dependency: Backbone.ModelRef.js. */(function(){var e,t,n,r,i,s={}.hasOwnProperty,o=function(e,t){function r(){this.constructor=e}for(var n in t)s.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};if(typeof require!="undefined")try{i=require("lodash")}catch(u){i=require("underscore")}else i=this._;i&&i.hasOwnProperty("_")&&(i=i._),e=!this.Backbone&&typeof require!="undefined"?require("backbone"):this.Backbone,r=!this.ko&&typeof require!="undefined"?require("knockout"):this.ko,t=n=this.Knockback=this.kb=typeof exports!="undefined"?exports:{},n.VERSION="0.15.4",n.locale_manager=void 0,n.stats={collection_observables:0,view_models:0},n.stats_on=!1,n.utils={},n.utils.legacyWarning=function(e,t,r){var i;return n._legacy_warnings||(n._legacy_warnings={}),(i=n._legacy_warnings)[e]||(i[e]=0),n._legacy_warnings[e]++,console.warn("warning: '"+e+"' has been deprecated (will be removed in Knockback "+t+"). "+r+".")},n.utils.wrappedObservable=function(e,t){if(arguments.length===1){if(!(e&&e.__kb&&e.__kb.observable))throw"Knockback: instance is not wrapping an observable";return e.__kb.observable}if(!e)throw"Knockback: no instance for wrapping a observable";return e.__kb||(e.__kb={}),e.__kb.observable&&e.__kb.observable.__kb&&(e.__kb.observable.__kb.instance=null),e.__kb.observable=t,t&&(t.__kb||(t.__kb={}),t.__kb.instance=e),t},n.wrappedObservable=function(e){return n.utils.legacyWarning("kb.wrappedObservable","0.16.0","Please use kb.utils.wrappedObservable instead"),n.utils.wrappedObservable(e)},n.utils.observableInstanceOf=function(e,t){return e?!e.__kb||!e.__kb.instance?!1:e.__kb.instance instanceof t:!1},n.utils.wrappedModel=function(e,t){if(arguments.length===1)return e&&e.__kb&&e.__kb.hasOwnProperty("model")?e.__kb.model:e;if(!e)throw"Knockback: no view_model for wrapping a model";return e.__kb||(e.__kb={}),e.__kb.model=t,t},n.viewModelGetModel=n.vmModel=function(e){return n.utils.legacyWarning("kb.vmModel","0.16.0","Please use kb.utils.wrappedModel instead"),n.utils.wrappedModel(e)},n.utils.setToDefault=function(e){var t,s,o;if(!e)return;if(r.isObservable(e))return typeof e.setToDefault=="function"?e.setToDefault():void 0;if(i.isObject(e)){o=[];for(t in e)s=e[t],o.push(s&&t!=="__kb"?n.utils.setToDefault(s):void 0);return o}},n.vmSetToDefault=function(e){return n.utils.legacyWarning("kb.vmSetToDefault","0.16.0","Please use kb.utils.release instead"),n.utils.setToDefault(e)},n.utils.release=function(e,t){var s,o;if(!e)return!1;if(!t&&(r.isObservable(e)||e instanceof n.Observables||typeof e.release=="function"||typeof e.destroy=="function"))return e.release?e.release():e.destroy?e.destroy():e.dispose&&e.dispose(),!0;if(i.isObject(e)&&typeof e!="function"){for(s in e){o=e[s];if(!o||s==="__kb")continue;n.utils.release(o)&&(e[s]=null)}return!0}return!1},n.vmRelease=function(e){return n.utils.legacyWarning("kb.vmRelease","0.16.0","Please use kb.utils.release instead"),n.utils.release(e)},n.vmReleaseObservable=function(e){return n.utils.legacyWarning("kb.vmReleaseObservable","0.16.0","Please use kb.utils.release instead"),n.utils.release(e)},n.utils.optionsCreateClear=function(e){return delete e.create,delete e.children,delete e.view_model,delete e.view_model_create},n.utils.optionsCreateOverride=function(e,t){return n.utils.optionsCreateClear(e),i.extend(e,t)},n.RefCountable=function(){function t(){this.__kb||(this.__kb={}),this.__kb.ref_count=1}return t.extend=e.Model.extend,t.prototype.__destroy=function(){},t.prototype.retain=function(){if(this.__kb.ref_count<=0)throw"RefCountable: ref_count is corrupt: "+this.__kb.ref_count;return this.__kb.ref_count++,this},t.prototype.release=function(){if(this.__kb.ref_count<=0)throw"RefCountable: ref_count is corrupt: "+this.__kb.ref_count;return this.__kb.ref_count--,this.__kb.ref_count||this.__destroy(),this},t.prototype.refCount=function(){return this.__kb.ref_count},t}(),n.Store=function(){function e(){this.keys=[],this.values=[]}return e.prototype.destroy=function(){var e,t,r,i;this.keys=null,r=this.values;for(e in r){t=r[e];if(!n.utils.observableInstanceOf(t,n.CollectionObservable))continue;this.values[e]=null;while(t.refCount()>0)t.release()}i=this.values;for(e in i){t=i[e];if(!t)continue;this.values[e]=null;if(t instanceof n.RefCountable)while(t.refCount()>0)t.release();else n.utils.release(t)}return this.values=null},e.prototype.registerValue=function(e,t){var r;return t instanceof n.RefCountable&&t.retain(),r=i.indexOf(this.keys,e),r>=0?this.values[r]=t:(this.keys.push(e),this.values.push(t)),t},e.prototype.resolveValue=function(e,t,r){var s,o;s=i.indexOf(this.keys,e);if(s>=0){if(this.values[s]){if(!(this.values[s]instanceof n.RefCountable&&this.values[s].refCount()<=0))return this.values[s]instanceof n.RefCountable?this.values[s].retain():this.values[s];this.values[s]=null}}else s=this.keys.length,this.keys.push(e),this.values.push(void 0);return o=t.apply(null,Array.prototype.slice.call(arguments,2)),this.keys[s]!==e?this.registerValue(e,o):this.values[s]||(o instanceof n.RefCountable&&o.retain(),this.values[s]=o),o},e.prototype.releaseValue=function(e){var t;if(e instanceof n.RefCountable){e.release();if(e.refCount()>0)return;t=i.indexOf(this.values,e);if(t>=0)return this.values[t]=0;return}return},e.prototype.addResolverToOptions=function(e,t){return i.extend(e,{store:this,store_key:t})},e.resolveFromOptions=function(e,t){if(!e.store||!e.store_key)return;return e.store.registerValue(e.store_key,t)},e}(),n.CollectionObservable=function(e){function t(e,s){var o,u,a=this;s==null&&(s={});if(!e)throw"CollectionObservable: collection is missing";t.__super__.constructor.apply(this,arguments),n.stats_on&&n.stats.collection_observables++,r.isObservable(s)&&s.hasOwnProperty("indexOf")?(n.utils.legacyWarning("kb.collectionObservable with an external ko.observableArray","0.16.0","Please use the kb.collectionObservable directly instead of passing a ko.observableArray"),u=n.utils.wrappedObservable(this,s),s=arguments[2]||{},o=!0):u=n.utils.wrappedObservable(this,r.observableArray([])),s.store_skip_resolve||n.Store.resolveFromOptions(s,n.utils.wrappedObservable(this)),s.store?this.__kb.store=s.store:(this.__kb.store=new n.Store,this.__kb.store_is_owned=!0);if(s.hasOwnProperty("view_model")){if(!s.view_model)throw"kb.CollectionObservable: options.view_model is empty";this.view_model_create_fn=s.view_model,this.view_model_create_with_new=!0}else if(s.hasOwnProperty("view_model_constructor")){if(!s.view_model_constructor)throw"kb.CollectionObservable: options.view_model_constructor is empty";n.utils.legacyWarning("kb.collectionObservable option view_model_constructor","0.16.0","Please use view_model option instead"),this.view_model_create_fn=s.view_model_constructor,this.view_model_create_with_new=!0}else if(s.hasOwnProperty("view_model_create")){if(!s.view_model_create)throw"kb.CollectionObservable: options.view_model_create is empty";this.view_model_create_fn=s.view_model_create}else if(s.hasOwnProperty("create")){if(!s.create)throw"kb.CollectionObservable: options.create is empty";this.view_model_create_fn=s.create}return this.sort_attribute=s.sort_attribute,this.sorted_index=s.sorted_index,this.__kb._onCollectionReset=i.bind(this._onCollectionReset,this),this.__kb._onCollectionResort=i.bind(this._onCollectionResort,this),this.__kb._onModelAdd=i.bind(this._onModelAdd,this),this.__kb._onModelRemove=i.bind(this._onModelRemove,this),this.__kb._onModelChange=i.bind(this._onModelChange,this),o&&e&&e.bind("change",function(){return n.utils.wrappedObservable(a).valueHasMutated()}),u.retain=i.bind(this.retain,this),u.refCount=i.bind(this.refCount,this),u.release=i.bind(this.release,this),u.collection=i.bind(this.collection,this),u.viewModelByModel=i.bind(this.viewModelByModel,this),u.sortedIndex=i.bind(this.sortedIndex,this),u.sortAttribute=i.bind(this.sortAttribute,this),u.hasViewModels=i.bind(this.hasViewModels,this),u.bind=i.bind(this.bind,this),u.unbind=i.bind(this.unbind,this),u.trigger=i.bind(this.trigger,this),this.collection(e,{silent:!0,defer:s.defer}),u}return o(t,e),t.prototype.__destroy=function(){this.collection(null),this.hasViewModels()&&this.__kb.store_is_owned&&(this.__kb.store.destroy(),this.__kb.store=null),this.view_model_create_fn=null,this.__kb.collection=null,n.utils.wrappedObservable(this,null),t.__super__.__destroy.apply(this,arguments);if(n.stats_on)return n.stats.collection_observables--},t.prototype.retain=function(){return t.__super__.retain.apply(this,arguments),n.utils.wrappedObservable(this)},t.prototype.release=function(){var e;return e=n.utils.wrappedObservable(this),t.__super__.release.apply(this,arguments),e},t.prototype.collection=function(e,t){var r,i,s;r=n.utils.wrappedObservable(this);if(arguments.length===0)return r(),this.__kb.collection;if(e===this.__kb.collection)return;this.__kb.collection&&(this._clear(),this._collectionUnbind(this.__kb.collection),typeof (i=this.__kb.collection).release=="function"&&i.release(),this.__kb.collection=null),this.__kb.collection=e;if(this.__kb.collection)return typeof (s=this.__kb.collection).retain=="function"&&s.retain(),this._collectionBind(this.__kb.collection),this.sortedIndex(this.sorted_index,this.sort_attribute,t)},t.prototype.sortedIndex=function(e,t,r){var s,o=this;return r==null&&(r={}),e?(this.sorted_index=e,this.sort_attribute=t):t?(this.sort_attribute=t,this.sorted_index=this._sortAttributeFn(t)):(this.sort_attribute=null,this.sorted_index=null),s=function(){var e;e=n.utils.wrappedObservable(o);if(o.__kb.collection.models.length===0&&e().length===0)return;o._collectionResync(!0);if(!r.silent)return o.trigger("resort",e())},r.defer?i.defer(s):s(),this},t.prototype.sortAttribute=function(e,t,n){return this.sortedIndex(t,e,n)},t.prototype.viewModelByModel=function(e){var t,r;return this.hasViewModels()?(r=n.utils.wrappedObservable(this),t=e.hasOwnProperty(e.idAttribute)?e.idAttribute:"cid",i.find(r(),function(n){return n.__kb.model[t]===e[t]})):null},t.prototype.hasViewModels=function(){return!!this.view_model_create_fn},t.prototype._collectionBind=function(e){var t,n,r,i,s,o,u;if(!e)return;e.bind("reset",this.__kb._onCollectionReset),this.sorted_index||e.bind("resort",this.__kb._onCollectionResort),o=["new","add"];for(n=0,i=o.length;n=0)o=o.replace("{"+i+"}",u),s=e.indexOf("{"+i+"}",s+1)}return o},n.parseFormattedString=function(e,t){var n,r,s,o,u,a,f,l,c,h,p,d,v,m;h=t.slice(),s=0,a=0,l={};while(h.search("\\{"+s+"\\}")>=0){f=t.indexOf("{"+s+"}");while(f>=0)h=h.replace("{"+s+"}","(.*)"),l[f]=s,a++,f=t.indexOf("{"+s+"}",f+1);s++}n=s,c=new RegExp(h),u=c.exec(e),u&&u.shift();if(!u||u.length!==a)return i.map(function(){m=[];for(var e=1;1<=n?e<=n:e>=n;1<=n?e++:e--)m.push(e);return m}.apply(this),function(){return""});d=i.sortBy(i.keys(l),function(e,t){return parseInt(e,10)}),r={};for(o in d){f=d[o],s=l[f];if(r.hasOwnProperty(s))continue;r[s]=o}p=[],s=0;while(s0&&e.release(),t.__super__.destroy.apply(this,arguments)},t.prototype.update=function(){var e,t,r,i=this;t=n.utils.wrappedModel(this);if(!t)return;r=t.get(this.key),e=this.__kb.value_observable();if(!e)return this.options.store?this.__kb.value_observable(this.options.store.resolveValue(r,function(){return n.collectionObservable(r,i.options)})):this.__kb.value_observable(n.collectionObservable(r,this.options));if(e.collection()!==r)return e.collection(r),this.__kb.value_observable.valueHasMutated()},t.prototype.read=function(){var e;return e=this.__kb.value_observable(),e?e():void 0},t}(n.AttributeConnector),n.collectionAttributeConnector=function(e,t,r){return new n.CollectionAttributeConnector(e,t,r)},n.ViewModelAttributeConnector=function(e){function t(){return t.__super__.constructor.apply(this,arguments),n.utils.wrappedObservable(this)}return o(t,e),t.prototype.destroy=function(){var e;return e=this.__kb.value_observable(),e&&typeof e.refCount=="function"&&e.refCount()>0&&e.release(),t.__super__.destroy.apply(this,arguments)},t.prototype.update=function(){var e,t,r,s,o=this;t=n.utils.wrappedModel(this);if(!t)return;r=t.get(this.key),e=this.__kb.value_observable();if(!e)return s=this.options.options?i.clone(this.options.options):{},s.store?this.__kb.value_observable(s.store.resolveValue(r,function(){return o.options.view_model?new o.options.view_model(r,s):o.options.view_model_create(r,s)})):this.__kb.value_observable(this.options.view_model?new this.options.view_model(r,s):this.options.view_model_create(r,s));if(!e.model||typeof e.model!="function")throw"kb.viewModelAttributeConnector: unknown how to model a view model";if(e.model()!==r)return e.model(r),this.__kb.value_observable.valueHasMutated()},t}(n.AttributeConnector),n.viewModelAttributeConnector=function(e,t,r){return new n.ViewModelAttributeConnector(e,t,r)},n.ViewModel=function(t){function r(t,s){var o,u,a,f;s==null&&(s={}),r.__super__.constructor.apply(this,arguments),n.stats_on&&n.stats.view_models++,s.store_skip_resolve||n.Store.resolveFromOptions(s,this),s.store?this.__kb.store=s.store:(this.__kb.store=new n.Store,this.__kb.store_is_owned=!0),this.__kb._onModelChange=i.bind(this._onModelChange,this),this.__kb._onModelLoaded=i.bind(this._onModelLoaded,this),this.__kb._onModelUnloaded=i.bind(this._onModelUnloaded,this),this.__kb.internals=s.internals,this.__kb.requires=s.requires,this.__kb.children=s.children,this.__kb.create=s.create,this.__kb.read_only=s.read_only,n.utils.wrappedModel(this,t),e.ModelRef&&t instanceof e.ModelRef&&(this.__kb.model_ref=t,this.__kb.model_ref.retain(),n.utils.wrappedModel(this,this.__kb.model_ref.getModel()),this.__kb.model_ref.bind("loaded",this.__kb._onModelLoaded),this.__kb.model_ref.bind("unloaded",this.__kb._onModelUnloaded)),this.__kb.model&&this._onModelLoaded(this.__kb.model);if(!this.__kb.internals&&!this.__kb.requires)return this;u=i.union(this.__kb.internals?this.__kb.internals:[],this.__kb.requires?this.__kb.requires:[]);if(!this.__kb.model_ref||this.__kb.model_ref.isLoaded())u=i.difference(u,i.keys(this.__kb.model.attributes));for(a=0,f=u.length;a