define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'upload', 'bootstrap-validator', 'bootstrap-checkbox', 'bootstrap-radio', 'bootstrap-switch'], function ($, undefined, Backend, Config, Toastr, Upload, undefined) { var Form = { config: { }, api: { submit: function (form, onBeforeSubmit, onAfterSubmit) { if (form.size() == 0) return Toastr.error("表单未初始化完成,无法提交"); //提交前事件 var beforeSubmit = form.data("before-submit"); //元素绑定函数 if (beforeSubmit && typeof Form.api.custom[beforeSubmit] == 'function') { if (!Form.api.custom[beforeSubmit].call(form)) { return false; } } //自定义函数 if (typeof onBeforeSubmit == 'function') { if (!onBeforeSubmit.call(form)) { return false; } } var type = form.attr("method"); type = type && (type == 'GET' || type == 'POST') ? type : 'GET'; url = form.attr("action"); url = url ? url : location.href; $.ajax({ type: type, url: url, data: form.serialize(), dataType: 'json', success: function (data) { if (data.hasOwnProperty("code")) { var content = data.hasOwnProperty("content") && data.content != "" ? data.content : ""; if (data.code === 1) { $('.form-group', form).removeClass('has-feedback has-success has-error'); //成功提交后事件 var afterSubmit = form.data("after-submit"); //元素绑定函数 if (afterSubmit && typeof Form.api.custom[afterSubmit] == 'function') { if (!Form.api.custom[afterSubmit].call(form, content)) { return false; } } //自定义函数 if (typeof onAfterSubmit == 'function') { if (!onAfterSubmit.call(form, content)) { return false; } } Toastr.success(content ? content : __('Operation completed')); } else { Toastr.error(content ? content : __('Operation failed')); } } else { Toastr.error(__('Unknown data format')); } }, error: function () { Toastr.error(__('Network error')); }, complete: function (e) { } }); return false; }, bindevent: function (form, onBeforeSubmit, onAfterSubmit) { form.validator().on('submit', function (e) { if (e.isDefaultPrevented()) { //验证不通过 Toastr.error("验证失败,请检查表单输入是否正确"); //Backend.api.error(); } else { //验证通过提交表单 Form.api.submit(form, onBeforeSubmit, function (content) { if (typeof onAfterSubmit == 'function') { if (!onAfterSubmit.call(form, content)) { return false; } } //提示及关闭当前窗口 parent.Layer.msg(__('Operation completed'), { offset: 0, icon: 1 }); parent.$(".btn-refresh").trigger("click"); var index = parent.Layer.getFrameIndex(window.name); parent.Layer.close(index); }); return false; } }); // Activate the switches with icons if ($('.switch').length != 0) { $('.switch')['bootstrapSwitch'](); } // Activate regular switches if ($("[data-toggle='switch']").length != 0) { $("[data-toggle='switch']").wrap('
').parent().bootstrapSwitch(); } //绑定select元素事件 if ($(".selectpicker", form).size() > 0) { require(['bootstrap-select'], function () { $('.selectpicker', form).selectpicker(); }); } if ($(".typeahead").size() > 0 || $(".tagsinput").size() > 0) { require(['bloodhound'], function () { var remotesource = function (input) { return new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: '/ajax/typeahead?search=%QUERY&field=' + $(input).attr("name"), wildcard: '%QUERY', transform: function (data) { return data.content.searchlist; } } }); }; //绑定typeahead事件 if ($(".typeahead", form).size() > 0) { require(['typeahead'], function () { $(".typeahead", form).each(function () { $(this).typeahead({ hint: true, highlight: true, minLength: 0 }, { name: 'typeahead', limit: 20, displayKey: 'id', source: remotesource(this), templates: { empty: '