(function (exports) {
'use strict';

/**
 * 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 util tools
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

/**
 * 创建一个带壳的对象,防止外部修改
 * @param {Object} proto
 */
var isArray = Array.isArray || function(val) {
	return Object.prototype.toString.call(val) === '[object Array]';
};
var each = function(obj, callback) {
	if(obj.forEach) {
		obj.forEach(function(v, k) {
			callback(k, v);
		});

	} else if(obj instanceof Object) {
		for(var k in obj) {
			callback(k, obj[k]);
		}
	} else {
		return;
	}

};
try{
	NodeList.prototype.forEach = Array.prototype.forEach;
}catch(e){
	
}


/**
 * 获得字符串的字节长度
 */
String.prototype.lengthb = function() {
	//	var str = this.replace(/[^\x800-\x10000]/g, "***");
	var str = this.replace(/[^\x00-\xff]/g, "**");
	return str.length;
};

/**
 * 将AFindText全部替换为ARepText
 */
String.prototype.replaceAll = function(AFindText, ARepText) {
	//自定义String对象的方法
	var raRegExp = new RegExp(AFindText, "g");
	return this.replace(raRegExp, ARepText);
};

/**
 * 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);
		}
	}

};

/**
 * Module : Sparrow dom
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-16 13:59:17
 */
/**
 * 删除元素上指定样式
 * @param {Object} element
 * @param {Object} value
 */
var removeClass = function(element, value) {
	if(element){
		if(typeof element.classList === 'undefined') {
			if(u._removeClass){
				u._removeClass(element, value);
			}else{
				$(element).removeClass(value);
			}

		} else {
			element.classList.remove(value);
		}
	}
	return this;
};
/**
 * 元素上是否存在该类
 * @param {Object} element
 * @param {Object} value
 */
var hasClass = function(element, value) {
	if(!element) return false;
	if(element.nodeName && (element.nodeName === '#text' || element.nodeName === '#comment')) return false;
	if(typeof element.classList === 'undefined') {
		if(u._hasClass){
			return u._hasClass(element, value);
		}else{
			return $(element).hasClass(value);
		}

		return false;
	} else {
		return element.classList.contains(value);
	}
};
/**
 * 元素CSS操作
 * @param {Object} element
 * @param {Object} csstext
 * @param {Object} val
 */
var css = function(element, csstext, val) { //TO DO : 实现u.相关方法
	if(csstext instanceof Object) {
		for(var k in csstext) {
			var tmpcss = csstext[k];
			if(["width", "height", "top", "bottom", "left", "right"].indexOf(k) > -1 && isNumber(tmpcss)) {
				tmpcss = tmpcss + "px";
			}
			element.style[k] = tmpcss;
		}
	} else {
		if(arguments.length > 2) {
			element.style[csstext] = val;
		} else {
			return getStyle(element, csstext);
		}
	}
};

var wrap = function(element, parent) {
	var p = makeDOM(parent);
	element.parentNode.insertBefore(p, element);
	p.appendChild(element);
};
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 ""
	}
};
var makeDOM = function(htmlString) {
	var tempDiv = document.createElement("div");
	tempDiv.innerHTML = htmlString;
	var _dom = tempDiv.children[0];
	return _dom;
};

/**
 * Module : neoui-multilang
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-02 20:19:37
 */

