(function (exports) {
'use strict';

/**
 * Module : Sparrow touch event
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-28 14:41:17
 */

var on = function(element, eventName, child, listener) {
	if(!element)
		return;
	if(arguments.length < 4) {
		listener = child;
		child = undefined;
	} else {
		var childlistener = function(e) {
			if(!e) {
				return;
			}
			var tmpchildren = element.querySelectorAll(child);
			tmpchildren.forEach(function(node) {
				if(node == e.target) {
					listener.call(e.target, e);
				}
			});
		};
	}
	//capture = capture || false;

	if(!element["uEvent"]) {
		//在dom上添加记录区
		element["uEvent"] = {};
	}
	//判断是否元素上是否用通过on方法填加进去的事件
	if(!element["uEvent"][eventName]) {
		element["uEvent"][eventName] = [child ? childlistener : listener];
		if(u.event && u.event[eventName] && u.event[eventName].setup) {
			u.event[eventName].setup.call(element);
		}
		element["uEvent"][eventName + 'fn'] = function(e) {
			//火狐下有问题修改判断
			if(!e)
				e = typeof event != 'undefined' && event ? event : window.event;
			element["uEvent"][eventName].forEach(function(fn) {
				try {
					e.target = e.target || e.srcElement; //兼容IE8
				} catch(ee) {}
				if(fn)
					fn.call(element, e);
			});
		};
		if(element.addEventListener) { // 用于支持DOM的浏览器
			element.addEventListener(eventName, element["uEvent"][eventName + 'fn']);
		} else if(element.attachEvent) { // 用于IE浏览器
			element.attachEvent("on" + eventName, element["uEvent"][eventName + 'fn']);
		} else { // 用于其它浏览器
			element["on" + eventName] = element["uEvent"][eventName + 'fn'];
		}
	} else {
		//如果有就直接往元素的记录区添加事件
		var lis = child ? childlistener : listener;
		var hasLis = false;
		element["uEvent"][eventName].forEach(function(fn) {
			if(fn == lis) {
				hasLis = true;
			}
		});
		if(!hasLis) {
			element["uEvent"][eventName].push(child ? childlistener : listener);
		}
	}

};

var trigger = function(element, eventName) {
	if(element["uEvent"] && element["uEvent"][eventName]) {
		element["uEvent"][eventName + 'fn']();
	}
};

/**
 * Module : Sparrow dom
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-16 13:59:17
 */
var getStyle = function(element, key) {
	//不要在循环里用
	var allCSS;
	if(window.getComputedStyle) {
		allCSS = window.getComputedStyle(element);
	} else {
		allCSS = element.currentStyle;
	}
	if(allCSS[key] !== undefined) {
		return allCSS[key]
	} else {
		return ""
	}
};

/**
 * Module : Sparrow extend enum
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

var enumerables = true;
var enumerablesTest = {
		toString: 1
	};
for(var i in enumerablesTest) {
	enumerables = null;
}
if(enumerables) {
	enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable',
		'toLocaleString', 'toString', 'constructor'
	];
}

/**
 * Module : Sparrow extend
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

/**
 * 复制对象属性
 *
 * @param {Object}  目标对象
 * @param {config} 源对象
 */
var extend = function(object, config) {
	var args = arguments,
		options;
	if(args.length > 1) {
		for(var len = 1; len < args.length; len++) {
			options = args[len];
			if(object && options && typeof options === 'object') {
				var i, j, k;
				for(i in options) {
					object[i] = options[i];
				}
				if(enumerables) {
					for(j = enumerables.length; j--;) {
						k = enumerables[j];
						if(options.hasOwnProperty && options.hasOwnProperty(k)) {
							object[k] = options[k];
						}
					}
				}
			}
		}
	}
	return object;
};

if(!Object.assign){
	Object.assign = extend;
}

/**
 * Module : Sparrow browser environment
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

var u$1 = {};


extend(u$1, {
	isIE:  false,
	isFF: false,
	isOpera: false,
	isChrome: false,
	isSafari: false,
	isWebkit: false,
	isIE8_BEFORE: false,
	isIE8: false,
	isIE8_CORE: false,
	isIE9: false,
	isIE9_CORE: false,
	isIE10: false,
	isIE10_ABOVE: false,
	isIE11: false,
	isEdge: false,
	isIOS: false,
	isIphone: false,
	isIPAD: false,
	isStandard: false,
	version: 0,
	isWin: false,
	isUnix: false,
	isLinux: false,
	isAndroid: false,
	isAndroidPAD:false,
	isAndroidPhone:false,
	isMac: false,
	hasTouch: false,
	isMobile: false
});

(function() {
	var userAgent = navigator.userAgent,
		rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
		rFirefox = /(firefox)\/([\w.]+)/,
		rOpera = /(opera).+version\/([\w.]+)/,
		rChrome = /(chrome)\/([\w.]+)/,
		rSafari = /version\/([\w.]+).*(safari)/,
		version,
		ua = userAgent.toLowerCase(),
		s,
		browserMatch = {
			browser: "",
			version: ''
		},
		match = rMsie.exec(ua);

	if(match != null) {
		browserMatch = {
			browser: "IE",
			version: match[2] || "0"
		};
	}
	match = rFirefox.exec(ua);
	if(match != null) {
		browserMatch = {
			browser: match[1] || "",
			version: match[2] || "0"
		};
	}
	match = rOpera.exec(ua);
	if(match != null) {
		browserMatch = {
			browser: match[1] || "",
			version: match[2] || "0"
		};
	}
	match = rChrome.exec(ua);
	if(match != null) {
		browserMatch = {
			browser: match[1] || "",
			version: match[2] || "0"
		};
	}
	match = rSafari.exec(ua);
	if(match != null) {
		browserMatch = {
			browser: match[2] || "",
			version: match[1] || "0"
		};
	}

	if(userAgent.indexOf("Edge") > -1){
		u$1.isEdge = true;
	}
	if(s = ua.match(/opera.([\d.]+)/)) {
		u$1.isOpera = true;
	} else if(browserMatch.browser == "IE" && browserMatch.version == 11) {
		u$1.isIE11 = true;
		u$1.isIE = true;
	} else if(s = ua.match(/chrome\/([\d.]+)/)) {
		u$1.isChrome = true;
		u$1.isStandard = true;
	} else if(s = ua.match(/version\/([\d.]+).*safari/)) {
		u$1.isSafari = true;
		u$1.isStandard = true;
	} else if(s = ua.match(/gecko/)) {
		//add by licza : support XULRunner
		u$1.isFF = true;
		u$1.isStandard = true;
	} else if(s = ua.match(/msie ([\d.]+)/)) {
		u$1.isIE = true;
	} else if(s = ua.match(/firefox\/([\d.]+)/)) {
		u$1.isFF = true;
		u$1.isStandard = true;
	}
	if(ua.match(/webkit\/([\d.]+)/)) {
		u$1.isWebkit = true;
	}
	if(ua.match(/ipad/i)) {
		u$1.isIOS = true;
		u$1.isIPAD = true;
		u$1.isStandard = true;
	}

	if(ua.match(/iphone/i)) {
		u$1.isIOS = true;
		u$1.isIphone = true;
	}

	if((navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel")) {
		//u.isIOS = true;
		u$1.isMac = true;
	}

	if((navigator.platform == "Win32") || (navigator.platform == "Windows") || (navigator.platform == "Win64")) {
		u$1.isWin = true;
	}

	if((navigator.platform == "X11") && !u$1.isWin && !u$1.isMac) {
		u$1.isUnix = true;
	}
	if((String(navigator.platform).indexOf("Linux") > -1)) {
		u$1.isLinux = true;
	}

	if(ua.indexOf('Android') > -1 || ua.indexOf('android') > -1 || ua.indexOf('Adr') > -1 || ua.indexOf('adr') > -1) {
		u$1.isAndroid = true;
	}

	u$1.version = version ? (browserMatch.version ? browserMatch.version : 0) : 0;
	if(u$1.isAndroid){
		if(window.screen.width>=768&&window.screen.width<1024){
			u$1.isAndroidPAD=true;
		}
		if(window.screen.width<=768) {
			u$1.isAndroidPhone = true;
		}
	}
	if(u$1.isIE) {
		var intVersion = parseInt(u$1.version);
		var mode = document.documentMode;
		if(mode == null) {
			if(intVersion == 6 || intVersion == 7) {
				u$1.isIE8_BEFORE = true;
			}
		} else {
			if(mode == 7) {
				u$1.isIE8_BEFORE = true;
			} else if(mode == 8) {
				u$1.isIE8 = true;
			} else if(mode == 9) {
				u$1.isIE9 = true;
				u$1.isSTANDARD = true;
			} else if(mode == 10) {
				u$1.isIE10 = true;
				u$1.isSTANDARD = true;
				u$1.isIE10_ABOVE = true;
			} else {
				u$1.isSTANDARD = true;
			}
			if(intVersion == 8) {
				u$1.isIE8_CORE = true;
			} else if(intVersion == 9) {
				u$1.isIE9_CORE = true;
			} else if(browserMatch.version == 11) {
				u$1.isIE11 = true;
			}
		}
	}
	if("ontouchend" in document) {
		u$1.hasTouch = true;
	}
	if(u$1.isIphone || u$1.isAndroidPhone)
		u$1.isMobile = true;

})();

var env = u$1;

/**
 * Module : neoui-combobox
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-02 18:42:07
 */

var Combobox = u.BaseComponent.extend({
    DEFAULTS: {
        dataSource: {},
        mutil: false,
        enable: true,
        single: true,
        onSelect: function() {}
    },
    init: function() {
        var self = this;
        var element = this.element;
        this.options = extend({}, this.DEFAULTS, this.options);
        this.items = [];
        //this.oLis = [];
        this.mutilPks = [];
        this.oDiv = null;
        Object.defineProperty(element, 'value', {
            get: function() {

                return this.trueValue;
            },
            set: function(pk) {

                var items = self.items;
                //var oLis = self.oLis;
                var oLis = self.oDiv.childNodes;

                if (self.options.single == "true" || self.options.single == true) {

                    for (var i = 0, length = items.length; i < length; i++) {

                        var ipk = items[i].pk;
                        if (ipk == pk) {
                            this.innerHTML = items[i].name;
                            this.trueValue = pk;
                            break;
                        } else {

                            this.trueValue = '';
                            this.innerHTML = '';
                        }

                    }

                } else if (self.options.mutil == "true" || self.options.mutil == true) {

                    if (!env.isArray(pk)) {
                        if (typeof pk == "string" && pk !== "") {
                            pk = pk.split(',');
                            self.mutilPks = pk;
                        } else {
                            return
                        }
                    }

                    if (self.mutilPks.length == 0) {
                        self.mutilPks = pk;
                    }

                    this.innerHTML = '';
                    var valueArr = [];

                    for (var j = 0; j < pk.length; j++) {

                        for (var i = 0, length = oLis.length; i < length; i++) {
                            var ipk = oLis[i].item.pk;
                            if (pk[j] == ipk) {

                                valueArr.push(pk[j]);

                                oLis[i].style.display = 'none';
                                var activeSelect = document.createElement("Div");
                                activeSelect.className = "mutil-select-div";
                                var selectName = "<i class='itemName'>" + items[i].name + "</i>";
                                var imageFont = "<i class='uf uf-removesymbol'></i>";
                                activeSelect.insertAdjacentHTML("beforeEnd", imageFont + selectName);
                                this.appendChild(activeSelect);

                                //activeSelect.append(imageFont);
                                //	activeSelect.append(selectName);

                                on(activeSelect.querySelector(".uf-removesymbol"), 'mousedown', function() {

                                    //var $this = $(this);
                                    //var lis = self.oLis;
                                    //var lis = $(self.oDiv).find('li');
                                    var lis = self.oDiv.childNodes;
                                    for (var j = 0, len = lis.length; j < len; j++) {
                                        if (lis[j].item.name == this.nextSibling.innerHTML) {
                                            lis[j].style.display = 'block';

                                            for (var h = 0; h < self.mutilPks.length; h++) {
                                                if (self.mutilPks[h] == lis[j].item.pk) {
                                                    self.mutilPks.splice(h, 1);
                                                    h--;
                                                }
                                            }

                                            for (var b = 0; b < valueArr.length; b++) {
                                                if (valueArr[b] == lis[j].item.pk) {
                                                    valueArr.splice(b, 1);
                                                    b--;
                                                }
                                            }

                                        }
                                    }

                                    activeSelect.removeChild(this.parentNode);
                                    element.trueValue = '';
                                    element.trueValue = valueArr.toString();
                                    trigger(element, 'mutilSelect', valueArr.toString());
                                });



                                //	var selectName = $("<i class='itemName'>" + items[i].name + "</i>");

                                //	var activeSelect = $("<div class='mutil-select-div'></div>")






                            }

                        }


                    }

                    this.trueValue = valueArr.toString();


                }


            }
        });
        //禁用下拉框
        if (this.options.readonly === "readonly") return;

        if (this.options.single == "true" || this.options.single == true) {
            this.singleSelect();
        }

        if (this.options.mutil == "true" || this.options.mutil == true) {
            this.mutilSelect();
        }

        this.clickEvent();

        this.blurEvent();

        this.comboFilter();

        this.comboFilterClean();
    }
});



Combobox.fn = Combobox.prototype;

Combobox.fn.createDom = function() {

    var data = this.options.dataSource;
    if (env.isEmptyObject(data)) {
        throw new Error("dataSource为空！");
    }

    var oDiv = document.createElement("div");
    oDiv.className = 'select-list-div';
    //this.oDiv
    this.oDiv = oDiv;
    //新增搜索框

    var searchDiv = document.createElement("div");
    searchDiv.className = 'select-search';
    var searchInput = document.createElement("input");
    searchDiv.appendChild(searchInput);
    oDiv.appendChild(searchDiv);
    //禁用搜索框
    if (this.options.readchange) {
        searchDiv.style.display = "none";
    }
    var oUl = document.createElement("ul");

    oUl.className = 'select-list-ul';

    for (var i = 0; i < data.length; i++) {
        var item = {
            pk: data[i].pk,
            name: data[i].name
        };
        this.items.push(item);
        var oLi = document.createElement("li");

        oLi.item = item;
        oLi.innerHTML = data[i]['name'];

        //this.oLis.push(oLi);

        oUl.appendChild(oLi);

    }


    oDiv.appendChild(oUl);
    oDiv.style.display = 'none';
    document.body.appendChild(oDiv);

};

Combobox.fn.focusEvent = function() {
    var self = this;
    on(this.element, 'click', function(e) {
        if (self.options.readchange == true) return;
        var returnValue = self.show();

        if (returnValue === 1) return;
        // self.show();

        self.floatLayer();

        self.floatLayerEvent();

        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }

    });
};

//下拉图标的点击事件
Combobox.fn.clickEvent = function() {
    var self = this;
    //var caret = this.$element.next('.input-group-addon')[0] || this.$element.next(':button')[0];
    var caret = this.element.nextSibling;
    on(caret, 'click', function(e) {
        self.show();
        self.floatLayer();
        self.floatLayerEvent();
        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }

    });
};

