formSelects-v4.js 72 KB

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