formselects.js 74 KB

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