/*
Copyright 2013, KISSY UI Library v1.31
MIT Licensed
build time: Feb 18 14:59
*/
KISSY.add("menubutton/base",function(c,d,g,n,i,l,j){function f(a,q){var b=a.get("menu");if(b&&!b.isController)if(q)b=l.create(b,a),a.setInternal("menu",b);else return null;return b}function o(){var a,b,e=f(this);e&&e.get("visible")&&(a=e.get("align"),b=a.node,delete a.node,a=c.clone(a),a.node=b||this.get("el"),c.mix(a,k,!1),e.set("align",a))}var p=c.Env.host,m=d.all,h=d.KeyCodes,k={points:["bl","tl"],overflow:{adjustX:1,adjustY:1}},b=g.extend([l.DecorateChild],{_onSetCollapsed:function(a){if(a)(a=
f(this))&&a.hide();else{var a=this.get("el"),b=f(this,1);b&&!b.get("visible")&&(b.render(),this.bindMenu(),this.get("matchElWidth")&&b.set("width",m(b.get("align").node||a).innerWidth()),b.show(),o.call(this),a.attr("aria-haspopup",b.get("el").attr("id")))}},bindMenu:function(){var a=this,b=a.get("menu");b.on("afterActiveItemChange",function(b){a.set("activeItem",b.newVal)});a.__repositionBuffer=c.buffer(o,50);m(p).on("resize",a.__repositionBuffer,a);if(a.get("collapseOnClick"))b.on("click",function(){a.set("collapsed",
!0)});a.bindMenu=c.noop},handleKeyEventInternal:function(a){var b=f(this);if(a.keyCode==h.SPACE){if(a.preventDefault(),"keyup"!=a.type)return j}else if("keydown"!=a.type)return j;return b&&b.get("visible")?(b=b.handleKeydown(a),a.keyCode==h.ESC?(this.set("collapsed",!0),!0):b):a.keyCode==h.SPACE||a.keyCode==h.DOWN||a.keyCode==h.UP?(this.set("collapsed",!1),!0):j},performActionInternal:function(){this.set("collapsed",!this.get("collapsed"))},handleBlur:function(a){b.superclass.handleBlur.call(this,
a);this.set("collapsed",!0)},addItem:function(a,b){f(this,1).addChild(a,b)},removeItem:function(a,b){var e=f(this);e&&e.removeChild(a,b)},removeItems:function(a){var b=this.get("menu");b&&(b.removeChildren?b.removeChildren(a):b.children&&(b.children=[]))},getItemAt:function(a){var b=f(this);return b&&b.getChildAt(a)},_onSetDisabled:function(a){!a&&this.set("collapsed",!0)},decorateChildrenInternal:function(a,b){var e=this.get("menu");c.one(b[0].ownerDocument.body).prepend(b);this.setInternal("menu",
new a(c.mix({srcNode:b,prefixCls:this.get("prefixCls")},e)))},destructor:function(){var a;if(a=this.__repositionBuffer)m(p).detach("resize",a,this),a.stop();a=this.get("menu");a.destroy&&a.destroy()}},{ATTRS:{activeItem:{view:1},matchElWidth:{value:!0},collapseOnClick:{value:!1},decorateChildCls:{valueFn:function(){return this.get("prefixCls")+"popupmenu"}},menu:{value:{},setter:function(a){a instanceof i&&a.setInternal("parent",this)}},defaultChildXClass:{value:"popupmenu"},collapsed:{view:1},xrender:{value:n}}},
{xclass:"menu-button",priority:20});return b},{requires:["node","button","./baseRender","menu","component/base"]});
KISSY.add("menubutton/baseRender",function(c,d){return d.Render.extend({createDom:function(){this.get("el").append(c.substitute('<div class="{prefixCls}menu-button-dropdown"><div class="{prefixCls}menu-button-dropdown-inner"></div></div>',{prefixCls:this.get("prefixCls")})).attr("aria-haspopup",!0)},_onSetCollapsed:function(c){var d=this.get("el"),i=this.getCssClassWithPrefix("menu-button-open");d[c?"removeClass":"addClass"](i).attr("aria-expanded",!c)},_onSetActiveItem:function(c){this.get("el").attr("aria-activedescendant",
c&&c.get("el").attr("id")||"")}},{ATTRS:{contentEl:{valueFn:function(){return c.all(c.substitute('<div class="{prefixCls}menu-button-caption"></div>',{prefixCls:this.get("prefixCls")}))}},activeItem:{},collapsed:{value:!0}},HTML_PARSER:{contentEl:function(c){return c.children("."+this.get("prefixCls")+"menu-button-caption")}}})},{requires:["button"]});
KISSY.add("menubutton",function(c,d,g,n,i){d.Render=g;d.Select=n;d.Option=i;return d},{requires:["menubutton/base","menubutton/baseRender","menubutton/select","menubutton/option"]});KISSY.add("menubutton/option",function(c,d){var g=d.Item.extend({handleBlur:function(){return g.superclass.handleBlur.apply(this,arguments)}},{ATTRS:{selectable:{value:!0},textContent:{}}},{xclass:"option",priority:10});return g},{requires:["menu"]});
KISSY.add("menubutton/select",function(c,d,g,n,i,l){function j(b){var a=b.get("menu"),a=a.children||a.get&&a.get("children")||[],b=b.get("value"),c,e;for(e=0;e<a.length;e++)if(c=a[e],f(c)==b)return c;return null}function f(b){var a;if(b)if(b.get){if((a=b.get("value"))===l)a=b.get("textContent")||b.get("content")}else if((a=b.value)===l)a=b.textContent||b.content;return a}function o(b){var a=b.get("menu"),d=b.get("value"),b=a&&a.get&&a.get("children");c.each(b,function(a){a&&a.set&&a.set("selected",
f(a)==d)})}function p(){var b=j(this),a=this.get("menu");a.set("highlightedItem",b||a.getChildAt(0));b&&b.set("selected",!0)}function m(b){var a=j(b),c=a&&(a.textContent||a.get&&a.get("textContent")),a=a&&(a.content||a.get&&a.get("content"));b.set("content",c||a||b.get("defaultCaption"))}function h(b){b=b.target;if(b instanceof n.Item){var a=f(b),c=this.get("value");this.set("value",f(b));a!=c&&this.fire("change",{prevVal:c,newVal:a})}}var k=g.extend({bindUI:function(){this.on("click",h,this)},bindMenu:function(){k.superclass.bindMenu.call(this);
this.get("menu").on("show",p,this)},removeItems:function(){k.superclass.removeItems.apply(this,arguments);this.set("value",null)},removeItem:function(b){k.superclass.removeItem.apply(this,arguments);b.get("value")==this.get("value")&&this.set("value",null)},_onSetValue:function(){o(this);m(this)},_onSetDefaultCaption:function(){m(this)}},{ATTRS:{value:{},defaultCaption:{value:""},collapseOnClick:{value:!0}},decorate:function(b,a){b=c.one(b);a=a||{};a.elBefore=b;var f,e=[],g,h=null,i=b.val();b.all("option").each(function(a){var b=
{xclass:"option",content:a.text(),elCls:a.attr("class"),value:a.val()};i==a.val()&&(h={content:b.content,value:b.value});e.push(b)});c.mix(a,{menu:c.mix({children:e},a.menuCfg)});delete a.menuCfg;g=(new k(c.mix(a,h))).render();if(f=b.attr("name")){var j=(new d("<input type='hidden' name='"+f+"' value='"+i+"'>")).insertBefore(b,l);g.on("afterValueChange",function(a){j.val(a.newVal||"")})}b.remove();return g}},{xclass:"select",priority:30});return k},{requires:["node","./base","menu","./option"]});
