require-form.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. define(['jquery', 'bootstrap', 'upload', 'validator', 'validator-lang'], function ($, undefined, Upload, Validator, undefined) {
  2. var Form = {
  3. config: {
  4. fieldlisttpl: '<dd class="form-inline"><input type="text" name="<%=name%>[<%=index%>][key]" class="form-control" value="<%=key%>" placeholder="<%=options.keyPlaceholder||\'\'%>" size="10" /> <input type="text" name="<%=name%>[<%=index%>][value]" class="form-control" value="<%=value%>" placeholder="<%=options.valuePlaceholder||\'\'%>" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>'
  5. },
  6. events: {
  7. validator: function (form, success, error, submit) {
  8. if (!form.is("form"))
  9. return;
  10. //绑定表单事件
  11. form.validator($.extend({
  12. rules: {
  13. username: [/^\w{3,30}$/, __('Username must be 3 to 30 characters')],
  14. password: [/^[\S]{6,30}$/, __('Password must be 6 to 30 characters')]
  15. },
  16. validClass: 'has-success',
  17. invalidClass: 'has-error',
  18. bindClassTo: '.form-group',
  19. formClass: 'n-default n-bootstrap',
  20. msgClass: 'n-right',
  21. stopOnError: true,
  22. display: function (elem) {
  23. return $(elem).closest('.form-group').find(".control-label").text().replace(/\:/, '');
  24. },
  25. dataFilter: function (data) {
  26. if (data.code === 1) {
  27. return data.msg ? {"ok": data.msg} : '';
  28. } else {
  29. return data.msg;
  30. }
  31. },
  32. target: function (input) {
  33. var target = $(input).data("target");
  34. if (target && $(target).length > 0) {
  35. return $(target);
  36. }
  37. var $formitem = $(input).closest('.form-group'),
  38. $msgbox = $formitem.find('span.msg-box');
  39. if (!$msgbox.length) {
  40. return [];
  41. }
  42. return $msgbox;
  43. },
  44. valid: function (ret) {
  45. var that = this, submitBtn = $(".layer-footer [type=submit]", form);
  46. that.holdSubmit(true);
  47. submitBtn.addClass("disabled");
  48. //验证通过提交表单
  49. var submitResult = Form.api.submit($(ret), function (data, ret) {
  50. that.holdSubmit(false);
  51. submitBtn.removeClass("disabled");
  52. if (false === $(this).triggerHandler("success.form", [data, ret])) {
  53. return false;
  54. }
  55. if (typeof success === 'function') {
  56. if (false === success.call($(this), data, ret)) {
  57. return false;
  58. }
  59. }
  60. //提示及关闭当前窗口
  61. var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
  62. parent.Toastr.success(msg);
  63. parent.$(".btn-refresh").trigger("click");
  64. if (window.name) {
  65. var index = parent.Layer.getFrameIndex(window.name);
  66. parent.Layer.close(index);
  67. }
  68. return false;
  69. }, function (data, ret) {
  70. that.holdSubmit(false);
  71. if (false === $(this).triggerHandler("error.form", [data, ret])) {
  72. return false;
  73. }
  74. submitBtn.removeClass("disabled");
  75. if (typeof error === 'function') {
  76. if (false === error.call($(this), data, ret)) {
  77. return false;
  78. }
  79. }
  80. }, submit);
  81. //如果提交失败则释放锁定
  82. if (!submitResult) {
  83. that.holdSubmit(false);
  84. submitBtn.removeClass("disabled");
  85. }
  86. return false;
  87. }
  88. }, form.data("validator-options") || {}));
  89. //移除提交按钮的disabled类
  90. $(".layer-footer [type=submit],.fixed-footer [type=submit],.normal-footer [type=submit]", form).removeClass("disabled");
  91. //自定义关闭按钮事件
  92. form.on("click", ".layer-close", function () {
  93. var index = parent.Layer.getFrameIndex(window.name);
  94. parent.Layer.close(index);
  95. return false;
  96. });
  97. },
  98. selectpicker: function (form) {
  99. //绑定select元素事件
  100. if ($(".selectpicker", form).length > 0) {
  101. require(['bootstrap-select', 'bootstrap-select-lang'], function () {
  102. $('.selectpicker', form).selectpicker();
  103. $(form).on("reset", function () {
  104. setTimeout(function () {
  105. $('.selectpicker').selectpicker('refresh').trigger("change");
  106. }, 1);
  107. });
  108. });
  109. }
  110. },
  111. selectpage: function (form) {
  112. //绑定selectpage元素事件
  113. if ($(".selectpage", form).length > 0) {
  114. require(['selectpage'], function () {
  115. $('.selectpage', form).selectPage({
  116. eAjaxSuccess: function (data) {
  117. data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
  118. data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
  119. return data;
  120. }
  121. });
  122. });
  123. //给隐藏的元素添加上validate验证触发事件
  124. $(document).on("change", ".sp_hidden", function () {
  125. $(this).trigger("validate");
  126. });
  127. $(document).on("change", ".sp_input", function () {
  128. $(this).closest(".sp_container").find(".sp_hidden").trigger("change");
  129. });
  130. $(form).on("reset", function () {
  131. setTimeout(function () {
  132. $(".selectpage", form).each(function () {
  133. var selectpage = $(this).data("selectPageObject");
  134. selectpage.elem.hidden.val($(this).val());
  135. $(this).selectPageRefresh();
  136. });
  137. }, 1);
  138. });
  139. }
  140. },
  141. cxselect: function (form) {
  142. //绑定cxselect元素事件
  143. if ($("[data-toggle='cxselect']", form).length > 0) {
  144. require(['cxselect'], function () {
  145. $.cxSelect.defaults.jsonName = 'name';
  146. $.cxSelect.defaults.jsonValue = 'value';
  147. $.cxSelect.defaults.jsonSpace = 'data';
  148. $("[data-toggle='cxselect']", form).cxSelect();
  149. });
  150. }
  151. },
  152. citypicker: function (form) {
  153. //绑定城市远程插件
  154. if ($("[data-toggle='city-picker']", form).length > 0) {
  155. require(['citypicker'], function () {
  156. $(form).on("reset", function () {
  157. setTimeout(function () {
  158. $("[data-toggle='city-picker']").citypicker('refresh');
  159. }, 1);
  160. });
  161. });
  162. }
  163. },
  164. datetimepicker: function (form) {
  165. //绑定日期时间元素事件
  166. if ($(".datetimepicker", form).length > 0) {
  167. require(['bootstrap-datetimepicker'], function () {
  168. var options = {
  169. format: 'YYYY-MM-DD HH:mm:ss',
  170. icons: {
  171. time: 'fa fa-clock-o',
  172. date: 'fa fa-calendar',
  173. up: 'fa fa-chevron-up',
  174. down: 'fa fa-chevron-down',
  175. previous: 'fa fa-chevron-left',
  176. next: 'fa fa-chevron-right',
  177. today: 'fa fa-history',
  178. clear: 'fa fa-trash',
  179. close: 'fa fa-remove'
  180. },
  181. showTodayButton: true,
  182. showClose: true
  183. };
  184. $('.datetimepicker', form).parent().css('position', 'relative');
  185. $('.datetimepicker', form).datetimepicker(options).on('dp.change', function (e) {
  186. $(this, document).trigger("changed");
  187. });
  188. });
  189. }
  190. },
  191. daterangepicker: function (form) {
  192. //绑定日期时间元素事件
  193. if ($(".datetimerange", form).length > 0) {
  194. require(['bootstrap-daterangepicker'], function () {
  195. var ranges = {};
  196. ranges[__('Today')] = [Moment().startOf('day'), Moment().endOf('day')];
  197. ranges[__('Yesterday')] = [Moment().subtract(1, 'days').startOf('day'), Moment().subtract(1, 'days').endOf('day')];
  198. ranges[__('Last 7 Days')] = [Moment().subtract(6, 'days').startOf('day'), Moment().endOf('day')];
  199. ranges[__('Last 30 Days')] = [Moment().subtract(29, 'days').startOf('day'), Moment().endOf('day')];
  200. ranges[__('This Month')] = [Moment().startOf('month'), Moment().endOf('month')];
  201. ranges[__('Last Month')] = [Moment().subtract(1, 'month').startOf('month'), Moment().subtract(1, 'month').endOf('month')];
  202. var options = {
  203. timePicker: false,
  204. autoUpdateInput: false,
  205. timePickerSeconds: true,
  206. timePicker24Hour: true,
  207. autoApply: true,
  208. locale: {
  209. format: 'YYYY-MM-DD HH:mm:ss',
  210. customRangeLabel: __("Custom Range"),
  211. applyLabel: __("Apply"),
  212. cancelLabel: __("Clear"),
  213. },
  214. ranges: ranges,
  215. };
  216. var origincallback = function (start, end) {
  217. $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
  218. $(this.element).trigger('blur');
  219. };
  220. $(".datetimerange", form).each(function () {
  221. var callback = typeof $(this).data('callback') == 'function' ? $(this).data('callback') : origincallback;
  222. $(this).on('apply.daterangepicker', function (ev, picker) {
  223. callback.call(picker, picker.startDate, picker.endDate);
  224. });
  225. $(this).on('cancel.daterangepicker', function (ev, picker) {
  226. $(this).val('').trigger('blur');
  227. });
  228. $(this).daterangepicker($.extend(true, options, $(this).data() || {}, $(this).data("daterangepicker-options") || {}));
  229. });
  230. });
  231. }
  232. },
  233. /**
  234. * 绑定上传事件
  235. * @param form
  236. * @deprecated Use faupload instead.
  237. */
  238. plupload: function (form) {
  239. Form.events.faupload(form);
  240. },
  241. /**
  242. * 绑定上传事件
  243. * @param form
  244. */
  245. faupload: function (form) {
  246. //绑定上传元素事件
  247. if ($(".plupload,.faupload", form).length > 0) {
  248. Upload.api.upload($(".plupload,.faupload", form));
  249. }
  250. },
  251. faselect: function (form) {
  252. //绑定fachoose选择附件事件
  253. if ($(".faselect,.fachoose", form).length > 0) {
  254. $(".faselect,.fachoose", form).off('click').on('click', function () {
  255. var that = this;
  256. var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
  257. var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
  258. var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
  259. var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
  260. mimetype = mimetype.replace(/\/\*/ig, '/');
  261. var url = $(this).data("url") ? $(this).data("url") : (typeof Backend !== 'undefined' ? "general/attachment/select" : "user/attachment");
  262. parent.Fast.api.open(url + "?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
  263. callback: function (data) {
  264. var button = $("#" + $(that).attr("id"));
  265. var maxcount = $(button).data("maxcount");
  266. var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
  267. maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
  268. if (input_id && data.multiple) {
  269. var urlArr = [];
  270. var inputObj = $("#" + input_id);
  271. var value = $.trim(inputObj.val());
  272. if (value !== "") {
  273. urlArr.push(inputObj.val());
  274. }
  275. var nums = value === '' ? 0 : value.split(/\,/).length;
  276. var files = data.url !== "" ? data.url.split(/\,/) : [];
  277. $.each(files, function (i, j) {
  278. var url = Config.upload.fullmode ? Fast.api.cdnurl(j) : j;
  279. urlArr.push(url);
  280. });
  281. if (maxcount > 0) {
  282. var remains = maxcount - nums;
  283. if (files.length > remains) {
  284. Toastr.error(__('You can choose up to %d file%s', remains));
  285. return false;
  286. }
  287. }
  288. var result = urlArr.join(",");
  289. inputObj.val(result).trigger("change").trigger("validate");
  290. } else {
  291. var url = Config.upload.fullmode ? Fast.api.cdnurl(data.url) : data.url;
  292. $("#" + input_id).val(url).trigger("change").trigger("validate");
  293. }
  294. }
  295. });
  296. return false;
  297. });
  298. }
  299. },
  300. fieldlist: function (form) {
  301. //绑定fieldlist
  302. if ($(".fieldlist", form).length > 0) {
  303. require(['dragsort', 'template'], function (undefined, Template) {
  304. //刷新隐藏textarea的值
  305. var refresh = function (container) {
  306. var data = {};
  307. var name = container.data("name");
  308. var textarea = $("textarea[name='" + name + "']", form);
  309. var template = container.data("template");
  310. $.each($("input,select,textarea", container).serializeArray(), function (i, j) {
  311. var reg = /\[(\w+)\]\[(\w+)\]$/g;
  312. var match = reg.exec(j.name);
  313. if (!match)
  314. return true;
  315. match[1] = "x" + parseInt(match[1]);
  316. if (typeof data[match[1]] == 'undefined') {
  317. data[match[1]] = {};
  318. }
  319. data[match[1]][match[2]] = j.value;
  320. });
  321. var result = template ? [] : {};
  322. $.each(data, function (i, j) {
  323. if (j) {
  324. if (!template) {
  325. if (j.key != '') {
  326. result[j.key] = j.value;
  327. }
  328. } else {
  329. result.push(j);
  330. }
  331. }
  332. });
  333. textarea.val(JSON.stringify(result));
  334. };
  335. //追加一行数据
  336. var append = function (container, row, initial) {
  337. var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
  338. var index = container.data("index");
  339. var name = container.data("name");
  340. var template = container.data("template");
  341. var data = container.data();
  342. index = index ? parseInt(index) : 0;
  343. container.data("index", index + 1);
  344. row = row ? row : {};
  345. row = typeof row.key === 'undefined' || typeof row.value === 'undefined' ? {key: '', value: row} : row;
  346. var options = container.data("fieldlist-options") || {};
  347. var vars = {index: index, name: name, data: data, options: options, key: row.key, value: row.value, row: row.value};
  348. var html = template ? Template(template, vars) : Template.render(Form.config.fieldlisttpl, vars);
  349. var obj = $(html);
  350. if ((options.deleteBtn === false || options.removeBtn === false) && initial)
  351. obj.find(".btn-remove").remove();
  352. if (options.dragsortBtn === false && initial)
  353. obj.find(".btn-dragsort").remove();
  354. if ((options.readonlyKey === true || options.disableKey === true) && initial) {
  355. obj.find("input[name$='[key]']").prop("readonly", true);
  356. }
  357. obj.attr("fieldlist-item", true);
  358. obj.insertAfter($(tagName + "[fieldlist-item]", container).length > 0 ? $(tagName + "[fieldlist-item]:last", container) : $(tagName + ":first", container));
  359. if ($(".btn-append,.append", container).length > 0) {
  360. //兼容旧版本事件
  361. $(".btn-append,.append", container).trigger("fa.event.appendfieldlist", obj);
  362. } else {
  363. //新版本事件
  364. container.trigger("fa.event.appendfieldlist", obj);
  365. }
  366. return obj;
  367. };
  368. var fieldlist = $(".fieldlist", form);
  369. //表单重置
  370. form.on("reset", function () {
  371. setTimeout(function () {
  372. fieldlist.trigger("fa.event.refreshfieldlist");
  373. });
  374. });
  375. //监听文本框改变事件
  376. $(document).on('change keyup changed', ".fieldlist input,.fieldlist textarea,.fieldlist select", function () {
  377. var container = $(this).closest(".fieldlist");
  378. refresh(container);
  379. });
  380. //追加控制(点击按钮)
  381. fieldlist.on("click", ".btn-append,.append", function (e, row) {
  382. var container = $(this).closest(".fieldlist");
  383. append(container, row);
  384. // refresh(container);
  385. });
  386. //移除控制(点击按钮)
  387. fieldlist.on("click", ".btn-remove", function () {
  388. var container = $(this).closest(".fieldlist");
  389. var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
  390. $(this).closest(tagName).remove();
  391. refresh(container);
  392. });
  393. //追加控制(通过事件)
  394. fieldlist.on("fa.event.appendtofieldlist", function (e, row) {
  395. var container = $(this);
  396. append(container, row);
  397. refresh(container);
  398. });
  399. //根据textarea内容重新渲染
  400. fieldlist.on("fa.event.refreshfieldlist", function () {
  401. var container = $(this);
  402. var textarea = $("textarea[name='" + container.data("name") + "']", form);
  403. //先清空已有的数据
  404. $("[fieldlist-item]", container).remove();
  405. var json = {};
  406. try {
  407. json = JSON.parse(textarea.val());
  408. } catch (e) {
  409. }
  410. $.each(json, function (i, j) {
  411. append(container, {key: i, value: j}, true);
  412. });
  413. });
  414. //拖拽排序
  415. fieldlist.each(function () {
  416. var container = $(this);
  417. var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
  418. container.dragsort({
  419. itemSelector: tagName,
  420. dragSelector: ".btn-dragsort",
  421. dragEnd: function () {
  422. refresh(container);
  423. },
  424. placeHolderTemplate: $("<" + tagName + "/>")
  425. });
  426. if (typeof container.data("options") === 'object' && container.data("options").appendBtn === false) {
  427. $(".btn-append,.append", container).hide();
  428. }
  429. $("textarea[name='" + container.data("name") + "']", form).on("fa.event.refreshfieldlist", function () {
  430. //兼容旧版本事件
  431. $(this).closest(".fieldlist").trigger("fa.event.refreshfieldlist");
  432. });
  433. });
  434. fieldlist.trigger("fa.event.refreshfieldlist");
  435. });
  436. }
  437. },
  438. switcher: function (form) {
  439. form.on("click", "[data-toggle='switcher']", function () {
  440. if ($(this).hasClass("disabled")) {
  441. return false;
  442. }
  443. var switcher = $.proxy(function () {
  444. var input = $(this).prev("input");
  445. input = $(this).data("input-id") ? $("#" + $(this).data("input-id")) : input;
  446. if (input.length > 0) {
  447. var yes = $(this).data("yes");
  448. var no = $(this).data("no");
  449. if (input.val() == yes) {
  450. input.val(no);
  451. $("i", this).addClass("fa-flip-horizontal text-gray");
  452. } else {
  453. input.val(yes);
  454. $("i", this).removeClass("fa-flip-horizontal text-gray");
  455. }
  456. input.trigger('change');
  457. }
  458. }, this);
  459. if (typeof $(this).data("confirm") !== 'undefined') {
  460. Layer.confirm($(this).data("confirm"), function (index) {
  461. switcher();
  462. Layer.close(index);
  463. });
  464. } else {
  465. switcher();
  466. }
  467. return false;
  468. });
  469. },
  470. bindevent: function (form) {
  471. },
  472. slider: function (form) {
  473. if ($(".slider", form).length > 0) {
  474. require(['bootstrap-slider'], function () {
  475. $('.slider').removeClass('hidden').css('width', function (index, value) {
  476. return $(this).parents('.form-control').width();
  477. }).slider().on('slide', function (ev) {
  478. var data = $(this).data();
  479. if (typeof data.unit !== 'undefined') {
  480. $(this).parents('.form-control').siblings('.value').text(ev.value + data.unit);
  481. }
  482. });
  483. });
  484. }
  485. },
  486. tagsinput: function (form) {
  487. if ($("[data-role='tagsinput']", form).length > 0) {
  488. require(['tagsinput', 'autocomplete'], function () {
  489. $("[data-role='tagsinput']").tagsinput();
  490. form.on("reset", function () {
  491. setTimeout(function () {
  492. $("[data-role='tagsinput']").tagsinput('reset');
  493. }, 0);
  494. });
  495. });
  496. }
  497. },
  498. autocomplete: function (form) {
  499. if ($("[data-role='autocomplete']", form).length > 0) {
  500. require(['autocomplete'], function () {
  501. $("[data-role='autocomplete']").autocomplete();
  502. });
  503. }
  504. },
  505. favisible: function (form) {
  506. if ($("[data-favisible]", form).length == 0) {
  507. return;
  508. }
  509. var checkCondition = function (condition) {
  510. var conditionArr = condition.split(/&&/);
  511. var success = 0;
  512. var baseregex = /^([a-z0-9\_]+)([>|<|=|\!]=?)(.*)$/i, strregex = /^('|")(.*)('|")$/, regregex = /^regex:(.*)$/;
  513. // @formatter:off
  514. var operator_result = {
  515. '>': function (a, b) {
  516. return a > b;
  517. },
  518. '>=': function (a, b) {
  519. return a >= b;
  520. },
  521. '<': function (a, b) {
  522. return a < b;
  523. },
  524. '<=': function (a, b) {
  525. return a <= b;
  526. },
  527. '==': function (a, b) {
  528. return a == b;
  529. },
  530. '!=': function (a, b) {
  531. return a != b;
  532. },
  533. 'in': function (a, b) {
  534. return b.split(/\,/).indexOf(a) > -1;
  535. },
  536. 'regex': function (a, b) {
  537. var regParts = b.match(/^\/(.*?)\/([gim]*)$/);
  538. var regexp = regParts ? new RegExp(regParts[1], regParts[2]) : new RegExp(b);
  539. return regexp.test(a);
  540. }
  541. };
  542. // @formatter:on
  543. var dataArr = form.serializeArray(), dataObj = {}, fieldName, fieldValue;
  544. $(dataArr).each(function (i, field) {
  545. fieldName = field.name;
  546. fieldValue = field.value;
  547. fieldName = fieldName.substr(-2) === '[]' ? fieldName.substr(0, fieldName.length - 2) : fieldName;
  548. dataObj[fieldName] = typeof dataObj[fieldName] !== 'undefined' ? [dataObj[fieldName], fieldValue].join(',') : fieldValue;
  549. });
  550. $.each(conditionArr, function (i, item) {
  551. var basematches = baseregex.exec(item);
  552. if (basematches) {
  553. var name = basematches[1], operator = basematches[2], value = basematches[3].toString();
  554. if (operator === '=') {
  555. var strmatches = strregex.exec(value);
  556. operator = strmatches ? '==' : 'in';
  557. value = strmatches ? strmatches[2] : value;
  558. }
  559. var regmatches = regregex.exec(value);
  560. if (regmatches) {
  561. operator = 'regex';
  562. value = regmatches[1];
  563. }
  564. var chkname = "row[" + name + "]";
  565. if (typeof dataObj[chkname] === 'undefined') {
  566. return false;
  567. }
  568. var objvalue = dataObj[chkname];
  569. if ($.isArray(objvalue)) {
  570. objvalue = dataObj[chkname].join(",");
  571. }
  572. if (['>', '>=', '<', '<='].indexOf(operator) > -1) {
  573. objvalue = parseFloat(objvalue);
  574. value = parseFloat(value);
  575. }
  576. var result = operator_result[operator](objvalue, value);
  577. success += (result ? 1 : 0);
  578. }
  579. });
  580. return success === conditionArr.length;
  581. };
  582. form.on("keyup change click configchange", "input,select", function () {
  583. $("[data-favisible][data-favisible!='']", form).each(function () {
  584. var visible = $(this).data("favisible");
  585. var groupArr = visible.split(/\|\|/);
  586. var success = 0;
  587. $.each(groupArr, function (i, j) {
  588. if (checkCondition(j)) {
  589. success++;
  590. }
  591. });
  592. if (success > 0) {
  593. $(this).removeClass("hidden");
  594. } else {
  595. $(this).addClass("hidden");
  596. }
  597. });
  598. });
  599. //追加上忽略元素
  600. setTimeout(function () {
  601. form.data('validator').options.ignore += ((form.data('validator').options.ignore ? ',' : '') + '[data-favisible] :hidden,[data-favisible]:hidden');
  602. }, 0);
  603. $("input,select", form).trigger("configchange");
  604. }
  605. },
  606. api: {
  607. submit: function (form, success, error, submit) {
  608. if (form.length === 0) {
  609. Toastr.error("表单未初始化完成,无法提交");
  610. return false;
  611. }
  612. if (typeof submit === 'function') {
  613. if (false === submit.call(form, success, error)) {
  614. return false;
  615. }
  616. }
  617. var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
  618. type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
  619. url = form.attr("action");
  620. url = url ? url : location.href;
  621. //修复当存在多选项元素时提交的BUG
  622. var params = {};
  623. var multipleList = $("[name$='[]']", form);
  624. if (multipleList.length > 0) {
  625. var postFields = form.serializeArray().map(function (obj) {
  626. return $(obj).prop("name");
  627. });
  628. $.each(multipleList, function (i, j) {
  629. if (postFields.indexOf($(this).prop("name")) < 0) {
  630. params[$(this).prop("name")] = '';
  631. }
  632. });
  633. }
  634. //调用Ajax请求方法
  635. Fast.api.ajax({
  636. type: type,
  637. url: url,
  638. data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : ''),
  639. dataType: 'json',
  640. complete: function (xhr) {
  641. var token = xhr.getResponseHeader('__token__');
  642. if (token) {
  643. $("input[name='__token__']").val(token);
  644. }
  645. }
  646. }, function (data, ret) {
  647. $('.form-group', form).removeClass('has-feedback has-success has-error');
  648. if (data && typeof data === 'object') {
  649. //刷新客户端token
  650. if (typeof data.token !== 'undefined') {
  651. $("input[name='__token__']").val(data.token);
  652. }
  653. //调用客户端事件
  654. if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
  655. data.callback.call(form, data);
  656. }
  657. }
  658. if (typeof success === 'function') {
  659. if (false === success.call(form, data, ret)) {
  660. return false;
  661. }
  662. }
  663. }, function (data, ret) {
  664. if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
  665. $("input[name='__token__']").val(data.token);
  666. }
  667. if (typeof error === 'function') {
  668. if (false === error.call(form, data, ret)) {
  669. return false;
  670. }
  671. }
  672. });
  673. return true;
  674. },
  675. bindevent: function (form, success, error, submit) {
  676. form = typeof form === 'object' ? form : $(form);
  677. var events = Form.events;
  678. events.bindevent(form);
  679. events.validator(form, success, error, submit);
  680. events.selectpicker(form);
  681. events.daterangepicker(form);
  682. events.selectpage(form);
  683. events.cxselect(form);
  684. events.citypicker(form);
  685. events.datetimepicker(form);
  686. events.faupload(form);
  687. events.faselect(form);
  688. events.fieldlist(form);
  689. events.slider(form);
  690. events.switcher(form);
  691. events.tagsinput(form);
  692. events.autocomplete(form);
  693. events.favisible(form);
  694. },
  695. custom: {}
  696. },
  697. };
  698. return Form;
  699. });