This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0

优化组件解析;修复attr指令的一个古老bug;表单组件input增加密码类型;优化md5插件;增加store储存管理模块

old
宇天 2018-06-28 17:41:51 +08:00
parent 37500b2917
commit a56ececfb5
9 changed files with 364 additions and 49 deletions

View File

@ -3784,6 +3784,14 @@ const _Anot = (function() {
return obj return obj
} }
function parseVmValue(vm, key, val) {
if (arguments.length === 2) {
return Function('o', 'return o.' + key)(vm)
} else if (arguments.length === 3) {
Function('o', 'v', 'return o.' + key + ' = v')(vm, val)
}
}
Anot.components = {} Anot.components = {}
Anot.component = function(name, opts) { Anot.component = function(name, opts) {
if (opts) { if (opts) {
@ -3824,12 +3832,12 @@ const _Anot = (function() {
if (props.hasOwnProperty('hostPush')) { if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push') elem.removeAttribute('host-push')
__willpush__ = props.hostPush __willpush__ = props.hostPush
props.hostPush = parentVm[__willpush__] props.hostPush = parseVmValue(parentVm, __willpush__)
} }
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
state.disabled = parentVm[disabledKey] state.disabled = parseVmValue(parentVm, disabledKey)
parentVm.$watch(disabledKey, function(val) { parentVm.$watch(disabledKey, function(val) {
parentVm.$fire('component!' + $id + '!disabled', val) parentVm.$fire('component!' + $id + '!disabled', val)
}) })
@ -3838,7 +3846,7 @@ const _Anot = (function() {
} }
if (props.hasOwnProperty(':loading')) { if (props.hasOwnProperty(':loading')) {
var loadingKey = props[':loading'] var loadingKey = props[':loading']
state.loading = parentVm[loadingKey] state.loading = parseVmValue(parentVm, loadingKey)
parentVm.$watch(loadingKey, function(val) { parentVm.$watch(loadingKey, function(val) {
parentVm.$fire('component!' + $id + '!loading', val) parentVm.$fire('component!' + $id + '!loading', val)
}) })
@ -3848,7 +3856,7 @@ const _Anot = (function() {
// :value可实现双向同步值 // :value可实现双向同步值
if (props.hasOwnProperty(':value')) { if (props.hasOwnProperty(':value')) {
var valueKey = props[':value'] var valueKey = props[':value']
state.value = parentVm[valueKey] state.value = parseVmValue(parentVm, valueKey)
parentVm.$watch(valueKey, function(val) { parentVm.$watch(valueKey, function(val) {
parentVm.$fire('component!' + $id + '!value', val) parentVm.$fire('component!' + $id + '!value', val)
}) })
@ -3857,12 +3865,12 @@ const _Anot = (function() {
? [hooks.watch['value.length']] ? [hooks.watch['value.length']]
: [] : []
hooks.watch['value.length'].push(function(val) { hooks.watch['value.length'].push(function(val) {
parentVm[valueKey] = this.value.$model parseVmValue(parentVm, valueKey, this.value.$model)
}) })
} else { } else {
hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : [] hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : []
hooks.watch.value.push(function(val) { hooks.watch.value.push(function(val) {
parentVm[valueKey] = val parseVmValue(parentVm, valueKey, val)
}) })
} }
@ -3926,7 +3934,7 @@ const _Anot = (function() {
if (__willpush__) { if (__willpush__) {
hideProperty(vmodel, '$push', function(val) { hideProperty(vmodel, '$push', function(val) {
parentVm[__willpush__] = val parseVmValue(parentVm, __willpush__, val)
}) })
} }
@ -4168,7 +4176,8 @@ const _Anot = (function() {
obj[i] = !!obj[i] obj[i] = !!obj[i]
} }
if (obj[i] === false) { if (obj[i] === false) {
return elem.removeAttribute(k) elem.removeAttribute(k)
continue
} }
} }

View File

@ -3799,6 +3799,14 @@
return obj return obj
} }
function parseVmValue(vm, key, val) {
if (arguments.length === 2) {
return Function('o', 'return o.' + key)(vm)
} else if (arguments.length === 3) {
Function('o', 'v', 'return o.' + key + ' = v')(vm, val)
}
}
Anot.components = {} Anot.components = {}
Anot.component = function(name, opts) { Anot.component = function(name, opts) {
if (opts) { if (opts) {
@ -3839,12 +3847,12 @@
if (props.hasOwnProperty('hostPush')) { if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push') elem.removeAttribute('host-push')
__willpush__ = props.hostPush __willpush__ = props.hostPush
props.hostPush = parentVm[__willpush__] props.hostPush = parseVmValue(parentVm, __willpush__)
} }
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
state.disabled = parentVm[disabledKey] state.disabled = parseVmValue(parentVm, disabledKey)
parentVm.$watch(disabledKey, function(val) { parentVm.$watch(disabledKey, function(val) {
parentVm.$fire('component!' + $id + '!disabled', val) parentVm.$fire('component!' + $id + '!disabled', val)
}) })
@ -3853,7 +3861,7 @@
} }
if (props.hasOwnProperty(':loading')) { if (props.hasOwnProperty(':loading')) {
var loadingKey = props[':loading'] var loadingKey = props[':loading']
state.loading = parentVm[loadingKey] state.loading = parseVmValue(parentVm, loadingKey)
parentVm.$watch(loadingKey, function(val) { parentVm.$watch(loadingKey, function(val) {
parentVm.$fire('component!' + $id + '!loading', val) parentVm.$fire('component!' + $id + '!loading', val)
}) })
@ -3863,7 +3871,7 @@
// :value可实现双向同步值 // :value可实现双向同步值
if (props.hasOwnProperty(':value')) { if (props.hasOwnProperty(':value')) {
var valueKey = props[':value'] var valueKey = props[':value']
state.value = parentVm[valueKey] state.value = parseVmValue(parentVm, valueKey)
parentVm.$watch(valueKey, function(val) { parentVm.$watch(valueKey, function(val) {
parentVm.$fire('component!' + $id + '!value', val) parentVm.$fire('component!' + $id + '!value', val)
}) })
@ -3872,12 +3880,12 @@
? [hooks.watch['value.length']] ? [hooks.watch['value.length']]
: [] : []
hooks.watch['value.length'].push(function(val) { hooks.watch['value.length'].push(function(val) {
parentVm[valueKey] = this.value.$model parseVmValue(parentVm, valueKey, this.value.$model)
}) })
} else { } else {
hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : [] hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : []
hooks.watch.value.push(function(val) { hooks.watch.value.push(function(val) {
parentVm[valueKey] = val parseVmValue(parentVm, valueKey, val)
}) })
} }
@ -3941,7 +3949,7 @@
if (__willpush__) { if (__willpush__) {
hideProperty(vmodel, '$push', function(val) { hideProperty(vmodel, '$push', function(val) {
parentVm[__willpush__] = val parseVmValue(parentVm, __willpush__, val)
}) })
} }
@ -4183,7 +4191,8 @@
obj[i] = !!obj[i] obj[i] = !!obj[i]
} }
if (obj[i] === false) { if (obj[i] === false) {
return elem.removeAttribute(k) elem.removeAttribute(k)
continue
} }
} }

View File

@ -3784,6 +3784,14 @@ const _Anot = (function() {
return obj return obj
} }
function parseVmValue(vm, key, val) {
if (arguments.length === 2) {
return Function('o', 'return o.' + key)(vm)
} else if (arguments.length === 3) {
Function('o', 'v', 'return o.' + key + ' = v')(vm, val)
}
}
Anot.components = {} Anot.components = {}
Anot.component = function(name, opts) { Anot.component = function(name, opts) {
if (opts) { if (opts) {
@ -3824,12 +3832,12 @@ const _Anot = (function() {
if (props.hasOwnProperty('hostPush')) { if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push') elem.removeAttribute('host-push')
__willpush__ = props.hostPush __willpush__ = props.hostPush
props.hostPush = parentVm[__willpush__] props.hostPush = parseVmValue(parentVm, __willpush__)
} }
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
state.disabled = parentVm[disabledKey] state.disabled = parseVmValue(parentVm, disabledKey)
parentVm.$watch(disabledKey, function(val) { parentVm.$watch(disabledKey, function(val) {
parentVm.$fire('component!' + $id + '!disabled', val) parentVm.$fire('component!' + $id + '!disabled', val)
}) })
@ -3838,7 +3846,7 @@ const _Anot = (function() {
} }
if (props.hasOwnProperty(':loading')) { if (props.hasOwnProperty(':loading')) {
var loadingKey = props[':loading'] var loadingKey = props[':loading']
state.loading = parentVm[loadingKey] state.loading = parseVmValue(parentVm, loadingKey)
parentVm.$watch(loadingKey, function(val) { parentVm.$watch(loadingKey, function(val) {
parentVm.$fire('component!' + $id + '!loading', val) parentVm.$fire('component!' + $id + '!loading', val)
}) })
@ -3848,7 +3856,7 @@ const _Anot = (function() {
// :value可实现双向同步值 // :value可实现双向同步值
if (props.hasOwnProperty(':value')) { if (props.hasOwnProperty(':value')) {
var valueKey = props[':value'] var valueKey = props[':value']
state.value = parentVm[valueKey] state.value = parseVmValue(parentVm, valueKey)
parentVm.$watch(valueKey, function(val) { parentVm.$watch(valueKey, function(val) {
parentVm.$fire('component!' + $id + '!value', val) parentVm.$fire('component!' + $id + '!value', val)
}) })
@ -3857,12 +3865,12 @@ const _Anot = (function() {
? [hooks.watch['value.length']] ? [hooks.watch['value.length']]
: [] : []
hooks.watch['value.length'].push(function(val) { hooks.watch['value.length'].push(function(val) {
parentVm[valueKey] = this.value.$model parseVmValue(parentVm, valueKey, this.value.$model)
}) })
} else { } else {
hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : [] hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : []
hooks.watch.value.push(function(val) { hooks.watch.value.push(function(val) {
parentVm[valueKey] = val parseVmValue(parentVm, valueKey, val)
}) })
} }
@ -3926,7 +3934,7 @@ const _Anot = (function() {
if (__willpush__) { if (__willpush__) {
hideProperty(vmodel, '$push', function(val) { hideProperty(vmodel, '$push', function(val) {
parentVm[__willpush__] = val parseVmValue(parentVm, __willpush__, val)
}) })
} }
@ -4168,7 +4176,8 @@ const _Anot = (function() {
obj[i] = !!obj[i] obj[i] = !!obj[i]
} }
if (obj[i] === false) { if (obj[i] === false) {
return elem.removeAttribute(k) elem.removeAttribute(k)
continue
} }
} }

View File

@ -3799,6 +3799,14 @@
return obj return obj
} }
function parseVmValue(vm, key, val) {
if (arguments.length === 2) {
return Function('o', 'return o.' + key)(vm)
} else if (arguments.length === 3) {
Function('o', 'v', 'return o.' + key + ' = v')(vm, val)
}
}
Anot.components = {} Anot.components = {}
Anot.component = function(name, opts) { Anot.component = function(name, opts) {
if (opts) { if (opts) {
@ -3839,12 +3847,12 @@
if (props.hasOwnProperty('hostPush')) { if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push') elem.removeAttribute('host-push')
__willpush__ = props.hostPush __willpush__ = props.hostPush
props.hostPush = parentVm[__willpush__] props.hostPush = parseVmValue(parentVm, __willpush__)
} }
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
state.disabled = parentVm[disabledKey] state.disabled = parseVmValue(parentVm, disabledKey)
parentVm.$watch(disabledKey, function(val) { parentVm.$watch(disabledKey, function(val) {
parentVm.$fire('component!' + $id + '!disabled', val) parentVm.$fire('component!' + $id + '!disabled', val)
}) })
@ -3853,7 +3861,7 @@
} }
if (props.hasOwnProperty(':loading')) { if (props.hasOwnProperty(':loading')) {
var loadingKey = props[':loading'] var loadingKey = props[':loading']
state.loading = parentVm[loadingKey] state.loading = parseVmValue(parentVm, loadingKey)
parentVm.$watch(loadingKey, function(val) { parentVm.$watch(loadingKey, function(val) {
parentVm.$fire('component!' + $id + '!loading', val) parentVm.$fire('component!' + $id + '!loading', val)
}) })
@ -3863,7 +3871,7 @@
// :value可实现双向同步值 // :value可实现双向同步值
if (props.hasOwnProperty(':value')) { if (props.hasOwnProperty(':value')) {
var valueKey = props[':value'] var valueKey = props[':value']
state.value = parentVm[valueKey] state.value = parseVmValue(parentVm, valueKey)
parentVm.$watch(valueKey, function(val) { parentVm.$watch(valueKey, function(val) {
parentVm.$fire('component!' + $id + '!value', val) parentVm.$fire('component!' + $id + '!value', val)
}) })
@ -3872,12 +3880,12 @@
? [hooks.watch['value.length']] ? [hooks.watch['value.length']]
: [] : []
hooks.watch['value.length'].push(function(val) { hooks.watch['value.length'].push(function(val) {
parentVm[valueKey] = this.value.$model parseVmValue(parentVm, valueKey, this.value.$model)
}) })
} else { } else {
hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : [] hooks.watch.value = hooks.watch.value ? [hooks.watch.value] : []
hooks.watch.value.push(function(val) { hooks.watch.value.push(function(val) {
parentVm[valueKey] = val parseVmValue(parentVm, valueKey, val)
}) })
} }
@ -3941,7 +3949,7 @@
if (__willpush__) { if (__willpush__) {
hideProperty(vmodel, '$push', function(val) { hideProperty(vmodel, '$push', function(val) {
parentVm[__willpush__] = val parseVmValue(parentVm, __willpush__, val)
}) })
} }
@ -4183,7 +4191,8 @@
obj[i] = !!obj[i] obj[i] = !!obj[i]
} }
if (obj[i] === false) { if (obj[i] === false) {
return elem.removeAttribute(k) elem.removeAttribute(k)
continue
} }
} }

View File

@ -225,16 +225,14 @@ Anot.component('input', {
this.classList.add('icon-' + state.pos) this.classList.add('icon-' + state.pos)
} }
this.setAttribute(':class', '{disabled: disabled, active: active}') this.setAttribute(':class', '{disabled: disabled, active: active}')
this.setAttribute( this.setAttribute(':css', '{width: props.width}')
':css',
'{width: props.width, height: props.height, lineHeight: props.height + "px"}'
)
delete props.disabled delete props.disabled
delete props.color delete props.color
next() next()
}, },
render() { render() {
// console.log(this.props)
let { icon, placeholder } = this.props let { icon, placeholder } = this.props
let holder = ` let holder = `
<span <span
@ -244,7 +242,7 @@ Anot.component('input', {
let input = ` let input = `
<input <input
class="do-input__input" class="do-input__input"
:attr-disabled="disabled" :attr="{disabled: disabled, type: props.type }"
:duplex="value" :duplex="value"
:blur="onBlur" :blur="onBlur"
:focus="onFocus" />` :focus="onFocus" />`
@ -260,8 +258,8 @@ Anot.component('input', {
}, },
skip: ['pos'], skip: ['pos'],
props: { props: {
type: 'text',
width: 180, width: 180,
height: 30,
placeholder: '', placeholder: '',
default: '' default: ''
}, },

View File

@ -92,7 +92,7 @@
/* 特殊类弹层(toast弹层) */ /* 特殊类弹层(toast弹层) */
&.type-toast {min-width:10px;padding:0;background:transparent; &.type-toast {position:fixed;min-width:10px;padding:0;background:transparent;
.layer-content {min-height:40px;height:40px;padding:0} .layer-content {min-height:40px;height:40px;padding:0}

View File

@ -377,6 +377,7 @@ if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) {
*/ */
function toUtf8(str) { function toUtf8(str) {
str += ''
if (/[\u0080-\uFFFF]/.test(str)) { if (/[\u0080-\uFFFF]/.test(str)) {
str = unescape(encodeURIComponent(str)) str = unescape(encodeURIComponent(str))
} }

View File

@ -246,13 +246,13 @@ class MEObject {
} }
getVal() { getVal() {
return this.vm.plainTxt.trim() return this.vm.value.trim()
} }
getHtml() { getHtml() {
return this.vm.__tmp__ return this.vm.__tmp__
} }
setVal(txt) { setVal(txt) {
this.vm.plainTxt = txt || '' this.vm.value = txt || ''
} }
} }
@ -296,7 +296,7 @@ Anot.component('meditor', {
class="editor-body" class="editor-body"
spellcheck="false" spellcheck="false"
:attr="{disabled: disabled}" :attr="{disabled: disabled}"
:duplex="plainTxt"></textarea> :duplex="value"></textarea>
<content <content
ref="preview" ref="preview"
class="md-preview do-marked-theme" class="md-preview do-marked-theme"
@ -342,7 +342,7 @@ Anot.component('meditor', {
} else if (txt) { } else if (txt) {
this.insert(txt) this.insert(txt)
} }
this.plainTxt = this.$refs.editor.value this.value = this.$refs.editor.value
}) })
$editor.bind('scroll', ev => { $editor.bind('scroll', ev => {
@ -355,16 +355,20 @@ Anot.component('meditor', {
if (typeof this.props.created === 'function') { if (typeof this.props.created === 'function') {
this.props.created(new MEObject(this)) this.props.created(new MEObject(this))
} }
this.compile()
if (this.preview) {
this.htmlTxt = this.__tmp__
}
}, },
watch: { watch: {
plainTxt: function(val) { value: function(val) {
this.compile() this.compile()
//只有开启实时预览,才会赋值给htmlTxt //只有开启实时预览,才会赋值给htmlTxt
if (this.preview) { if (this.preview) {
this.htmlTxt = this.__tmp__ this.htmlTxt = this.__tmp__
} }
if (typeof this.props.onUpdate === 'function') { if (typeof this.props.onUpdate === 'function') {
this.props.onUpdate(this.plainTxt, this.__tmp__) this.props.onUpdate(this.value, this.__tmp__)
} }
} }
}, },
@ -374,7 +378,7 @@ Anot.component('meditor', {
fullscreen: false, //是否全屏 fullscreen: false, //是否全屏
preview: true, //是否显示预览 preview: true, //是否显示预览
htmlTxt: '', //用于预览渲染 htmlTxt: '', //用于预览渲染
plainTxt: '', //纯md文本 value: '', //纯md文本
addon // 已有插件 addon // 已有插件
}, },
props: { props: {
@ -383,7 +387,7 @@ Anot.component('meditor', {
onUpdate: Anot.PropsTypes.isFunction(), onUpdate: Anot.PropsTypes.isFunction(),
onFullscreen: Anot.PropsTypes.isFunction() onFullscreen: Anot.PropsTypes.isFunction()
}, },
skip: ['addon', 'insert', 'selection'], skip: ['addon', 'insert', 'selection', '__tmp__'],
methods: { methods: {
// 往文本框中插入内容 // 往文本框中插入内容
insert(val, isSelect) { insert(val, isSelect) {
@ -412,7 +416,7 @@ Anot.component('meditor', {
dom.value += val dom.value += val
dom.focus() dom.focus()
} }
this.plainTxt = dom.value this.value = dom.value
}, },
/** /**
* [selection 获取选中的文本] * [selection 获取选中的文本]
@ -465,7 +469,7 @@ Anot.component('meditor', {
}, },
compile: function() { compile: function() {
let txt = this.plainTxt.trim() let txt = this.value.trim()
if (this.props.safelyCompile) { if (this.props.safelyCompile) {
txt = txt txt = txt

276
src/js/store/index.js Normal file
View File

@ -0,0 +1,276 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2018-06-25 21:39:42
* @version $Id$
*/
const __STORE__ = {}
// 解析and条件
function parse$And(it) {
let result = ''
for (let k in it) {
let tmp = it[k]
switch (Anot.type(tmp)) {
case 'object':
if (tmp.$has) {
result += `it.${k}.indexOf(${JSON.stringify(tmp.$has)}) > -1`
break
}
if (tmp.$in) {
result += `${JSON.stringify(tmp.$in)}.indexOf(it.${k}) > -1`
break
}
if (tmp.$regex) {
result += `${tmp.$regex}.test(it.${k})`
break
}
// 区间解析
if (tmp.$lt || tmp.$lte) {
result += `it.${k} <${tmp.$lte ? '=' : ''} ${tmp.$lt || tmp.$lte}`
if (tmp.$gt || tmp.$gte) {
result += ` && it.${k} >${tmp.$gte ? '=' : ''} ${tmp.$gt ||
tmp.$gte}`
}
break
}
if (tmp.$gt || tmp.$gte) {
result += `it.${k} >${tmp.$gte ? '=' : ''} ${tmp.$gt || tmp.$gte}`
break
}
if (tmp.$eq) {
result += `it.${k} === ${tmp.$eq}`
break
}
default:
result += `it.${k} === ${JSON.stringify(it[k])}`
break
}
result += ' && '
}
result = result.slice(0, -4)
return result
}
// 解析or条件
function parse$Or(arr) {
let result = ''
arr.forEach(it => {
result += '('
result += parse$And(it)
result += ') || '
})
return result.slice(0, -4)
}
class AnotStore {
constructor(name) {
Anot.hideProperty(this, '__name__', name)
Anot.hideProperty(this, '__LAST_QUERY__', '')
Anot.hideProperty(this, '__QUERY_HISTORY__', [])
if (!__STORE__[name]) {
__STORE__[name] = []
__STORE__[`${name}Dict`] = {}
}
}
static collection(name) {
return new this(name)
}
__MAKE_FN__(opt) {
let fnStr = `
let result = [];
let num = 0;
for (let it of arr) {
if(`
if (opt.$or) {
fnStr += parse$Or(opt.$or)
} else {
fnStr += parse$And(opt)
}
fnStr += `){
result.push(it)
num++
if(limit > 0 && num >= limit){
break
}
}
}
return result;`
return Function('arr', 'limit', fnStr)
}
// 清除当前集合的数据及缓存, 默认只清除缓存
clear(force) {
this.__QUERY_HISTORY__ = []
this.__LAST_QUERY__ = ''
if (force) {
__STORE__[this.__name__] = []
__STORE__[`${this.__name__}Dict`] = {}
}
}
// 查询多条记录,返回数组
getAll({ filter, limit = [] }) {
const collection = __STORE__[this.__name__]
let result = []
let forceLimited = false // 强制限制查询结果集
if (!collection || !collection.length) {
return result
}
if (limit.length < 1) {
limit = [0]
}
if (limit.length < 2 && filter) {
forceLimited = true
if (limit[0] > 0) {
limit.unshift(0)
}
}
if (filter) {
let query = JSON.stringify(filter)
if (this.__LAST_QUERY__ === query) {
result = this.__QUERY_HISTORY__.slice.apply(
this.__QUERY_HISTORY__,
limit
)
} else {
let tmpFn = this.__MAKE_FN__(filter)
result = tmpFn(collection, forceLimited ? limit[1] || 0 : 0)
// 非强制限制的查询, 缓存结果集
if (!forceLimited) {
this.__LAST_QUERY__ = query
this.__QUERY_HISTORY__ = result
result = this.__QUERY_HISTORY__.slice.apply(
this.__QUERY_HISTORY__,
limit
)
}
}
} else {
result = collection.slice.apply(collection, limit)
}
return Anot.deepCopy(result)
}
// 查询单条记录, 返回Object对象
get(_id) {
const collectionDict = __STORE__[`${this.__name__}Dict`]
return Anot.deepCopy(collectionDict[_id]) || null
}
// 查询总数
count({ filter }) {
if (filter) {
if (this.__LAST_QUERY__ === JSON.stringify(filter)) {
return this.__QUERY_HISTORY__.length
} else {
return this.getAll({ filter, limit: [0] }).length
}
}
return __STORE__[this.__name__].length
}
__INSERT__(item, primary) {
let collection = __STORE__[this.__name__]
let collectionDict = __STORE__[`${this.__name__}Dict`]
let _id = item[primary || `${this.__name__}_id`]
let tmp = collectionDict[_id]
// 已存在, 则直接更新
if (tmp) {
this.update(_id, item)
} else {
collection.push(item)
collectionDict[_id] = item
}
}
// 插入数据, 可以同时插入多条
insert(items, primary) {
if (!Array.isArray(items)) {
items = [items]
}
items.forEach(item => {
this.__INSERT__(item, primary)
})
this.clear()
}
// 按指定字段排序, 是否字符串排序, 是否逆序
sort(key, locale, desc) {
let fnStr = ''
if (locale && window.Intl) {
fnStr += `
let col = new Intl.Collator('zh')
`
}
if (desc) {
fnStr += 'return arr.sort((b, a) => {'
} else {
fnStr += 'return arr.sort((a, b) => {'
}
fnStr += `
let filter = function(val) {
try {
return val.${key} || ''
} catch (err) {
return ''
}
}
`
if (locale) {
if (window.Intl) {
fnStr += `return col.compare(filter(a), filter(b))`
} else {
fnStr += `return (filter(a) + '').localeCompare(filter(b), 'zh')`
}
} else {
fnStr += `return filter(a) - filter(b)`
}
fnStr += '\n})'
Function('arr', fnStr).call(this, __STORE__[this.__name__])
this.clear()
}
// 更新集合中的数据
update(_id, data) {
let collection = __STORE__[this.__name__]
let collectionDict = __STORE__[`${this.__name__}Dict`]
let tmp = collectionDict[_id]
let idx = collection.indexOf(tmp)
Object.assign(tmp, data)
collection.splice(idx, 1, tmp)
collectionDict[_id] = tmp
}
// 删除集合中单条数据
remove(id) {
let collection = __STORE__[this.__name__]
let collectionDict = __STORE__[`${this.__name__}Dict`]
let tmp = collectionDict[_id]
let idx = collection.indexOf(tmp)
collection.splice(idx, 1)
delete collectionDict[_id]
}
}
Anot.store = window.store = AnotStore
export default AnotStore