重构代码:同步longbow.common脚本

This commit is contained in:
Argo-MacBookPro 2018-11-21 16:19:46 +08:00 committed by Argo-Surface
parent bf86754982
commit 30d5a1d0e9
7 changed files with 239 additions and 103 deletions

View File

@ -17,7 +17,7 @@
$('#useragent').text(navigator.userAgent);
$('#versions').text(JSON.stringify(browser.versions));
$('footer').footer();
$.footer();
};
</script>
}

View File

@ -111,6 +111,13 @@
};
$.extend({
fullScreenStatus: function fullScreenStatus(value) {
if (value !== undefined) window.fullscreen = value;
return document.fullscreen ||
document.mozFullScreen ||
document.webkitIsFullScreen || window.fullscreen ||
false;
},
bc: function (options) {
options = $.extend({
id: "",
@ -125,25 +132,31 @@
cors: false,
contentType: 'application/json',
dataType: 'json',
method: 'get'
method: 'get',
autoFooter: false
}, options);
if (!options.url || options.url === "") {
toastr.error('参数错误: 未设置请求地址Url');
toastr.error('未设置请求地址Url', '参数错误');
return;
}
var loadFlag = "loading";
var loadingHandler = null;
if (options.loading && options.modal) {
var $modal = $(options.modal);
if (!$modal.hasClass('event')) {
$modal.on('shown.bs.modal', function () {
var $this = $(this);
if (loadingHandler !== null) {
window.clearTimeout(loadingHandler);
loadingHandler = null;
}
if ($this.hasClass(loadFlag)) return;
$this.modal('hide');
});
}
$(options.modal).addClass(loadFlag).modal('show');
loadingHandler = window.setTimeout(function () { $(options.modal).addClass(loadFlag).modal('show'); }, 300);
setTimeout(function () {
$(options.modal).find('.close').removeClass('d-none');
}, options.loadingTimeout);
@ -161,13 +174,21 @@
}
function success(result) {
if (options.modal && (result || options.loading)) {
if (loadingHandler !== null) {
// cancel show modal event
window.clearTimeout(loadingHandler);
loadingHandler = null;
}
else $(options.modal).removeClass(loadFlag).modal('hide');
}
if (options.title) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
if ($.isFunction(options.callback)) {
options.callback.call(options, result);
}
if (options.modal && (result || options.loading)) {
$(options.modal).removeClass(loadFlag).modal('hide');
if (options.autoFooter === true) {
$.footer();
}
if (options.title) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
}
var ajaxSettings = {
@ -181,6 +202,7 @@
success(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (window.toastr) toastr.error(XMLHttpRequest.status === 500 ? '后台应用程序错误' : errorThrown, '程序错误');
success(false);
}
};
@ -201,6 +223,11 @@
while (document.getElementById(prefix));
return prefix;
},
footer: function (options) {
var op = $.extend({ header: "header", content: ".main-content", ele: 'footer' }, options);
var $ele = $(op.ele);
return $(op.header).outerHeight() + $(op.content).outerHeight() + $ele.outerHeight() > $(window).height() ? $ele.removeClass('position-fixed') : $ele.addClass('position-fixed');
},
formatUrl: function (url) {
if (!url) return url;
if (url.substr(0, 4) === "http") return url;
@ -225,11 +252,6 @@
that.css({ marginTop: getHeight(), transition: "all .5s linear" });
return this;
},
footer: function (options) {
if ($(window).width() >= 768) { return this.addClass('position-fixed'); }
var op = $.extend({ header: "header", content: ".main-content" }, options);
return $(op.header).outerHeight() + $(op.content).outerHeight() + this.outerHeight() > $(window).height() ? this.removeClass('position-fixed') : this.addClass('position-fixed');
},
lgbTable: function (options) {
var bsa = new DataTable($.extend(options.dataBinder, { url: options.url }));
@ -268,8 +290,11 @@
showToggle: true, //是否显示详细视图和列表视图的切换按钮
cardView: $(window).width() < 768, //是否显示详细视图
queryButton: '#btn_query',
onLoadSuccess: function () {
$('footer').footer();
onLoadSuccess: function (data) {
$.footer();
if (data.IsSuccess === false) {
toastr.error(data.HttpResult.Message, data.HttpResult.Name);
}
}
}, options);
settings.url = $.formatUrl(settings.url);
@ -285,6 +310,32 @@
}
return this;
},
lgbPopover: function (options) {
this.each(function (index, ele) {
var $ele = $(ele);
var data = $ele.data($.fn.popover.Constructor.DATA_KEY);
if (data) {
$.extend(data.config, options);
}
else {
$ele.popover(options);
}
});
return this;
},
lgbTooltip: function (options) {
this.each(function (index, ele) {
var $ele = $(ele);
var data = $ele.data($.fn.tooltip.Constructor.DATA_KEY);
if (data) {
$.extend(data.config, options);
}
else {
$ele.tooltip(options);
}
});
return this;
},
lgbDatePicker: function (options) {
if (!$.isFunction(this.datetimepicker)) return this;
var option = $.extend({
@ -303,6 +354,11 @@
this.datetimepicker(option);
return this;
},
getTextByValue: function (value) {
var text = this.children().filter(function () { return $(this).val() === value; }).text();
if (text === "") text = value;
return text;
},
lgbInfo: function (option) {
this.each(function () {
var $element = $(this);
@ -331,7 +387,7 @@
if ($.isFunction(op.callback)) op.callback.apply(that, arguments);
return console.error(err.toString());
}).then(function () {
if (op.invoke) op.invoke(connection).catch(err => console.error(err.toString()));
if (op.invoke) op.invoke(connection).then(result => console.log(result)).catch(err => console.error(err.toString()));
});
return this;
}
@ -358,15 +414,17 @@
// fix bug bootstrap-table 1.12.1 showToggle
if ($.fn.bootstrapTable) $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggle = $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggleOff;
$(document).ajaxStart(function () {
return NProgress.start();
});
if (window.NProgress) {
$(document).ajaxStart(function () {
return NProgress.start();
});
$(document).ajaxComplete(function (e) {
return NProgress.done();
});
$(document).ajaxComplete(function (e) {
return NProgress.done();
});
}
toastr.options = {
if (window.toastr) toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
@ -382,6 +440,26 @@
"hideMethod": "fadeOut"
};
$('[data-toggle="dropdown"].dropdown-select').dropdown('select');
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="popover"]').popover();
$('[data-toggle="lgbinfo"]').lgbInfo();
$('.date').lgbDatePicker();
$('.collapse').on('shown.bs.collapse', function () {
$.footer().removeClass('d-none');
}).on('hidden.bs.collapse', function () {
$.footer().removeClass('d-none');
}).on('hide.bs.collapse', function () {
$('footer').addClass('d-none');
}).on('show.bs.collapse', function () {
$('footer').addClass('d-none');
});
// fix bug bootstrap-table 1.12.1 showToggle
if ($.fn.bootstrapTable) $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggle = $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggleOff;
$(window).on('resize', function () {
$.footer();
});

View File

@ -80,13 +80,20 @@
},
settings: $.validator.defaults
}, this.defaults(), options);
// fix bug Edge
this.$element.find('select' + this.options.childClass).on('input', function (e) {
e.stopPropagation();
}).on('change', function () {
$(this).trigger('input.lgb.validate');
});
this.$element.on('input.lgb.validate', this.options.childClass, function () {
if (!that.validElement(this)) $(this).tooltip('show');
}).on('inserted.bs.tooltip', this.options.childClass, function () {
$('#' + $(this).attr('aria-describedby')).addClass(that.options.errorClass);
}).on('change.lgb.validate', this.options.childClass, function () {
if (!that.validElement(this)) $(this).tooltip('show');
});
if (!this.options.validButtons) return;
this.$element.find(this.options.validButtons).on('click.lgb.validate', function (e) {
var valid = that.valid();

View File

@ -58,7 +58,7 @@
this._initZoom();
};
$('footer').footer();
$.footer();
var dataBinder = new DataEntity({
Password: "#currentPassword",

View File

@ -58,6 +58,7 @@
$.bc({
url: options.url,
cors: !item.Self,
autoFooter: true,
callback: function (result) {
if ($.isArray(result)) {
var html = '<div class="cache-item"><i class="fa fa-ellipsis-v"></i><span data-toggle="tooltip" title="{2}">{2}</span><span class="badge badge-pill badge-success">{0}</span><span title="{3}">{3}</span><div><span>{6}</span><button class="btn btn-danger" title="{1}" data-url="{4}?cacheKey={1}" data-toggle="tooltip" data-self="{5}" data-placement="left"><i class="fa fa-trash-o"></i></button></div></div>';
@ -78,7 +79,6 @@
});
}
$refresh.removeClass('fa-spin');
$('footer').footer();
}
});
};