//tab键切换 下拉隐藏
Combobox.fn.blurEvent = function() {
    var self = this;

    on(this.element, 'keyup', function(e) {
        var key = e.which || e.keyCode;
        if (key == 9)
            self.show();

    });
    on(this.element, 'keydown', function(e) {
        var key = e.which || e.keyCode;
        if (key == 9)
            self.hide();
    });
};



Combobox.fn.floatLayer = function() {

    if (!document.querySelector(".select-floatDiv")) {

        var oDivTwo = document.createElement("div");
        oDivTwo.className = 'select-floatDiv';
        document.body.appendChild(oDivTwo);
    }

};

Combobox.fn.floatLayerEvent = function() {
    var self = this;
    on(document.querySelector(".select-floatDiv"), "click", function(e) {

        self.hide();
        this.parentNode.removeChild(this);

        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    });


};

Combobox.fn.show = function() {

    //var oLis = this.oLis;
    var oLis = this.oDiv.querySelector("ul").childNodes;
    var vote = 0;
    for (var i = 0, length = oLis.length; i < length; i++) {

        if (oLis[i].style.display == 'none') {
            vote++;
        }
    }

    if (vote === length) return 1;

    var left = this.element.offsetLeft;
    var top = this.element.offsetTop;

    var selectHeight = this.options.dataSource.length * 30 + 10 + 10;

    var differ = (top + getStyle(this.element, "height") + selectHeight) - (window.outerHeight + window.scrollY);
    var oDiv = this.oDiv;

    if (differ > 0) {

        oDiv.style.left = left + 'px';
        oDiv.style.top = top - selectHeight + 'px';

    } else {

        oDiv.style.left = left + 'px';
        oDiv.style.top = top + getStyle(this.element, "height") + 'px';

    }

    oDiv.style.display = 'block';
};

