mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-09 23:16:18 +08:00
quickselect新增json格式,bugfix
This commit is contained in:
parent
b4ebd2be31
commit
c21fb57031
@ -774,13 +774,37 @@ li.CodeMirror-hint {
|
|||||||
|
|
||||||
#quickselect .select2-results__option {
|
#quickselect .select2-results__option {
|
||||||
padding: 5px 10px;
|
padding: 5px 10px;
|
||||||
max-height: 50px;
|
height: 40px;
|
||||||
/* line-height: 50px; */
|
/* line-height: 50px; */
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#quickselect .select2-results__option .title {
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#quickselect .select2-results__option .description {
|
||||||
|
color: rgba(0, 0, 0, 0.5);
|
||||||
|
line-height: 20px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#quickselect .select2-results__option .icon {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
line-height: 40px;
|
||||||
|
text-align: center;
|
||||||
|
float: left;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#quickselect .select2-results__option .icon>img {
|
||||||
|
max-width: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
#quickselect .select2-results__option--highlighted {
|
#quickselect .select2-results__option--highlighted {
|
||||||
background-color: #ebebeb;
|
background-color: #ebebeb;
|
||||||
color: unset;
|
color: unset;
|
||||||
|
@ -172,33 +172,47 @@ quickcommand = {
|
|||||||
var index = choise.index
|
var index = choise.index
|
||||||
var text = choise.text
|
var text = choise.text
|
||||||
//do something...
|
//do something...
|
||||||
}, [option1, option2...], { placeholder, enableHTML, closeOnSelect })`
|
}, [option1, option2...], { placeholder, optionType, enableSearch, closeOnSelect })`
|
||||||
if (!(callback instanceof Function)) throw helps
|
if (!(callback instanceof Function)) throw helps
|
||||||
if (!(selects instanceof Array)) throw helps
|
if (!(selects instanceof Array)) throw helps
|
||||||
opt.placeholder || (opt.placeholder = "搜索,支持拼音")
|
opt.optionType || (opt.optionType = 'plaintext')
|
||||||
opt.enableHTML || (opt.enableHTML = false)
|
typeof opt.placeholder == 'undefined' && (opt.placeholder = "搜索,支持拼音")
|
||||||
|
typeof opt.enableSearch == 'undefined' && (opt.enableSearch = true)
|
||||||
typeof opt.closeOnSelect == 'undefined' && (opt.closeOnSelect = true)
|
typeof opt.closeOnSelect == 'undefined' && (opt.closeOnSelect = true)
|
||||||
if ($('#quickselect').length) $('#quickselect').remove()
|
if ($('#quickselect').length) $('#quickselect').remove()
|
||||||
$("body").append(`<div id="quickselect"><select id="selectBox"></select></div>`)
|
$("body").append(`<div id="quickselect"><select id="selectBox"></select></div>`)
|
||||||
var selectBoxNumbers = selects.length
|
let item, data = []
|
||||||
var data = []
|
selects.forEach((s, i) => {
|
||||||
for (let i = 0; i < selectBoxNumbers; i++) {
|
item = {id: i}
|
||||||
data.push({ id: i, text: selects[i] })
|
if (opt.optionType == 'json') {
|
||||||
}
|
item.text = ''
|
||||||
|
Object.keys(s).forEach(k => item[k] = s[k])
|
||||||
|
s.icon && (item.text += `<div class="icon"><img src="${s.icon}"></div>`)
|
||||||
|
s.title && (item.text += `<div class="title">${s.title}</div>`)
|
||||||
|
s.description && (item.text += `<div class="description">${s.description}</div>`)
|
||||||
|
} else {
|
||||||
|
item.text = s
|
||||||
|
}
|
||||||
|
data.push(item)
|
||||||
|
})
|
||||||
|
$('#selectBox').data('options', data)
|
||||||
|
$('#selectBox').data('type', opt.optionType)
|
||||||
var prefer = {
|
var prefer = {
|
||||||
data: data,
|
// data: data,
|
||||||
width: "100%",
|
width: "100%",
|
||||||
dropdownParent: $("#quickselect"),
|
dropdownParent: $("#quickselect"),
|
||||||
closeOnSelect: opt.closeOnSelect,
|
closeOnSelect: opt.closeOnSelect,
|
||||||
// 支持无限滚动
|
// 支持无限滚动
|
||||||
ajax: {
|
ajax: {
|
||||||
transport: (params, success, failure) => {
|
transport: (params, success, failure) => {
|
||||||
let pageSize = 50
|
let cont, pageSize = 50
|
||||||
let term = (params.data.term || '').toLowerCase()
|
let term = (params.data.term || '').toLowerCase()
|
||||||
let page = (params.data.page || 1)
|
let page = (params.data.page || 1)
|
||||||
let items = $('#selectBox > option').length ? Array.from($('#selectBox > option').map(function (x) { return { id: x, text: $(this).html() } })) : data
|
let items = $('#selectBox').data('options')
|
||||||
let results = items.filter(x => {
|
let results = items.filter(x => {
|
||||||
cont = opt.enableHTML ? x.text.replace(/<[^<>]+>/g, '') : x.text
|
if (opt.optionType == 'json') cont = x.title
|
||||||
|
else if (opt.optionType == 'html') cont = x.text.replace(/<[^<>]+>/g, '')
|
||||||
|
else cont = x.text
|
||||||
return cont.toLowerCase().includes(term) || PinyinMatch.match(cont, term)
|
return cont.toLowerCase().includes(term) || PinyinMatch.match(cont, term)
|
||||||
})
|
})
|
||||||
let paged = results.slice((page - 1) * pageSize, page * pageSize)
|
let paged = results.slice((page - 1) * pageSize, page * pageSize)
|
||||||
@ -208,19 +222,22 @@ quickcommand = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
// 显示html时不转义标签
|
// 显示html时不转义标签
|
||||||
if (opt.enableHTML) prefer.escapeMarkup = markup => markup
|
if (opt.optionType != 'plaintext') prefer.escapeMarkup = markup => markup
|
||||||
$('#selectBox').select2(prefer)
|
$('#selectBox').select2(prefer)
|
||||||
$('#selectBox').val(null).trigger('change')
|
$('#selectBox').val(null).trigger('change')
|
||||||
$('#selectBox').select2('open')
|
$('#selectBox').select2('open')
|
||||||
|
$("#quickselect .select2-search__field").focus()
|
||||||
$('#quickselect .select2').hide()
|
$('#quickselect .select2').hide()
|
||||||
opt.enableHTML || $('.select2-results').css({'line-height': '50px'})
|
opt.optionType == 'plaintext' && $('.select2-results').css({'line-height': '40px'})
|
||||||
modWindowHeight($('.select2-results').height())
|
modWindowHeight($('.select2-results').outerHeight())
|
||||||
utools.setSubInput(({text})=>{
|
opt.enableSearch && utools.setSubInput(({text})=>{
|
||||||
$("#quickselect .select2-search__field").val(text).trigger('input')
|
$("#quickselect .select2-search__field").val(text).trigger('input')
|
||||||
modWindowHeight($('.select2-results').height())
|
modWindowHeight($('.select2-results').outerHeight())
|
||||||
}, opt.placeholder)
|
}, opt.placeholder)
|
||||||
$('#selectBox').on('select2:select', function (e) {
|
$('#selectBox').on('select2:select', function (e) {
|
||||||
callback({ index: $(this).val(), text: $(`option[value="${$(this).val()}"]`).text() })
|
let result = $('#selectBox').data('options')[$(this).val()]
|
||||||
|
delete result.selected
|
||||||
|
callback(result)
|
||||||
if (opt.closeOnSelect) {
|
if (opt.closeOnSelect) {
|
||||||
$('#selectBox').off('select2:select')
|
$('#selectBox').off('select2:select')
|
||||||
utools.removeSubInput()
|
utools.removeSubInput()
|
||||||
@ -230,15 +247,29 @@ quickcommand = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 更新选项列表
|
// 更新选项列表
|
||||||
updateSelectList: function (opt) {
|
updateSelectList: function (selects) {
|
||||||
if(!$('#selectBox').length) throw '当前没有选择列表, 请结合 quickcommand.showSelectList 使用'
|
if(!$('#selectBox').length) throw '当前没有选择列表, 请结合 quickcommand.showSelectList 使用'
|
||||||
var num = $('#quickselect > #selectBox > option').length
|
let data = $('#selectBox').data('options')
|
||||||
var newOption
|
let num = data.length
|
||||||
if (opt instanceof Array) newOption = opt.map(o => new Option(o, num++, false, false).outerHTML).join('')
|
let optionType = $('#selectBox').data('type')
|
||||||
else newOption = new Option(opt, num, false, false)
|
selects instanceof Array || (selects = [selects])
|
||||||
$('#selectBox').append(newOption).val(null).trigger('change')
|
let item
|
||||||
|
selects.forEach(s => {
|
||||||
|
item = {id: num++}
|
||||||
|
if (optionType == 'json') {
|
||||||
|
item.text = ''
|
||||||
|
Object.keys(s).forEach(k => item[k] = s[k])
|
||||||
|
s.icon && (item.text += `<div class="icon"><img src="${s.icon}"></div>`)
|
||||||
|
s.title && (item.text += `<div class="title">${s.title}</div>`)
|
||||||
|
s.description && (item.text += `<div class="description">${s.description}</div>`)
|
||||||
|
} else {
|
||||||
|
item.text = s
|
||||||
|
}
|
||||||
|
data.push(item)
|
||||||
|
})
|
||||||
|
$('#selectBox').data('options', data).val(null).trigger('change')
|
||||||
$("#quickselect .select2-search__field").trigger('input')
|
$("#quickselect .select2-search__field").trigger('input')
|
||||||
modWindowHeight($('.select2-results').height())
|
modWindowHeight($('.select2-results').outerHeight())
|
||||||
},
|
},
|
||||||
|
|
||||||
// 显示文本输入框
|
// 显示文本输入框
|
||||||
|
Loading…
x
Reference in New Issue
Block a user