formselects.js 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. 'use strict';
  2. var surl = '';
  3. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. /**
  6. * name: formSelects
  7. * 基于Layui Select多选
  8. * version: 4.0.0.0910
  9. * http://sun.faysunshine.com/layui/formSelects-v4/dist/formSelects-v4.js
  10. */
  11. (function (layui, window, factory) {
  12. if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
  13. // 支持 CommonJS
  14. module.exports = factory();
  15. } else if (typeof define === 'function' && define.amd) {
  16. // 支持 AMD
  17. define(factory);
  18. } else if (window.layui && layui.define) {
  19. //layui加载
  20. layui.define(['jquery'], function (exports) {
  21. exports('formSelects', factory());
  22. });
  23. } else {
  24. window.formSelects = factory();
  25. }
  26. })(typeof layui == 'undefined' ? null : layui, window, function () {
  27. var v = '4.0.0.0910',
  28. NAME = 'xm-select',
  29. PNAME = 'xm-select-parent',
  30. INPUT = 'xm-select-input',
  31. TDIV = 'xm-select--suffix',
  32. THIS = 'xm-select-this',
  33. LABEL = 'xm-select-label',
  34. SEARCH = 'xm-select-search',
  35. SEARCH_TYPE = 'xm-select-search-type',
  36. CALL = 'xm-select-callback',
  37. CALLPARAM = 'xm-select-callback-param',
  38. SHOW_COUNT = 'xm-select-show-count',
  39. CREATE = 'xm-select-create',
  40. CREATE_LONG = 'xm-select-create-long',
  41. MAX = 'xm-select-max',
  42. SKIN = 'xm-select-skin',
  43. DIRECTION = "xm-select-direction",
  44. HEIGHT = 'xm-select-height',
  45. DISABLED = 'xm-dis-disabled',
  46. DIS = 'xm-select-dis',
  47. TEMP = 'xm-select-temp',
  48. RADIO = 'xm-select-radio',
  49. LINKAGE = 'xm-select-linkage',
  50. DL = 'xm-select-dl',
  51. DD_HIDE = 'xm-select-hide',
  52. HIDE_INPUT = 'xm-hide-input',
  53. SANJIAO = 'xm-select-sj',
  54. ICON_CLOSE = 'xm-icon-close',
  55. FORM_TITLE = 'xm-select-title',
  56. FORM_SELECT = 'xm-form-select',
  57. FORM_SELECTED = 'xm-form-selected',
  58. FORM_NONE = 'xm-select-none',
  59. FORM_EMPTY = 'xm-select-empty',
  60. FORM_INPUT = 'xm-input',
  61. FORM_DL_INPUT = 'xm-dl-input',
  62. FORM_SELECT_TIPS = 'xm-select-tips',
  63. FORM_SELECT_PH = 'xm-select-placeholder',
  64. CHECKBOX_YES = 'xm-iconfont',
  65. FORM_TEAM_PID = 'XM_PID_VALUE',
  66. CZ = 'xm-cz',
  67. CZ_GROUP = 'xm-cz-group',
  68. TIPS = '请选择',
  69. data = {},
  70. events = {
  71. on: {},
  72. endOn: {},
  73. filter: {},
  74. maxTips: {},
  75. opened: {},
  76. closed: {}
  77. },
  78. ajax = {
  79. type: 'get',
  80. header: {},
  81. first: true,
  82. data: {},
  83. searchUrl: '',
  84. searchName: 'keyword',
  85. searchVal: null,
  86. keyName: 'name',
  87. keyVal: 'value',
  88. keySel: 'selected',
  89. keyDis: 'disabled',
  90. keyChildren: 'children',
  91. dataType: '',
  92. delay: 500,
  93. beforeSuccess: null,
  94. success: null,
  95. error: null,
  96. beforeSearch: null,
  97. response: {
  98. statusCode: 0,
  99. statusName: 'code',
  100. msgName: 'msg',
  101. dataName: 'data'
  102. },
  103. tree: {
  104. nextClick: function nextClick(id, item, callback) {
  105. callback([]);
  106. },
  107. folderChoose: true,
  108. lazy: true
  109. }
  110. },
  111. quickBtns = [{ icon: 'xm-iconfont icon-quanxuan', name: '全选', click: function click(id, cm) {
  112. cm.selectAll(id, true, true);
  113. } }, { icon: 'xm-iconfont icon-qingkong', name: '清空', click: function click(id, cm) {
  114. cm.removeAll(id, true, true);
  115. } }, { icon: 'xm-iconfont icon-fanxuan', name: '反选', click: function click(id, cm) {
  116. cm.reverse(id, true, true);
  117. } }, { icon: 'xm-iconfont icon-pifu', name: '换肤', click: function click(id, cm) {
  118. cm.skin(id);
  119. } }],
  120. $ = window.$ || window.layui && window.layui.jquery,
  121. $win = $(window),
  122. ajaxs = {},
  123. fsConfig = {},
  124. fsConfigs = {},
  125. FormSelects = function FormSelects(options) {
  126. var _this = this;
  127. this.config = {
  128. name: null, //xm-select="xxx"
  129. max: null,
  130. maxTips: function maxTips(id, vals, val, max) {
  131. var ipt = $('[xid="' + _this.config.name + '"]').prev().find('.' + NAME);
  132. if (ipt.parents('.layui-form-item[pane]').length) {
  133. ipt = ipt.parents('.layui-form-item[pane]');
  134. }
  135. ipt.attr('style', 'border-color: red !important');
  136. setTimeout(function () {
  137. ipt.removeAttr('style');
  138. }, 300);
  139. },
  140. init: null, //初始化的选择值,
  141. on: null, //select值发生变化
  142. opened: null,
  143. closed: null,
  144. filter: function filter(id, inputVal, val, isDisabled) {
  145. return val.name.indexOf(inputVal) == -1;
  146. },
  147. clearid: -1,
  148. direction: 'auto',
  149. height: null,
  150. isEmpty: false,
  151. btns: [quickBtns[0], quickBtns[1], quickBtns[2]],
  152. searchType: 0,
  153. create: function create(id, name) {
  154. return Date.now();
  155. },
  156. template: function template(id, item) {
  157. return item.name;
  158. },
  159. showCount: 0,
  160. isCreate: false,
  161. placeholder: TIPS,
  162. clearInput: false
  163. };
  164. this.select = null;
  165. this.values = [];
  166. $.extend(true, this.config, options, {
  167. searchUrl: options.isSearch ? options.searchUrl : null,
  168. placeholder: options.placeholder ? TIPS + options.placeholder : TIPS,
  169. btns: options.radio ? [quickBtns[1]] : [quickBtns[0], quickBtns[1], quickBtns[2]]
  170. }, fsConfigs[options.name] || fsConfig);
  171. this.config.searchUrl = options.searchUrl;
  172. if (isNaN(this.config.showCount) || this.config.showCount <= 0) {
  173. this.config.showCount = 19921012;
  174. }
  175. };
  176. //一些简单的处理方法
  177. var Common = function Common() {
  178. this.appender();
  179. this.on();
  180. this.onreset();
  181. };
  182. Common.prototype.appender = function () {
  183. //针对IE做的一些拓展
  184. //拓展Array map方法
  185. if (!Array.prototype.map) {
  186. Array.prototype.map = function (i, h) {
  187. var b,
  188. a,
  189. c,
  190. e = Object(this),
  191. f = e.length >>> 0;if (h) {
  192. b = h;
  193. }a = new Array(f);c = 0;while (c < f) {
  194. var d, g;if (c in e) {
  195. d = e[c];g = i.call(b, d, c, e);a[c] = g;
  196. }c++;
  197. }return a;
  198. };
  199. };
  200. //拓展Array foreach方法
  201. if (!Array.prototype.forEach) {
  202. Array.prototype.forEach = function forEach(g, b) {
  203. var d, c;if (this == null) {
  204. throw new TypeError("this is null or not defined");
  205. }var f = Object(this);var a = f.length >>> 0;if (typeof g !== "function") {
  206. throw new TypeError(g + " is not a function");
  207. }if (arguments.length > 1) {
  208. d = b;
  209. }c = 0;while (c < a) {
  210. var e;if (c in f) {
  211. e = f[c];g.call(d, e, c, f);
  212. }c++;
  213. }
  214. };
  215. };
  216. //拓展Array filter方法
  217. if (!Array.prototype.filter) {
  218. Array.prototype.filter = function (b) {
  219. if (this === void 0 || this === null) {
  220. throw new TypeError();
  221. }var f = Object(this);var a = f.length >>> 0;if (typeof b !== "function") {
  222. throw new TypeError();
  223. }var e = [];var d = arguments[1];for (var c = 0; c < a; c++) {
  224. if (c in f) {
  225. var g = f[c];if (b.call(d, g, c, f)) {
  226. e.push(g);
  227. }
  228. }
  229. }return e;
  230. };
  231. };
  232. };
  233. Common.prototype.init = function (target) {
  234. var _this2 = this;
  235. //初始化页面上已有的select
  236. $(target ? target : 'select[' + NAME + ']').each(function (index, select) {
  237. var othis = $(select),
  238. id = othis.attr(NAME),
  239. hasLayuiRender = othis.next('.layui-form-select'),
  240. hasRender = othis.next('.' + PNAME),
  241. options = {
  242. name: id,
  243. disabled: select.disabled,
  244. max: othis.attr(MAX) - 0,
  245. isSearch: othis.attr(SEARCH) != undefined,
  246. searchUrl: othis.attr(SEARCH),
  247. callback: othis.attr(CALL),
  248. callbackParam: othis.attr(CALLPARAM),
  249. isCreate: othis.attr(CREATE) != undefined,
  250. radio: othis.attr(RADIO) != undefined,
  251. skin: othis.attr(SKIN),
  252. direction: othis.attr(DIRECTION),
  253. optionsFirst: select.options[0],
  254. placeholder: othis.attr(FORM_SELECT_PH),
  255. height: othis.attr(HEIGHT),
  256. formname: othis.attr('name') || othis.attr('_name'),
  257. layverify: othis.attr('lay-verify') || othis.attr('_lay-verify'),
  258. layverType: othis.attr('lay-verType'),
  259. searchType: othis.attr(SEARCH_TYPE) == 'dl' ? 1 : 0,
  260. showCount: othis.attr(SHOW_COUNT) - 0
  261. },
  262. value = othis.find('option[selected]').toArray().map(function (option) {
  263. //获取已选中的数据
  264. return {
  265. name: option.innerHTML,
  266. value: option.value
  267. };
  268. }),
  269. fs = new FormSelects(options);
  270. fs.values = value;
  271. if (fs.config.init) {
  272. fs.values = fs.config.init.map(function (item) {
  273. if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) == 'object') {
  274. return item;
  275. }
  276. return {
  277. name: othis.find('option[value="' + item + '"]').text(),
  278. value: item
  279. };
  280. }).filter(function (item) {
  281. return item.name;
  282. });
  283. fs.config.init = fs.values.concat([]);
  284. } else {
  285. fs.config.init = value.concat([]);
  286. }
  287. !fs.values && (fs.values = []);
  288. data[id] = fs;
  289. surl = fs.config.searchUrl;
  290. //先取消layui对select的渲染
  291. hasLayuiRender[0] && hasLayuiRender.remove();
  292. hasRender[0] && hasRender.remove();
  293. //构造渲染div
  294. var dinfo = _this2.renderSelect(id, fs.config.placeholder, select);
  295. var heightStyle = !fs.config.height || fs.config.height == 'auto' ? '' : 'xm-hg style="height: 34px;"';
  296. var inputHtml = ['<div class="' + LABEL + '">', '<input type="text" fsw class="' + FORM_INPUT + ' ' + INPUT + '" ' + (fs.config.isSearch ? '' : 'style="display: none;"') + ' autocomplete="off" debounce="0" />', '</div>'];
  297. var reElem = $('<div class="' + FORM_SELECT + '" ' + SKIN + '="' + fs.config.skin + '">\n\t\t\t\t\t<input class="' + HIDE_INPUT + '" value="" name="' + fs.config.formname + '" lay-verify="' + fs.config.layverify + '" lay-verType="' + fs.config.layverType + '" type="text" style="position: absolute;bottom: 0; z-index: -1;width: 100%; height: 100%; border: none; opacity: 0;"/>\n\t\t\t\t\t<div class="' + FORM_TITLE + ' ' + (fs.config.disabled ? DIS : '') + '">\n\t\t\t\t\t\t<div class="' + FORM_INPUT + ' ' + NAME + '" ' + heightStyle + '>\n\t\t\t\t\t\t\t' + inputHtml.join('') + '\n\t\t\t\t\t\t\t<i class="' + SANJIAO + '"></i>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div class="' + TDIV + '">\n\t\t\t\t\t\t\t<input type="text" autocomplete="off" placeholder="' + fs.config.placeholder + '" readonly="readonly" unselectable="on" class="' + FORM_INPUT + '">\n\t\t\t\t\t\t</div>\n\t\t\t\t\t\t<div></div>\n\t\t\t\t\t</div>\n\t\t\t\t\t<dl xid="' + id + '" class="' + DL + ' ' + (fs.config.radio ? RADIO : '') + '">' + dinfo + '</dl>\n\t\t\t\t</div>');
  298. var $parent = $('<div class="' + PNAME + '" FS_ID="' + id + '"></div>');
  299. $parent.append(reElem);
  300. othis.after($parent);
  301. othis.attr('lay-ignore', '');
  302. othis.removeAttr('name') && othis.attr('_name', fs.config.formname);
  303. othis.removeAttr('lay-verify') && othis.attr('_lay-verify', fs.config.layverify);
  304. //如果可搜索, 加上事件
  305. if (fs.config.isSearch) {
  306. ajaxs[id] = $.extend({}, ajax, { searchUrl: fs.config.searchUrl }, ajaxs[id]);
  307. ajaxs[id].searchUrl = fs.config.searchUrl;
  308. ajaxs[id].callback = fs.config.callback;
  309. ajaxs[id].callbackParam = fs.config.callbackParam;
  310. $(document).on('input', 'div.' + PNAME + '[FS_ID="' + id + '"] .' + INPUT, function (e) {
  311. _this2.search(id, e, fs.config.searchUrl);
  312. });
  313. if (fs.config.searchUrl) {
  314. //触发第一次请求事件
  315. _this2.triggerSearch(reElem, true);
  316. }
  317. } else {
  318. //隐藏第二个dl
  319. reElem.find('dl dd.' + FORM_DL_INPUT).css('display', 'none');
  320. }
  321. });
  322. };
  323. Common.prototype.search = function (id, e, searchUrl, call) {
  324. var _this3 = this;
  325. var input = void 0;
  326. if (call) {
  327. input = call;
  328. } else {
  329. input = e.target;
  330. var keyCode = e.keyCode;
  331. if (keyCode === 9 || keyCode === 13 || keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40) {
  332. return false;
  333. }
  334. }
  335. var inputValue = $.trim(input.value);
  336. //过滤一下tips
  337. this.changePlaceHolder($(input));
  338. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  339. searchUrl = ajaxConfig.searchUrl || searchUrl;
  340. var fs = data[id],
  341. isCreate = fs.config.isCreate,
  342. reElem = $('dl[xid="' + id + '"]').parents('.' + FORM_SELECT);
  343. //如果开启了远程搜索
  344. if (searchUrl) {
  345. if (ajaxConfig.searchVal) {
  346. inputValue = ajaxConfig.searchVal;
  347. ajaxConfig.searchVal = '';
  348. }
  349. if (!ajaxConfig.beforeSearch || ajaxConfig.beforeSearch && ajaxConfig.beforeSearch instanceof Function && ajaxConfig.beforeSearch(id, searchUrl, inputValue)) {
  350. var delay = ajaxConfig.delay;
  351. if (ajaxConfig.first) {
  352. ajaxConfig.first = false;
  353. delay = 10;
  354. }
  355. clearTimeout(fs.clearid);
  356. fs.clearid = setTimeout(function () {
  357. //reElem.find('dl > *:not(.' + FORM_SELECT_TIPS + ')').remove();
  358. //reElem.find('dd.' + FORM_NONE).addClass(FORM_EMPTY).text('请求中');
  359. _this3.ajax(id, searchUrl, inputValue, false, null, true);
  360. }, delay);
  361. }
  362. } else {
  363. reElem.find('dl .' + DD_HIDE).removeClass(DD_HIDE);
  364. //遍历选项, 选择可以显示的值
  365. reElem.find('dl dd:not(.' + FORM_SELECT_TIPS + ')').each(function (idx, item) {
  366. var _item = $(item);
  367. var searchFun = events.filter[id] || data[id].config.filter;
  368. if (searchFun && searchFun(id, inputValue, _this3.getItem(id, _item), _item.hasClass(DISABLED)) == true) {
  369. _item.addClass(DD_HIDE);
  370. }
  371. });
  372. //控制分组名称
  373. reElem.find('dl dt').each(function (index, item) {
  374. if (!$(item).nextUntil('dt', ':not(.' + DD_HIDE + ')').length) {
  375. $(item).addClass(DD_HIDE);
  376. }
  377. });
  378. //动态创建
  379. this.create(id, isCreate, inputValue);
  380. var shows = reElem.find('dl dd:not(.' + FORM_SELECT_TIPS + '):not(.' + DD_HIDE + ')');
  381. if (!shows.length) {
  382. reElem.find('dd.' + FORM_NONE).addClass(FORM_EMPTY).text('无匹配项');
  383. } else {
  384. reElem.find('dd.' + FORM_NONE).removeClass(FORM_EMPTY);
  385. }
  386. }
  387. };
  388. Common.prototype.isArray = function (obj) {
  389. return Object.prototype.toString.call(obj) == "[object Array]";
  390. };
  391. Common.prototype.triggerSearch = function (div, isCall) {
  392. var _this4 = this;
  393. (div ? [div] : $('.' + FORM_SELECT).toArray()).forEach(function (reElem, index) {
  394. reElem = $(reElem);
  395. var id = reElem.find('dl').attr('xid');
  396. if (id && data[id] && data[id].config.isEmpty || isCall) {
  397. _this4.search(id, null, null, data[id].config.searchType == 0 ? reElem.find('.' + LABEL + ' .' + INPUT) : reElem.find('dl .' + FORM_DL_INPUT + ' .' + INPUT));
  398. }
  399. });
  400. };
  401. Common.prototype.clearInput = function (id) {
  402. var div = $('.' + PNAME + '[fs_id="' + id + '"]');
  403. var input = data[id].config.searchType == 0 ? div.find('.' + LABEL + ' .' + INPUT) : div.find('dl .' + FORM_DL_INPUT + ' .' + INPUT);
  404. input.val('');
  405. };
  406. Common.prototype.ajax = function (id, searchUrl, inputValue, isLinkage, linkageWidth, isSearch, successCallback, isReplace) {
  407. var _this5 = this;
  408. var reElem = $('.' + PNAME + ' dl[xid="' + id + '"]').parents('.' + FORM_SELECT);
  409. if (!reElem[0] || !searchUrl) {
  410. return;
  411. }
  412. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  413. var ajaxData = $.extend(true, {}, ajaxConfig.data);
  414. ajaxData[ajaxConfig.searchName] = inputValue;
  415. if (searchUrl.indexOf('{') != -1) {
  416. var start = searchUrl.split('{');
  417. var end = start[1].split('}');
  418. searchUrl = start[0] + end[1];
  419. if (end[0].indexOf(',')) {
  420. var param = end[0].split(',');
  421. } else {
  422. var param = [];
  423. param[0] = end[0];
  424. }
  425. for (var i in param) {
  426. if (typeof(param[i]) == 'string') {
  427. var e = $('#update_'+param[i]);
  428. if (e.length > 1) {
  429. e.each(function() {
  430. if ($(this).get(0).checked) {
  431. ajaxData[param[i]] = $(this).val();
  432. }
  433. })
  434. } else {
  435. ajaxData[param[i]] = e.val();
  436. }
  437. }
  438. }
  439. }
  440. //是否需要对ajax添加随机时间
  441. //ajaxData['_'] = Date.now();
  442. $.ajax({
  443. type: ajaxConfig.type,
  444. headers: ajaxConfig.header,
  445. url: searchUrl,
  446. data: ajaxConfig.dataType == 'json' ? JSON.stringify(ajaxData) : ajaxData,
  447. success: function success(res) {
  448. if (typeof res == 'string') {
  449. res = JSON.parse(res);
  450. }
  451. ajaxConfig.beforeSuccess && ajaxConfig.beforeSuccess instanceof Function && (res = ajaxConfig.beforeSuccess(id, searchUrl, inputValue, res));
  452. if (_this5.isArray(res)) {
  453. var newRes = {};
  454. newRes[ajaxConfig.response.statusName] = ajaxConfig.response.statusCode;
  455. newRes[ajaxConfig.response.msgName] = "";
  456. newRes[ajaxConfig.response.dataName] = res;
  457. res = newRes;
  458. }
  459. if (res[ajaxConfig.response.statusName] != ajaxConfig.response.statusCode) {
  460. reElem.find('dd.' + FORM_NONE).removeClass(FORM_EMPTY);
  461. _this5.renderData(id, [], isLinkage, linkageWidth, isSearch, isReplace);
  462. data[id].config.isEmpty = true;
  463. } else {
  464. reElem.find('dd.' + FORM_NONE).removeClass(FORM_EMPTY);
  465. _this5.renderData(id, res[ajaxConfig.response.dataName], isLinkage, linkageWidth, isSearch, isReplace);
  466. data[id].config.isEmpty = res[ajaxConfig.response.dataName].length == 0;
  467. }
  468. successCallback && successCallback(id);
  469. ajaxConfig.success && ajaxConfig.success instanceof Function && ajaxConfig.success(id, searchUrl, inputValue, res);
  470. },
  471. error: function error(err) {
  472. reElem.find('dd[lay-value]:not(.' + FORM_SELECT_TIPS + ')').remove();
  473. reElem.find('dd.' + FORM_NONE).addClass(FORM_EMPTY).text('服务异常');
  474. ajaxConfig.error && ajaxConfig.error instanceof Function && ajaxConfig.error(id, searchUrl, inputValue, err);
  475. }
  476. });
  477. };
  478. Common.prototype.renderData = function (id, dataArr, linkage, linkageWidth, isSearch, isReplace) {
  479. var _this6 = this;
  480. if (linkage) {
  481. //渲染多级联动
  482. this.renderLinkage(id, dataArr, linkageWidth);
  483. return;
  484. }
  485. if (isReplace) {
  486. this.renderReplace(id, dataArr);
  487. return;
  488. }
  489. var reElem = $('.' + PNAME + ' dl[xid="' + id + '"]').parents('.' + FORM_SELECT);
  490. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  491. var pcInput = reElem.find('.' + TDIV + ' input');
  492. dataArr = this.exchangeData(id, dataArr);
  493. var values = [];
  494. reElem.find('dl').html(this.renderSelect(id, pcInput.attr('placeholder') || pcInput.attr('back'), dataArr.map(function (item) {
  495. var itemVal = $.extend({}, item, {
  496. innerHTML: item[ajaxConfig.keyName],
  497. value: item[ajaxConfig.keyVal],
  498. sel: item[ajaxConfig.keySel],
  499. disabled: item[ajaxConfig.keyDis],
  500. type: item.type,
  501. name: item[ajaxConfig.keyName]
  502. });
  503. if (itemVal.sel) {
  504. values.push(itemVal);
  505. }
  506. return itemVal;
  507. })));
  508. var label = reElem.find('.' + LABEL);
  509. var dl = reElem.find('dl[xid]');
  510. if (isSearch) {
  511. //如果是远程搜索, 这里需要判重
  512. var oldVal = data[id].values;
  513. oldVal.forEach(function (item, index) {
  514. dl.find('dd[lay-value="' + item.value + '"]').addClass(THIS);
  515. });
  516. values.forEach(function (item, index) {
  517. if (_this6.indexOf(oldVal, item) == -1) {
  518. _this6.addLabel(id, label, item);
  519. dl.find('dd[lay-value="' + item.value + '"]').addClass(THIS);
  520. oldVal.push(item);
  521. }
  522. });
  523. } else {
  524. values.forEach(function (item, index) {
  525. _this6.addLabel(id, label, item);
  526. dl.find('dd[lay-value="' + item.value + '"]').addClass(THIS);
  527. });
  528. data[id].values = values;
  529. }
  530. this.commonHandler(id, label);
  531. };
  532. Common.prototype.renderLinkage = function (id, dataArr, linkageWidth) {
  533. var result = [],
  534. index = 0,
  535. temp = { "0": dataArr },
  536. ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  537. db[id] = {};
  538. var _loop = function _loop() {
  539. var group = result[index++] = [],
  540. _temp = temp;
  541. temp = {};
  542. $.each(_temp, function (pid, arr) {
  543. $.each(arr, function (idx, item) {
  544. var val = {
  545. pid: pid,
  546. name: item[ajaxConfig.keyName],
  547. value: item[ajaxConfig.keyVal]
  548. };
  549. db[id][val.value] = $.extend(item, val);
  550. group.push(val);
  551. var children = item[ajaxConfig.keyChildren];
  552. if (children && children.length) {
  553. temp[val.value] = children;
  554. }
  555. });
  556. });
  557. };
  558. do {
  559. _loop();
  560. } while (Object.getOwnPropertyNames(temp).length);
  561. var reElem = $('.' + PNAME + ' dl[xid="' + id + '"]').parents('.' + FORM_SELECT);
  562. var html = ['<div class="xm-select-linkage">'];
  563. $.each(result, function (idx, arr) {
  564. var groupDiv = ['<div style="left: ' + (linkageWidth - 0) * idx + 'px;" class="xm-select-linkage-group xm-select-linkage-group' + (idx + 1) + ' ' + (idx != 0 ? 'xm-select-linkage-hide' : '') + '">'];
  565. $.each(arr, function (idx2, item) {
  566. var span = '<li title="' + item.name + '" pid="' + item.pid + '" xm-value="' + item.value + '"><span>' + item.name + '</span></li>';
  567. groupDiv.push(span);
  568. });
  569. groupDiv.push('</div>');
  570. html = html.concat(groupDiv);
  571. });
  572. html.push('<div style="clear: both; height: 288px;"></div>');
  573. html.push('</div>');
  574. reElem.find('dl').html(html.join(''));
  575. reElem.find('.' + INPUT).css('display', 'none'); //联动暂时不支持搜索
  576. };
  577. Common.prototype.renderReplace = function (id, dataArr) {
  578. var _this7 = this;
  579. var dl = $('.' + PNAME + ' dl[xid="' + id + '"]');
  580. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  581. dataArr = this.exchangeData(id, dataArr);
  582. db[id] = dataArr;
  583. var html = dataArr.map(function (item) {
  584. var itemVal = $.extend({}, item, {
  585. innerHTML: item[ajaxConfig.keyName],
  586. value: item[ajaxConfig.keyVal],
  587. sel: item[ajaxConfig.keySel],
  588. disabled: item[ajaxConfig.keyDis],
  589. type: item.type,
  590. name: item[ajaxConfig.keyName]
  591. });
  592. return _this7.createDD(id, itemVal);
  593. }).join('');
  594. dl.find('dd:not(.' + FORM_SELECT_TIPS + '),dt:not([style])').remove();
  595. dl.find('dt[style]').after($(html));
  596. };
  597. Common.prototype.exchangeData = function (id, arr) {
  598. //这里处理树形结构
  599. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  600. var childrenName = ajaxConfig['keyChildren'];
  601. var disabledName = ajaxConfig['keyDis'];
  602. db[id] = {};
  603. var result = this.getChildrenList(arr, childrenName, disabledName, [], false);
  604. return result;
  605. };
  606. Common.prototype.getChildrenList = function (arr, childrenName, disabledName, pid, disabled) {
  607. var result = [],
  608. offset = 0;
  609. for (var a = 0; a < arr.length; a++) {
  610. var item = arr[a];
  611. if (item.type && item.type == 'optgroup') {
  612. result.push(item);
  613. continue;
  614. } else {
  615. offset++;
  616. }
  617. var parentIds = pid.concat([]);
  618. parentIds.push(offset - 1 + '_E');
  619. item[FORM_TEAM_PID] = JSON.stringify(parentIds);
  620. item[disabledName] = item[disabledName] || disabled;
  621. result.push(item);
  622. var child = item[childrenName];
  623. if (child && common.isArray(child) && child.length) {
  624. item['XM_TREE_FOLDER'] = true;
  625. var pidArr = parentIds.concat([]);
  626. var childResult = this.getChildrenList(child, childrenName, disabledName, pidArr, item[disabledName]);
  627. result = result.concat(childResult);
  628. }
  629. }
  630. return result;
  631. };
  632. Common.prototype.create = function (id, isCreate, inputValue) {
  633. if (isCreate && inputValue) {
  634. var fs = data[id],
  635. dl = $('[xid="' + id + '"]'),
  636. tips = dl.find('dd.' + FORM_SELECT_TIPS + '.' + FORM_DL_INPUT),
  637. tdd = null,
  638. temp = dl.find('dd.' + TEMP);
  639. dl.find('dd:not(.' + FORM_SELECT_TIPS + '):not(.' + TEMP + ')').each(function (index, item) {
  640. if (inputValue == $(item).find('span').attr('name')) {
  641. tdd = item;
  642. }
  643. });
  644. if (!tdd) {
  645. //如果不存在, 则创建
  646. var val = fs.config.create(id, inputValue);
  647. if (temp[0]) {
  648. temp.attr('lay-value', val);
  649. temp.find('span').text(inputValue);
  650. temp.find('span').attr("name", inputValue);
  651. temp.removeClass(DD_HIDE);
  652. } else {
  653. tips.after($(this.createDD(id, {
  654. name: inputValue,
  655. innerHTML: inputValue,
  656. value: val
  657. }, TEMP + ' ' + CREATE_LONG)));
  658. }
  659. }
  660. } else {
  661. $('[xid=' + id + '] dd.' + TEMP).remove();
  662. }
  663. };
  664. Common.prototype.createDD = function (id, item, clz) {
  665. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  666. var name = $.trim(item.innerHTML);
  667. db[id][item.value] = $(item).is('option') ? item = function () {
  668. var resultItem = {};
  669. resultItem[ajaxConfig.keyName] = name;
  670. resultItem[ajaxConfig.keyVal] = item.value;
  671. resultItem[ajaxConfig.keyDis] = item.disabled;
  672. return resultItem;
  673. }() : item;
  674. var template = data[id].config.template(id, item);
  675. var pid = item[FORM_TEAM_PID];
  676. pid ? pid = JSON.parse(pid) : pid = [-1];
  677. var attr = pid[0] == -1 ? '' : 'tree-id="' + pid.join('-') + '" tree-folder="' + !!item['XM_TREE_FOLDER'] + '"';
  678. return '<dd lay-value="' + item.value + '" class="' + (item.disabled ? DISABLED : '') + ' ' + (clz ? clz : '') + '" ' + attr + '>\n\t\t\t\t\t<div class="xm-unselect xm-form-checkbox ' + (item.disabled ? DISABLED : '') + '" style="margin-left: ' + (pid.length - 1) * 30 + 'px">\n\t\t\t\t\t\t<i class="' + CHECKBOX_YES + '"></i>\n\t\t\t\t\t\t<span name="' + name + '">' + template + '</span>\n\t\t\t\t\t</div>\n\t\t\t\t</dd>';
  679. };
  680. Common.prototype.createQuickBtn = function (obj, right) {
  681. return '<div class="' + CZ + '" method="' + obj.name + '" title="' + obj.name + '" ' + (right ? 'style="margin-right: ' + right + '"' : '') + '><i class="' + obj.icon + '"></i><span>' + obj.name + '</span></div>';
  682. };
  683. Common.prototype.renderBtns = function (id, show, right) {
  684. var _this8 = this;
  685. var quickBtn = [];
  686. var dl = $('dl[xid="' + id + '"]');
  687. quickBtn.push('<div class="' + CZ_GROUP + '" show="' + show + '" style="max-width: ' + (dl.prev().width() - 54) + 'px;">');
  688. $.each(data[id].config.btns, function (index, item) {
  689. quickBtn.push(_this8.createQuickBtn(item, right));
  690. });
  691. quickBtn.push('</div>');
  692. quickBtn.push(this.createQuickBtn({ icon: 'xm-iconfont icon-caidan', name: '' }));
  693. return quickBtn.join('');
  694. };
  695. Common.prototype.renderSelect = function (id, tips, select) {
  696. var _this9 = this;
  697. db[id] = {};
  698. var arr = [];
  699. if (data[id].config.btns.length) {
  700. setTimeout(function () {
  701. var dl = $('dl[xid="' + id + '"]');
  702. dl.parents('.' + FORM_SELECT).attr(SEARCH_TYPE, data[id].config.searchType);
  703. dl.find('.' + CZ_GROUP).css('max-width', dl.prev().width() - 54 + 'px');
  704. }, 10);
  705. arr.push(['<dd lay-value="" class="' + FORM_SELECT_TIPS + '" style="background-color: #FFF!important;display:none;">', this.renderBtns(id, null, '30px'), '</dd>', '<dd lay-value="" class="' + FORM_SELECT_TIPS + ' ' + FORM_DL_INPUT + '" style="background-color: #FFF!important;">', '<i class="xm-iconfont icon-sousuo"></i>', '<input type="text" class="' + FORM_INPUT + ' ' + INPUT + '" placeholder="\u8BF7\u641C\u7D22"/>', '</dd>'].join(''));
  706. } else {
  707. arr.push('<dd lay-value="" class="' + FORM_SELECT_TIPS + '">' + tips + '</dd>');
  708. }
  709. if (this.isArray(select)) {
  710. $(select).each(function (index, item) {
  711. if (item) {
  712. if (item.type && item.type === 'optgroup') {
  713. arr.push('<dt>' + item.name + '</dt>');
  714. } else {
  715. arr.push(_this9.createDD(id, item));
  716. }
  717. }
  718. });
  719. } else {
  720. $(select).find('*').each(function (index, item) {
  721. if (item.tagName.toLowerCase() == 'option' && index == 0 && !item.value) {
  722. return;
  723. }
  724. if (item.tagName.toLowerCase() === 'optgroup') {
  725. arr.push('<dt>' + item.label + '</dt>');
  726. } else {
  727. arr.push(_this9.createDD(id, item));
  728. }
  729. });
  730. }
  731. arr.push('<dt style="display:none;"> </dt>');
  732. arr.push('<dd class="' + FORM_SELECT_TIPS + ' ' + FORM_NONE + ' ' + (arr.length === 2 ? FORM_EMPTY : '') + '">\u6CA1\u6709\u9009\u9879</dd>');
  733. return arr.join('');
  734. };
  735. Common.prototype.on = function () {
  736. var _this10 = this;
  737. //事件绑定
  738. this.one();
  739. $(document).on('click', function (e) {
  740. if (!$(e.target).parents('.' + FORM_TITLE)[0]) {
  741. //清空input中的值
  742. $('.' + PNAME + ' dl .' + DD_HIDE).removeClass(DD_HIDE);
  743. $('.' + PNAME + ' dl dd.' + FORM_EMPTY).removeClass(FORM_EMPTY);
  744. $('.' + PNAME + ' dl dd.' + TEMP).remove();
  745. $.each(data, function (key, fs) {
  746. _this10.clearInput(key);
  747. if (!fs.values.length) {
  748. _this10.changePlaceHolder($('div[FS_ID="' + key + '"] .' + LABEL));
  749. }
  750. });
  751. }
  752. $('.' + PNAME + ' .' + FORM_SELECTED).each(function (index, item) {
  753. _this10.changeShow($(item).find('.' + FORM_TITLE), false);
  754. });
  755. });
  756. };
  757. Common.prototype.calcLabelLeft = function (label, w, call) {
  758. var pos = this.getPosition(label[0]);
  759. pos.y = pos.x + label[0].clientWidth;
  760. var left = label[0].offsetLeft;
  761. if (!label.find('span').length) {
  762. left = 0;
  763. } else if (call) {
  764. //校正归位
  765. var span = label.find('span:last');
  766. span.css('display') == 'none' ? span = span.prev()[0] : span = span[0];
  767. var spos = this.getPosition(span);
  768. spos.y = spos.x + span.clientWidth;
  769. if (spos.y > pos.y) {
  770. left = left - (spos.y - pos.y) - 5;
  771. } else {
  772. left = 0;
  773. }
  774. } else {
  775. if (w < 0) {
  776. var _span = label.find(':last');
  777. _span.css('display') == 'none' ? _span = _span.prev()[0] : _span = _span[0];
  778. var _spos = this.getPosition(_span);
  779. _spos.y = _spos.x + _span.clientWidth;
  780. if (_spos.y > pos.y) {
  781. left -= 10;
  782. }
  783. } else {
  784. if (left < 0) {
  785. left += 10;
  786. }
  787. if (left > 0) {
  788. left = 0;
  789. }
  790. }
  791. }
  792. label.css('left', left + 'px');
  793. };
  794. Common.prototype.one = function (target) {
  795. var _this11 = this;
  796. //一次性事件绑定
  797. $(target ? target : document).off('click', '.' + FORM_TITLE).on('click', '.' + FORM_TITLE, function (e) {
  798. var othis = $(e.target),
  799. title = othis.is(FORM_TITLE) ? othis : othis.parents('.' + FORM_TITLE),
  800. dl = title.next(),
  801. id = dl.attr('xid');
  802. //清空非本select的input val
  803. $('dl[xid]').not(dl).each(function (index, item) {
  804. _this11.clearInput($(item).attr('xid'));
  805. });
  806. $('dl[xid]').not(dl).find('dd.' + DD_HIDE).removeClass(DD_HIDE);
  807. //如果是disabled select
  808. if (title.hasClass(DIS)) {
  809. return false;
  810. }
  811. //如果点击的是右边的三角或者只读的input
  812. if (othis.is('.' + SANJIAO) || othis.is('.' + INPUT + '[readonly]')) {
  813. _this11.changeShow(title, !title.parents('.' + FORM_SELECT).hasClass(FORM_SELECTED));
  814. return false;
  815. }
  816. //如果点击的是input的右边, focus一下
  817. if (title.find('.' + INPUT + ':not(readonly)')[0]) {
  818. var input = title.find('.' + INPUT),
  819. epos = { x: e.pageX, y: e.pageY },
  820. pos = _this11.getPosition(title[0]),
  821. width = title.width();
  822. while (epos.x > pos.x) {
  823. if ($(document.elementFromPoint(epos.x, epos.y)).is(input)) {
  824. input.focus();
  825. _this11.changeShow(title, true);
  826. return false;
  827. }
  828. epos.x -= 50;
  829. }
  830. }
  831. //如果点击的是可搜索的input
  832. if (othis.is('.' + INPUT)) {
  833. _this11.changeShow(title, true);
  834. return false;
  835. }
  836. //如果点击的是x按钮
  837. if (othis.is('i[fsw="' + NAME + '"]')) {
  838. var val = _this11.getItem(id, othis),
  839. dd = dl.find('dd[lay-value=\'' + val.value + '\']');
  840. if (dd.hasClass(DISABLED)) {
  841. //如果是disabled状态, 不可选, 不可删
  842. return false;
  843. }
  844. _this11.handlerLabel(id, dd, false, val);
  845. return false;
  846. }
  847. _this11.changeShow(title, !title.parents('.' + FORM_SELECT).hasClass(FORM_SELECTED));
  848. return false;
  849. });
  850. $(target ? target : document).off('click', 'dl.' + DL).on('click', 'dl.' + DL, function (e) {
  851. var othis = $(e.target);
  852. if (othis.is('.' + LINKAGE) || othis.parents('.' + LINKAGE)[0]) {
  853. //linkage的处理
  854. othis = othis.is('li') ? othis : othis.parents('li[xm-value]');
  855. var _group = othis.parents('.xm-select-linkage-group'),
  856. _id = othis.parents('dl').attr('xid');
  857. if (!_id) {
  858. return false;
  859. }
  860. //激活li
  861. _group.find('.xm-select-active').removeClass('xm-select-active');
  862. othis.addClass('xm-select-active');
  863. //激活下一个group, 激活前显示对应数据
  864. _group.nextAll('.xm-select-linkage-group').addClass('xm-select-linkage-hide');
  865. var nextGroup = _group.next('.xm-select-linkage-group');
  866. nextGroup.find('li').addClass('xm-select-linkage-hide');
  867. nextGroup.find('li[pid="' + othis.attr('xm-value') + '"]').removeClass('xm-select-linkage-hide');
  868. //如果没有下一个group, 或没有对应的值
  869. if (!nextGroup[0] || nextGroup.find('li:not(.xm-select-linkage-hide)').length == 0) {
  870. var vals = [],
  871. index = 0,
  872. isAdd = !othis.hasClass('xm-select-this');
  873. if (data[_id].config.radio) {
  874. othis.parents('.xm-select-linkage').find('.xm-select-this').removeClass('xm-select-this');
  875. }
  876. do {
  877. vals[index++] = {
  878. name: othis.find('span').text(),
  879. value: othis.attr('xm-value')
  880. };
  881. othis = othis.parents('.xm-select-linkage-group').prev().find('li[xm-value="' + othis.attr('pid') + '"]');
  882. } while (othis.length);
  883. vals.reverse();
  884. var val = {
  885. name: vals.map(function (item) {
  886. return item.name;
  887. }).join('/'),
  888. value: vals.map(function (item) {
  889. return item.value;
  890. }).join('/')
  891. };
  892. _this11.handlerLabel(_id, null, isAdd, val);
  893. } else {
  894. nextGroup.removeClass('xm-select-linkage-hide');
  895. }
  896. return false;
  897. }
  898. if (othis.is('dl')) {
  899. return false;
  900. }
  901. if (othis.is('dt')) {
  902. othis.nextUntil('dt').each(function (index, item) {
  903. item = $(item);
  904. if (item.hasClass(DISABLED) || item.hasClass(THIS)) {} else {
  905. item.find('i:not(.icon-expand)').click();
  906. }
  907. });
  908. return false;
  909. }
  910. var dd = othis.is('dd') ? othis : othis.parents('dd');
  911. var id = dd.parent('dl').attr('xid');
  912. if (dd.hasClass(DISABLED)) {
  913. //被禁用选项的处理
  914. return false;
  915. }
  916. //菜单功效
  917. if (othis.is('i.icon-caidan')) {
  918. var opens = [],
  919. closes = [];
  920. othis.parents('dl').find('dd[tree-folder="true"]').each(function (index, item) {
  921. $(item).attr('xm-tree-hidn') == undefined ? opens.push(item) : closes.push(item);
  922. });
  923. var arr = closes.length ? closes : opens;
  924. arr.forEach(function (item) {
  925. return item.click();
  926. });
  927. return false;
  928. }
  929. //树状结构的选择
  930. var treeId = dd.attr('tree-id');
  931. if (treeId) {
  932. //忽略右边的图标
  933. if (othis.is('i:not(.icon-expand)')) {
  934. _this11.handlerLabel(id, dd, !dd.hasClass(THIS));
  935. return false;
  936. }
  937. var ajaxConfig = ajaxs[id] || ajax;
  938. var treeConfig = ajaxConfig.tree;
  939. var childrens = dd.nextAll('dd[tree-id^="' + treeId + '"]');
  940. if (childrens && childrens.length) {
  941. var len = childrens[0].clientHeight;
  942. len ? (_this11.addTreeHeight(dd, len), len = 0) : (len = dd.attr('xm-tree-hidn') || 36, dd.removeAttr('xm-tree-hidn'), dd.find('>i').remove(), childrens = childrens.filter(function (index, item) {
  943. return $(item).attr('tree-id').split('-').length - 1 == treeId.split('-').length;
  944. }));
  945. childrens.animate({
  946. height: len
  947. }, 150);
  948. return false;
  949. } else {
  950. if (treeConfig.nextClick && treeConfig.nextClick instanceof Function) {
  951. treeConfig.nextClick(id, _this11.getItem(id, dd), function (res) {
  952. if (!res || !res.length) {
  953. _this11.handlerLabel(id, dd, !dd.hasClass(THIS));
  954. } else {
  955. dd.attr('tree-folder', 'true');
  956. var ddChilds = [];
  957. res.forEach(function (item, idx) {
  958. item.innerHTML = item[ajaxConfig.keyName];
  959. item[FORM_TEAM_PID] = JSON.stringify(treeId.split('-').concat([idx]));
  960. ddChilds.push(_this11.createDD(id, item));
  961. db[id][item[ajaxConfig.keyVal]] = item;
  962. });
  963. dd.after(ddChilds.join(''));
  964. }
  965. });
  966. return false;
  967. }
  968. }
  969. }
  970. if (dd.hasClass(FORM_SELECT_TIPS)) {
  971. //tips的处理
  972. var btn = othis.is('.' + CZ) ? othis : othis.parents('.' + CZ);
  973. if (!btn[0]) {
  974. return false;
  975. }
  976. var method = btn.attr('method');
  977. var obj = data[id].config.btns.filter(function (bean) {
  978. return bean.name == method;
  979. })[0];
  980. obj && obj.click && obj.click instanceof Function && obj.click(id, _this11);
  981. return false;
  982. }
  983. _this11.handlerLabel(id, dd, !dd.hasClass(THIS));
  984. return false;
  985. });
  986. };
  987. Common.prototype.addTreeHeight = function (dd, len) {
  988. var _this12 = this;
  989. var treeId = dd.attr('tree-id');
  990. var childrens = dd.nextAll('dd[tree-id^="' + treeId + '"]');
  991. if (childrens.length) {
  992. dd.append('<i class="xm-iconfont icon-expand"></i>');
  993. dd.attr('xm-tree-hidn', len);
  994. childrens.each(function (index, item) {
  995. var that = $(item);
  996. _this12.addTreeHeight(that, len);
  997. });
  998. }
  999. };
  1000. var db = {};
  1001. Common.prototype.getItem = function (id, value) {
  1002. if (value instanceof $) {
  1003. if (value.is('i[fsw="' + NAME + '"]')) {
  1004. var span = value.parent();
  1005. return db[id][value] || {
  1006. name: span.find('font').text(),
  1007. value: span.attr('value')
  1008. };
  1009. }
  1010. var val = value.attr('lay-value');
  1011. return !db[id][val] ? db[id][val] = {
  1012. name: value.find('span[name]').attr('name'),
  1013. value: val
  1014. } : db[id][val];
  1015. } else if (typeof value == 'string' && value.indexOf('/') != -1) {
  1016. return db[id][value] || {
  1017. name: this.valToName(id, value),
  1018. value: value
  1019. };
  1020. }
  1021. return db[id][value];
  1022. };
  1023. Common.prototype.linkageAdd = function (id, val) {
  1024. var dl = $('dl[xid="' + id + '"]');
  1025. dl.find('.xm-select-active').removeClass('xm-select-active');
  1026. var vs = val.value.split('/');
  1027. var pid = void 0,
  1028. li = void 0,
  1029. index = 0;
  1030. var lis = [];
  1031. do {
  1032. pid = vs[index];
  1033. li = dl.find('.xm-select-linkage-group' + (index + 1) + ' li[xm-value="' + pid + '"]');
  1034. li[0] && lis.push(li);
  1035. index++;
  1036. } while (li.length && pid != undefined);
  1037. if (lis.length == vs.length) {
  1038. $.each(lis, function (idx, item) {
  1039. item.addClass('xm-select-this');
  1040. });
  1041. }
  1042. };
  1043. Common.prototype.linkageDel = function (id, val) {
  1044. var dl = $('dl[xid="' + id + '"]');
  1045. var vs = val.value.split('/');
  1046. var pid = void 0,
  1047. li = void 0,
  1048. index = vs.length - 1;
  1049. do {
  1050. pid = vs[index];
  1051. li = dl.find('.xm-select-linkage-group' + (index + 1) + ' li[xm-value="' + pid + '"]');
  1052. if (!li.parent().next().find('li[pid=' + pid + '].xm-select-this').length) {
  1053. li.removeClass('xm-select-this');
  1054. }
  1055. index--;
  1056. } while (li.length && pid != undefined);
  1057. };
  1058. Common.prototype.valToName = function (id, val) {
  1059. var dl = $('dl[xid="' + id + '"]');
  1060. var vs = (val + "").split('/');
  1061. if (!vs.length) {
  1062. return null;
  1063. }
  1064. var names = [];
  1065. $.each(vs, function (idx, item) {
  1066. var name = dl.find('.xm-select-linkage-group' + (idx + 1) + ' li[xm-value="' + item + '"] span').text();
  1067. names.push(name);
  1068. });
  1069. return names.length == vs.length ? names.join('/') : null;
  1070. };
  1071. Common.prototype.commonHandler = function (key, label) {
  1072. if (!label || !label[0]) {
  1073. return;
  1074. }
  1075. this.checkHideSpan(key, label);
  1076. //计算input的提示语
  1077. this.changePlaceHolder(label);
  1078. //计算高度
  1079. this.retop(label.parents('.' + FORM_SELECT));
  1080. this.calcLabelLeft(label, 0, true);
  1081. //表单默认值
  1082. this.setHidnVal(key, label);
  1083. //title值
  1084. label.parents('.' + FORM_TITLE + ' .' + NAME).attr('title', data[key].values.map(function (val) {
  1085. return val.name;
  1086. }).join(','));
  1087. };
  1088. Common.prototype.initVal = function (id) {
  1089. var _this13 = this;
  1090. var target = {};
  1091. if (id) {
  1092. target[id] = data[id];
  1093. } else {
  1094. target = data;
  1095. }
  1096. $.each(target, function (key, val) {
  1097. var values = val.values,
  1098. div = $('dl[xid="' + key + '"]').parent(),
  1099. label = div.find('.' + LABEL),
  1100. dl = div.find('dl');
  1101. dl.find('dd.' + THIS).removeClass(THIS);
  1102. var _vals = values.concat([]);
  1103. _vals.concat([]).forEach(function (item, index) {
  1104. _this13.addLabel(key, label, item);
  1105. dl.find('dd[lay-value="' + item.value + '"]').addClass(THIS);
  1106. });
  1107. if (val.config.radio) {
  1108. _vals.length && values.push(_vals[_vals.length - 1]);
  1109. }
  1110. _this13.commonHandler(key, label);
  1111. });
  1112. };
  1113. Common.prototype.setHidnVal = function (key, label) {
  1114. if (!label || !label[0]) {
  1115. return;
  1116. }
  1117. label.parents('.' + PNAME).find('.' + HIDE_INPUT).val(data[key].values.map(function (val) {
  1118. return val.value;
  1119. }).join(','));
  1120. };
  1121. Common.prototype.handlerLabel = function (id, dd, isAdd, oval, notOn) {
  1122. var div = $('[xid="' + id + '"]').prev().find('.' + LABEL),
  1123. val = dd && this.getItem(id, dd),
  1124. vals = data[id].values,
  1125. on = data[id].config.on || events.on[id],
  1126. endOn = data[id].config.endOn || events.endOn[id];
  1127. if (oval) {
  1128. val = oval;
  1129. }
  1130. var fs = data[id];
  1131. if (isAdd && fs.config.max && fs.values.length >= fs.config.max) {
  1132. var maxTipsFun = events.maxTips[id] || data[id].config.maxTips;
  1133. maxTipsFun && maxTipsFun(id, vals.concat([]), val, fs.config.max);
  1134. return;
  1135. }
  1136. if (!notOn) {
  1137. if (on && on instanceof Function && on(id, vals.concat([]), val, isAdd, dd && dd.hasClass(DISABLED)) == false) {
  1138. return;
  1139. }
  1140. }
  1141. var dl = $('dl[xid="' + id + '"]');
  1142. isAdd ? (dd && dd[0] ? (dd.addClass(THIS), dd.removeClass(TEMP)) : dl.find('.xm-select-linkage')[0] && this.linkageAdd(id, val), this.addLabel(id, div, val), vals.push(val)) : (dd && dd[0] ? dd.removeClass(THIS) : dl.find('.xm-select-linkage')[0] && this.linkageDel(id, val), this.delLabel(id, div, val), this.remove(vals, val));
  1143. if (!div[0]) return;
  1144. //单选选完后直接关闭选择域
  1145. if (fs.config.radio) {
  1146. this.changeShow(div, false);
  1147. }
  1148. //移除表单验证的红色边框
  1149. div.parents('.' + FORM_TITLE).prev().removeClass('layui-form-danger');
  1150. //清空搜索值
  1151. fs.config.clearInput && this.clearInput(id);
  1152. this.commonHandler(id, div);
  1153. var ajaxConfig = ajaxs[id] ? ajaxs[id] : ajax;
  1154. if (ajaxConfig.callback) {
  1155. $('#' + id).attr('v', val.id);
  1156. var param = eval('(' + ajaxConfig.callbackParam + ')');
  1157. if (param['col']) {
  1158. if (param['col'].indexOf(',') != -1) {
  1159. var temp = param['col'].split(',');
  1160. for (var i in temp) {
  1161. if (val[temp[i]]) {
  1162. param[temp[i]] = val[temp[i]];
  1163. }
  1164. }
  1165. } else if (val[param['col']]) {
  1166. param[param['col']] = val[param['col']];
  1167. }
  1168. }
  1169. loading($('#' + id), param, 1);
  1170. }
  1171. !notOn && endOn && endOn instanceof Function && endOn(id, vals.concat([]), val, isAdd, dd && dd.hasClass(DISABLED));
  1172. };
  1173. Common.prototype.addLabel = function (id, div, val) {
  1174. if (!val) return;
  1175. var tips = 'fsw="' + NAME + '"';
  1176. var _ref = [$('<span ' + tips + ' value="' + val.value + '"><font ' + tips + '>' + val.name + '</font></span>'), $('<i ' + tips + ' class="xm-iconfont icon-close"></i>')],
  1177. $label = _ref[0],
  1178. $close = _ref[1];
  1179. $label.append($close);
  1180. //如果是radio模式
  1181. var fs = data[id];
  1182. if (fs.config.radio) {
  1183. fs.values.length = 0;
  1184. $('dl[xid="' + id + '"]').find('dd.' + THIS + ':not([lay-value="' + val.value + '"])').removeClass(THIS);
  1185. div.find('span').remove();
  1186. }
  1187. //如果是固定高度
  1188. div.find('input').css('width', '50px');
  1189. div.find('input').before($label);
  1190. };
  1191. Common.prototype.delLabel = function (id, div, val) {
  1192. if (!val) return;
  1193. div.find('span[value="' + val.value + '"]:first').remove();
  1194. };
  1195. Common.prototype.checkHideSpan = function (id, div) {
  1196. var parentHeight = div.parents('.' + NAME)[0].offsetHeight + 5;
  1197. div.find('span.xm-span-hide').removeClass('xm-span-hide');
  1198. div.find('span[style]').remove();
  1199. var count = data[id].config.showCount;
  1200. div.find('span').each(function (index, item) {
  1201. if (index >= count) {
  1202. $(item).addClass('xm-span-hide');
  1203. }
  1204. });
  1205. var prefix = div.find('span:eq(' + count + ')');
  1206. prefix[0] && prefix.before($('<span style="padding-right: 6px;" fsw="' + NAME + '"> + ' + (div.find('span').length - count) + '</span>'));
  1207. };
  1208. Common.prototype.retop = function (div) {
  1209. //计算dl显示的位置
  1210. var dl = div.find('dl'),
  1211. top = div.offset().top + div.outerHeight() + 5 - $win.scrollTop(),
  1212. dlHeight = dl.outerHeight();
  1213. var up = div.hasClass('layui-form-selectup') || dl.css('top').indexOf('-') != -1 || top + dlHeight > $win.height() && top >= dlHeight;
  1214. div = div.find('.' + NAME);
  1215. var fs = data[dl.attr('xid')];
  1216. var base = dl.parents('.layui-form-pane')[0] && dl.prev()[0].clientHeight > 38 ? 14 : 10;
  1217. if (fs && fs.config.direction == 'up' || up) {
  1218. up = true;
  1219. if (fs && fs.config.direction == 'down') {
  1220. up = false;
  1221. }
  1222. }
  1223. var reHeight = div[0].offsetTop + div.height() + base;
  1224. if (up) {
  1225. dl.css({
  1226. top: 'auto',
  1227. bottom: reHeight + 3 + 'px'
  1228. });
  1229. } else {
  1230. dl.css({
  1231. top: reHeight + 'px',
  1232. bottom: 'auto'
  1233. });
  1234. }
  1235. };
  1236. Common.prototype.changeShow = function (children, isShow) {
  1237. //显示于隐藏
  1238. $('.layui-form-selected').removeClass('layui-form-selected');
  1239. var top = children.parents('.' + FORM_SELECT),
  1240. realShow = top.hasClass(FORM_SELECTED),
  1241. id = top.find('dl').attr('xid');
  1242. $('.' + PNAME + ' .' + FORM_SELECT).not(top).removeClass(FORM_SELECTED);
  1243. if (isShow) {
  1244. this.retop(top);
  1245. top.addClass(FORM_SELECTED);
  1246. top.find('.' + INPUT).focus();
  1247. var searchUrl = surl;
  1248. if (searchUrl.indexOf('{') != -1) {
  1249. this.triggerSearch(top, true);
  1250. }
  1251. if (!top.find('dl dd[lay-value]:not(.' + FORM_SELECT_TIPS + ')').length) {
  1252. top.find('dl .' + FORM_NONE).addClass(FORM_EMPTY);
  1253. }
  1254. } else {
  1255. top.removeClass(FORM_SELECTED);
  1256. this.clearInput(id);
  1257. top.find('dl .' + FORM_EMPTY).removeClass(FORM_EMPTY);
  1258. top.find('dl dd.' + DD_HIDE).removeClass(DD_HIDE);
  1259. top.find('dl dd.' + TEMP).remove();
  1260. //计算ajax数据是否为空, 然后重新请求数据
  1261. if (id && data[id] && data[id].config.isEmpty) {
  1262. this.triggerSearch(top);
  1263. }
  1264. this.changePlaceHolder(top.find('.' + LABEL));
  1265. }
  1266. if (isShow != realShow) {
  1267. var openFun = data[id].config.opened || events.opened[id];
  1268. isShow && openFun && openFun instanceof Function && openFun(id);
  1269. var closeFun = data[id].config.closed || events.closed[id];
  1270. !isShow && closeFun && closeFun instanceof Function && closeFun(id);
  1271. }
  1272. };
  1273. Common.prototype.changePlaceHolder = function (div) {
  1274. //显示于隐藏提示语
  1275. //调整pane模式下的高度
  1276. var title = div.parents('.' + FORM_TITLE);
  1277. title[0] || (title = div.parents('dl').prev());
  1278. if (!title[0]) {
  1279. return;
  1280. }
  1281. var id = div.parents('.' + PNAME).find('dl[xid]').attr('xid');
  1282. if (data[id] && data[id].config.height) {//既然固定高度了, 那就看着办吧
  1283. } else {
  1284. var height = title.find('.' + NAME)[0].clientHeight;
  1285. //title.css('height', (height > 36 ? height + 4 : height) + 'px');
  1286. //如果是layui pane模式, 处理label的高度
  1287. var label = title.parents('.' + PNAME).parent().prev();
  1288. if (label.is('.layui-form-label') && title.parents('.layui-form-pane')[0]) {
  1289. height = height > 36 ? height + 4 : height;
  1290. //title.css('height', height + 'px');
  1291. label.css({
  1292. height: height + 2 + 'px',
  1293. lineHeight: height - 18 + 'px'
  1294. });
  1295. }
  1296. }
  1297. var input = title.find('.' + TDIV + ' input'),
  1298. isShow = !div.find('span:last')[0] && !title.find('.' + INPUT).val();
  1299. if (isShow) {
  1300. var ph = input.attr('back');
  1301. input.removeAttr('back');
  1302. input.attr('placeholder', ph);
  1303. } else {
  1304. var _ph = input.attr('placeholder');
  1305. input.removeAttr('placeholder');
  1306. input.attr('back', _ph);
  1307. }
  1308. };
  1309. Common.prototype.indexOf = function (arr, val) {
  1310. for (var i = 0; i < arr.length; i++) {
  1311. if (arr[i].value == val || arr[i].value == (val ? val.value : val) || arr[i] == val || JSON.stringify(arr[i]) == JSON.stringify(val)) {
  1312. return i;
  1313. }
  1314. }
  1315. return -1;
  1316. };
  1317. Common.prototype.remove = function (arr, val) {
  1318. var idx = this.indexOf(arr, val ? val.value : val);
  1319. if (idx > -1) {
  1320. arr.splice(idx, 1);
  1321. return true;
  1322. }
  1323. return false;
  1324. };
  1325. Common.prototype.selectAll = function (id, isOn, skipDis) {
  1326. var _this14 = this;
  1327. var dl = $('[xid="' + id + '"]');
  1328. if (!dl[0]) {
  1329. return;
  1330. }
  1331. if (dl.find('.xm-select-linkage')[0]) {
  1332. return;
  1333. }
  1334. dl.find('dd[lay-value]:not(.' + FORM_SELECT_TIPS + '):not(.' + THIS + ')' + (skipDis ? ':not(.' + DISABLED + ')' : '')).each(function (index, item) {
  1335. item = $(item);
  1336. var val = _this14.getItem(id, item);
  1337. _this14.handlerLabel(id, dl.find('dd[lay-value="' + val.value + '"]'), true, val, !isOn);
  1338. });
  1339. };
  1340. Common.prototype.removeAll = function (id, isOn, skipDis) {
  1341. var _this15 = this;
  1342. var dl = $('[xid="' + id + '"]');
  1343. if (!dl[0]) {
  1344. return;
  1345. }
  1346. if (dl.find('.xm-select-linkage')[0]) {
  1347. //针对多级联动的处理
  1348. data[id].values.concat([]).forEach(function (item, idx) {
  1349. var vs = item.value.split('/');
  1350. var pid = void 0,
  1351. li = void 0,
  1352. index = 0;
  1353. do {
  1354. pid = vs[index++];
  1355. li = dl.find('.xm-select-linkage-group' + index + ':not(.xm-select-linkage-hide) li[xm-value="' + pid + '"]');
  1356. li.click();
  1357. } while (li.length && pid != undefined);
  1358. });
  1359. return;
  1360. }
  1361. data[id].values.concat([]).forEach(function (item, index) {
  1362. if (skipDis && dl.find('dd[lay-value="' + item.value + '"]').hasClass(DISABLED)) {} else {
  1363. _this15.handlerLabel(id, dl.find('dd[lay-value="' + item.value + '"]'), false, item, !isOn);
  1364. }
  1365. });
  1366. };
  1367. Common.prototype.reverse = function (id, isOn, skipDis) {
  1368. var _this16 = this;
  1369. var dl = $('[xid="' + id + '"]');
  1370. if (!dl[0]) {
  1371. return;
  1372. }
  1373. if (dl.find('.xm-select-linkage')[0]) {
  1374. return;
  1375. }
  1376. dl.find('dd[lay-value]:not(.' + FORM_SELECT_TIPS + ')' + (skipDis ? ':not(.' + DISABLED + ')' : '')).each(function (index, item) {
  1377. item = $(item);
  1378. var val = _this16.getItem(id, item);
  1379. _this16.handlerLabel(id, dl.find('dd[lay-value="' + val.value + '"]'), !item.hasClass(THIS), val, !isOn);
  1380. });
  1381. };
  1382. Common.prototype.skin = function (id) {
  1383. var skins = ['default', 'primary', 'normal', 'warm', 'danger'];
  1384. var skin = skins[Math.floor(Math.random() * skins.length)];
  1385. $('dl[xid="' + id + '"]').parents('.' + PNAME).find('.' + FORM_SELECT).attr('xm-select-skin', skin);
  1386. this.check(id) && this.commonHandler(id, $('dl[xid="' + id + '"]').parents('.' + PNAME).find('.' + LABEL));
  1387. };
  1388. Common.prototype.getPosition = function (e) {
  1389. var x = 0,
  1390. y = 0;
  1391. while (e != null) {
  1392. x += e.offsetLeft;
  1393. y += e.offsetTop;
  1394. e = e.offsetParent;
  1395. }
  1396. return { x: x, y: y };
  1397. };
  1398. Common.prototype.onreset = function () {
  1399. //监听reset按钮, 然后重置多选
  1400. $(document).on('click', '[type=reset]', function (e) {
  1401. $(e.target).parents('form').find('.' + PNAME + ' dl[xid]').each(function (index, item) {
  1402. var id = item.getAttribute('xid'),
  1403. dl = $(item),
  1404. dd = void 0,
  1405. temp = {};
  1406. common.removeAll(id);
  1407. data[id].config.init.forEach(function (val, idx) {
  1408. if (val && (!temp[val] || data[id].config.repeat) && (dd = dl.find('dd[lay-value="' + val.value + '"]'))[0]) {
  1409. common.handlerLabel(id, dd, true);
  1410. temp[val] = 1;
  1411. }
  1412. });
  1413. });
  1414. });
  1415. };
  1416. Common.prototype.bindEvent = function (name, id, fun) {
  1417. if (id && id instanceof Function) {
  1418. fun = id;
  1419. id = null;
  1420. }
  1421. if (fun && fun instanceof Function) {
  1422. if (!id) {
  1423. $.each(data, function (id, val) {
  1424. data[id] ? data[id].config[name] = fun : events[name][id] = fun;
  1425. });
  1426. } else {
  1427. data[id] ? (data[id].config[name] = fun, delete events[name][id]) : events[name][id] = fun;
  1428. }
  1429. }
  1430. };
  1431. Common.prototype.check = function (id, notAutoRender) {
  1432. if ($('dl[xid="' + id + '"]').length) {
  1433. return true;
  1434. } else if ($('select[xm-select="' + id + '"]').length) {
  1435. if (!notAutoRender) {
  1436. this.render(id, $('select[xm-select="' + id + '"]'));
  1437. return true;
  1438. }
  1439. } else {
  1440. delete data[id];
  1441. return false;
  1442. }
  1443. };
  1444. Common.prototype.render = function (id, select) {
  1445. common.init(select);
  1446. common.one($('dl[xid="' + id + '"]').parents('.' + PNAME));
  1447. common.initVal(id);
  1448. };
  1449. Common.prototype.log = function (obj) {
  1450. console.log(obj);
  1451. };
  1452. var Select4 = function Select4() {
  1453. this.v = v;
  1454. this.render();
  1455. };
  1456. var common = new Common();
  1457. Select4.prototype.value = function (id, type, isAppend) {
  1458. if (typeof id != 'string') {
  1459. return [];
  1460. }
  1461. var fs = data[id];
  1462. if (!common.check(id)) {
  1463. return [];
  1464. }
  1465. if (typeof type == 'string' || type == undefined) {
  1466. var arr = fs.values.concat([]) || [];
  1467. if (type == 'val') {
  1468. return arr.map(function (val) {
  1469. return val.value;
  1470. });
  1471. }
  1472. if (type == 'valStr') {
  1473. return arr.map(function (val) {
  1474. return val.value;
  1475. }).join(',');
  1476. }
  1477. if (type == 'name') {
  1478. return arr.map(function (val) {
  1479. return val.name;
  1480. });
  1481. }
  1482. if (type == 'nameStr') {
  1483. return arr.map(function (val) {
  1484. return val.name;
  1485. }).join(',');
  1486. }
  1487. return arr;
  1488. }
  1489. if (common.isArray(type)) {
  1490. var dl = $('[xid="' + id + '"]'),
  1491. temp = {},
  1492. dd = void 0,
  1493. isAdd = true;
  1494. if (isAppend == false) {
  1495. //删除传入的数组
  1496. isAdd = false;
  1497. } else if (isAppend == true) {
  1498. //追加模式
  1499. isAdd = true;
  1500. } else {
  1501. //删除原有的数据
  1502. common.removeAll(id);
  1503. }
  1504. if (isAdd) {
  1505. fs.values.forEach(function (val, index) {
  1506. temp[val.value] = 1;
  1507. });
  1508. }
  1509. type.forEach(function (val, index) {
  1510. if (val && (!temp[val] || fs.config.repeat)) {
  1511. if ((dd = dl.find('dd[lay-value="' + val + '"]'))[0]) {
  1512. common.handlerLabel(id, dd, isAdd, null, true);
  1513. temp[val] = 1;
  1514. } else {
  1515. var name = common.valToName(id, val);
  1516. if (name) {
  1517. common.handlerLabel(id, dd, isAdd, common.getItem(id, val), true);
  1518. temp[val] = 1;
  1519. }
  1520. }
  1521. }
  1522. });
  1523. }
  1524. };
  1525. Select4.prototype.on = function (id, fun, isEnd) {
  1526. common.bindEvent(isEnd ? 'endOn' : 'on', id, fun);
  1527. return this;
  1528. };
  1529. Select4.prototype.filter = function (id, fun) {
  1530. common.bindEvent('filter', id, fun);
  1531. return this;
  1532. };
  1533. Select4.prototype.maxTips = function (id, fun) {
  1534. common.bindEvent('maxTips', id, fun);
  1535. return this;
  1536. };
  1537. Select4.prototype.opened = function (id, fun) {
  1538. common.bindEvent('opened', id, fun);
  1539. return this;
  1540. };
  1541. Select4.prototype.closed = function (id, fun) {
  1542. common.bindEvent('closed', id, fun);
  1543. return this;
  1544. };
  1545. Select4.prototype.config = function (id, config, isJson) {
  1546. if (id && (typeof id === 'undefined' ? 'undefined' : _typeof(id)) == 'object') {
  1547. isJson = config == true;
  1548. config = id;
  1549. id = null;
  1550. }
  1551. if (config && (typeof config === 'undefined' ? 'undefined' : _typeof(config)) == 'object') {
  1552. if (isJson) {
  1553. config.header || (config.header = {});
  1554. config.header['Content-Type'] = 'application/json; charset=UTF-8';
  1555. config.dataType = 'json';
  1556. }
  1557. id ? (ajaxs[id] = $.extend(true, {}, ajaxs[id] || ajax, config), !common.check(id) && this.render(id), data[id] && config.direction && (data[id].config.direction = config.direction), data[id] && config.clearInput && (data[id].config.clearInput = true), config.searchUrl && data[id] && common.triggerSearch($('.' + PNAME + ' dl[xid="' + id + '"]').parents('.' + FORM_SELECT), true)) : ($.extend(true, ajax, config), $.each(ajaxs, function (key, item) {
  1558. $.extend(true, item, config);
  1559. }));
  1560. }
  1561. return this;
  1562. };
  1563. Select4.prototype.render = function (id, options) {
  1564. var _ref2;
  1565. if (id && (typeof id === 'undefined' ? 'undefined' : _typeof(id)) == 'object') {
  1566. options = id;
  1567. id = null;
  1568. }
  1569. var config = options ? (_ref2 = {
  1570. init: options.init,
  1571. disabled: options.disabled,
  1572. max: options.max,
  1573. isSearch: options.isSearch,
  1574. searchUrl: options.searchUrl,
  1575. callback: options.callback,
  1576. callbackParam: options.callbackParam,
  1577. isCreate: options.isCreate,
  1578. radio: options.radio,
  1579. skin: options.skin,
  1580. direction: options.direction,
  1581. height: options.height,
  1582. formname: options.formname,
  1583. layverify: options.layverify,
  1584. layverType: options.layverType,
  1585. showCount: options.showCount,
  1586. placeholder: options.placeholder,
  1587. create: options.create,
  1588. filter: options.filter,
  1589. maxTips: options.maxTips,
  1590. on: options.on
  1591. }, _defineProperty(_ref2, 'on', options.on), _defineProperty(_ref2, 'opened', options.opened), _defineProperty(_ref2, 'closed', options.closed), _defineProperty(_ref2, 'template', options.template), _defineProperty(_ref2, 'clearInput', options.clearInput), _ref2) : {};
  1592. options && options.searchType != undefined && (config.searchType = options.searchType == 'dl' ? 1 : 0);
  1593. if (id) {
  1594. fsConfigs[id] = {};
  1595. $.extend(fsConfigs[id], data[id] ? data[id].config : {}, config);
  1596. } else {
  1597. $.extend(fsConfig, config);
  1598. }
  1599. ($('select[' + NAME + '="' + id + '"]')[0] ? $('select[' + NAME + '="' + id + '"]') : $('select[' + NAME + ']')).each(function (index, select) {
  1600. var sid = select.getAttribute(NAME);
  1601. common.render(sid, select);
  1602. setTimeout(function () {
  1603. return common.setHidnVal(sid, $('select[xm-select="' + sid + '"] + div.' + PNAME + ' .' + LABEL));
  1604. }, 10);
  1605. });
  1606. return this;
  1607. };
  1608. Select4.prototype.disabled = function (id) {
  1609. var target = {};
  1610. id ? common.check(id) && (target[id] = data[id]) : target = data;
  1611. $.each(target, function (key, val) {
  1612. $('dl[xid="' + key + '"]').prev().addClass(DIS);
  1613. });
  1614. return this;
  1615. };
  1616. Select4.prototype.undisabled = function (id) {
  1617. var target = {};
  1618. id ? common.check(id) && (target[id] = data[id]) : target = data;
  1619. $.each(target, function (key, val) {
  1620. $('dl[xid="' + key + '"]').prev().removeClass(DIS);
  1621. });
  1622. return this;
  1623. };
  1624. Select4.prototype.data = function (id, type, config) {
  1625. if (!id || !type || !config) {
  1626. common.log('id: ' + id + ' param error !!!');
  1627. return this;
  1628. }
  1629. if (!common.check(id)) {
  1630. common.log('id: ' + id + ' not render !!!');
  1631. return this;
  1632. }
  1633. this.value(id, []);
  1634. this.config(id, config);
  1635. if (type == 'local') {
  1636. common.renderData(id, config.arr, config.linkage == true, config.linkageWidth ? config.linkageWidth : '100');
  1637. } else if (type == 'server') {
  1638. common.ajax(id, config.url, config.keyword, config.linkage == true, config.linkageWidth ? config.linkageWidth : '100');
  1639. }
  1640. return this;
  1641. };
  1642. Select4.prototype.btns = function (id, btns, config) {
  1643. if (id && common.isArray(id)) {
  1644. btns = id;
  1645. id = null;
  1646. }
  1647. if (!btns || !common.isArray(btns)) {
  1648. return this;
  1649. };
  1650. var target = {};
  1651. id ? common.check(id) && (target[id] = data[id]) : target = data;
  1652. btns = btns.map(function (obj) {
  1653. if (typeof obj == 'string') {
  1654. if (obj == 'select') {
  1655. return quickBtns[0];
  1656. }
  1657. if (obj == 'remove') {
  1658. return quickBtns[1];
  1659. }
  1660. if (obj == 'reverse') {
  1661. return quickBtns[2];
  1662. }
  1663. if (obj == 'skin') {
  1664. return quickBtns[3];
  1665. }
  1666. }
  1667. return obj;
  1668. });
  1669. $.each(target, function (key, val) {
  1670. val.config.btns = btns;
  1671. var dd = $('dl[xid="' + key + '"]').find('.' + FORM_SELECT_TIPS + ':first');
  1672. if (btns.length) {
  1673. var show = config && config.show && (config.show == 'name' || config.show == 'icon') ? config.show : '';
  1674. var html = common.renderBtns(key, show, config && config.space ? config.space : '30px');
  1675. dd.html(html);
  1676. } else {
  1677. var pcInput = dd.parents('.' + FORM_SELECT).find('.' + TDIV + ' input');
  1678. var _html = pcInput.attr('placeholder') || pcInput.attr('back');
  1679. dd.html(_html);
  1680. dd.removeAttr('style');
  1681. }
  1682. });
  1683. return this;
  1684. };
  1685. Select4.prototype.search = function (id, val) {
  1686. if (id && common.check(id)) {
  1687. ajaxs[id] = $.extend(true, {}, ajaxs[id] || ajax, {
  1688. first: true,
  1689. searchVal: val
  1690. });
  1691. common.triggerSearch($('dl[xid="' + id + '"]').parents('.' + FORM_SELECT), true);
  1692. }
  1693. return this;
  1694. };
  1695. Select4.prototype.replace = function (id, type, config) {
  1696. var _this17 = this;
  1697. if (!id || !type || !config) {
  1698. common.log('id: ' + id + ' param error !!!');
  1699. return this;
  1700. }
  1701. if (!common.check(id, true)) {
  1702. common.log('id: ' + id + ' not render !!!');
  1703. return this;
  1704. }
  1705. var oldVals = this.value(id, 'val');
  1706. this.value(id, []);
  1707. this.config(id, config);
  1708. if (type == 'local') {
  1709. common.renderData(id, config.arr, config.linkage == true, config.linkageWidth ? config.linkageWidth : '100', false, true);
  1710. this.value(id, oldVals, true);
  1711. } else if (type == 'server') {
  1712. common.ajax(id, config.url, config.keyword, config.linkage == true, config.linkageWidth ? config.linkageWidth : '100', false, function (id) {
  1713. _this17.value(id, oldVals, true);
  1714. }, true);
  1715. }
  1716. };
  1717. return new Select4();
  1718. });