drupal-civicrm/sites/all/modules/civicrm/packages/backbone/backbone.collectionsubset.min.js

5 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2018-01-14 15:10:16 +02:00
/*! backbone.collectionsubset - v0.1.2 - 2012-12-20
* https://github.com/anthonyshort/backbone.collectionsubset
* Copyright (c) 2012 Anthony Short; Licensed MIT */
(function(_){Backbone.CollectionSubset=function(){function e(e){e==null&&(e={}),e=_.defaults(e,{refresh:!0,triggers:null,filter:function(){return!0},name:null,child:null,parent:null}),this.triggers=e.triggers?e.triggers.split(" "):[],e.child||(e.child=new e.parent.constructor),this.setParent(e.parent),this.setChild(e.child),this.setFilter(e.filter),e.model&&(this.child.model=e.model),e.refresh&&this.refresh(),this.name=e.name}return e.extend=Backbone.Model.extend,_.extend(e.prototype,Backbone.Events),e.prototype.setParent=function(e){var t,n=this;return(t=this.parent)!=null&&t.off(null,null,this),this.parent=e,this.parent.on("add",this._onParentAdd,this),this.parent.on("remove",this._onParentRemove,this),this.parent.on("reset",this._onParentReset,this),this.parent.on("change",this._onParentChange,this),this.parent.on("dispose",this.dispose,this),this.parent.on("loading",function(){return n.child.trigger("loading")},this),this.parent.on("ready",function(){return n.child.trigger("ready")},this)},e.prototype.setChild=function(e){var t;return(t=this.child)!=null&&t.off(null,null,this),this.child=e,this.child.on("add",this._onChildAdd,this),this.child.on("reset",this._onChildReset,this),this.child.on("dispose",this.dispose,this),this.child.superset=this.parent,this.child.filterer=this,this.child.url=this.parent.url,this.child.model=this.parent.model},e.prototype.setFilter=function(e){var t;return t=function(t){var n,r;return n=e.call(this,t),r=this.parent.filterer?this.parent.filterer.filter(t):!0,n&&r},this.filter=_.bind(t,this)},e.prototype.refresh=function(e){var t;return e==null&&(e={}),t=this.parent.filter(this.filter),this.child.reset(t,{subset:this}),this.child.trigger("refresh")},e.prototype._replaceChildModel=function(e){var t,n;t=this._getByCid(this.child,e.cid);if(t===e)return;return _.isUndefined(t)?this.child.add(e,{subset:this}):(n=this.child.indexOf(t),this.child.remove(t),this.child.add(e,{at:n,subset:this}))},e.prototype._onParentAdd=function(e,t,n){if(n&&n.subset===this)return;if(this.filter(e))return this._replaceChildModel(e)},e.prototype._onParentRemove=function(e,t,n){return this.child.remove(e,n)},e.prototype._onParentReset=function(e,t){return this.refresh()},e.prototype._onParentChange=function(e,t){if(!this.triggerMatched(e))return;return this.filter(e)?this.child.add(e):this.child.remove(e)},e.prototype._onChildAdd=function(e,t,n){var r;if(n&&n.subset===this)return;this.parent.add(e),r=this._getByCid(this.parent,e.cid);if(!r)return;return this.filter(r)?this._replaceChildModel(r):this.child.remove(e)},e.prototype._onChildReset=function(e,t){if(t&&t.subset===this)return;return this.parent.add(this.child.models),this.refresh()},e.prototype._getByCid=function(e,t){var n;return n=e.getByCid||e.get,n.apply(e,[t])},e.prototype.triggerMatched=function(e){var t;return this.triggers.length===0?!0:e.hasChanged()?(t=_.keys(e.changedAttributes()),_.intersection(this.triggers,t).length>0):!1},e.prototype.dispose=function(){var e,t,n,r,i;if(this.disposed)return;this.trigger("dispose",this),this.parent.off(null,null,this),this.child.off(null,null,this),typeof (t=this.child).dispose=="function"&&t.dispose(),this.off(),i=["parent","child","options"];for(n=0,r=i.length;n<r;n++)e=i[n],delete this[e];return this.disposed=!0},e}(),Backbone.Collection.prototype.subcollection=function(e){var t;return e==null&&(e={}),_.defaults(e,{child:new this.constructor,parent:this}),t=new Backbone.CollectionSubset(e),t.child},typeof module!="undefined"&&module!==null&&(module.exports=Backbone.CollectionSubset)}(_));