require-form.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'upload', 'validator'], function ($, undefined, Backend, Config, Toastr, Upload, Validator) {
  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({
  68. validClass: 'has-success',
  69. invalidClass: 'has-error',
  70. bindClassTo: '.form-group',
  71. formClass: 'n-default n-bootstrap',
  72. msgClass: 'n-right',
  73. stopOnError: true,
  74. valid: function (ret) {
  75. //验证通过提交表单
  76. Form.api.submit(form, onBeforeSubmit, function (data) {
  77. if (typeof onAfterSubmit == 'function') {
  78. if (!onAfterSubmit.call(form, data)) {
  79. return false;
  80. }
  81. }
  82. //提示及关闭当前窗口
  83. parent.Toastr.success(__('Operation completed'));
  84. parent.$(".btn-refresh").trigger("click");
  85. var index = parent.Layer.getFrameIndex(window.name);
  86. parent.Layer.close(index);
  87. });
  88. return false;
  89. }
  90. });
  91. //绑定select元素事件
  92. if ($(".selectpicker", form).size() > 0) {
  93. require(['bootstrap-select'], function () {
  94. $('.selectpicker', form).selectpicker();
  95. });
  96. }
  97. if ($(".typeahead").size() > 0 || $(".tagsinput").size() > 0) {
  98. require(['bloodhound'], function () {
  99. var remotesource = function (input) {
  100. return new Bloodhound({
  101. datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  102. queryTokenizer: Bloodhound.tokenizers.whitespace,
  103. remote: {
  104. url: '/ajax/typeahead?search=%QUERY&field=' + $(input).attr("name"),
  105. wildcard: '%QUERY',
  106. transform: function (ret) {
  107. return ret.data.searchlist;
  108. }
  109. }
  110. });
  111. };
  112. //绑定typeahead事件
  113. if ($(".typeahead", form).size() > 0) {
  114. require(['typeahead'], function () {
  115. $(".typeahead", form).each(function () {
  116. $(this).typeahead({
  117. hint: true,
  118. highlight: true,
  119. minLength: 0
  120. }, {
  121. name: 'typeahead',
  122. limit: 20,
  123. displayKey: 'id',
  124. source: remotesource(this),
  125. templates: {
  126. empty: '<li class="notfound">' + __('No matches found') + '</li>',
  127. suggestion: function (item) {
  128. return '<li>' + item.name + '</li>';
  129. }
  130. }
  131. });
  132. });
  133. });
  134. }
  135. //绑定tagsinput事件
  136. if ($(".tagsinput", form).size() > 0) {
  137. require(['bootstrap-tagsinput'], function () {
  138. $('.tagsinput', form).each(function () {
  139. $(this).tagsinput({
  140. freeInput: false,
  141. typeaheadjs: {
  142. name: 'tagsinput',
  143. limit: 20,
  144. displayKey: 'name',
  145. valueKey: 'id',
  146. source: remotesource(this),
  147. templates: {
  148. empty: '<li class="notfound">' + __('No matches found') + '</li>',
  149. suggestion: function (item) {
  150. return '<li>' + item.name + '</li>';
  151. }
  152. }
  153. }
  154. });
  155. });
  156. $('.bootstrap-tagsinput .twitter-typeahead').css('display', 'inline');
  157. });
  158. }
  159. });
  160. }
  161. //绑定日期时间元素事件
  162. if ($(".datetimepicker", form).size() > 0) {
  163. require(['bootstrap-datetimepicker'], function () {
  164. $('.datetimepicker', form).parent().css('position', 'relative');
  165. $('.datetimepicker', form)
  166. .datetimepicker({
  167. format: 'YYYY-MM-DD HH:mm:ss',
  168. icons: {
  169. time: 'fa fa-clock-o',
  170. date: 'fa fa-calendar',
  171. up: 'fa fa-chevron-up',
  172. down: 'fa fa-chevron-down',
  173. previous: 'fa fa-chevron-left',
  174. next: 'fa fa-chevron-right',
  175. today: 'fa fa-history',
  176. clear: 'fa fa-trash',
  177. close: 'fa fa-remove'
  178. },
  179. showTodayButton: true,
  180. showClose: true
  181. });
  182. });
  183. }
  184. //绑定summernote事件
  185. if ($(".summernote", form).size() > 0) {
  186. require(['summernote'], function () {
  187. $(".summernote", form).summernote({
  188. height: 250,
  189. lang: 'zh-CN',
  190. dialogsInBody: true,
  191. callbacks: {
  192. onChange: function (contents) {
  193. $(this).val(contents);
  194. $(this).trigger('change');
  195. },
  196. onInit: function () {
  197. },
  198. onImageUpload: function (files) {
  199. var that = this;
  200. //依次上传图片
  201. for (var i = 0; i < files.length; i++) {
  202. Upload.api.send(files[i], function (data) {
  203. var url = Config.upload.cdnurl + data.url;
  204. $(that).summernote("insertImage", url, 'filename');
  205. });
  206. }
  207. }
  208. }
  209. });
  210. });
  211. }
  212. //绑定plupload上传元素事件
  213. if ($(".plupload", form).size() > 0) {
  214. Upload.api.plupload();
  215. }
  216. },
  217. custom: {}
  218. },
  219. };
  220. return Form;
  221. });