Combobox.fn.hide = function() {
    this.oDiv.style.display = 'none';
};

Combobox.fn.singleDivValue = function() {
    var self = this;
    //var oLis = this.oLis;
    var oLis = this.oDiv.querySelector("ul").childNodes;
    for (var i = 0; i < oLis.length; i++) {

        on(oLis[i], "click", function() {

            var item = this.item;
            self.element.value = item.pk;

            self.oDiv.style.display = 'none';

            self.options.onSelect(item);

            trigger(self.element, 'change');

        });

    }
};

Combobox.fn.mutilDivValue = function() {
    var self = this;
    //var oLis = this.oLis;
    var oLis = this.oDiv.querySelector("ul").childNodes;
    for (var i = 0; i < oLis.length; i++) {
        on(oLis[i], "click", function() {

            var pk = this.item.pk;
            var mutilpks = self.mutilPks;
            var mutilLenth = mutilpks.length;

            if (mutilLenth > 0) {

                for (var k = 0; k < mutilLenth; k++) {

                    if (pk == mutilpks[k]) {

                        mutilpks.splice(k, 1);
                        k--;
                    }

                }

            }

            mutilpks.push(pk);

            self.element.value = mutilpks;
            trigger(self.element, 'mutilSelect', mutilpks.toString());
            // element.trigger('mutilSelect',mutilpks.toString())

            self.oDiv.style.display = 'none';
            this.style.display = 'none';
            trigger(self.element, 'change');



        });

    }
};

