UNPKG

@cw-devops/bk-magic-vue

Version:

基于蓝鲸 Magicbox 和 Vue 的前端组件库

1,416 lines (1,356 loc) 47 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue')) : typeof define === 'function' && define.amd ? define(['exports', 'vue'], factory) : (global = global || self, factory(global.library = {}, global.Vue)); }(this, function (exports, Vue) { 'use strict'; Vue = Vue && Vue.hasOwnProperty('default') ? Vue['default'] : Vue; var _defined = function (it) { if (it == undefined) throw TypeError("Can't call method on " + it); return it; }; var _toObject = function (it) { return Object(_defined(it)); }; var hasOwnProperty = {}.hasOwnProperty; var _has = function (it, key) { return hasOwnProperty.call(it, key); }; var toString = {}.toString; var _cof = function (it) { return toString.call(it).slice(8, -1); }; var _iobject = Object('z').propertyIsEnumerable(0) ? Object : function (it) { return _cof(it) == 'String' ? it.split('') : Object(it); }; var _toIobject = function (it) { return _iobject(_defined(it)); }; var ceil = Math.ceil; var floor = Math.floor; var _toInteger = function (it) { return isNaN(it = +it) ? 0 : (it > 0 ? floor : ceil)(it); }; var min = Math.min; var _toLength = function (it) { return it > 0 ? min(_toInteger(it), 0x1fffffffffffff) : 0; }; var max = Math.max; var min$1 = Math.min; var _toAbsoluteIndex = function (index, length) { index = _toInteger(index); return index < 0 ? max(index + length, 0) : min$1(index, length); }; var _arrayIncludes = function (IS_INCLUDES) { return function ($this, el, fromIndex) { var O = _toIobject($this); var length = _toLength(O.length); var index = _toAbsoluteIndex(fromIndex, length); var value; if (IS_INCLUDES && el != el) while (length > index) { value = O[index++]; if (value != value) return true; } else for (;length > index; index++) if (IS_INCLUDES || index in O) { if (O[index] === el) return IS_INCLUDES || index || 0; } return !IS_INCLUDES && -1; }; }; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var _core = createCommonjsModule(function (module) { var core = module.exports = { version: '2.6.12' }; if (typeof __e == 'number') __e = core; }); var _core_1 = _core.version; var _global = createCommonjsModule(function (module) { var global = module.exports = typeof window != 'undefined' && window.Math == Math ? window : typeof self != 'undefined' && self.Math == Math ? self : Function('return this')(); if (typeof __g == 'number') __g = global; }); var _shared = createCommonjsModule(function (module) { var SHARED = '__core-js_shared__'; var store = _global[SHARED] || (_global[SHARED] = {}); (module.exports = function (key, value) { return store[key] || (store[key] = value !== undefined ? value : {}); })('versions', []).push({ version: _core.version, mode: 'pure' , copyright: '© 2020 Denis Pushkarev (zloirock.ru)' }); }); var id = 0; var px = Math.random(); var _uid = function (key) { return 'Symbol('.concat(key === undefined ? '' : key, ')_', (++id + px).toString(36)); }; var shared = _shared('keys'); var _sharedKey = function (key) { return shared[key] || (shared[key] = _uid(key)); }; var arrayIndexOf = _arrayIncludes(false); var IE_PROTO = _sharedKey('IE_PROTO'); var _objectKeysInternal = function (object, names) { var O = _toIobject(object); var i = 0; var result = []; var key; for (key in O) if (key != IE_PROTO) _has(O, key) && result.push(key); while (names.length > i) if (_has(O, key = names[i++])) { ~arrayIndexOf(result, key) || result.push(key); } return result; }; var _enumBugKeys = ( 'constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf' ).split(','); var _objectKeys = Object.keys || function keys(O) { return _objectKeysInternal(O, _enumBugKeys); }; var _aFunction = function (it) { if (typeof it != 'function') throw TypeError(it + ' is not a function!'); return it; }; var _ctx = function (fn, that, length) { _aFunction(fn); if (that === undefined) return fn; switch (length) { case 1: return function (a) { return fn.call(that, a); }; case 2: return function (a, b) { return fn.call(that, a, b); }; case 3: return function (a, b, c) { return fn.call(that, a, b, c); }; } return function () { return fn.apply(that, arguments); }; }; var _isObject = function (it) { return typeof it === 'object' ? it !== null : typeof it === 'function'; }; var _anObject = function (it) { if (!_isObject(it)) throw TypeError(it + ' is not an object!'); return it; }; var _fails = function (exec) { try { return !!exec(); } catch (e) { return true; } }; var _descriptors = !_fails(function () { return Object.defineProperty({}, 'a', { get: function () { return 7; } }).a != 7; }); var document = _global.document; var is = _isObject(document) && _isObject(document.createElement); var _domCreate = function (it) { return is ? document.createElement(it) : {}; }; var _ie8DomDefine = !_descriptors && !_fails(function () { return Object.defineProperty(_domCreate('div'), 'a', { get: function () { return 7; } }).a != 7; }); var _toPrimitive = function (it, S) { if (!_isObject(it)) return it; var fn, val; if (S && typeof (fn = it.toString) == 'function' && !_isObject(val = fn.call(it))) return val; if (typeof (fn = it.valueOf) == 'function' && !_isObject(val = fn.call(it))) return val; if (!S && typeof (fn = it.toString) == 'function' && !_isObject(val = fn.call(it))) return val; throw TypeError("Can't convert object to primitive value"); }; var dP = Object.defineProperty; var f = _descriptors ? Object.defineProperty : function defineProperty(O, P, Attributes) { _anObject(O); P = _toPrimitive(P, true); _anObject(Attributes); if (_ie8DomDefine) try { return dP(O, P, Attributes); } catch (e) { } if ('get' in Attributes || 'set' in Attributes) throw TypeError('Accessors not supported!'); if ('value' in Attributes) O[P] = Attributes.value; return O; }; var _objectDp = { f: f }; var _propertyDesc = function (bitmap, value) { return { enumerable: !(bitmap & 1), configurable: !(bitmap & 2), writable: !(bitmap & 4), value: value }; }; var _hide = _descriptors ? function (object, key, value) { return _objectDp.f(object, key, _propertyDesc(1, value)); } : function (object, key, value) { object[key] = value; return object; }; var PROTOTYPE = 'prototype'; var $export = function (type, name, source) { var IS_FORCED = type & $export.F; var IS_GLOBAL = type & $export.G; var IS_STATIC = type & $export.S; var IS_PROTO = type & $export.P; var IS_BIND = type & $export.B; var IS_WRAP = type & $export.W; var exports = IS_GLOBAL ? _core : _core[name] || (_core[name] = {}); var expProto = exports[PROTOTYPE]; var target = IS_GLOBAL ? _global : IS_STATIC ? _global[name] : (_global[name] || {})[PROTOTYPE]; var key, own, out; if (IS_GLOBAL) source = name; for (key in source) { own = !IS_FORCED && target && target[key] !== undefined; if (own && _has(exports, key)) continue; out = own ? target[key] : source[key]; exports[key] = IS_GLOBAL && typeof target[key] != 'function' ? source[key] : IS_BIND && own ? _ctx(out, _global) : IS_WRAP && target[key] == out ? (function (C) { var F = function (a, b, c) { if (this instanceof C) { switch (arguments.length) { case 0: return new C(); case 1: return new C(a); case 2: return new C(a, b); } return new C(a, b, c); } return C.apply(this, arguments); }; F[PROTOTYPE] = C[PROTOTYPE]; return F; })(out) : IS_PROTO && typeof out == 'function' ? _ctx(Function.call, out) : out; if (IS_PROTO) { (exports.virtual || (exports.virtual = {}))[key] = out; if (type & $export.R && expProto && !expProto[key]) _hide(expProto, key, out); } } }; $export.F = 1; $export.G = 2; $export.S = 4; $export.P = 8; $export.B = 16; $export.W = 32; $export.U = 64; $export.R = 128; var _export = $export; var _objectSap = function (KEY, exec) { var fn = (_core.Object || {})[KEY] || Object[KEY]; var exp = {}; exp[KEY] = exec(fn); _export(_export.S + _export.F * _fails(function () { fn(1); }), 'Object', exp); }; _objectSap('keys', function () { return function keys(it) { return _objectKeys(_toObject(it)); }; }); var keys = _core.Object.keys; var keys$1 = keys; var _isArray = Array.isArray || function isArray(arg) { return _cof(arg) == 'Array'; }; _export(_export.S, 'Array', { isArray: _isArray }); var isArray = _core.Array.isArray; var isArray$1 = isArray; function merge (target) { for (var i = 1, j = arguments.length; i < j; i++) { var source = arguments[i] || {}; for (var prop in source) { if (source.hasOwnProperty(prop)) { var value = source[prop]; if (value !== undefined) { target[prop] = value; } } } } return target; } var _stringWs = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' + '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF'; var space = '[' + _stringWs + ']'; var non = '\u200b\u0085'; var ltrim = RegExp('^' + space + space + '*'); var rtrim = RegExp(space + space + '*$'); var exporter = function (KEY, exec, ALIAS) { var exp = {}; var FORCE = _fails(function () { return !!_stringWs[KEY]() || non[KEY]() != non; }); var fn = exp[KEY] = FORCE ? exec(trim) : _stringWs[KEY]; if (ALIAS) exp[ALIAS] = fn; _export(_export.P + _export.F * FORCE, 'String', exp); }; var trim = exporter.trim = function (string, TYPE) { string = String(_defined(string)); if (TYPE & 1) string = string.replace(ltrim, ''); if (TYPE & 2) string = string.replace(rtrim, ''); return string; }; var _stringTrim = exporter; var $parseInt = _global.parseInt; var $trim = _stringTrim.trim; var hex = /^[-+]?0[xX]/; var _parseInt = $parseInt(_stringWs + '08') !== 8 || $parseInt(_stringWs + '0x16') !== 22 ? function parseInt(str, radix) { var string = $trim(String(str), 3); return $parseInt(string, (radix >>> 0) || (hex.test(string) ? 16 : 10)); } : $parseInt; _export(_export.G + _export.F * (parseInt != _parseInt), { parseInt: _parseInt }); var _parseInt$1 = _core.parseInt; var $JSON = _core.JSON || (_core.JSON = { stringify: JSON.stringify }); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _toPrimitive$1(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _toPropertyKey(arg) { var key = _toPrimitive$1(arg, "string"); return typeof key === "symbol" ? key : String(key); } var getValueByPath = function getValueByPath(object, prop) { prop = prop || ''; var paths = prop.split('.'); var current = object; var result = null; for (var i = 0, j = paths.length; i < j; i++) { var path = paths[i]; if (!current) break; if (i === j - 1) { result = current[path]; break; } current = current[path]; } return result; }; var arrayFindIndex = function arrayFindIndex(arr, pred) { for (var i = 0; i !== arr.length; ++i) { if (pred(arr[i])) { return i; } } return -1; }; var arrayFind = function arrayFind(arr, pred) { var idx = arrayFindIndex(arr, pred); return idx !== -1 ? arr[idx] : undefined; }; var isObject = function isObject(obj) { return obj !== null && _typeof(obj) === 'object'; }; var orderBy = function orderBy(array, sortKey, reverse, sortMethod, sortBy) { if (!sortKey && !sortMethod && (!sortBy || isArray$1(sortBy)) && !sortBy.length) { return array; } if (typeof reverse === 'string') { reverse = reverse === 'descending' ? -1 : 1; } else { reverse = reverse && reverse < 0 ? -1 : 1; } var getKey = sortMethod ? null : function (value, index) { if (sortBy) { if (!isArray$1(sortBy)) { sortBy = [sortBy]; } return sortBy.map(function (by) { if (typeof by === 'string') { return getValueByPath(value, by); } else { return by(value, index, array); } }); } if (sortKey !== '$key') { if (isObject(value) && '$value' in value) value = value.$value; } return [isObject(value) ? getValueByPath(value, sortKey) : value]; }; var compare = function compare(a, b) { if (sortMethod) { return sortMethod(a.value, b.value); } for (var i = 0, len = a.key.length; i < len; i++) { if (a.key[i] < b.key[i]) { return -1; } if (a.key[i] > b.key[i]) { return 1; } } return 0; }; return array.map(function (value, index) { return { value: value, index: index, key: getKey ? getKey(value, index) : null }; }).sort(function (a, b) { var order = compare(a, b); if (!order) { order = a.index - b.index; } return order * reverse; }).map(function (item) { return item.value; }); }; var getColumnById = function getColumnById(table, columnId) { var column = null; table.columns.forEach(function (item) { if (item.id === columnId) { column = item; } }); return column; }; var getColumnByKey = function getColumnByKey(table, columnKey) { var column = null; for (var i = 0; i < table.columns.length; i++) { var item = table.columns[i]; if (item.columnKey === columnKey) { column = item; break; } } return column; }; var getRowIdentity = function getRowIdentity(row, rowKey) { if (!row) throw new Error('row is required when get row identity'); if (typeof rowKey === 'string') { if (rowKey.indexOf('.') < 0) { return row[rowKey]; } var key = rowKey.split('.'); var current = row; for (var i = 0; i < key.length; i++) { current = current[key[i]]; } return current; } else if (typeof rowKey === 'function') { return rowKey.call(null, row); } }; var getKeysMap = function getKeysMap(array, rowKey) { var arrayMap = {}; (array || []).forEach(function (row, index) { arrayMap[getRowIdentity(row, rowKey)] = { row: row, index: index }; }); return arrayMap; }; function toggleRowStatus(statusArr, row, newVal) { var changed = false; var index = statusArr.indexOf(row); var included = index !== -1; var addRow = function addRow() { statusArr.push(row); changed = true; }; var removeRow = function removeRow() { statusArr.splice(index, 1); changed = true; }; if (typeof newVal === 'boolean') { if (newVal && !included) { addRow(); } else if (!newVal && included) { removeRow(); } } else { if (included) { removeRow(); } else { addRow(); } } return changed; } function walkTreeNode(root, cb) { var childrenKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children'; var lazyKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'hasChildren'; var isNil = function isNil(array) { return !(isArray$1(array) && array.length); }; function _walker(parent, children, level) { cb(parent, children, level); children.forEach(function (item) { if (item[lazyKey]) { cb(item, null, level + 1); return; } var children = item[childrenKey]; if (!isNil(children)) { _walker(item, children, level + 1); } }); } root.forEach(function (item) { if (item[lazyKey]) { cb(item, null, 0); return; } var children = item[childrenKey]; if (!isNil(children)) { _walker(item, children, 0); } }); } var expand = { data: function data() { return { states: { defaultExpandAll: false, expandRows: [] } }; }, methods: { updateExpandRows: function updateExpandRows() { var _this$states = this.states, _this$states$data = _this$states.data, data = _this$states$data === void 0 ? [] : _this$states$data, rowKey = _this$states.rowKey, defaultExpandAll = _this$states.defaultExpandAll, expandRows = _this$states.expandRows; if (defaultExpandAll) { this.states.expandRows = data.slice(); } else if (rowKey) { var expandRowsMap = getKeysMap(expandRows, rowKey); this.states.expandRows = data.reduce(function (prev, row) { var rowId = getRowIdentity(row, rowKey); var rowInfo = expandRowsMap[rowId]; if (rowInfo) { prev.push(row); } return prev; }, []); } else { this.states.expandRows = []; } }, toggleRowExpansion: function toggleRowExpansion(row, expanded) { var changed = toggleRowStatus(this.states.expandRows, row, expanded); if (changed) { this.table.$emit('expand-change', row, this.states.expandRows.slice()); this.scheduleLayout(); } }, setExpandRowKeys: function setExpandRowKeys(rowKeys) { this.assertRowKey(); var _this$states2 = this.states, data = _this$states2.data, rowKey = _this$states2.rowKey; var keysMap = getKeysMap(data, rowKey); this.states.expandRows = rowKeys.reduce(function (prev, cur) { var info = keysMap[cur]; if (info) { prev.push(info.row); } return prev; }, []); }, isRowExpanded: function isRowExpanded(row) { var _this$states3 = this.states, _this$states3$expandR = _this$states3.expandRows, expandRows = _this$states3$expandR === void 0 ? [] : _this$states3$expandR, rowKey = _this$states3.rowKey; if (rowKey) { var expandMap = getKeysMap(expandRows, rowKey); return !!expandMap[getRowIdentity(row, rowKey)]; } return expandRows.indexOf(row) !== -1; } } }; var current = { data: function data() { return { states: { _currentRowKey: null, currentRow: null } }; }, methods: { setCurrentRowKey: function setCurrentRowKey(key) { this.assertRowKey(); this.states._currentRowKey = key; this.setCurrentRowByKey(key); }, restoreCurrentRowKey: function restoreCurrentRowKey() { this.states._currentRowKey = null; }, setCurrentRowByKey: function setCurrentRowByKey(key) { var states = this.states; var _states$data = states.data, data = _states$data === void 0 ? [] : _states$data, rowKey = states.rowKey; var currentRow = null; if (rowKey) { currentRow = arrayFind(data, function (item) { return getRowIdentity(item, rowKey) === key; }); } states.currentRow = currentRow; }, updateCurrentRow: function updateCurrentRow(currentRow) { var states = this.states, table = this.table; var oldCurrentRow = states.currentRow; if (currentRow && currentRow !== oldCurrentRow) { states.currentRow = currentRow; table.$emit('current-change', currentRow, oldCurrentRow); return; } if (!currentRow && oldCurrentRow) { states.currentRow = null; table.$emit('current-change', null, oldCurrentRow); } }, updateCurrentRowData: function updateCurrentRowData() { var states = this.states, table = this.table; var rowKey = states.rowKey, _currentRowKey = states._currentRowKey; var data = states.data || []; var oldCurrentRow = states.currentRow; if (data.indexOf(oldCurrentRow) === -1 && oldCurrentRow) { if (rowKey) { var currentRowKey = getRowIdentity(oldCurrentRow, rowKey); this.setCurrentRowByKey(currentRowKey); } else { states.currentRow = null; } if (states.currentRow === null) { table.$emit('current-change', null, oldCurrentRow); } } else if (_currentRowKey) { this.setCurrentRowByKey(_currentRowKey); this.restoreCurrentRowKey(); } } } }; var tree = { data: function data() { return { states: { expandRowKeys: [], treeData: {}, indent: 16, lazy: false, lazyTreeNodeMap: {}, lazyColumnIdentifier: 'hasChildren', childrenColumnName: 'children' } }; }, computed: { normalizedData: function normalizedData() { if (!this.states.rowKey) return {}; var data = this.states.data || []; return this.normalize(data); }, normalizedLazyNode: function normalizedLazyNode() { var _this$states = this.states, rowKey = _this$states.rowKey, lazyTreeNodeMap = _this$states.lazyTreeNodeMap, lazyColumnIdentifier = _this$states.lazyColumnIdentifier; var keys = keys$1(lazyTreeNodeMap); var res = {}; if (!keys.length) return res; keys.forEach(function (key) { if (lazyTreeNodeMap[key].length) { var item = { children: [] }; lazyTreeNodeMap[key].forEach(function (row) { var currentRowKey = getRowIdentity(row, rowKey); item.children.push(currentRowKey); if (row[lazyColumnIdentifier] && !res[currentRowKey]) { res[currentRowKey] = { children: [] }; } }); res[key] = item; } }); return res; } }, watch: { normalizedData: 'updateTreeData', normalizedLazyNode: 'updateTreeData' }, methods: { normalize: function normalize(data) { var _this$states2 = this.states, childrenColumnName = _this$states2.childrenColumnName, lazyColumnIdentifier = _this$states2.lazyColumnIdentifier, rowKey = _this$states2.rowKey, lazy = _this$states2.lazy; var res = {}; walkTreeNode(data, function (parent, children, level) { var parentId = getRowIdentity(parent, rowKey); if (isArray$1(children)) { res[parentId] = { children: children.map(function (row) { return getRowIdentity(row, rowKey); }), level: level }; } else if (lazy) { res[parentId] = { children: [], lazy: true, level: level }; } }, childrenColumnName, lazyColumnIdentifier); return res; }, updateTreeData: function updateTreeData() { var nested = this.normalizedData; var normalizedLazyNode = this.normalizedLazyNode; var keys = keys$1(nested); var newTreeData = {}; if (keys.length) { var _this$states3 = this.states, oldTreeData = _this$states3.treeData, defaultExpandAll = _this$states3.defaultExpandAll, expandRowKeys = _this$states3.expandRowKeys, lazy = _this$states3.lazy; var rootLazyRowKeys = []; var getExpanded = function getExpanded(oldValue, key) { var included = defaultExpandAll || expandRowKeys && expandRowKeys.indexOf(key) !== -1; return !!(oldValue && oldValue.expanded || included); }; keys.forEach(function (key) { var oldValue = oldTreeData[key]; var newValue = _objectSpread({}, nested[key]); newValue.expanded = getExpanded(oldValue, key); if (newValue.lazy) { var _ref = oldValue || {}, _ref$loaded = _ref.loaded, loaded = _ref$loaded === void 0 ? false : _ref$loaded, _ref$loading = _ref.loading, loading = _ref$loading === void 0 ? false : _ref$loading; newValue.loaded = !!loaded; newValue.loading = !!loading; rootLazyRowKeys.push(key); } newTreeData[key] = newValue; }); var lazyKeys = keys$1(normalizedLazyNode); if (lazy && lazyKeys.length && rootLazyRowKeys.length) { lazyKeys.forEach(function (key) { var oldValue = oldTreeData[key]; var lazyNodeChildren = normalizedLazyNode[key].children; if (rootLazyRowKeys.indexOf(key) !== -1) { if (newTreeData[key].children.length !== 0) { throw new Error('[ElTable]children must be an empty array.'); } newTreeData[key].children = lazyNodeChildren; } else { var _ref2 = oldValue || {}, _ref2$loaded = _ref2.loaded, loaded = _ref2$loaded === void 0 ? false : _ref2$loaded, _ref2$loading = _ref2.loading, loading = _ref2$loading === void 0 ? false : _ref2$loading; newTreeData[key] = { lazy: true, loaded: !!loaded, loading: !!loading, expanded: getExpanded(oldValue, key), children: lazyNodeChildren, level: '' }; } }); } } this.states.treeData = newTreeData; this.updateTableScrollY(); }, updateTreeExpandKeys: function updateTreeExpandKeys(value) { this.states.expandRowKeys = value; this.updateTreeData(); }, toggleTreeExpansion: function toggleTreeExpansion(row, expanded) { this.assertRowKey(); var _this$states4 = this.states, rowKey = _this$states4.rowKey, treeData = _this$states4.treeData; var id = getRowIdentity(row, rowKey); var data = id && treeData[id]; if (id && data && 'expanded' in data) { var oldExpanded = data.expanded; expanded = typeof expanded === 'undefined' ? !data.expanded : expanded; treeData[id].expanded = expanded; if (oldExpanded !== expanded) { this.table.$emit('expand-change', row, expanded); } this.updateTableScrollY(); } }, loadOrToggle: function loadOrToggle(row) { this.assertRowKey(); var _this$states5 = this.states, lazy = _this$states5.lazy, treeData = _this$states5.treeData, rowKey = _this$states5.rowKey; var id = getRowIdentity(row, rowKey); var data = treeData[id]; if (lazy && data && 'loaded' in data && !data.loaded) { this.loadData(row, id, data); } else { this.toggleTreeExpansion(row); } }, loadData: function loadData(row, key, treeNode) { var _this = this; var load = this.table.load; var rawTreeData = this.states.treeData; if (load && !rawTreeData[key].loaded) { rawTreeData[key].loading = true; load(row, treeNode, function (data) { if (!isArray$1(data)) { throw new Error('[ElTable] data must be an array'); } var _this$states6 = _this.states, lazyTreeNodeMap = _this$states6.lazyTreeNodeMap, treeData = _this$states6.treeData; treeData[key].loading = false; treeData[key].loaded = true; treeData[key].expanded = true; if (data.length) { _this.$set(lazyTreeNodeMap, key, data); } _this.table.$emit('expand-change', row, true); }); } } } }; var sortData = function sortData(data, states) { var sortingColumn = states.sortingColumn; if (!sortingColumn || typeof sortingColumn.sortable === 'string') { return data; } return orderBy(data, states.sortProp, states.sortOrder, sortingColumn.sortMethod, sortingColumn.sortBy); }; var doFlattenColumns = function doFlattenColumns(columns) { var result = []; columns.forEach(function (column) { if (column.children) { result.push.apply(result, doFlattenColumns(column.children)); } else { result.push(column); } }); return result; }; var Watcher = Vue.extend({ mixins: [expand, current, tree], data: function data() { return { states: { rowKey: null, data: [], isComplex: false, _columns: [], originColumns: [], columns: [], fixedColumns: [], rightFixedColumns: [], leafColumns: [], fixedLeafColumns: [], rightFixedLeafColumns: [], leafColumnsLength: 0, fixedLeafColumnsLength: 0, rightFixedLeafColumnsLength: 0, isAllSelected: false, selection: [], reserveSelection: false, selectOnIndeterminate: false, selectable: null, filters: {}, filteredData: null, sortingColumn: null, sortProp: null, sortOrder: null, hoverRow: null } }; }, methods: { assertRowKey: function assertRowKey() { var rowKey = this.states.rowKey; if (!rowKey) throw new Error('[ElTable] prop row-key is required'); }, updateColumns: function updateColumns() { var states = this.states; var _columns = states._columns || []; states.fixedColumns = _columns.filter(function (column) { return column.fixed === true || column.fixed === 'left'; }); states.rightFixedColumns = _columns.filter(function (column) { return column.fixed === 'right'; }); if (states.fixedColumns.length > 0 && _columns[0] && _columns[0].type === 'selection' && !_columns[0].fixed) { _columns[0].fixed = true; states.fixedColumns.unshift(_columns[0]); } var notFixedColumns = _columns.filter(function (column) { return !column.fixed; }); states.originColumns = [].concat(states.fixedColumns).concat(notFixedColumns).concat(states.rightFixedColumns); var leafColumns = doFlattenColumns(notFixedColumns); var fixedLeafColumns = doFlattenColumns(states.fixedColumns); var rightFixedLeafColumns = doFlattenColumns(states.rightFixedColumns); states.leafColumnsLength = leafColumns.length; states.fixedLeafColumnsLength = fixedLeafColumns.length; states.rightFixedLeafColumnsLength = rightFixedLeafColumns.length; states.columns = [].concat(fixedLeafColumns).concat(leafColumns).concat(rightFixedLeafColumns); states.isComplex = states.fixedColumns.length > 0 || states.rightFixedColumns.length > 0; }, scheduleLayout: function scheduleLayout(needUpdateColumns) { if (needUpdateColumns) { this.updateColumns(); } this.table.debouncedUpdateLayout(); }, isSelected: function isSelected(row) { var _this$states$selectio = this.states.selection, selection = _this$states$selectio === void 0 ? [] : _this$states$selectio; return selection.indexOf(row) > -1; }, clearSelection: function clearSelection() { var states = this.states; states.isAllSelected = false; var oldSelection = states.selection; if (oldSelection.length) { states.selection = []; this.table.$emit('selection-change', []); } }, cleanSelection: function cleanSelection() { var states = this.states; var data = states.data, rowKey = states.rowKey, selection = states.selection; var deleted; if (rowKey) { deleted = []; var selectedMap = getKeysMap(selection, rowKey); var dataMap = getKeysMap(data, rowKey); for (var key in selectedMap) { if (selectedMap.hasOwnProperty(key) && !dataMap[key]) { deleted.push(selectedMap[key].row); } } } else { deleted = selection.filter(function (item) { return data.indexOf(item) === -1; }); } if (deleted.length) { var newSelection = selection.filter(function (item) { return deleted.indexOf(item) === -1; }); states.selection = newSelection; this.table.$emit('selection-change', newSelection.slice()); } }, toggleRowSelection: function toggleRowSelection(row, selected) { var emitChange = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var changed = toggleRowStatus(this.states.selection, row, selected); if (changed) { var newSelection = (this.states.selection || []).slice(); if (emitChange) { this.table.$emit('select', newSelection, row); } this.table.$emit('selection-change', newSelection); } }, _toggleAllSelection: function _toggleAllSelection() { var states = this.states; var _states$data = states.data, data = _states$data === void 0 ? [] : _states$data, selection = states.selection; var value = states.selectOnIndeterminate ? !states.isAllSelected : !(states.isAllSelected || selection.length); states.isAllSelected = value; var selectionChanged = false; data.forEach(function (row, index) { if (states.selectable) { if (states.selectable.call(null, row, index) && toggleRowStatus(selection, row, value)) { selectionChanged = true; } } else { if (toggleRowStatus(selection, row, value)) { selectionChanged = true; } } }); if (selectionChanged) { this.table.$emit('selection-change', selection ? selection.slice() : []); } this.table.$emit('select-all', selection); }, updateSelectionByRowKey: function updateSelectionByRowKey() { var states = this.states; var selection = states.selection, rowKey = states.rowKey, data = states.data; var selectedMap = getKeysMap(selection, rowKey); data.forEach(function (row) { var rowId = getRowIdentity(row, rowKey); var rowInfo = selectedMap[rowId]; if (rowInfo) { selection[rowInfo.index] = row; } }); }, updateAllSelected: function updateAllSelected() { var states = this.states; var selection = states.selection, rowKey = states.rowKey, selectable = states.selectable; var data = states.data || []; if (data.length === 0) { states.isAllSelected = false; return; } var selectedMap; if (rowKey) { selectedMap = getKeysMap(selection, rowKey); } var isSelected = function isSelected(row) { if (selectedMap) { return !!selectedMap[getRowIdentity(row, rowKey)]; } else { return selection.indexOf(row) !== -1; } }; var isAllSelected = true; var selectedCount = 0; for (var i = 0, j = data.length; i < j; i++) { var item = data[i]; var isRowSelectable = selectable && selectable.call(null, item, i); if (!isSelected(item)) { if (!selectable || isRowSelectable) { isAllSelected = false; break; } } else { selectedCount++; } } if (selectedCount === 0) isAllSelected = false; states.isAllSelected = isAllSelected; }, updateFilters: function updateFilters(columns, values) { if (!isArray$1(columns)) { columns = [columns]; } var states = this.states; var filters = {}; columns.forEach(function (col) { states.filters[col.id] = values; filters[col.columnKey || col.id] = values; }); return filters; }, updateSort: function updateSort(column, prop, order) { if (this.states.sortingColumn && this.states.sortingColumn !== column) { this.states.sortingColumn.order = null; } this.states.sortingColumn = column; this.states.sortProp = prop; this.states.sortOrder = order; }, execFilter: function execFilter() { var _this = this; var states = this.states; var _data = states._data, filters = states.filters; var data = _data; keys$1(filters).forEach(function (columnId) { var values = states.filters[columnId]; if (!values || values.length === 0) return; var column = getColumnById(_this.states, columnId); if (column && column.filterMethod) { data = data.filter(function (row) { return values.some(function (value) { return column.filterMethod.call(null, value, row, column); }); }); } }); states.filteredData = data; }, execSort: function execSort() { var states = this.states; states.data = sortData(states.filteredData, states); }, execQuery: function execQuery(ignore) { if (!(ignore && ignore.filter)) { this.execFilter(); } this.execSort(); }, clearFilter: function clearFilter(columnKeys) { var states = this.states; var _this$table$$refs = this.table.$refs, tableHeader = _this$table$$refs.tableHeader, fixedTableHeader = _this$table$$refs.fixedTableHeader, rightFixedTableHeader = _this$table$$refs.rightFixedTableHeader; var panels = {}; if (tableHeader) panels = merge(panels, tableHeader.filterPanels); if (fixedTableHeader) panels = merge(panels, fixedTableHeader.filterPanels); if (rightFixedTableHeader) panels = merge(panels, rightFixedTableHeader.filterPanels); var keys = keys$1(panels); if (!keys.length) return; if (typeof columnKeys === 'string') { columnKeys = [columnKeys]; } if (isArray$1(columnKeys)) { var columns = columnKeys.map(function (key) { return getColumnByKey(states, key); }); keys.forEach(function (key) { var column = columns.find(function (col) { return col.id === key; }); if (column) { panels[key].filteredValue = []; } }); this.commit('filterChange', { column: columns, values: [], silent: true, multi: true }); } else { keys.forEach(function (key) { panels[key].filteredValue = []; }); states.filters = {}; this.commit('filterChange', { column: {}, values: [], silent: true }); } }, clearSort: function clearSort() { var states = this.states; if (!states.sortingColumn) return; this.updateSort(null, null, null); this.commit('changeSortCondition', { silent: true }); }, setExpandRowKeysAdapter: function setExpandRowKeysAdapter(val) { this.setExpandRowKeys(val); this.updateTreeExpandKeys(val); }, toggleRowExpansionAdapter: function toggleRowExpansionAdapter(row, expanded) { var hasExpandColumn = this.states.columns.some(function (_ref) { var type = _ref.type; return type === 'expand'; }); if (hasExpandColumn) { this.toggleRowExpansion(row, expanded); } else { this.toggleTreeExpansion(row, expanded); } } } }); Watcher.prototype.mutations = { setData: function setData(states, data) { var dataInstanceChanged = states._data !== data; states._data = data; this.execQuery(); this.updateCurrentRowData(); this.updateExpandRows(); if (states.reserveSelection) { this.assertRowKey(); this.updateSelectionByRowKey(); } else { if (dataInstanceChanged) { this.clearSelection(); } else { this.cleanSelection(); } } this.updateAllSelected(); this.updateTableScrollY(); }, insertColumn: function insertColumn(states, column, index, parent) { var array = states._columns; if (parent) { array = parent.children; if (!array) array = parent.children = []; } if (typeof index !== 'undefined') { array.splice(index, 0, column); } else { array.push(column); } if (column.type === 'selection') { states.selectable = column.selectable; states.reserveSelection = column.reserveSelection; } if (this.table.$ready) { this.updateColumns(); this.scheduleLayout(); } }, removeColumn: function removeColumn(states, column, parent) { var array = states._columns; if (parent) { array = parent.children; if (!array) array = parent.children = []; } if (array) { array.splice(array.indexOf(column), 1); } if (this.table.$ready) { this.updateColumns(); this.scheduleLayout(); } }, sort: function sort(states, options) { var prop = options.prop, order = options.order, init = options.init; if (prop) { var column = arrayFind(states.columns, function (column) { return column.property === prop; }); if (column) { column.order = order; this.updateSort(column, prop, order); this.commit('changeSortCondition', { init: init }); } } }, changeSortCondition: function changeSortCondition(states, options) { var column = states.sortingColumn, prop = states.sortProp, order = states.sortOrder; if (order === null) { states.sortingColumn = null; states.sortProp = null; } var ingore = { filter: true }; this.execQuery(ingore); if (!options || !(options.silent || options.init)) { this.table.$emit('sort-change', { column: column, prop: prop, order: order }); } this.updateTableScrollY(); }, filterChange: function filterChange(states, options) { var column = options.column, values = options.values, silent = options.silent; var newFilters = this.updateFilters(column, values); this.execQuery(); if (!silent) { this.table.$emit('filter-change', newFilters); } this.updateTableScrollY(); }, toggleAllSelection: function toggleAllSelection() { this.toggleAllSelection(); }, rowSelectedChanged: function rowSelectedChanged(states, row) { this.toggleRowSelection(row); this.updateAllSelected(); }, setHoverRow: function setHoverRow(states, row) { states.hoverRow = row; }, setCurrentRow: function setCurrentRow(states, row) { this.updateCurrentRow(row); } }; Watcher.prototype.commit = function (name) { var mutations = this.mutations; if (mutations[name]) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } mutations[name].apply(this, [this.states].concat(args)); } else { throw new Error("Action not found: ".concat(name)); } }; Watcher.prototype.updateTableScrollY = function () { Vue.nextTick(this.table.updateScrollY); }; exports.default = Watcher; Object.defineProperty(exports, '__esModule', { value: true }); }));