1
2
3
4
5
6
7
8
|
/*
YUI 3.17.2 (build 9c3c78e)
Copyright 2014 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
YUI.add("model-list",function(e,t){function c(){c.superclass.constructor.apply(this,arguments)}var n=e.Attribute.prototype,r=e.Lang,i=e.Array,s="add",o="create",u="error",a="load",f="remove",l="reset";e.ModelList=e.extend(c,e.Base,{model:e.Model,_isYUIModelList:!0,initializer:function(t){t||(t={});var n=this.model=t.model||this.model;typeof n=="string"&&(this.model=e.Object.getValue(e,n.split(".")),this.model||e.error("ModelList: Model class not found: "+n)),this.publish(s,{defaultFn:this._defAddFn}),this.publish(l,{defaultFn:this._defResetFn}),this.publish(f,{defaultFn:this._defRemoveFn}),this.after("*:idChange",this._afterIdChange),this._clear(),t.items&&this.add(t.items,{silent:!0})},destructor:function(){this._clear()},add:function(t,n){var s=t._isYUIModelList;return s||r.isArray(t)?i.map(s?t.toArray():t,function(t,r){var i=n||{};return"index"in i&&(i=e.merge(i,{index:i.index+r})),this._add(t,i)},this):this._add(t,n)},create:function(t,n,r){var i=this;return typeof n=="function"&&(r=n,n={}),n||(n={}),t._isYUIModel||(t=new this.model(t)),i.fire(o,e.merge(n,{model:t})),t.save(n,function(e){e||i.add(t,n),r&&r.apply(null,arguments)})},each:function(e,t){var n=this._items.concat(),r,i,s;for(r=0,s=n.length;r<s;r++)i=n[r],e.call(t||i,i,r,this);return this},filter:function(e,t){var n=[],r=this._items,i,s,o,u;typeof e=="function"&&(t=e,e={});for(i=0,o=r.length;i<o;++i)s=r[i],t.call(this,s,i,this)&&n.push(s);return e.asList?(u=new this.constructor({model:this.model}),n.length&&u.add(n,{silent:!0}),u):n},get:function(e){return this.attrAdded(e)?n.get.apply(this,arguments):this.invoke("get",e)},getAsHTML:function(t){return this.attrAdded(t)?e.Escape.html(n.get.apply(this,arguments)):this.invoke("getAsHTML",t)},getAsURL:function(e){return this.attrAdded(e)?encodeURIComponent(n.get.apply(this,arguments)):this.invoke("getAsURL",e)},getByClientId:function(e){return this._clientIdMap[e]||null},getById:function(e){return this._idMap[e]||null},invoke:function(e){var t=[this._items,e].concat(i(arguments,1,!0));return i.invoke.apply(i,t)},load:function(e,t){var n=this;return typeof e=="function"&&(t=e,e={}),e||(e={}),this.sync("read",e,function(r,i){var s={options:e,response:i},o;r?(s.error=r,s.src="load",n.fire(u,s)):(n._loadEvent||(n._loadEvent=n.publish(a,{preventable:!1})),o=s.parsed=n._parse(i),n.reset(o,e),n.fire(a,s)),t&&t.apply(null,arguments)}),this},map:function(e,t){return i.map(this._items,e,t)},parse:function(t){if(typeof t=="string")try{return e.JSON.parse(t)||[]}catch(n){return this.fire(u,{error:n,response:t,src:"parse"}),null}return t||[]},remove:function(e,t){var n=e._isYUIModelList;return n||r.isArray(e)?(e=i.map(n?e.toArray():e,function(e){return r.isNumber(e)?this.item(e):e},this),i.map(e,function(e){return this._remove(e,t)},this)):this._remove(e,t)},reset:function(t,n){t||(t=[]),n||(n={});var r=e.merge({src:"reset"},n);return t._isYUIModelList?t=t.toArray():t=i.map(t,function(e){return e._isYUIModel?e:new this.model(e)},this),r.models=t,n.silent?this._defResetFn(r):(this.comparator&&t.sort(e.bind(this._sort,this)),this.fire(l,r)),this},some:function(e,t){var n=this._items.concat(),r,i,s;for(r=0,s=n.length;r<s;r++){i=n[r];if(e.call(t||i,i,r,this))return!0}return!1},sort:function(t){if(!this.comparator)return this;var n=this._items.concat(),r;return t||(t={}),n.sort(e.rbind(this._sort,this,t)),r=e.merge(t,{models:n,src:"sort"}),t.silent?this._defResetFn(r):this.fire(l,r),this},sync:function(){var e=i(arguments,0,!0).pop();typeof e=="function"&&e()},toArray:function(){return this._items.concat()},toJSON:function(){return this.map(function(e){return e.toJSON()})},_add:function(t,n){var i,o;n||(n={}),t._isYUIModel||(t=new this.model(t)),o=t.get("id");if(this._clientIdMap[t.get("clientId")]||r.isValue(o)&&this._idMap[o]){this.fire(u,{error:"Model is already in the list.",model:t,src:"add"});return}return i=e.merge(n,{index:"index"in n?n.index:this._findIndex(t),model:t}),n.silent?this._defAddFn(i):this.fire(s,i),t},_attachList:function(e){e.lists.push(this),e.addTarget(this)},_clear:function(){i.each(this._items,this._detachList,this),this._clientIdMap={},this._idMap={},this._items=[]},_compare:function(e,t){return e<t?-1:e>t?1:0},_detachList:function(e){var t=i.indexOf(e.lists,this);t>-1&&(e.lists.splice(t,1),e.removeTarget(this))},_findIndex:function(e){var t=this._items,n=t.length,r=0,i,s,o;if(!this.comparator||!n)return n;o=this.comparator(e);while(r<n)s=r+n>>1,i=t[s],this._compare(this.comparator(i),o)<0?r=s+1:n=s;return r},_parse:function(e){return this.parse(e)},_remove:function(t,n){var i,s;n||(n={}),r.isNumber(t)?(i=t,t=this.item(i)):i=this.indexOf(t);if(i===-1||!t){this.fire(u,{error:"Model is not in the list.",index:i,model:t,src:"remove"});return}return s=e.merge(n,{index:i,model:t}),n.silent?this._defRemoveFn(s):this.fire(f,s),t},_sort:function(e,t,n){var r=this._compare(this.comparator(e),this.comparator(t));return r===0?r:n&&n.descending?-r:r},_afterIdChange:function(e){var t=e.newVal,n=e.prevVal,i=e.target;if(r.isValue(n)){if(this._idMap[n]!==i)return;delete this._idMap[n]}else if(this.indexOf(i)===-1)return;r.isValue(t)&&(this._idMap[t]=i)},_defAddFn:function(e){var t=e.model,n=t.get("id");this._clientIdMap[t.get("clientId")]=t,r.isValue(n)&&(this._idMap[n]=t),this._attachList(t),this._items.splice(e.index,0,t)},_defRemoveFn:function(e){var t=e.model,n=t.get("id");this._detachList(t),delete this._clientIdMap[t.get("clientId")],r.isValue(n)&&delete this._idMap[n],this._items.splice(e.index,1)},_defResetFn:function(e){if(e.src==="sort"){this._items=e.models.concat();return}this._clear(),e.models.length&&this.add(e.models,{silent:!0})}},{NAME:"modelList"}),e.augment(c,e.ArrayList)},"3.17.2",{requires:["array-extras","array-invoke","arraylist","base-build","escape","json-parse","model"]});
|