/** @Name:transfer 穿梭框组件 @License:MIT */ layui.define(['laytpl', 'form'], function(exports){ "use strict"; var $ = layui.$ ,laytpl = layui.laytpl ,form = layui.form //模块名 ,MOD_NAME = 'transfer' //外部接口 ,transfer = { config: {} ,index: layui[MOD_NAME] ? (layui[MOD_NAME].index + 10000) : 0 //设置全局项 ,set: function(options){ var that = this; that.config = $.extend({}, that.config, options); return that; } //事件 ,on: function(events, callback){ return layui.onevent.call(this, MOD_NAME, events, callback); } } //操作当前实例 ,thisModule = function(){ var that = this ,options = that.config ,id = options.id || that.index; thisModule.that[id] = that; //记录当前实例对象 thisModule.config[id] = options; //记录当前实例配置项 return { config: options //重置实例 ,reload: function(options){ that.reload.call(that, options); } //获取右侧数据 ,getData: function(){ return that.getData.call(that); } } } //获取当前实例配置项 ,getThisModuleConfig = function(id){ var config = thisModule.config[id]; if(!config) hint.error('The ID option was not found in the '+ MOD_NAME +' instance'); return config || null; } //字符常量 ,ELEM = 'layui-transfer', HIDE = 'layui-hide', DISABLED = 'layui-btn-disabled', NONE = 'layui-none' ,ELEM_BOX = 'layui-transfer-box', ELEM_HEADER = 'layui-transfer-header', ELEM_SEARCH = 'layui-transfer-search', ELEM_ACTIVE = 'layui-transfer-active', ELEM_DATA = 'layui-transfer-data' //穿梭框模板 ,TPL_BOX = function(obj){ obj = obj || {}; return ['
'+ (text || '') +'
'); if(thisDataElem.find('.'+ NONE)[0]){ thisDataElem.find('.'+ NONE).remove(); } text.replace(/\s/g, '') && thisDataElem.append(createNoneElem); }; //同步 value 属性值 Class.prototype.setValue = function(){ var that = this ,options = that.config ,arr = []; that.layBox.eq(1).find('.'+ ELEM_DATA +' input[type="checkbox"]').each(function(){ var isHide = $(this).data('hide'); isHide || arr.push(this.value); }); options.value = arr; return that; }; //解析数据 Class.prototype.parseData = function(callback){ var that = this ,options = that.config ,newData = []; layui.each(options.data, function(index, item){ //解析格式 item = (typeof options.parseData === 'function' ? options.parseData(item) : item) || item; newData.push(item = $.extend({}, item)) layui.each(options.value, function(index2, item2){ if(item2 == item.value){ item.selected = true; } }); callback && callback(item); }); options.data = newData; return that; }; //获得右侧面板数据 Class.prototype.getData = function(value){ var that = this ,options = that.config ,selectedData = []; that.setValue(); layui.each(value || options.value, function(index, item){ layui.each(options.data, function(index2, item2){ delete item2.selected; if(item == item2.value){ selectedData.push(item2); }; }); }); return selectedData; }; //事件 Class.prototype.events = function(){ var that = this ,options = that.config; //左右复选框 that.elem.on('click', 'input[lay-filter="layTransferCheckbox"]+', function(){ var thisElemCheckbox = $(this).prev() ,checked = thisElemCheckbox[0].checked ,thisDataElem = thisElemCheckbox.parents('.'+ ELEM_BOX).eq(0).find('.'+ ELEM_DATA); if(thisElemCheckbox[0].disabled) return; //判断是否全选 if(thisElemCheckbox.attr('lay-type') === 'all'){ thisDataElem.find('input[type="checkbox"]').each(function(){ if(this.disabled) return; this.checked = checked; }); } that.renderCheckBtn({stopNone: true}); }); //按钮事件 that.layBtn.on('click', function(){ var othis = $(this) ,_index = othis.data('index') ,thisBoxElem = that.layBox.eq(_index) ,arr = []; if(othis.hasClass(DISABLED)) return; that.layBox.eq(_index).each(function(_index){ var othis = $(this) ,thisDataElem = othis.find('.'+ ELEM_DATA); thisDataElem.children('li').each(function(){ var thisList = $(this) ,thisElemCheckbox = thisList.find('input[type="checkbox"]') ,isHide = thisElemCheckbox.data('hide'); if(thisElemCheckbox[0].checked && !isHide){ thisElemCheckbox[0].checked = false; thisBoxElem.siblings('.'+ ELEM_BOX).find('.'+ ELEM_DATA).append(thisList.clone()); thisList.remove(); //记录当前穿梭的数据 arr.push(thisElemCheckbox[0].value); } that.setValue(); }); }); that.renderCheckBtn(); //穿梭时,如果另外一个框正在搜索,则触发匹配 var siblingInput = thisBoxElem.siblings('.'+ ELEM_BOX).find('.'+ ELEM_SEARCH +' input') siblingInput.val() === '' || siblingInput.trigger('keyup'); //穿梭时的回调 options.onchange && options.onchange(that.getData(arr), _index); }); //搜索 that.laySearch.find('input').on('keyup', function(){ var value = this.value ,thisDataElem = $(this).parents('.'+ ELEM_SEARCH).eq(0).siblings('.'+ ELEM_DATA) ,thisListElem = thisDataElem.children('li'); thisListElem.each(function(){ var thisList = $(this) ,thisElemCheckbox = thisList.find('input[type="checkbox"]') ,isMatch = thisElemCheckbox[0].title.indexOf(value) !== -1; thisList[isMatch ? 'removeClass': 'addClass'](HIDE); thisElemCheckbox.data('hide', isMatch ? false : true); }); that.renderCheckBtn(); //无匹配数据视图 var isNone = thisListElem.length === thisDataElem.children('li.'+ HIDE).length; that.noneView(thisDataElem, isNone ? options.text.searchNone : ''); }); }; //记录所有实例 thisModule.that = {}; //记录所有实例对象 thisModule.config = {}; //记录所有实例配置项 //重载实例 transfer.reload = function(id, options){ var that = thisModule.that[id]; that.reload(options); return thisModule.call(that); }; //获得选中的数据(右侧面板) transfer.getData = function(id){ var that = thisModule.that[id]; return that.getData(); }; //核心入口 transfer.render = function(options){ var inst = new Class(options); return thisModule.call(inst); }; exports(MOD_NAME, transfer); });