(function(){"use strict";u8.Base.Widgets.CardPanel=function(e,t){this.Panel(e,$.extend({},u8.Base.Widgets.CardPanel.defaults,t));if(this.hideItemsIfEmpty){this.createOnDemand=false}if(!("value"in this)){this["value"]=-1}if(!this.items){this.items=[]}this._isCreated=false;this._create()};u8.Base.Widgets.CardPanel.makeClass({name:"CardPanel",deriveFrom:u8.Base.Widgets.Panel,events:["ValueChange"]});u8.Base.Widgets.CardPanel.defaults={createOnDemand:true};$.extend(u8.Base.Widgets.CardPanel.prototype,{getValue:function(){return this.value},setValue:function(e){e=e||0;if(e>=this.items.length){e=this.items.length-1}else if(e<0||isNaN(e)){e=-1}if(this.value==e){return}var t=this.at(e);this.value=e;if(t.length){t.siblings().css("display","none");t.css("display","");this._assertItemContent(e)}else{this.$(this.element).children().css("display","none")}if(this._isCreated){this.raise("ValueChange",{value:e,el:t[0]});u8.services.commands.execute(this,"Widget/size",{el:this.element})}},at:function(e){return e==-1?this.$():this.$(this.element).children(":eq("+e+")")},indexOf:function(e){if(u8.tools.isInstanceOfClass(e,"Widget")){e=e.__CardPanel_value}return this.children.indexWhere(u8._.eqish("__CardPanel_value",e))},add:function(e,t){var i=this._createItemElement(e);if(this.createOnDemand){i.data("u8CardPanel_createMe",true)}else{this._createItemContent(i)}if(this.hideItemsIfEmpty===true&&this._shouldIgnoreItem(e,i)){e.ignore=true}i.css("display",t);var a=this.$(this.element);var s=a.children().length;if(typeof t=="number"&&t<s){var r=t;if(i.parent().get(0)==this.element){var n=u8.Base.Dom.indexOfChild(this.element,i);if(n==t){return}if(n<t){r++}}if(r>=s){a.append(i)}else{this.at(r).before(i)}}else{a.append(i)}if(t<=this.value){this.setValue(this.value+1)}},remove:function(e){var t=this.at(e);t.removeData("u8CardPanel").remove();if(e==this.value){this.value=-1;this.setValue(e)}else if(this.value>=e){this.setValue(Math.max(this.value-1,0))}return t},focus:function(){var e=u8.Base.Widgets.Tools.getFirstFocusableDescendant(this);if(e){e.focus()}},_create:function(){this.$(this.element).addClass("u8-form-cardpanel");if(this.designer){this.$(this.element).addClass("u8-formcardpanel-designer")}for(var e=0;e<this.items.length;e++){var t=this.items[e];this.add(t)}var i=this.value||0;delete this.value;this.setValue(i);u8.tools.createdWhenReady(this)},_assertItemContent:function(e){var t=this.at(e);var i=t.data("u8CardPanel_createMe");if(!i){return}t.removeData("u8CardPanel_createMe");var a=this._createItemContent(t);if(!a){return}a.__CardPanel_value=e;if(!this.designer){u8.Base.ValidationManager.addToWidget(a)}}});$.extend(u8.Base.Widgets.CardPanel.prototype,{"Designer.getDropTargets":function(e){if(e.kind!="widget"||e.property!="items"){return[]}var t=this.$(this.element);var i=t.offset();var a=$u(".CardPanel",t);var s=!!a.length;if(!s){return[{rect:{left:i.left,top:i.top,width:t.outerWidth(),height:t.outerHeight()},arrayName:"items"}]}var r=t.find(".u8-form-widget:visible").eq(0);var n=r.offset();if(!r.length){return[]}return[{rect:{left:i.left,top:i.top,width:t.innerWidth(),height:n.top-i.top},arrayName:"items"},{rect:{left:i.left,top:n.top+r.outerHeight(),width:t.innerWidth(),height:i.top+t.innerHeight()-(n.top+r.outerHeight())},arrayName:"items"}]},"Designer.insertItem":function(e){if(e.arrayName=="items"){this.add(e.element,e.arrayIndex);var t=typeof e.arrayIndex=="number"?e.arrayIndex:this.items.length-1;this.setValue(t);this._assertItemContent(t)}},"Designer.removeItem":function(e){if(e.arrayName=="items"){return this.remove(e.arrayIndex)}},"Designer.executeAction":function(e){var t=this.getValue();switch(e.actionName){case"prev":t--;break;case"next":t++;break}var i=this.$(this.element).children().length;if(t>=i){t=0}else if(t<0){t=i-1}this.setValue(t)},"Designer.ensureChildVisible":function(e){if(e.arrayIndex!=this.getValue()){this.setValue(e.arrayIndex)}}});$.extend(u8.Base.Widgets.CardPanel,{"Designer.prepareModelForDesignMode":function(e){var t=e.model.options;var i=t?t.items:[];if(!i.length){i.push({})}}})})();