(function(){"use strict";u8.Base.ItemsModelMixIn=function(){};$.extend(u8.Base.ItemsModelMixIn,{isLoaded:function(){return!!this.getItems()},getItems:function(){return this.items},setItems:function(e){this.items=e},getItem:function(e){var t=this.getItems();return t&&t.find(u8._.eqish("id",e))},indexOf:function(e){var t=this.getItems();return t?t.indexWhere(u8._.eq("id",e)):-1},createItemModel:function(e,t){if(u8.tools.isInstanceOfClass(t,e)){return t}t=u8.create(e,t);for(var i in this._itemAttributeFactories){var r=this._itemAttributeFactories[i];t.setAttribute(i,r&&r(t)||false)}return t},setSelectedItems:function(i,r){var s=this.getItems();if(!s){return}var n=this.getSelectedItems();var u=this;if(!i||i=="clear"){s.each(o.curry(false));d();return}if(!u8.tools.isArray(i)){i=[i]}i=i.map(this.getItem,this).clean();if(r){i.each(a)}else{var m=i.toObject(u8._.attr("id"));s.each(function(e){o(e.id in m,e)})}function a(t){if(r=="select"){o(true,t)}else if(r=="deselect"){o(false,t)}else if(r=="toggle"){o(!e(t),t)}}function o(e,i){t(i,e)}function d(){var e=u.getSelectedItems();if(e.length==1){u._setFocusedItem(e[0])}if(!n.compare(e)){u.raise("Update",{reason:"selection",what:"item"});u.raise("SelectedItemsChange",{value:e})}}d()},getSelectedItems:function(){var t=this.getItems();if(!t){return[]}return t.filter(e)},setItemAttributeFactory:function(e,t){if(!this._itemAttributeFactories){this._itemAttributeFactories={}}if(this._itemAttributeFactories[e]==t){return}this._itemAttributeFactories[e]=t;this.getItems().each(function(i){i.setAttribute(e,t&&t(i)||false)})},setVirtualItem:function(e){if(!this.virtualItemEquals){$error("This model does not support virtual item management")}var t=this._virtualItem;if(!e&&!t){return}if(e&&t&&e.id==t.id&&this.virtualItemEquals(e,t)){return}this._virtualItem=e;var i={reason:"virtualItem",item:e&&this.getItem(e.id)};if(this.virtualItemFinishUpdateArguments){this.virtualItemFinishUpdateArguments(i,e)}this.raise("Update",i)},getVirtualItem:function(){return this._virtualItem},getOverlayItem:function(e){if(u8.tools.isClass(e)){return e}var t=this.getItem(e.id);if(t){var i=$.extend({__proto__:t},e);if(i.hasOwnProperty("__proto__")){var r=function(){};r.prototype=t;i=new r;$.extend(i,e)}return i}return e},setActiveItem:function(e){var t=e?typeof e=="string"?e:e.id:null;e=t?this.getItem(t):null;if(e){t=e.id}if(t==this._activeItemId){return}this._activeItemId=t;this.raise("Update",{reason:"activeItem",item:e})},getActiveItem:function(){return this.getItem(this._activeItemId)},updateItemsPreservingSelection:function(e){var i=this.getSelectedItems().map(u8._.attr("id"));this.setItems(e);this.getItems().each(function(e){if(!e.id&&e.uid){e.id=u8.recordUidToString(e.uid)}if(i.contains(e.id)){t(e,true);i.remove(e.id)}});if(i.length>0){this.raise("Update",{reason:"selection",what:"items"});this.raise("SelectedItemsChange",{value:this.getSelectedItems()})}},deleteItem:function(e,t){var i=this._getDeleteItemProvider();var r=this.getItem(e.item.id);if(!r){return t&&t()}if(i.deleteItem){i.deleteItem($.extend({},e,{item:r}),t)}else{i.deleteItems($.extend({},e,{items:[r]}),t)}},deleteItems:function(e,t){var i=this._getDeleteItemProvider();if(!e||!e.items||e.items.length==0){return t&&t()}var r=e.items.mapAndFilter([u8._.attr("id"),this.getItem],u8._.trueish(),this);if(r.length==0){return t&&t()}if(i.deleteItems){i.deleteItems($.extend({},e,{items:r}),t)}else{Function.join(r.map(function(t){return i.deleteItem.bind(i,$.extend({},e,{item:t}))}))(t)}},registerItemSelection:function(){if(this.registerSelection){this.registerSelection({id:"item",getterName:"getSelectedItems",setterName:"setSelectedItems"})}else if(this.addAdapter){this.addAdapter({id:"uids",model:this,clearOtherSelections:true,eventName:"SelectedItemsChange",getter:function(e){var t=e.getSelectedItems();if(!t||t.length<1){return null}return t.map(u8._.attr("uid"))},setter:function(e,t,i){if(t!=="clear"){t=t.map(u8.recordUidToString)}e.setSelectedItems(t,i)},projections:{uid:function(e){return!e?null:e[0]}}})}},_getDeleteItemProvider:function(){if(!this.getItemProvider){$error("This model does not support item management")}var e=this.getItemProvider();if(!e){$error("This model does not support item management at the moment")}if(!e.deleteItem&&!e.deleteItems){$error("This model does not support item deletion")}return e},_setFocusedItem:function(e){if(!e){return}this.focusedItem=e;if(e.setFocus){e.setFocus()}}});function e(e){return e.getSelected?e.getSelected():e.selected}function t(e,t){if(e.setSelected){e.setSelected(t)}else{e.selected=t}}})();