增加lgbSwal静态方法,对swal方法进行包装

This commit is contained in:
Argo-Lenovo 2017-03-22 12:53:30 +08:00
parent 67f38ef924
commit 061de44b67
2 changed files with 66 additions and 62 deletions

View File

@ -1,7 +1,7 @@
(function ($) {
(function($) {
// 增加Array扩展
if (!$.isFunction(Array.prototype.filter)) {
Array.prototype.filter = function (callback, thisObject) {
Array.prototype.filter = function(callback, thisObject) {
if ($.isFunction(callback)) {
var res = new Array();
for (var i = 0; i < this.length; i++) {
@ -14,7 +14,7 @@
// 增加String扩展
if (!$.isFunction(String.prototype.trim)) {
String.prototype.trim = function () {
String.prototype.trim = function() {
if (this == null) return "";
var trimLeft = /^\s+/, trimRight = /\s+$/;
return this.replace(trimLeft, "").replace(trimRight, "");
@ -23,7 +23,7 @@
// 扩展Date
if (!$.isFunction(Date.prototype.format)) {
Date.prototype.format = function (format) {
Date.prototype.format = function(format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
@ -59,7 +59,7 @@
// 扩展format
$.extend({
"format": function (source, params) {
"format": function(source, params) {
if (params === undefined) {
return source;
}
@ -69,8 +69,8 @@
if (params.constructor !== Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), function () {
$.each(params, function(i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), function() {
return n;
});
});
@ -81,7 +81,7 @@
// enhance window.console.log
if (!window.console) {
window.console = {
log: function () {
log: function() {
}
};
@ -91,7 +91,7 @@
// client
jQuery.browser = {
versions: function () {
versions: function() {
var u = navigator.userAgent;
return { //移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1, //IE内核
@ -111,7 +111,7 @@
}
$.extend({
bc: function (options, callback) {
bc: function(options, callback) {
var data = $.extend({
remote: true,
Id: "",
@ -126,7 +126,7 @@
}, options);
if (!data.url || data.url == "") {
swal('参数错误', '未设置请求地址Url', 'error');
lgbSwal({ title: '参数错误', text: '未设置请求地址Url', type: 'error' });
return;
}
@ -136,10 +136,10 @@
data: data.data,
type: data.method,
async: true,
success: function (result) {
success: function(result) {
success(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
error: function(XMLHttpRequest, textStatus, errorThrown) {
if ($.isFunction(data.callback)) data.callback(false);
}
});
@ -152,13 +152,20 @@
$("#" + data.modal).modal('hide');
}
if (data.swal) {
if (result) { swal("成功", data.title, "success"); }
else { swal("失败", data.title, "error"); }
if (result) { lgbSwal({ title: data.title, type: 'success' }); }
else { lgbSwal({ title: data.title, type: 'error' }); }
}
}
},
lgbSwal: function(options) {
if ($.isFunction(swal)) {
swal($.extend({ showConfirmButton: false, showCancelButton: false, timer: 800, title: '未设置', type: "success" }, options));
}
}
});
window.lgbSwal = $.lgbSwal;
// Roles
Role = {
url: '../api/Roles/',
@ -222,32 +229,32 @@
}
$.fn.extend({
adjustDialog: function () {
adjustDialog: function() {
var $modal_dialog = this;
var m_top = Math.max(0, ($(window).height() - $modal_dialog.height()) / 2);
$modal_dialog.css({ 'margin': m_top + 'px auto' });
return this;
},
autoCenter: function () {
autoCenter: function() {
var that = this;
var getHeight = function () {
var getHeight = function() {
return ($(window).height() - $(that).outerHeight()) / 2 + $(document).scrollTop();
}
$(window).resize(function () {
$(window).resize(function() {
$(that).css({
marginTop: getHeight()
});
});
that.animate({ marginTop: "+=" + getHeight() });
},
lgbTooltip: function (option) {
lgbTooltip: function(option) {
if (option == undefined) option = { container: 'body', delay: { "show": 500, "hide": 100 } };
else if (typeof option == "object") option = $.extend({ container: 'body', delay: { "show": 500, "hide": 100 } }, option);
$(this).tooltip(option);
if (option == 'destroy') $(this).removeAttr('data-original-title');
return this;
},
autoValidate: function (rules, messages, handler) {
autoValidate: function(rules, messages, handler) {
var parent = 'body';
var $wrapper = $('#dialogNew');
if ($wrapper.length == 1) parent = '#dialogNew';
@ -266,13 +273,13 @@
ignore: ".ignore",
rules: $.extend({}, rules),
messages: $.extend({}, messages),
highlight: function (element, errorClass, validClass) {
highlight: function(element, errorClass, validClass) {
$(element).parents('.form-group').addClass(errorClass).removeClass(validClass);
},
unhighlight: function (element, errorClass, validClass) {
unhighlight: function(element, errorClass, validClass) {
$(element).lgbTooltip('destroy').parents('.form-group').removeClass(errorClass).addClass(validClass);
},
errorPlacement: function (label, element) {
errorPlacement: function(label, element) {
var $ele = $(element);
if (!$ele.attr('data-original-title')) $ele.lgbTooltip({ container: parent });
$ele.attr('data-original-title', $(label).text());
@ -281,14 +288,14 @@
}
});
if (handler && $.isArray(handler.button)) {
$.each(handler.button, function (index, btn) {
$('#' + btn).on('click', function () {
$.each(handler.button, function(index, btn) {
$('#' + btn).on('click', function() {
$(this).attr('data-valid', $this.valid());
});
});
}
},
smartTable: function (options) {
smartTable: function(options) {
var settings = $.extend({
method: 'get', //请求方式(*
toolbar: '#toolbar', //工具按钮用哪个容器
@ -316,14 +323,14 @@
clickToSelect: false
}, options);
$(this).bootstrapTable(settings);
$('div.toolbar').on('click', 'a', function (e) {
$('div.toolbar').on('click', 'a', function(e) {
e.preventDefault();
var ctl = $('#' + $(this).attr('id').replace('tb_', 'btn_'));
ctl.trigger("click");
}).insertBefore($('div.bootstrap-table > div.fixed-table-toolbar > div.bs-bars'));
$(settings.toolbar).removeClass('hidden');
},
lgbDropdown: function (options) {
lgbDropdown: function(options) {
var $this = $(this);
var op = typeof options == 'object' && options;
if (/val/.test(options)) {
@ -334,8 +341,8 @@
}
}
else {
$this.each(function () {
$(this).on('click', '.dropdown-menu a', { $parent: $(this) }, function (event) {
$this.each(function() {
$(this).on('click', '.dropdown-menu a', { $parent: $(this) }, function(event) {
event.preventDefault();
var $op = $(this);
event.data.$parent.children('a').val($op.attr('data-val')).children(':first').text($op.text());
@ -346,7 +353,7 @@
});
//fix bug
$.fn.modal.Constructor.prototype.adjustDialog = function () {
$.fn.modal.Constructor.prototype.adjustDialog = function() {
var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight
this.$element.css({
@ -360,11 +367,11 @@
}
})(jQuery);
$(function () {
$(function() {
// loading customer css
$.bc({
Id: 1, url: Dicts.url, data: { type: 'activeCss' }, swal: false,
callback: function (result) {
callback: function(result) {
if (result.length > 0)
$('head').append($.format('<link href="../Content/{0}" rel="stylesheet" type="text/css" />', result[0].Code));
}

View File

@ -1,5 +1,5 @@
(function ($) {
BootstrapAdmin = function (options) {
(function($) {
BootstrapAdmin = function(options) {
var that = this;
options = options || {};
options.click = $.extend({}, BootstrapAdmin.settings.click, options.click);
@ -27,7 +27,7 @@
// handler modal window show event
if (this.options.modal && this.options.modal.constructor === String) {
$('#' + this.options.modal).on('show.bs.modal', function (e) {
$('#' + this.options.modal).on('show.bs.modal', function(e) {
if (that.options.validateForm && that.options.validateForm.constructor === String) {
var v = $('#' + that.options.validateForm);
v.validate().resetForm();
@ -41,7 +41,7 @@
var source = $("#" + cId);
source.data('click', name);
if (event !== null) source.data('event', event);
source.click(function (e) {
source.click(function(e) {
e.preventDefault();
var method = source.data('click');
BootstrapAdmin.prototype[method].call(that, this, source.data('event'));
@ -68,13 +68,13 @@
}
};
BootstrapAdmin.idFormatter = function (value, row, index) {
BootstrapAdmin.idFormatter = function(value, row, index) {
return "<a class='edit' href='javascript:void(0)'>" + value + "</a>";
};
BootstrapAdmin.prototype = {
constructor: BootstrapAdmin,
idEvents: function () {
idEvents: function() {
var op = {
dataEntity: $.extend({}, this.options.dataEntity),
table: this.options.bootstrapTable,
@ -82,7 +82,7 @@
src: this
};
return {
'click .edit': function (e, value, row, index) {
'click .edit': function(e, value, row, index) {
op.dataEntity.load(row);
$(op.table).bootstrapTable('uncheckAll');
$(op.table).bootstrapTable('check', index);
@ -92,29 +92,29 @@
}
},
query: function (e, callback) {
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) {
create: function(e, callback) {
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) {
edit: function(e, callback) {
var options = this.options;
var data = {};
if (options.bootstrapTable.constructor === String) {
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) {
swal('请选择要编辑的数据', "编辑操作", "warning");
lgbSwal({ title: '请选择要编辑的数据', type: "warning" });
return;
}
else if (arrselections.length > 1) {
swal('请选择一个要编辑的数据', "编辑操作", "warning");
lgbSwal({ title: '请选择一个要编辑的数据', type: "warning" });
return;
}
else {
@ -126,36 +126,33 @@
handlerCallback.call(this, callback, e, { oper: 'edit', data: data });
},
del: function (e, callback) {
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");
lgbSwal({ title: '请选择要删除的数据', type: "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(",");
}, 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) {
callback: function(result) {
if ($.isPlainObject(result)) {
var info = result.result ? "success" : "error";
var msg = result.msg
swal(msg, "删除数据", info);
lgbSwal({ title: result.msg, type: result.result ? "success" : "error" });
result = result.result;
this.swal = false;
}
@ -169,14 +166,14 @@
}
},
save: function (e, callback) {
save: function(e, callback) {
var that = this;
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;
$.bc({
url: options.url, data: options.data, title: "保存数据", modal: options.modal,
callback: function (result) {
callback: function(result) {
var finalData = null;
var index = 0;
if (result) {
@ -203,17 +200,17 @@
});
},
assign: function (e, callback) {
assign: function(e, callback) {
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");
lgbSwal({ title: '请选择要编辑的数据', type: "warning" });
return;
}
else if (arrselections.length > 1) {
swal('请选择一个要编辑的数据', "编辑操作", "warning");
lgbSwal({ title: '请选择一个要编辑的数据', type: "warning" });
return;
}
else {
@ -227,7 +224,7 @@
}
};
var handlerCallback = function (callback, e, data) {
var handlerCallback = function(callback, e, data) {
if ($.isFunction(callback)) callback.call(e, data);
if ($.isFunction(this.options.callback)) this.options.callback.call(e, data);
}