require-form.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'upload', 'bootstrap-validator', 'bootstrap-checkbox', 'bootstrap-radio', 'bootstrap-switch'], function ($, undefined, Backend, Config, Toastr, Upload, undefined) {
  2. var Form = {
  3. config: {
  4. },
  5. api: {
  6. submit: function (form, onBeforeSubmit, onAfterSubmit) {
  7. if (form.size() == 0)
  8. return Toastr.error("表单未初始化完成,无法提交");
  9. //提交前事件
  10. var beforeSubmit = form.data("before-submit");
  11. //元素绑定函数
  12. if (beforeSubmit && typeof Form.api.custom[beforeSubmit] == 'function') {
  13. if (!Form.api.custom[beforeSubmit].call(form)) {
  14. return false;
  15. }
  16. }
  17. //自定义函数
  18. if (typeof onBeforeSubmit == 'function') {
  19. if (!onBeforeSubmit.call(form)) {
  20. return false;
  21. }
  22. }
  23. var type = form.attr("method");
  24. type = type && (type == 'GET' || type == 'POST') ? type : 'GET';
  25. url = form.attr("action");
  26. url = url ? url : location.href;
  27. $.ajax({
  28. type: type,
  29. url: url,
  30. data: form.serialize(),
  31. dataType: 'json',
  32. success: function (ret) {
  33. if (ret.hasOwnProperty("code")) {
  34. var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : null;
  35. var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
  36. if (ret.code === 1) {
  37. $('.form-group', form).removeClass('has-feedback has-success has-error');
  38. //成功提交后事件
  39. var afterSubmit = form.data("after-submit");
  40. //元素绑定函数
  41. if (afterSubmit && typeof Form.api.custom[afterSubmit] == 'function') {
  42. if (!Form.api.custom[afterSubmit].call(form, data)) {
  43. return false;
  44. }
  45. }
  46. //自定义函数
  47. if (typeof onAfterSubmit == 'function') {
  48. if (!onAfterSubmit.call(form, data)) {
  49. return false;
  50. }
  51. }
  52. Toastr.success(msg ? msg : __('Operation completed'));
  53. } else {
  54. Toastr.error(msg ? msg : __('Operation failed'));
  55. }
  56. } else {
  57. Toastr.error(__('Unknown data format'));
  58. }
  59. }, error: function () {
  60. Toastr.error(__('Network error'));
  61. }, complete: function (e) {
  62. }
  63. });
  64. return false;
  65. },
  66. bindevent: function (form, onBeforeSubmit, onAfterSubmit) {
  67. form.validator().on('submit', function (e) {
  68. if (e.isDefaultPrevented()) {
  69. //验证不通过
  70. Toastr.error("验证失败,请检查表单输入是否正确");
  71. } else {
  72. //验证通过提交表单
  73. Form.api.submit(form, onBeforeSubmit, function (data) {
  74. if (typeof onAfterSubmit == 'function') {
  75. if (!onAfterSubmit.call(form, data)) {
  76. return false;
  77. }
  78. }
  79. //提示及关闭当前窗口
  80. parent.Toastr.success(__('Operation completed'));
  81. parent.$(".btn-refresh").trigger("click");
  82. var index = parent.Layer.getFrameIndex(window.name);
  83. parent.Layer.close(index);
  84. });
  85. return false;
  86. }
  87. });
  88. // Activate the switches with icons
  89. if ($('.switch').length != 0) {
  90. $('.switch')['bootstrapSwitch']();
  91. }
  92. // Activate regular switches
  93. if ($("[data-toggle='switch']").length != 0) {
  94. $("[data-toggle='switch']").wrap('<div class="switch" />').parent().bootstrapSwitch();
  95. }
  96. //绑定select元素事件
  97. if ($(".selectpicker", form).size() > 0) {
  98. require(['bootstrap-select'], function () {
  99. $('.selectpicker', form).selectpicker();
  100. });
  101. }
  102. if ($(".typeahead").size() > 0 || $(".tagsinput").size() > 0) {
  103. require(['bloodhound'], function () {
  104. var remotesource = function (input) {
  105. return new Bloodhound({
  106. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  107. queryTokenizer: Bloodhound.tokenizers.whitespace,
  108. remote: {
  109. url: '/ajax/typeahead?search=%QUERY&field=' + $(input).attr("name"),
  110. wildcard: '%QUERY',
  111. transform: function (ret) {
  112. return ret.data.searchlist;
  113. }
  114. }
  115. });
  116. };
  117. //绑定typeahead事件
  118. if ($(".typeahead", form).size() > 0) {
  119. require(['typeahead'], function () {
  120. $(".typeahead", form).each(function () {
  121. $(this).typeahead({
  122. hint: true,
  123. highlight: true,
  124. minLength: 0
  125. }, {
  126. name: 'typeahead',
  127. limit: 20,
  128. displayKey: 'id',
  129. source: remotesource(this),
  130. templates: {
  131. empty: '<li class="notfound">' + __('No matches found') + '</li>',
  132. suggestion: function (item) {
  133. return '<li>' + item.name + '</li>';
  134. }
  135. }
  136. });
  137. });
  138. });
  139. }
  140. //绑定tagsinput事件
  141. if ($(".tagsinput", form).size() > 0) {
  142. require(['bootstrap-tagsinput'], function () {
  143. $('.tagsinput', form).each(function () {
  144. $(this).tagsinput({
  145. freeInput: false,
  146. typeaheadjs: {
  147. name: 'tagsinput',
  148. limit: 20,
  149. displayKey: 'name',
  150. valueKey: 'id',
  151. source: remotesource(this),
  152. templates: {
  153. empty: '<li class="notfound">' + __('No matches found') + '</li>',
  154. suggestion: function (item) {
  155. return '<li>' + item.name + '</li>';
  156. }
  157. }
  158. }
  159. });
  160. });
  161. $('.bootstrap-tagsinput .twitter-typeahead').css('display', 'inline');
  162. });
  163. }
  164. });
  165. }
  166. //绑定日期时间元素事件
  167. if ($(".datetimepicker", form).size() > 0) {
  168. require(['bootstrap-datetimepicker'], function () {
  169. $('.datetimepicker', form).parent().css('position', 'relative');
  170. $('.datetimepicker', form)
  171. .datetimepicker({
  172. format: 'YYYY-MM-DD HH:mm:ss',
  173. icons: {
  174. time: 'fa fa-clock-o',
  175. date: 'fa fa-calendar',
  176. up: 'fa fa-chevron-up',
  177. down: 'fa fa-chevron-down',
  178. previous: 'fa fa-chevron-left',
  179. next: 'fa fa-chevron-right',
  180. today: 'fa fa-history',
  181. clear: 'fa fa-trash',
  182. close: 'fa fa-remove'
  183. },
  184. showTodayButton: true,
  185. showClose: true
  186. });
  187. });
  188. }
  189. //绑定summernote事件
  190. if ($(".summernote", form).size() > 0) {
  191. require(['summernote'], function () {
  192. $(".summernote", form).summernote({
  193. height: 250,
  194. lang: 'zh-CN',
  195. dialogsInBody: true,
  196. callbacks: {
  197. onChange: function (contents) {
  198. $(this).val(contents);
  199. $(this).trigger('change');
  200. },
  201. onInit: function () {
  202. },
  203. onImageUpload: function (files) {
  204. var that = this;
  205. //依次上传图片
  206. for (var i = 0; i < files.length; i++) {
  207. Upload.api.send(files[i], function (data) {
  208. var url = Config.upload.cdnurl + data.url;
  209. $(that).summernote("insertImage", url, 'filename');
  210. });
  211. }
  212. }
  213. }
  214. });
  215. });
  216. }
  217. //绑定plupload上传元素事件
  218. if ($(".plupload", form).size() > 0) {
  219. Upload.api.plupload();
  220. }
  221. },
  222. custom: {}
  223. },
  224. };
  225. return Form;
  226. });