/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
/**
 * Created by liushuai on 2015/6/15.
 */
define(["adoreapp/widget/Composite"], function (Composite) {
    return Composite.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                base._initWidget.call(this, attrs);
                this.template = attrs.template;
                this.typeFlag = "";
                this.instance = "Toolbar"
            },

            toString: function () {
                return this.typeFlag;
            },

            createDropDownBox:function()
            {
                var own = this;
                var dropDownListWidth = 108;
                this.ulNode = $("<ul>").addClass("card-panel adore_toolbar_dropDownList")
                    .insertAfter(this.jqueryNode)
                    .css({width: dropDownListWidth, display: "none", height: 0, padding:0})
                    .on('click', function () {
                        $(this).attr("popup", true).css({height: 0, display: "none"});
                    });
                this.dropDownBtn = $('<a class="waves-effect btn-flat adore_button adore_button_flat btn-large" style="color:white" tabindex="-1"><i style="position: absolute;right: 10px;font-size: 2.4em" class="mdi-navigation-more-vert"></i><span></span></a>')
                    .appendTo(this.attachNode).on('click', function () {
                        if (parseInt(own.ulNode.css("height").replace("px", "")) == 0) {
                            own.ulNode.attr("popup", true).css({height: own.children.length * 54, display: "block"});
                        } else {
                            own.ulNode.attr("popup", true).css({height: 0, display: "none"});
                        }
                    });
                this.attachNode = this.ulNode;
            },

            _layout: function (attrs) {
                base._layout.call(this, attrs);
                //add dropDownList
                var own = this;
                var originAttachNode = this.attachNode;
                var dropDownListWidth = 108
                if (parseInt(this.jqueryNode.css("width").replace("px", "")) < dropDownListWidth) {
                    this.typeFlag = "ToolBar";
                    this.createDropDownBox();
                }

                $(window).resize(function () {
                    if (parseInt(own.jqueryNode.css("width").replace("px", "")) < dropDownListWidth)
                    {
                        if(own.attachNode == originAttachNode)
                        {
                            //
                            if(own.ulNode)
                            {
                                own.attachNode = own.ulNode;
                                own.ulNode.css({display:"none", height:0});
                                own.dropDownBtn.css({display:"block"});
                            }else
                            {
                                own.createDropDownBox();
                            }
                            own.typeFlag = "ToolBar";
                            for(var i = 0; i < own.children.length; i++)
                            {
                                own.children[i]._layout();
                                var childCls = own.children[i].btnNode[1].className
                                    .replace("waves-effect btn-flat adore_button adore_button_flat btn-large","");
                                own.children[i].btnNode.removeClass(childCls).addClass("adore_toolbar_color");

                                if(own.children[i].tooltip)
                                {
                                    own.children[i].jqueryNode.find("i").removeAttr("class");
                                    own.children[i]._setText({text:own.children[i].tooltip})
                                    own.children[i].btnNode.find('span').addClass("adore_toolbar_btnSpan");
                                }
                            }
                        }else
                        {
                            //不处理
                        }
                    }else
                    {
                        if(own.attachNode == originAttachNode)
                        {
                            //不处理
                        }else
                        {
                            own.attachNode = originAttachNode;
                            own.typeFlag = "";
                            own.dropDownBtn.css("display","none");
                            own.ulNode.css({display:"none", height: 0});
                            for(var i = 0; i < own.children.length; i++)
                            {
                                own.children[i]._layout();
                                if(own.children[i].tooltip)
                                {
                                    own.children[i].jqueryNode.find("i").attr("class", own.children[i].icon);
                                    own.children[i]._setText({text:""})
                                }
                            }
                        }
                    }
                })
            }
        }
    })
})