加入block-ui及select2輔助函式

This commit is contained in:
kroutony 2020-02-22 21:53:38 +08:00
parent acbc178c41
commit 179e369f0b
4 changed files with 54 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import 'chart.js';
import '@coreui/coreui-plugin-chartjs-custom-tooltips';
import 'datatables.net-bs4';
import 'datatables.net-responsive-bs4';
import 'block-ui';
try {

View File

@ -1,5 +1,6 @@
import {dataTable} from "./utils/datatable";
import {block, blockElement, unblock, unblockElement} from "./utils/blockui";
import {init as initSelect2} from './utils/select2';
$('.logout-btn').on('click', e => {
e.preventDefault()
@ -11,4 +12,11 @@ $('.logout-btn').on('click', e => {
*/
app.utils = {
dataTable: dataTable,
blockui: {
blockElement: blockElement,
unblockElement: unblockElement,
block: block,
unblock: unblock
},
select2: initSelect2,
}

20
resources/js/utils/blockui.js vendored Normal file
View File

@ -0,0 +1,20 @@
export const blockElement = function(ele, option = {}) {
$(ele).block(Object.assign({
message: null
}, option))
}
export const unblockElement = function(ele) {
$(ele).unblock();
}
export const block = function(ele, option = {}) {
$.blockUI(Object.assign({
message: ele,
onOverlayClick: $.unblockUI
}, option))
}
export const unblock = function() {
$.unblockUI();
}

24
resources/js/utils/select2.js vendored Normal file
View File

@ -0,0 +1,24 @@
export const init = (selector, options = {}) => {
return $(selector).select2(Object.assign({
templateResult: item => {
if (!item.id) {
return item.text;
}
let text = item.text,
search = $(selector).data('select2').dropdown.$search.val(),
_text = text.split(''),
startIndex = 0
$.each(search.split(''), (index, ch) => {
for (let i = startIndex; i < _text.length; i++) {
if(_text[i] == ch) {
_text[i] = '<span class="select2-rendered-highlight" style="color:red;">' + ch + '</span>'
startIndex = i;
break;
}
}
})
return $('<span>' + _text.join('') + '</span>');
}
}, options))
}