Combobox.fn.singleSelect = function() {

    this.createDom();
    this.focusEvent();
    this.singleDivValue();

};

Combobox.fn.mutilSelect = function() {

    this.createDom();
    this.mutilDivValue();
    this.focusEvent();

};
//过滤下拉选项
Combobox.fn.comboFilter = function() {
    var self = this;
    on(this.oDiv, "keyup", function() {

        var content = this.querySelector('.select-search input').value;

        var oLis = this.oDiv.querySelector("ul").childNodes;
        for (var i = 0; i < oLis.length; i++) {
            if (oLis[i].item.name.indexOf(content) != -1) {
                oLis[i].style.display = 'block';
            } else {
                oLis[i].style.display = 'none';

            }
        }


    });
};

//过滤的后续处理
Combobox.fn.comboFilterClean = function() {
    var self = this;
    on(self.element, "click", function() {
        // $(this.$element).on('click',function(){
        // $(self.oDiv).find('.select-search input').val('')
        self.oDiv.querySelector('.select-search input').value = "";
        var oLis = this.oDiv.querySelector("ul").childNodes;
        if (self.options.single == "true" || self.options.single == true) {
            for (var i = 0; i < oLis.length; i++) {
                oLis[i].style.display = 'block';
            }
        } else if (self.options.mutil == "true" || self.options.mutil == true) {
            var selectLisIndex = [];
            var selectLisSpan = this.querySelector('.mutil-select-div .itemName');

            for (var i = 0; i < selectLisSpan.length; i++) {
                for (var k = 0; k < oLis.length; k++) {
                    if (selectLisSpan[i].innerHTML == oLis[k].item.name) {
                        //oLis[k].style.display = 'none';
                        selectLisIndex.push(k);
                    }
                }
            }

            for (var l = 0; l < oLis.length; l++) {
                oLis[l].style.display = 'block';
                for (var j = 0; j < selectLisIndex.length; j++) {
                    if (l == selectLisIndex[j])
                        oLis[l].style.display = 'none';
                }
            }


        }


    });
};
// var Plugin = function(option) {

