Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | 7x 150x 150x 7x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 154x 7x 14x 20x 20x 18x 16x 16x 16x 16x 26x 26x 26x 5x 14x 14x 10x 10x 10x 27x 27x 27x 27x 3x 3x 27x 27x 27x 27x 150x 2x 27x 27x 2x 2x 2x 2x 714x 27x 4x 27x 27x 27x 4x 4x 4x 4x 4x 4x 714x 714x 17x 17x 17x 17x 17x 17x 13x 13x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 1x 1x 1x 4x 4x 4x 4x 70x 70x 25x 4x 4x 17x 3x 3x 3x 6x 6x 2x 6x 6x 6x 3x 3x 6x 6x 6x 3x 3x 1x 3x 3x 3x 3x 14x 14x 14x 170x 170x 32x 138x 14x 14x 7x | import Vue from 'vue'; import get from 'lodash/get'; const isOperator = (value) => { const operators = [ '=', '==', 'eq', '!=', 'neq', '<', 'lt', '<=', 'lte', '>', 'gt', '>=', 'gte', 'includes', 'startsWith', 'endsWith', ]; return typeof value === 'function' || operators.includes(value); }; export const solveCondition = (condition, obj) => { Iif (Array.isArray(condition)) return condition.some((cond) => solveCondition(cond, obj)); else if (typeof condition === 'object') { return Object.keys(condition).every((key) => { let expression = condition[key]; Iif (expression === undefined) return true; Eif (typeof expression !== 'object') expression = ['=', expression]; Eif (Array.isArray(expression)) { Iif (!isOperator(expression[0])) { // 多选项过滤,暂时简单处理 const sourceValue = getType(obj) === 'String' ? obj : get(obj, key); const targetValue = expression; return targetValue.includes(sourceValue); } expression = { operator: expression[0], value: expression[1], }; } let sourceValue = getType(obj) === 'String' ? obj : get(obj, key); let targetValue = expression.value; Iif (expression.caseInsensitive) { sourceValue = typeof sourceValue === 'string' ? sourceValue.toLowerCase() : sourceValue; targetValue = typeof targetValue === 'string' ? targetValue.toLowerCase() : targetValue; } Iif (typeof expression.operator === 'function') return expression.operator(sourceValue, targetValue, expression); else if ( expression.operator === '=' || expression.operator === '==' || expression.operator === 'eq' ) return sourceValue === targetValue; else Eif (expression.operator === '!=' || expression.operator === 'neq') return sourceValue !== targetValue; else if (expression.operator === '<' || expression.operator === 'lt') return sourceValue < targetValue; else if (expression.operator === '<=' || expression.operator === 'lte') return sourceValue <= targetValue; else if (expression.operator === '>' || expression.operator === 'gt') return sourceValue > targetValue; else if (expression.operator === '>=' || expression.operator === 'gte') return sourceValue >= targetValue; else if (expression.operator === 'includes') return String(sourceValue).includes(targetValue); else if (expression.operator === 'startsWith') return String(sourceValue).startsWith(targetValue); else if (expression.operator === 'endsWith') return String(sourceValue).endsWith(targetValue); else throw new TypeError('Unknown operator in conditions!'); }); } else Ethrow new TypeError('Condition must be a Object or Array!'); }; function getType(o) { return Object.prototype.toString.call(o).slice(8, -1); } /** * @example 作为简单的 query * const dataSource = new DataSource(); * dataSource.query({ * paging, * sorting, * filtering, * }).then(); * * @example 作为状态储存 * const dataSource = new DataSource(); * dataSource.filter(); * */ const VueDataSource = Vue.extend({ data() { return { data: [], cache: true, viewMode: 'page', paging: undefined, // @TODO sorting: undefined, // @readonly filtering: undefined, // @readonly // grouping: undefined, remote: false, remotePaging: false, remoteSorting: false, remoteFiltering: false, // remoteGrouping: false, // ------ arrangedData: [], // 整理过的数据,用于缓存过滤和排序行为。比如多次获取分页的话,没有必要重新整理 arranged: false, // 无效 prependedData: [], dirty: false, // 无效 originTotal: Infinity, // @readonly - originTotal 作为很重要的判断有没有加载完所有数据的依据 initialLoaded: false, cleared: false, // 标志是否触发了清除本地数据 通常组件reload会触发 queryChanged: false, treeDisplay: false, }; }, computed: { offset() { return this.paging ? (this.paging.number - 1) * this.paging.size : 0; }, limit() { return this.paging ? this.paging.size : Infinity; }, /** * 当前的总数,过滤后的分页数目 */ total() { return this.originTotal === Infinity ? this.data.length : this.originTotal; }, totalPage() { Iif (!this.paging) return 1; const totalPage = Math.ceil(this.total / this.paging.size); Iif (totalPage === Infinity || totalPage === 0) return 1; else return totalPage; }, viewData() { if (this.paging) { Iif (this.viewMode === 'more') return this.arrangedData.slice(0, this.offset + this.limit); else return this.arrangedData.slice(this.offset, this.offset + this.limit); } else Ereturn this.arrangedData; }, }, watch: { data() { Iif (!this.rawTreeDisplayOnlyForNotice && (!this.remote || !this._load)) { this.arrange(); } }, }, // paging, sorting, filtering 暂不用 watch created() { this.remote = !!this._load; // 传 data 为本地数据模式,此时已知所有数据 if (!this.remote) { this.initialLoaded = true; this.originTotal = this.data.length; this.arrange(); } }, methods: { isSimpleArray(arr) { Iif (!Array.isArray(arr)) { return false; // 如果不是数组类型,则不满足条件,直接返回 false } return arr.every(function(item) { return typeof item !== 'object'; // 使用 typeof 判断是否为简单数据类型 }); }, arrange(data = this.data) { // 树形展示处理一下 if (this.treeDisplay) { data = this.listToTree(data, { valueField: this.treeDisplay.valueField, parentField: this.treeDisplay.parentField, childrenField: this.treeDisplay.childrenField, }); this.originTotal = data.length; } let arrangedData = Array.from(data); Iif(this.isSimpleArray(arrangedData) && this.tag === "u-table-view") { arrangedData = arrangedData.map(item => ({'simple': item})) } const filtering = this.filtering; if (!this.remoteFiltering && filtering && Object.keys(filtering).length) { arrangedData = arrangedData.filter((item) => solveCondition(filtering, item)); // // 前端筛选, 且无后端分页 时重置originTotal !this.remotePaging && (this.originTotal = arrangedData.length); } const sorting = this.sorting; if (!this.remoteSorting && sorting && sorting.field) { const field = sorting.field; const orderSign = sorting.order === 'asc' ? 1 : -1; Iif (sorting.compare) { arrangedData.sort((item1, item2) => sorting.compare(this.$at(item1, field), this.$at(item2, field), orderSign), ); } else { arrangedData.sort((item1, item2) => this.defaultCompare(this.$at(item1, field), this.$at(item2, field), orderSign), ); } } // 重置清除标志 if (this.cleared) { this.cleared = false; } Iif (this.queryChanged) { this.queryChanged = false; } this.arrangedData = arrangedData; return arrangedData; }, _process(data) { return data; }, clearLocalData() { this.data = []; this.arrangedData = []; this.originTotal = Infinity; // originTotal 必须清空,否则空列表不会更新 this.arranged = false; this.initialLoaded = false; this.cleared = true; }, mustRemote() { return ( !this.hasAllRemoteData() // 还有后端数据 || (this.queryChanged && (this.remoteFiltering || this.remoteSorting)) // query有变化 ); }, /** * 根据 viewData,是否还有数据 * @param {Number} offset - 位置 */ hasMore(offset) { if (offset === undefined || offset === Infinity) offset = this.offset + this.limit; return offset < this.prependedData.length + this.originTotal; }, /** * 是否还有后端数据 * @param {Number} offset - 位置 */ hasAllRemoteData() { // 非后端数据 if (!this.remote) return true; if (this.data.length >= this.originTotal) { for (let i = 0; i < this.data.length; i++) { const item = this.data[i]; if (!item) { return false; } } return true; } return false; }, hasChanges() { return false; }, defaultCompare(a, b, sign) { Iif (a === b) return 0; else return a > b ? sign : -sign; }, _getExtraParams() { return undefined; }, slice(offset, newOffset) { return this.arrangedData.slice(offset, newOffset); }, // _load(params) load(offset, limit, newPageNumber) { Eif (offset === undefined) offset = this.offset; Eif (limit === undefined) limit = this.limit; // reload 或 query变化 重置分页 Iif (this.cleared || this.queryChanged) { if (this.paging) { this.paging.number = 1; } offset = 0; } // 首次加载完 if (this.initialLoaded) { // 后端数据已经全部获取,调用前端缓存数据 Eif (!this.remote || !this.mustRemote()) { return Promise.resolve( this.arrange(this.data), ); } } // 调用后端数据 Eif (!this.initialLoaded || this.queryChanged) { this.clearLocalData(); } const paging = Object.assign( { offset: offset - this.prependedData.length, limit: this.limit, }, this.paging, ); Iif (newPageNumber !== undefined) { paging.number = newPageNumber; } const params = Object.assign( { paging: this.remotePaging ? paging : undefined, sorting: this.sorting, filtering: this.filtering, }, this._getExtraParams(), ); // 支持 JDL Eif (this.remotePaging && this.paging) { params.page = params.paging.number; params.start = params.paging.offset; params.size = params.paging.size; } Iif (this.sorting && this.sorting.field) { params.sort = params.sorting.field; params.order = params.sorting.order; } const extraParams = this._getExtraParams(); // fix: 2779855023152128 数据表格数据源为变量时,调用reload会报错 Iif (!this._load || typeof this._load !== 'function') return; return this._load(params, extraParams).then((result) => { this.initialLoaded = true; const finalResult = {}; // 判断是否后端数据 if (getType(result) === 'Object') { if (result.hasOwnProperty('list') && result.hasOwnProperty('total')) { finalResult.data = result.list; finalResult.total = result.total; } else Iif (result.hasOwnProperty('totalElements') && result.hasOwnProperty('content')) { finalResult.data = result.content; finalResult.total = result.totalElements; } else { finalResult.data = result.data; finalResult.total = result.total; } // 非后端数据 Iif (!finalResult.hasOwnProperty('data') || !finalResult.hasOwnProperty('total')) { this.remote = false; } } else Eif (getType(result) === 'Array') { finalResult.data = result; finalResult.total = result.length; this.remote = false; } else { this.remote = false; } Iif (!this.remote) { this.remotePaging = false; this.remoteSorting = false; this.remoteFiltering = false; this.data = finalResult.data; this.originTotal = finalResult.total; } else { // 后端不分页 Iif (!this.remotePaging || limit === Infinity) { this.data = finalResult.data; } else { for (let i = 0; i < limit; i++) { const item = finalResult.data[i]; if (item) { this.data[offset + i] = item; } } } this.originTotal = finalResult.total; } return this.arrange(this.data); }); }, loadMore() { if (!this.hasMore()) return Promise.resolve([]); else { const newPageNumber = this.paging.number + 1; return this.load( this.offset + this.limit, undefined, newPageNumber, ).then(() => (this.paging.number = newPageNumber)); } }, reload() { // fix: 2779855023152128 数据表格数据源为变量时,调用reload会报错 if (!this._load || typeof this._load !== 'function') return; this.clearLocalData(); this.load(); }, page(paging) { this.paging = paging; }, sort(sorting) { this.sorting = sorting; this.queryChanged = true; }, filter(filtering) { this.filtering = filtering; this.queryChanged = true; }, prepend(item) { this.data.unshift(item); this.prependedData.unshift(item); this.arrange(); }, add(item) { this.data.push(item); this.arrange(); }, get() { // 获取某一项 }, update() { // 更新某一项 }, remove() { // 删除某一项 }, save() { // 保存 }, listToTree(data, options) { const { valueField, parentField, childrenField } = options; // Map记录一下 const nodes = {}; // Record<id, { entity }> data.forEach((item) => { // fix 2820102516186880, 删除子节点数据,数据没有清除 const children = this.$at(item, childrenField); if (children) { this.$setAt(item, childrenField, []); } const id = this.$at(item, valueField); Eif (id) { nodes[id] = item; } }); const tree = []; data.forEach((item) => { const parentId = this.$at(item, parentField); const parent = nodes[parentId]; // 没有parentId 或者 parent不存在的不处理 if (!parentId || !parent) { tree.push(item); } else { if (!this.$at(parent, childrenField)) { this.$setAt(parent, childrenField, []); } // fix:2777648120272384 listToTree方法可能多次调用,如果已经存在,不再添加 const children = this.$at(parent, childrenField); const hasInChldren = children.some((child) => child === item); !hasInChldren && this.$at(parent, childrenField).push(item); } }); return tree; }, }, }); function DataSource(options) { const data = {}; const methods = {}; Object.keys(options).forEach((key) => { const option = options[key]; if (typeof option === 'function') methods['_' + key] = option; else data[key] = option; }); // if (options.data) // data.data = methods._process ? methods._process(options.data) : Array.from(options.data); VueDataSource.call(this, { data() { return data; }, methods, }); } DataSource.prototype = Object.create(VueDataSource.prototype); // DataSource.prototype.constructor = DataSource; export default DataSource; |