//-Created by zhuyan6 on 16/6/3.
block ui-widget-select
    -if(!document||(document&&!(document.querySelector("style[UID='ui-widget-select']"))))
    style(UID='ui-widget-select').
        .ui-widget-select {
            position: relative;
            display: inline-block;
            font: 12px/20px "Microsoft YaHei";
            color: #99999d;
            border: 1px solid #e7eaec;
            border-radius: 2px;
            vertical-align: middle;
        }

        .ui-widget-select .ui_select_title {
            display: block;
            text-align: left;
            text-indent: 4px;
        }

        .ui-widget-select span {
            pointer-events: none;
            overflow: hidden;
            text-overflow: clip;
            display: inline-block;
            white-space: nowrap;
            /*vertical-align: middle;*/
        }

        .ui-widget-select select {
            /*position: relative;*/
            position: absolute;
            top: 0;
            left: 0;
            width: 150px;
            height: 25px;
            filter: alpha(opacity:0);
            font: 12px/20px "Microsoft YaHei";
            color: #f80;
            /*display: none;*/
            /*opacity: 0;*/
            clip: rect(0,0,0,0);
        }

        .ui-widget-select select option {
            text-indent: 10px;
            /*opacity: 0;*/
            /*display: none;*/
            clip: rect(0,0,0,0);
        }

        .ui-widget-select .ui_select_datalist {
            width: 100%;
            position: absolute;
            left: -1px;
            right: 0;
            /*top: 39px;*/
            max-height: 100px;
            padding: 0;
            border: 1px solid #e7eaec;
            background-color: #fff;
            overflow: auto;
            display: none;
            z-index: 1;
        }

        .ui-widget-select .ui_select_datalist_li {
            display: block;
            line-height: 20px;
            padding: 6px 8px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            cursor: pointer;
            text-align: left;
            font-size: 12px;
        }

        .ui-widget-select .ui_select_datalist_li:hover {
            background-color: #0cbc9a !important;
            color: #fff;
        }

        .ui-widget-select .triangle {
            width: 0;
            height: 0;
            float: right;
            margin: 10px;
            overflow: hidden;
            border-width: 5px;
            border-style: solid;
            _border-style: dashed;
            border-color: transparent;
            pointer-events: none;
        }
        .ui-widget-select .top {
            border-top-color: #c3c3c3;
            _border-top-style: solid;
        }

        .ui-widget-select .bottom {
            border-bottom-color: #c3c3c3;
            _border-bottom-style: solid;
            margin-top:5px;
        }

        .ui-widget-select .left {
            border-right-color: #c3c3c3;
            _border-right-style: solid;
        }

    mixin init-ui-widget-select(id,content,arr,width,height,bgColor,keep)
        +nextTick(function() {
            var sel = document.getElementById(id);
            if (!sel)return;
            addSelectList();
            initSelect();

            function addSelectList() {
                var oSel = sel.getElementsByTagName("select")[0];
                var dlist = sel.getElementsByTagName("ul")[0];

                var list = Array.prototype.slice.apply(oSel.options);
                var ins = "";
                list.forEach(function (item, index) {
                    if (arr[index]) {
                        ins += '<li class="ui_select_datalist_li" style="background-color:' + bgColor + '" value="' + arr[index].value + '" data-index=' + index + '>' + arr[index].name + '</li>';
                    }
                })
                dlist.innerHTML = ins;

//                $(oSel).on('change',function(){//如果后面有覆盖监听比如:off完成再on,这里的监听(初始化),只会执行一次
//                    var val = $(this).find('option:selected').val();
//                    var txt = $(this).find('option:selected').text();
//                    $(this).siblings('.ui_select_title' ).find('span').html( txt );
//                    $(this).siblings('.ui_select_title' ).find('span').attr( 'value',val );
//                });
            }

            function selectHandler(curId,e) {
                var touchTarget = e.target;
                var prevShow;
                var dlist;
                var widgetSelect = $(touchTarget).closest('.ui-widget-select')[0];
                if(widgetSelect&&curId!==widgetSelect.id) {//如果点击的不是当前select控件,就退出
                    return;
                }
                if(widgetSelect){
                    dlist = $(touchTarget).siblings('.ui_select_datalist');
                    prevShow = dlist.is(':visible');//纪录一下之前的状态
                    //console.log('被点击的控件id:',widgetSelect.id)
                }
                //console.log('将要点击的控件id:',curId,e.target)
                if($(touchTarget).hasClass('ui_select_datalist')){//如果点击了控件旁边的下拉条
                    return;
                }
                $('.ui_select_datalist').hide();//所有select控件list隐藏
                $('.ui_select_title .triangle').removeClass().addClass('triangle top');//所有三角符号重置
                if ($(touchTarget).hasClass('ui_select_title')) {//如果点击的是select控件的title
                    var $sel_title = $(touchTarget);
                    if(prevShow){
                        if(dlist)dlist.hide();//只隐藏被点击的select控件list
                        $sel_title.find('.triangle')[0].className = 'triangle'+' '+'top';
                    }else {
                        if(dlist)dlist.show();//只显示被点击的select控件list
                        $sel_title.find('.triangle')[0].className = 'triangle'+' '+'bottom';
                    }
                    e.stopPropagation();
                    e.preventDefault();
                } else if ($(touchTarget).hasClass('ui_select_datalist_li')) {//如果点击的是select控件的list里面的li
                        var $sel_li = $(touchTarget);
                        var $select_title = $sel_li.parent().siblings('.ui_select_title');//li的父节点的select控件的title
                        if ($select_title[0]) {
                            $select_title.find('span').text($sel_li.text());
                            $select_title.find('span').attr('value',$sel_li.text());//2017.1.25日补充
                            var targetValue = $sel_li.attr('value');

                            var $select = $select_title.siblings('select')//改变原生select显示内容,并触发change事件
                            $select.val(targetValue);
                            $select.find('option[value="'+targetValue+'"]').prop('selected',true);
                            $select.trigger('change');

                            e.stopPropagation();
                            e.preventDefault();
                        }else {
                            return false;
                        }

                }else {
                    return false;
                }

            }
            function initSelect(){
                selectHandler.keep = keep;
                var docSelect = document.widget.selectHandlers;
                if(!docSelect) {
                    document.widget.selectHandlers={};
                    document.widget.selectHandlers[id]=selectHandler;
                    docSelect = document.widget.selectHandlers;
                    $(document).bind('mouseup',function(e){
                        //console.log('select控件事件集合',Object.keys(docSelect))
                        for(var i in docSelect){
                            var name =i;
                            var item = docSelect[i];
                            if(!$('#'+name)[0]){
                                if(!(item.keep)){//如果不支持持久化,就删除select控件的事件接收
                                    delete docSelect[i];
                                }
                                continue;
                            }
                            var bol = item(i,e);
                            if(String(bol)==='false'){//点击空白区域同时停止冒泡行为
                                break;
                            }
                        }
                    })
                }else {
                    docSelect[id] =selectHandler;
                }
            }
        })
    //-通用按钮元素,默认类型,以组的方式存在,组内样式通用
    //-示例:ui-widget-select-default()(content='文本内容',width='px',height='px',bgColor='#',dataArr='[]')
    mixin ui-widget-select-default(custom)
        +createHashId()
        - var content = attributes.content||'',
        - width = attributes.width||'127px',
        - height = attributes.height||'27px',
        - bgColor = attributes.bgColor||'#ffffff',
        - dataArr = attributes.dataArr||['请选择'],
        - keep = String(attributes.keep)==='false'?false:true//是否持久化,默认是持久化

        - var classHashId=__classHashId__;
        - var id = 'select_'+classHashId;
        - var spanWidth = parseInt(width)-40+'px';

        .ui-widget-select(custom=custom id=id style="width:" + width + ";height:"+ height +";line-height:" + height+";background-color:"+bgColor)
            .ui_select_title
                span(style='width:' + spanWidth value=content) #{content}
                .triangle.top
            ul.ui_select_datalist(style="top:"+height) 请选择
            select(id="#{id}" required)
                - for (var i = 0; i < dataArr.length; i++) {
                    option(value=dataArr[i].value) #{dataArr[i].name}
                - }

        +init-ui-widget-select(id,content,dataArr,width,height,bgColor,keep)



