BootstrapAdmin/Bootstrap.Admin/Content/js/framework.js

237 lines
11 KiB
JavaScript
Raw Normal View History

(function ($) {
BootstrapAdmin = function (options) {
var that = this;
options = options || {};
options.click = $.extend({}, BootstrapAdmin.settings.click, options.click);
this.options = $.extend({}, BootstrapAdmin.settings, options);
this.dataEntity = options.dataEntity;
// handler click event
for (name in this.options.click) {
2016-10-25 18:47:33 +08:00
var ele = this.options.click[name];
var cId = ele;
var event = null;
if ($.isArray(ele)) {
for (index in ele) {
if (ele[index].id === undefined) {
window.console.log('options.click.assign[{0}].{1}.id 未设置控件id', ele[index].id, name);
2016-10-25 18:47:33 +08:00
continue;
}
cId = ele[index]['id'];
event = ele[index]['click'];
handler(cId, event);
}
}
else handler(cId, event);
}
// handler modal window show event
if (this.options.modal && this.options.modal.constructor === String) {
$('#' + this.options.modal).on('show.bs.modal', function (e) {
if (that.options.validateForm && that.options.validateForm.constructor === String) {
var v = $('#' + that.options.validateForm);
var vf = v.validate();
2016-11-27 16:50:07 +08:00
vf.currentElements.each(function () {
var $this = $(this);
if ($this.hasClass('tipso_style')) $this.tipso('hide').tipso('destroy');
})
vf.resetForm();
v.find('div.form-group').removeClass("has-error has-success");
}
});
}
2016-10-25 18:47:33 +08:00
function handler(cid, event) {
var source = $("#" + cId);
source.data('click', name);
if (event !== null) source.data('event', event);
source.click(function () {
var method = source.data('click');
BootstrapAdmin.prototype[method].call(that, this, source.data('event'));
2016-10-25 18:47:33 +08:00
});
}
};
BootstrapAdmin.VERSION = "1.0";
BootstrapAdmin.Author = "Argo Zhang";
BootstrapAdmin.Email = "argo@163.com";
BootstrapAdmin.settings = {
url: undefined,
bootstrapTable: 'table',
validateForm: 'dataForm',
modal: 'dialogNew',
click: {
query: 'btn_query',
create: 'btn_add',
edit: 'btn_edit',
del: 'btn_delete',
2016-10-25 18:47:33 +08:00
save: 'btnSubmit',
assign: []
}
};
BootstrapAdmin.idFormatter = function (value, row, index) {
return "<a class='edit' href='javascript:void(0)'>" + value + "</a>";
};
BootstrapAdmin.prototype = {
constructor: BootstrapAdmin,
2016-10-24 19:12:22 +08:00
idEvents: function () {
var op = {
2016-10-25 18:47:33 +08:00
dataEntity: $.extend({}, this.options.dataEntity),
2016-10-24 19:12:22 +08:00
table: this.options.bootstrapTable,
modal: this.options.modal,
src: this
2016-10-24 19:12:22 +08:00
};
return {
'click .edit': function (e, value, row, index) {
op.dataEntity.load(row);
2016-10-25 18:47:33 +08:00
$(op.table).bootstrapTable('uncheckAll');
$(op.table).bootstrapTable('check', index);
handlerCallback.call(op.src, null, e, { oper: 'edit' });
2016-10-24 19:12:22 +08:00
$('#' + op.modal).modal("show");
}
}
},
query: function (e, callback) {
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('refresh');
handlerCallback.call(this, callback, e, { oper: 'query' });
},
create: function (e, callback) {
2016-10-25 18:47:33 +08:00
if (this.dataEntity instanceof DataEntity) this.dataEntity.reset();
if (this.options.modal.constructor === String) $('#' + this.options.modal).modal("show");
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll');
handlerCallback.call(this, callback, e, { oper: 'create' });
},
edit: function (e, callback) {
2016-10-25 18:47:33 +08:00
var options = this.options;
var data = {};
if (options.bootstrapTable.constructor === String) {
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) {
swal('请选择要编辑的数据', "编辑操作", "warning");
return;
}
else if (arrselections.length > 1) {
swal('请选择一个要编辑的数据', "编辑操作", "warning");
return;
}
else {
data = arrselections[0];
if (this.dataEntity instanceof DataEntity) this.dataEntity.load(data);
if (options.modal.constructor === String) $('#' + options.modal).modal("show");
}
}
handlerCallback.call(this, callback, e, { oper: 'edit', data: data });
},
del: function (e, callback) {
var that = this;
var options = this.options;
if (options.bootstrapTable.constructor === String) {
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) {
swal('请选择要删除的数据', "删除操作", "warning");
return;
}
else {
swal({
title: "您确定要删除吗?",
text: "删除操作",
type: "warning",
showCancelButton: true,
closeOnConfirm: true,
confirmButtonText: "是的,我要删除",
confirmButtonColor: "#d9534f",
cancelButtonText: "取消"
}, function () {
setTimeout(function () {
var iDs = arrselections.map(function (element, index) { return element.ID }).join(",");
options.IDs = iDs;
$.bc({
url: options.url, data: { "": iDs }, method: 'DELETE', title: '删除数据',
callback: function (result) {
if ($.isPlainObject(result)) {
var info = result.result ? "success" : "error";
var msg = result.msg
swal(msg, "删除数据", info);
result = result.result;
this.swal = false;
}
if (result) $(options.bootstrapTable).bootstrapTable('refresh');
handlerCallback.call(that, callback, e, { oper: 'del', success: result });
}
});
}, 100);
});
}
}
},
save: function (e, callback) {
var that = this;
2016-10-25 18:47:33 +08:00
var options = $.extend({ data: {} }, this.options);
if (this.dataEntity instanceof DataEntity) options = $.extend(options, { data: this.dataEntity.get() });
if (options.validateForm.constructor === String && !$("#" + options.validateForm).valid()) return;
2016-11-18 22:53:56 +08:00
$.bc({
url: options.url, data: options.data, title: "保存数据", modal: options.modal,
callback: function (result) {
var finalData = null;
var index = 0;
if (result) {
if (options.bootstrapTable.constructor === String && options.data.ID.constructor === String) {
// 更新表格
if (options.data.ID > 0) {
var allTableData = $(options.bootstrapTable).bootstrapTable('getData');
for (index = 0; index < allTableData.length; index++) {
finalData = allTableData[index];
if (finalData.ID == options.data.ID) {
$(options.bootstrapTable).bootstrapTable('updateRow', { index: index, row: $.extend(finalData, options.data) });
break;
}
}
}
else {
$(options.bootstrapTable).bootstrapTable('refresh');
finalData = options.data;
}
}
}
2016-11-18 22:53:56 +08:00
handlerCallback.call(that, callback, e, { oper: 'save', success: result, index: index, data: finalData });
}
});
2016-10-25 18:47:33 +08:00
},
assign: function (e, callback) {
2016-10-25 18:47:33 +08:00
var options = this.options;
var row = {};
if (options.bootstrapTable && options.bootstrapTable.constructor === String) {
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) {
swal('请选择要编辑的数据', "编辑操作", "warning");
return;
}
else if (arrselections.length > 1) {
swal('请选择一个要编辑的数据', "编辑操作", "warning");
return;
}
else {
row = arrselections[0];
2016-10-25 18:47:33 +08:00
}
}
var data = options.dataEntity;
if (data instanceof DataEntity) data = data.get();
if ($.isFunction(callback)) callback.call(e, row, $.extend({}, data));
if ($.isFunction(this.options.callback)) this.options.callback.call(e, { oper: 'assign', row: row, data: data });
}
2016-10-25 18:47:33 +08:00
};
2016-11-18 20:49:44 +08:00
var handlerCallback = function (callback, e, data) {
if ($.isFunction(callback)) callback.call(e, data);
if ($.isFunction(this.options.callback)) this.options.callback.call(e, data);
}
2016-10-20 17:55:29 +08:00
})(jQuery);