// var $this = $(this);
// var data = $this.data('s.select');
// var options = typeof option == 'object' && option

// if (!data) $this.data('s.select', (new Combobox(this, options)))

// }

//动态设置li值
// $.fn.setComboData = function(dataSourse) {
// var $this = $(this).data('s.select');
// if(!$this)return;
// var data = dataSourse;
// if (!$.isArray(data) || data.length == 0) return;

// $this.items.length = 0;

// var Olis = $($this.oDiv).find('li');


// if(data.length < Olis.length){

// for(var k=data.length;k<Olis.length;k++){
// $(Olis[k]).remove();
// }

// }else if(data.length > Olis.length){
// var liTemplate = Olis[0]
// var oUl = $($this.oDiv).find('ul')
// for(var j=0;j<(data.length-Olis.length);j++){
// $(liTemplate).clone(true).appendTo(oUl)
// }
// }

// Olis = $($this.oDiv).find('li');

// for (var i = 0; i < data.length; i++) {
// var item = {
// pk: data[i].pk,
// name: data[i].name
// }
// $this.items.push(item)
// Olis[i].item = item;
// Olis[i].innerHTML = data[i]['name']
// }

// }

// $.fn.Combobox = Plugin;
if (u.compMgr)
    u.compMgr.regComp({
        comp: Combobox,
        compAsString: 'u.Combobox',
        css: 'u-combobox'
    });

exports.Combobox = Combobox;

}((this.bar = this.bar || {})));
