// Generated by CoffeeScript 1.3.1 /* knockback.js 0.15.0 (c) 2011 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. */ var Backbone,Knockback,kb,ko,_;if(typeof exports!=="undefined"){Knockback=kb=exports}else{this.Knockback=this.kb={}}Knockback.VERSION="0.15.0";_=!this._&&(typeof require!=="undefined")?require("underscore"):this._;Backbone=!this.Backbone&&(typeof require!=="undefined")?require("backbone"):this.Backbone;ko=!this.ko&&(typeof require!=="undefined")?require("knockout"):this.ko;Knockback.locale_manager;Knockback.stats={collection_observables:0,view_models:0};Knockback.stats_on=false;Knockback.utils={};Knockback.utils.legacyWarning=function(a,d,c){var b;kb._legacy_warnings||(kb._legacy_warnings={});(b=kb._legacy_warnings)[a]||(b[a]=0);kb._legacy_warnings[a]++;return console.warn("Legacy warning! '"+a+"' has been deprecated (will be removed in Knockback "+d+"). "+c+".")};Knockback.utils.wrappedObservable=function(a,b){if(arguments.length===1){if(!(a&&a.__kb&&a.__kb.observable)){throw new Error("Knockback: instance is not wrapping an observable")}return a.__kb.observable}if(!a){throw new Error("Knockback: no instance for wrapping a observable")}a.__kb||(a.__kb={});if(a.__kb.observable&&a.__kb.observable.__kb){a.__kb.observable.__kb.instance=null}a.__kb.observable=b;if(b){b.__kb||(b.__kb={});b.__kb.instance=a}return b};Knockback.wrappedObservable=function(a){kb.utils.legacyWarning("kb.wrappedObservable","0.16.0","Please use kb.utils.wrappedObservable instead");return kb.utils.wrappedObservable(a)};Knockback.utils.observableInstanceOf=function(b,a){if(!b){return false}if(!(b.__kb&&b.__kb.instance)){return false}return b.__kb.instance instanceof a};Knockback.utils.wrappedModel=function(a,b){if(arguments.length===1){if(a&&a.__kb&&a.__kb.hasOwnProperty("model")){return a.__kb.model}else{return a}}if(!a){throw new Error("Knockback: no view_model for wrapping a model")}a.__kb||(a.__kb={});a.__kb.model=b;return b};Knockback.viewModelGetModel=Knockback.vmModel=function(a){kb.utils.legacyWarning("kb.vmModel","0.16.0","Please use kb.utils.wrappedModel instead");return kb.utils.wrappedModel(a)};Knockback.utils.setToDefault=function(d){var b,c,a;if(!d){return}if(ko.isObservable(d)){return typeof d.setToDefault==="function"?d.setToDefault():void 0}else{if(_.isObject(d)){a=[];for(b in d){c=d[b];a.push(c&&(b!=="__kb")?kb.utils.setToDefault(c):void 0)}return a}}};Knockback.vmSetToDefault=function(a){kb.utils.legacyWarning("kb.vmSetToDefault","0.16.0","Please use kb.utils.release instead");return kb.utils.setToDefault(a)};Knockback.utils.release=function(c){var a,b;if(!c){return false}if(ko.isObservable(c)||(c instanceof kb.Observables)||(typeof c.release==="function")||(typeof c.destroy==="function")){if(c.release){c.release()}else{if(c.destroy){c.destroy()}else{if(c.dispose){c.dispose()}}}return true}else{if(_.isObject(c)&&!(typeof c==="function")){for(a in c){b=c[a];if(!b||(a==="__kb")){continue}if(kb.utils.release(b)){c[a]=null}}return true}}return false};Knockback.vmRelease=function(a){kb.utils.legacyWarning("kb.vmRelease","0.16.0","Please use kb.utils.release instead");return kb.utils.release(a)};Knockback.vmReleaseObservable=function(a){kb.utils.legacyWarning("kb.vmReleaseObservable","0.16.0","Please use kb.utils.release instead");return kb.utils.release(a)};kb.utils.optionsCreateClear=function(a){delete a.create;delete a.children;delete a.view_model;return delete a.view_model_create};kb.utils.optionsCreateOverride=function(a,b){kb.utils.optionsCreateClear(a);return _.extend(a,b)};var __hasProp={}.hasOwnProperty,__extends=function(d,b){for(var a in b){if(__hasProp.call(b,a)){d[a]=b[a]}}function c(){this.constructor=d}c.prototype=b.prototype;d.prototype=new c;d.__super__=b.prototype;return d};Knockback.CollectionObservable=(function(b){__extends(a,b);a.name="CollectionObservable";function a(f,c){var e,d,g=this;if(c==null){c={}}if(!f){throw new Error("CollectionObservable: collection is missing")}a.__super__.constructor.apply(this,arguments);if(Knockback.stats_on){kb.stats.collection_observables++}if(ko.isObservable(c)&&c.hasOwnProperty("indexOf")){kb.utils.legacyWarning("kb.collectionObservable with an external ko.observableArray","0.16.0","Please use the kb.collectionObservable directly instead of passing a ko.observableArray");d=kb.utils.wrappedObservable(this,c);c=arguments[2]||{};e=true}else{d=kb.utils.wrappedObservable(this,ko.observableArray([]))}if(!c.store_skip_resolve){kb.Store.resolveFromOptions(c,kb.utils.wrappedObservable(this))}if(c.store){this.__kb.store=c.store}else{this.__kb.store=new kb.Store();this.__kb.store_is_owned=true}if(c.hasOwnProperty("view_model")){if(!c.view_model){throw new Error("Knockback.CollectionObservable: options.view_model is empty")}this.view_model_create_fn=c.view_model;this.view_model_create_with_new=true}else{if(c.hasOwnProperty("view_model_constructor")){if(!c.view_model_constructor){throw new Error("Knockback.CollectionObservable: options.view_model_constructor is empty")}kb.utils.legacyWarning("kb.collectionObservable option view_model_constructor","0.16.0","Please use view_model option instead");this.view_model_create_fn=c.view_model_constructor;this.view_model_create_with_new=true}else{if(c.hasOwnProperty("view_model_create")){if(!c.view_model_create){throw new Error("Knockback.CollectionObservable: options.view_model_create is empty")}this.view_model_create_fn=c.view_model_create}else{if(c.hasOwnProperty("create")){if(!c.create){throw new Error("Knockback.CollectionObservable: options.create is empty")}this.view_model_create_fn=c.create}}}}this.sort_attribute=c.sort_attribute;this.sorted_index=c.sorted_index;this.__kb._onCollectionReset=_.bind(this._onCollectionReset,this);this.__kb._onCollectionResort=_.bind(this._onCollectionResort,this);this.__kb._onModelAdd=_.bind(this._onModelAdd,this);this.__kb._onModelRemove=_.bind(this._onModelRemove,this);this.__kb._onModelChange=_.bind(this._onModelChange,this);if(e&&f){f.bind("change",function(){return kb.utils.wrappedObservable(g).valueHasMutated()})}d.retain=_.bind(this.retain,this);d.refCount=_.bind(this.refCount,this);d.release=_.bind(this.release,this);d.collection=_.bind(this.collection,this);d.viewModelByModel=_.bind(this.viewModelByModel,this);d.sortedIndex=_.bind(this.sortedIndex,this);d.sortAttribute=_.bind(this.sortAttribute,this);d.hasViewModels=_.bind(this.hasViewModels,this);d.bind=_.bind(this.bind,this);d.unbind=_.bind(this.unbind,this);d.trigger=_.bind(this.trigger,this);this.collection(f,{silent:true,defer:c.defer});return d}a.prototype.__destroy=function(){this.collection(null);if(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;kb.utils.wrappedObservable(this,null);a.__super__.__destroy.apply(this,arguments);if(Knockback.stats_on){return kb.stats.collection_observables--}};a.prototype.retain=function(){a.__super__.retain.apply(this,arguments);return kb.utils.wrappedObservable(this)};a.prototype.release=function(){var c;c=kb.utils.wrappedObservable(this);a.__super__.release.apply(this,arguments);return c};a.prototype.collection=function(g,c){var f,d,e;f=kb.utils.wrappedObservable(this);if(arguments.length===0){f();return this.__kb.collection}if(g===this.__kb.collection){return}if(this.__kb.collection){this._clear();this._collectionUnbind(this.__kb.collection);if(typeof(d=this.__kb.collection).release==="function"){d.release()}this.__kb.collection=null}this.__kb.collection=g;if(this.__kb.collection){if(typeof(e=this.__kb.collection).retain==="function"){e.retain()}this._collectionBind(this.__kb.collection);return this.sortedIndex(this.sorted_index,this.sort_attribute,c)}};a.prototype.sortedIndex=function(g,c,d){var e,f=this;if(d==null){d={}}if(g){this.sorted_index=g;this.sort_attribute=c}else{if(c){this.sort_attribute=c;this.sorted_index=this._sortAttributeFn(c)}else{this.sort_attribute=null;this.sorted_index=null}}e=function(){var h;h=kb.utils.wrappedObservable(f);if((f.__kb.collection.models.length===0)&&(h().length===0)){return}f._collectionResync(true);if(!d.silent){return f.trigger("resort",h())}};if(d.defer){_.defer(e)}else{e()}return this};a.prototype.sortAttribute=function(d,e,c){return this.sortedIndex(e,d,c)};a.prototype.viewModelByModel=function(d){var c,e;if(!this.hasViewModels()){return null}e=kb.utils.wrappedObservable(this);c=d.hasOwnProperty(d.idAttribute)?d.idAttribute:"cid";return _.find(e(),function(f){return f.__kb.model[c]===d[c]})};a.prototype.hasViewModels=function(){return !!this.view_model_create_fn};a.prototype._collectionBind=function(j){var g,i,f,e,d,h,c;if(!j){return}j.bind("reset",this.__kb._onCollectionReset);if(!this.sorted_index){j.bind("resort",this.__kb._onCollectionResort)}h=["new","add"];for(i=0,e=h.length;i=0){a=a.replace("{"+d+"}",f);c=g.indexOf("{"+d+"}",c+1)}}return a};Knockback.parseFormattedString=function(h,m){var i,a,j,k,g,p,o,f,l,b,e,n,d,c;b=m.slice();j=0;p=0;f={};while(b.search("\\{"+j+"\\}")>=0){o=m.indexOf("{"+j+"}");while(o>=0){b=b.replace("{"+j+"}","(.*)");f[o]=j;p++;o=m.indexOf("{"+j+"}",o+1)}j++}i=j;l=new RegExp(b);g=l.exec(h);if(g){g.shift()}if(!g||(g.length!==p)){return _.map((function(){c=[];for(var q=1;1<=i?q<=i:q>=i;1<=i?q++:q--){c.push(q)}return c}).apply(this),function(){return""})}n=_.sortBy(_.keys(f),function(q,r){return parseInt(q,10)});a={};for(k in n){o=n[k];j=f[o];if(a.hasOwnProperty(j)){continue}a[j]=k}e=[];j=0;while(j