formSelects-v4.js 57 KB

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