View File

@ -6,13 +6,13 @@
that.toggleClass('fa-spin');
$.bc({
url: Tasks.url,
autoFooter: true,
callback: function (result) {
if (result) {
var content = result.map(function (task) {
return $.format(htmlTask, task.TaskName, task.UserName, task.AssignTime, task.Id);
}).join('');
$('#list-task').html(content);
$('footer').footer();
}
that.toggleClass('fa-spin');
}

View File

@ -111,6 +111,13 @@
};
$.extend({
fullScreenStatus: function fullScreenStatus(value) {
if (value !== undefined) window.fullscreen = value;
return document.fullscreen ||
document.mozFullScreen ||
document.webkitIsFullScreen || window.fullscreen ||
false;
},
bc: function (options) {
options = $.extend({
id: "",
@ -124,25 +131,31 @@
cors: false,
contentType: 'application/json',
dataType: 'json',
method: 'get'
method: 'get',
autoFooter: false
}, options);
if (!options.url || options.url === "") {
toastr.error('参数错误: 未设置请求地址Url');
toastr.error('未设置请求地址Url', '参数错误');
return;
}
var loadFlag = "loading";
var loadingHandler = null;
if (options.loading && options.modal) {
var $modal = $(options.modal);
if (!$modal.hasClass('event')) {
$modal.on('shown.bs.modal', function () {
var $this = $(this);
if (loadingHandler !== null) {
window.clearTimeout(loadingHandler);
loadingHandler = null;
}
if ($this.hasClass(loadFlag)) return;
$this.modal('hide');
});
}
$(options.modal).addClass(loadFlag).modal('show');
loadingHandler = window.setTimeout(function () { $(options.modal).addClass(loadFlag).modal('show'); }, 300);
setTimeout(function () {
$(options.modal).find('.close').removeClass('d-none');
}, options.loadingTimeout);
@ -160,13 +173,21 @@
}
function success(result) {
if (options.modal && (result || options.loading)) {
if (loadingHandler !== null) {
// cancel show modal event
window.clearTimeout(loadingHandler);
loadingHandler = null;
}
else $(options.modal).removeClass(loadFlag).modal('hide');
}
if (options.title) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
if ($.isFunction(options.callback)) {
options.callback.call(options, result);
}
if (options.modal && (result || options.loading)) {
$(options.modal).removeClass(loadFlag).modal('hide');
if (options.autoFooter === true) {
$.footer();
}
if (options.title) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
}
var ajaxSettings = {
@ -180,6 +201,7 @@
success(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (window.toastr) toastr.error(XMLHttpRequest.status === 500 ? '后台应用程序错误' : errorThrown, '程序错误');
success(false);
}
};
@ -200,6 +222,11 @@
while (document.getElementById(prefix));
return prefix;
},
footer: function (options) {
var op = $.extend({ header: "header", content: ".container-fluid", ele: 'footer' }, options);
var $ele = $(op.ele);
return $(op.header).outerHeight() + $(op.content).outerHeight() + $ele.outerHeight() > $(window).height() ? $ele.removeClass('position-fixed') : $ele.addClass('position-fixed');
},
formatUrl: function (url) {
if (!url) return url;
if (url.substr(0, 4) === "http") return url;
@ -224,10 +251,6 @@
that.css({ marginTop: getHeight(), transition: "all .5s linear" });
return this;
},
footer: function (options) {
var op = $.extend({ header: "header", content: ".container-fluid" }, options);
return $(op.header).outerHeight() + $(op.content).outerHeight() + this.outerHeight() > $(window).height() ? this.removeClass('position-fixed') : this.addClass('position-fixed');
},
lgbTable: function (options) {
var bsa = new DataTable($.extend(options.dataBinder, { url: options.url }));
@ -266,8 +289,11 @@
showToggle: true, //是否显示详细视图和列表视图的切换按钮
cardView: $(window).width() < 768, //是否显示详细视图
queryButton: '#btn_query',
onLoadSuccess: function () {
$('footer').footer();
onLoadSuccess: function (data) {
$.footer();
if (data.IsSuccess === false) {
toastr.error(data.HttpResult.Message, data.HttpResult.Name);
}
}
}, options);
settings.url = $.formatUrl(settings.url);
@ -283,6 +309,32 @@
}
return this;
},
lgbPopover: function (options) {
this.each(function (index, ele) {
var $ele = $(ele);
var data = $ele.data($.fn.popover.Constructor.DATA_KEY);
if (data) {
$.extend(data.config, options);
}
else {
$ele.popover(options);
}
});
return this;
},
lgbTooltip: function (options) {
this.each(function (index, ele) {
var $ele = $(ele);
var data = $ele.data($.fn.tooltip.Constructor.DATA_KEY);
if (data) {
$.extend(data.config, options);
}
else {
$ele.tooltip(options);
}
});
return this;
},
lgbDatePicker: function (options) {
if (!$.isFunction(this.datetimepicker)) return this;
var option = $.extend({
@ -301,68 +353,10 @@
this.datetimepicker(option);
return this;
},
lgbIndicator: function (options) {
if (/update/.test(options)) {
var $indicator = this.data('radialIndicator');
if ($indicator.indOption.percentage) $indicator.animate(this.attr('data-val') * 100 / this.attr('data-max'));
else $indicator.value(this.attr('data-val'));
return this;
}
this.each(function () {
var op = $.extend({
barColor: {
0: '#33CC33',
70: '#c5c521',
80: '#e46121',
90: '#c92b2b',
100: '#FF0000'
},
radius: 34,
interaction: false,
barWidth: 5,
roundCorner: true,
percentage: true
}, options);
var $this = $(this);
$this.radialIndicator(op);
if ($this.attr('data-val')) {
var $indicator = $this.data('radialIndicator');
if ($indicator.indOption.percentage) $indicator.animate($this.attr('data-val') * 100 / $this.attr('data-max'));
else $indicator.value($this.attr('data-val'));
}
});
return this;
},
lgbCountUp: function (options) {
options = $.extend({}, options);
this.each(function () {
var option = {
useEasing: true,
useGrouping: false,
separator: ',',
decimal: '.'
};
var $element = $(this);
var startVal = options.startVal || 0;
var endVal = $element.text() || 100;
var decimals = options.decimals || 0;
var count = new CountUp(this, startVal, endVal, decimals, 1, $.extend(option, options));
count.start();
});
return this;
},
getTextByValue: function (key, value) {
// 通过Key指定一个下拉框通过value获得下拉框value值的text属性
if (this.length !== 1) throw 'element must be one';
var ele = this.get(0);
if (!ele[key]) {
ele[key] = {};
var that = ele;
$.each($('#' + key).children(), function (index, element) {
that[key][$(element).attr('value')] = $(element).text();
});
}
return ele[key][value];
getTextByValue: function (value) {
var text = this.children().filter(function () { return $(this).val() === value; }).text();
if (text === "") text = value;
return text;
},
lgbInfo: function (option) {
this.each(function () {
@ -392,7 +386,7 @@
if ($.isFunction(op.callback)) op.callback.apply(that, arguments);
return console.error(err.toString());
}).then(function () {
if (op.invoke) op.invoke(connection).catch(err => console.error(err.toString()));
if (op.invoke) op.invoke(connection).then(result => console.log(result)).catch(err => console.error(err.toString()));
});
return this;
}
@ -416,13 +410,70 @@
});
$(function () {
// fix bug bootstrap-table 1.12.1 showToggle
if ($.fn.bootstrapTable) $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggle = $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggleOff;
if (window.NProgress) {
$(document).ajaxStart(function () {
return NProgress.start();
});
$(document).ajaxComplete(function (e) {
return NProgress.done();
});
}
if (window.toastr) toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
"positionClass": "toast-bottom-right",
"onclick": null,
"showDuration": "600",
"hideDuration": "2000",
"timeOut": "4000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
$('[data-toggle="dropdown"].dropdown-select').dropdown('select');
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="popover"]').popover();
$('[data-toggle="lgbinfo"]').lgbInfo();
$('.date').lgbDatePicker();
$('.collapse').on('shown.bs.collapse', function () {
$.footer().removeClass('d-none');
}).on('hidden.bs.collapse', function () {
$.footer().removeClass('d-none');
}).on('hide.bs.collapse', function () {
$('footer').addClass('d-none');
}).on('show.bs.collapse', function () {
$('footer').addClass('d-none');
});
// fix bug bootstrap-table 1.12.1 showToggle
$.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggle = $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggleOff;
if ($.fn.bootstrapTable) $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggle = $.fn.bootstrapTable.Constructor.DEFAULTS.icons.toggleOff;
$(window).on('resize', function () {
$.footer();
});
$("#gotoTop").on('click', function (e) {
e.preventDefault();
$('html, body').animate({
scrollTop: 0
}, 200);
});
$('[data-toggle="dropdown"].dropdown-select').dropdown('select');
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="popover"]').popover();
$('[data-toggle="lgbinfo"]').lgbInfo();
$('.date').lgbDatePicker();
});
})(jQuery);