(function(){"use strict";u8.Base.Widgets.BreadCrumbs=function(e,t){this.Widget(e,$.extend({},u8.Base.Widgets.BreadCrumbs.defaults,t));this._create()};u8.Base.Widgets.BreadCrumbs.defaults={command:"Actions/Default",style:"default",theme:{css:{root:"u8-breadcrumbs"}}};u8.Base.Widgets.BreadCrumbs.makeClass({name:"BreadCrumbs",deriveFrom:u8.Base.Widgets.Widget,events:["Create","ItemClick","ItemActionsClick"]});$.extend(u8.Base.Widgets.BreadCrumbs.prototype,{update:function(){this._create();return this},execute:function(e){if(typeof e=="string"){e={name:e}}var t,i=e.name;if(i=="ItemsPopup"){this._showMenu({elSender:e.elSender,elEvent:e.elEvent})}else if(i=="ItemAction"){t={elSender:e.elSender,elEvent:e.elEvent};this.raise("ItemActionsClick",t);if(t.cancel){return undefined}this._showMenu({elSender:e.elSender,elEvent:e.elEvent,item:e.item})}else if(!i){this._hideMenu();var a=e.item;if(a&&!a.disabled){t={item:a,elSender:e.elSender,elEvent:e.elEvent};this.raise("ItemClick",t);if(t.cancel){return undefined}var s=this.$(e.elEvent.target);var n=s.filter("a.item")[0];if(!n){n=s.parents("a.item")[0]}var r=a.command||this.command;if(r){$command(this,r,{item:a,uid:a.uid,elSender:n,elEvent:e.elEvent})}}}return this},_create:function(){var e=this;this._load(function(){if(e._isDisposed){return}e._createView();e.layout().raise("Create",{})})},_createView:function(){page.factory.scripts.load(page.resolveUrl("~/scripts/lib/purify/purify"+(u8.debug.level?"":".min")+".js"));var e=this.$(this.element).empty().attr("data-style",this.style||"default").addClass(this.theme.css.root);this._popupItems=[];var t=this.items;if(!t||t.length<=0){return}var i="<ul>";for(var a=0;a<t.length;a++){var s=t[a];if(!s.text&&!s.image){continue}if(a){i+="<li class='img separator'></li>"}i+="<li data-index='"+a+"'>";if(s.disabled!==true){var n=u8.browser.msie?"Text":"text/plain";var r=DOMPurify.sanitize(s.text);i+='<a class="item" href="javascript:void(0)" draggable="true" ondragstart="event.dataTransfer.setData(\''+n+"','"+r+"')\";>"}else{i+="<span class='item'>"}if(s.image){i+=u8.images.html(s.image,"s")}if(s.text){i+="<span>"+u8.Base.Html.escape(s.text)+"</span>"}if(s.disabled!==true){i+="</a>";if(s.actions){i+="<a href='javascript:void(0)' class='itemaction' data-command='ItemAction' data-state='down'><span class='img'></span></a>"}}else{i+="</span>"}i+="</li>"}i+="</ul>";e.html(i);var l=e.children("ul").on("click",this._onClick.bind(this));var d=l[0];var o=e.width();if(u8.browser.msie){o+=1}if(d.scrollWidth-o>1){var m;var u;var c=Math.floor(t.length/2+.5)-1,h=c;var f=[],v=[];var p=false;for(a=0;a<t.length;a++){if(d.scrollWidth<=o){break}if(!(a%2)){if(c<0){continue}u=c--;f.unshift(t[u])}else{if(h>=t.length-2){continue}u=++h;v.push(t[u])}m=l.children("[data-index='"+u+"']");if(!p){this.$("<li class='u8-breadcrumbs-popup' data-command='ItemsPopup'><a href='javascript:void(0)' class='itemaction' data-state='down'><span>...</span><span class='img'></span></a></li>"+"<li class='img separator'></li>").insertBefore(m);p=true}m.next().remove();m.remove()}this._popupItems=f.concat(v)}},_load:function(e){if(!this.items){this.items=[]}e()},_onClick:function(e){var t=e.target;var i=$$(t).closest("[data-command]");var a=i.attr("data-command");var s=this.$(t).closest("li[data-index]").attr("data-index");var n=this.items&&this.items[s];e.preventDefault();e.stopPropagation();this.execute({name:a,item:n,elSender:i[0],elEvent:e})},_hideMenu:function(){var e=this._menu;if(e&&e.isShown()){delete this._menu;e.hide()}},_showMenu:function(e){var t=e.item;var i=this._menu;var a=i&&i.isShown()&&i.__breadCrumbs_item===t;this._hideMenu();if(a){return}var s;if(t){s=e.item.actions}else{var n=this._popupItems;var r=[],l;for(var d=0;d<n.length;d++){t=n[d];l=t.actions&&t.actions.rootItem;r.push({image:t.image,label:t.text,action:this.execute.bind(this,{item:t}),children:l&&l.clone().children})}s=new u8.Base.Widgets.MenuModel({rootItem:{children:r}})}var o=e.elEvent;if(o){o.stopPropagation();o.preventDefault()}var m=this.$(e.elSender);var u=m.hasClass("itemaction")?m:m.find(".itemaction");u.attr("data-state","up");this._menu=u8.widget("ContextMenu",$w(m),{alignTo:m.first(),model:s,parent:this,disposeOnHide:true,position:{anchor:"center-top",dy:-3},view:{imageDecorator:"s"},onHide:function(e,t){u.attr("data-state","down")},__breadCrumbs_item:e.item});this._menu.show()}})})();