重构代码:合并公共脚本如tooltop/popover/datetimepicker

This commit is contained in:
Argo-MacBookPro 2018-10-12 14:17:24 +08:00
parent 26581b6b3b
commit b33266e162
5 changed files with 65 additions and 113 deletions

View File

@ -210,11 +210,6 @@ $(function () {
$('body').toggleClass('sidebar-open');
});
$('[data-toggle="dropdown"].dropdown-select').dropdown('select');
// tooltip
$('[data-toggle="tooltip"]').tooltip();
// load widget data
$('.header .nav').reloadWidget().notifi({
url: 'NotiHub',

View File

@ -21,20 +21,6 @@
]
});
$('.date').datetimepicker({
language: 'zh-CN',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
format: 'yyyy-mm-dd',
pickerPosition: 'bottom-left',
fontAwesome: true
});
$('#btn_view').on('click', function (row) {
$.bc({
url: Exceptions.url, method: "post",

View File

@ -15,18 +15,4 @@
{ title: "备注", field: "ClientAgent", sortable: false }
]
});
$('.date').datetimepicker({
language: 'zh-CN',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
format: 'yyyy-mm-dd',
pickerPosition: 'bottom-left',
fontAwesome: true
});
});

View File

@ -286,6 +286,39 @@
}
return this;
},
lgbDatePicker: function (options) {
if (!$.isFunction(this.datetimepicker)) return this;
var option = $.extend({
language: 'zh-CN',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0,
format: 'yyyy-mm-dd',
pickerPosition: 'bottom-left',
fontAwesome: true
}, options);
this.datetimepicker(option);
return this;
},
lgbInfo: function (option) {
this.each(function () {
var $element = $(this);
$element.append($.format('<a href="#" tabindex="-1" role="button" data-toggle="popover"><i class="fa fa-question-circle"></i></a>'));
});
var container = $(this).parent().attr('data-container') || '#dialogNew';
this.find('[data-toggle="popover"]').popover($.extend({
title: function () {
return $(this).parent().text();
}, content: function () {
return $(this).parent().attr('data-content');
}, trigger: 'focus', html: true, container: container
}, option));
return this;
},
notifi: function (options) {
var op = $.extend({ url: '', method: 'rev', callback: false }, options);
var connection = new signalR.HubConnectionBuilder().withUrl($.formatUrl(op.url)).build();
@ -319,6 +352,14 @@
}
});
$(function () {
$('[data-toggle="dropdown"].dropdown-select').dropdown('select');
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="popover"]').popover();
$('[data-toggle="lgbinfo"]').lgbInfo();
$('.date').lgbDatePicker();
});
// Roles
Role = {
url: 'api/Roles',

View File

@ -352,6 +352,19 @@
});
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];
},
lgbInfo: function (option) {
this.each(function () {
var $element = $(this);
@ -367,87 +380,18 @@
}, option));
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];
},
msgHandler: function (options) {
var settings = {
url: 'api/WS',
interval: 10000,
sendMessage: '',
timerHandler: null,
onopen: function (e) { },
onmessage: function (e) { },
onclose: function (e) { },
errorHandler: function (e) { if (toastr && $.isFunction(toastr.error)) toastr.error("连接服务器失败!", "系统错误"); },
loop: function () {
var that = this;
var uri = window.location.protocol + "//" + window.location.host + $.formatUrl(settings.url);
$.bc({
url: uri,
id: this.sendMessage,
method: "post",
callback: function (result) {
if (!result) {
that.errorHandler.call(that.target);
return;
}
that.onmessage.call(that.target, { data: JSON.stringify(result) });
}
});
if (this.timerHandler !== null) clearTimeout(this.timerHandler);
this.timerHandler = setTimeout(function () { that.loop(); }, that.interval);
notifi: function (options) {
var op = $.extend({ url: '', method: 'rev', callback: false }, options);
var connection = new signalR.HubConnectionBuilder().withUrl($.formatUrl(op.url)).build();
var that = this;
connection.on(op.method, function () {
if ($.isFunction(op.callback)) {
op.callback.apply(that, arguments);
}
};
$.extend(settings, options, { target: this });
settings.loop();
return this;
},
socketHandler: function (options) {
// WebSocket消息处理方法
var settings = {
url: 'WS',
interval: 30000,
sendMessage: 'keepalive',
timerHandler: null,
onopen: function (e) { },
onerror: function (e) { },
errorHandler: function (e) { if (window.toastr && $.isFunction(window.toastr.error)) toastr.error("连接服务器失败!", "系统错误"); },
onmessage: function (e) { },
onclose: function (e) { },
loop: function (socket) {
var that = this;
if (socket.readyState === 1) {
socket.send(this.sendMessage);
if (this.timerHandler !== null) clearTimeout(this.timerHandler);
this.timerHandler = setTimeout(function () { that.loop(socket); }, that.interval);
}
else {
this.errorHandler();
}
}
};
$.extend(settings, options, { target: this });
var uri = "ws://" + window.location.host + $.formatUrl(settings.url);
var socket = new WebSocket(uri);
socket.onopen = function (e) { settings.onopen.call(settings.target, e); settings.loop(socket); };
socket.onerror = function (e) {
settings.onerror.call(settings.target, e);
settings.target.msgHandler(options);
};
socket.onmessage = function (e) { settings.onmessage.call(settings.target, e); };
socket.onclose = function (e) { settings.onclose.call(settings.target, e); };
});
connection.start().catch(function (err) {
return console.error(err.toString());
});
return this;
}
});