var Multilang = u.BaseComponent.extend({
    init: function() {
        var self = this;
        var element = this.element;
        this.options = extend({}, this.DEFAULTS, this.options);
        this.field = this.options.field || 'name';
        this.multinfo(this.options.multinfo);
        this.addData(this.options.multidata);

    }
});
Multilang.fn = Multilang.prototype;
Multilang.fn.addData = function(val) {
    var target = this.element,
        tmparray,
        target_div = target.parentNode;
    if (val === null || typeof(val) === 'undefined') {
        tmparray = [];
    } else if (typeof(val) == "object") {
        tmparray = val;
    } else {
        tmparray = val.split(",");
    }
    target_div.value = tmparray;
    each(tmparray, function(i, node) {
        target_div.querySelectorAll(".m_context")[i].innerHTML = node;
    });

};
Multilang.fn.multinfo = function(sort) {

    var target = this.element,
        self = this,
        tmplabel = "",
        close_menu = false;
    if (isArray(sort)) {

        wrap(target, "<div class='multilang_body'><input class='lang_value' contenteditable='true'><span class='uf uf-caretdown lang_icon'><span class='m_icon'></span></span>");
        css(target, "display", "none");

        each(sort, function(i, node) {
            if (i) {
                tmplabel += "<label attr='" + self.field + (i + 1) + "'><span class='m_context'></span><span class='m_icon'>" + node + "</span></label>";
            } else {
                tmplabel += "<label attr='" + self.field + "'><span class='m_context'></span><span class='m_icon'>" + node + "</span></label>";
            }
        });
        var target_div = target.parentNode;

        target_div.insertAdjacentHTML("beforeEnd", "<div class='multilang_menu '>" + tmplabel + "</div>");
        var tmpIconv = target_div.querySelector(".lang_icon"),
            target_menu = target_div.querySelector(".multilang_menu"),
            target_labels = target_menu.querySelectorAll('label'),
            tmpvaluebox = target_div.querySelector(".lang_value");
        on(tmpIconv, "click", function() {
            var target_icon = this;
            target_div.querySelector(".lang_value").focus();
            if (css(target_menu, "display") == "block") {
                css(target_menu, "display", "none");
            } else {
                css(target_menu, "display", "block");
            }
        });
        on(target_menu, "mouseenter", function() {
            close_menu = false;
        });
        on(target_menu, "mouseleave", function() {
            close_menu = true;
        });

        on(tmpvaluebox, "blur", function(e) {
            var target_input = $(this),
                target_div = target_input.parents(".multilang_body"),
                target = e.target,
                tmpkey = target.className.split(" ")[2],
                tmptext = target.value;

            if (hasClass(target, "ready_change")) {
                self.changeData(target_div[0], tmpkey, tmptext);
            }
            // if(close_menu) {
            // 	css(target_menu, "display", "none")
            // }

        });

        target_labels.forEach(function(ele) {
            on(ele, "click", function() {
                var target_label = this,
                    tempField = target_label.getAttribute("attr"),
                    tmptext = target_label.querySelector(".m_context").innerHTML,
                    tmpicon = target_label.querySelector(".m_icon").cloneNode(true);

                tmpvaluebox.setAttribute("class", "ready_change lang_value " + tempField);
                tmpvaluebox.value = tmptext;
                tmpvaluebox.focus();
                var tmpicom = target_div.querySelector(".lang_icon"),
                    oldicon = target_div.querySelector(".m_icon");
                removeClass(tmpicom, "uf-caretdown");
                tmpicom.replaceChild(tmpicon, oldicon);
            });
        });

    } else {
        console.error('Not object');
    }
};
Multilang.fn.changeData = function(target_div, field, text) {
    var tmpdata = target_div.value,
        tmplabel = target_div.querySelector("label[attr='" + field + "']"),
        tmpcontext = tmplabel.querySelector(".m_context");
    tmpcontext.innerHTML = text;
    tmpcontext.value = text;
    each(target_div.querySelectorAll(".m_context"), function(i, node) {
        tmpdata[i] = node.innerHTML;
    });

    this.trigger('change.u.multilang', {
        newValue: text,
        field: field
    });

};
Multilang.fn.getData = function() {
    var target = $(multilang.target).next(".multilang_body")[0],
        multilang_data = target.value;

    return multilang_data;
};

Multilang.fn.setDataValue = function(field, value) {
    var target_div = this.element.closest('.multilang_body'),
        tmplabel = target_div.querySelector("label[attr='" + field + "']"),
        tmpcontext = tmplabel.querySelector(".m_context");
    tmpcontext.innerHTML = value;
    tmpcontext.value = value;

    var tmpdata = [];
    each(this.element.closest('.multilang_body').querySelectorAll(".m_context"), function(i, node) {
        tmpdata[i] = node.innerHTML;
    });
    this.element.closest('.multilang_body').value = tmpdata;

};

if (u.compMgr)
    u.compMgr.regComp({
        comp: Multilang,
        compAsString: 'u.Multilang',
        css: 'u-multilang'
    });

exports.Multilang = Multilang;

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