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 | 7x 311x 311x 7x 311x 311x 311x 311x 311x 311x 236x 311x 311x 75x 75x 75x 236x 236x 236x 236x 236x 236x 236x 7x 169x 127x 127x 46x 28x 28x 28x 4x 24x 225x 46x 46x 179x 65x 31x 169x 169x 137x 137x 137x 199x 199x 6x 199x 13x 186x 186x 311x 186x 186x 6x 6x 6x 6x 830x 186x 186x 1x 32x 4x 4x 4x 32x 32x 19x 13x 13x 13x 13x 13x 830x 830x 199x 199x 193x 126x 126x 126x 126x 126x 126x 126x 94x 94x 32x 32x 32x 32x 32x 17x 17x 17x 32x 9x 9x 32x 32x 32x 31x 31x 31x 31x 3x 3x 31x 18x 18x 18x 18x 18x 13x 3x 3x 1x 10x 10x 10x 10x 13x 2x 2x 2x 11x 130x 130x 110x 11x 11x 28x 169x 169x 169x 2028x 2028x 370x 1658x 169x 169x 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; if (typeof expression !== 'object') expression = ['=', expression]; Eif (Array.isArray(expression)) { if (!isOperator(expression[0])) { // 多选项过滤,暂时简单处理 const sourceValue = get(obj, key); const targetValue = expression; return targetValue.includes(sourceValue); } expression = { operator: expression[0], value: expression[1], }; } let sourceValue = 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!'); }; /** * @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, params: {}, }; }, 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); if (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 return this.arrangedData; }, }, watch: { data() { if (!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: { arrange() { let arrangedData = Array.from(this.data); if(this.isSimpleArray(arrangedData) && this.tag === "u-table-view") { arrangedData = arrangedData.map(item => ({'simple': item})) } if (this.remotePaging) return this.arrangedData = arrangedData; const filtering = this.filtering; if (!this.remoteFiltering && filtering && Object.keys(filtering).length) arrangedData = arrangedData.filter((item) => solveCondition(filtering, item)); 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), ); } } this.arrangedData = arrangedData; if (this.remoteFiltering) { this.originTotal = arrangedData.length; // 有filtering的时候,total需要重新设值 } }, _process(data) { return data; }, clearLocalData() { this.data = []; this.arrangedData = []; this.originTotal = Infinity; // originTotal 必须清空,否则空列表不会更新 this.arranged = false; this.initialLoaded = false; }, mustRemote(offset, newOffset) { return !this.hasAllRemoteData(offset, newOffset) // 没有全部的后端数据 || (this.params.hasOwnProperty('filtering') && this.remoteFiltering) || (this.params.hasOwnProperty('sorting') && this.remoteSorting); }, /** * 根据 viewData,是否还有数据 * @param {Number} offset - 位置 */ hasMore(offset) { Eif (offset === undefined || offset === Infinity) offset = this.offset + this.limit; return offset < this.prependedData.length + this.originTotal; }, /** * 是否还有后端数据 * @param {Number} offset - 位置 */ hasAllRemoteData(offset, newOffset) { Iif (!this.remote) return true; if (!this.remotePaging) return this.data.length >= this.originTotal; offset += this.prependedData.length; newOffset += this.prependedData.length; for (let i = offset; i < newOffset; i++) Eif (!this.data[i]) return false; return true; }, 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); }, isSimpleArray(arr) { Iif (!Array.isArray(arr)) { return false; // 如果不是数组类型,则不满足条件,直接返回 false } return arr.every(function(item) { return typeof item !== 'object'; // 使用 typeof 判断是否为简单数据类型 }); }, // _load(params) load(offset, limit, newPageNumber) { Eif (offset === undefined) offset = this.offset; Eif (limit === undefined) limit = this.limit; let newOffset = offset + limit; const queryChanged = Object.keys(this.params).length; // 调用前端缓存数据 if (!this.remote || this.cache && !this.mustRemote(offset, newOffset)) { // 没有缓存数据或者有新的请求参数时,再尝试重新过滤和排序 Iif (queryChanged) { this.arrange(); this.params = {}; if (this.paging) this.paging.number = 1; offset = 0; newOffset = limit; } return Promise.resolve(this.arrangedData.slice(offset, newOffset)); } // 调用后端数据 // 如果有新的 query 参数的变更,则清除缓存 Iif (queryChanged) { this.clearLocalData(); this.params = {}; if (this.paging) this.paging.number = 1; offset = 0; } 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, sorting: this.sorting, filtering: this.filtering, }, this._getExtraParams()); // 支持 JDL if (this.paging) { params.page = params.paging.number; params.start = params.paging.offset; params.size = params.paging.size; } if (this.sorting && this.sorting.field) { params.sort = params.sorting.field; params.order = params.sorting.order; } const extraParams = this._getExtraParams(); Iif (!this._load || typeof this._load !== 'function') return; return this._load(params, extraParams).then((result) => { this.initialLoaded = true; // 支持 JDL Eif (result instanceof Object) { Iif (result.hasOwnProperty('list') && result.hasOwnProperty('total')) { result.data = result.list; } else if (result.hasOwnProperty('totalElements') && result.hasOwnProperty('content')) { result.total = result.totalElements; result.data = result.content; } } if (!this.remotePaging) { // 没有后端分页,认为是全部数据 if (result instanceof Array) { // 只返回数组,没有 total 字段 Iif (!result.length) this.originTotal = result.length; else this.data = this._process(result); } else Eif (result instanceof Object) { // 返回 { total, data } this.originTotal = result.total; this.data = this._process(result.data); } // 否则什么都不做 this.arrange(); return this.arrangedData.slice(offset, newOffset); } else { let partialData; if (result instanceof Array) { // 只返回数组,没有 total 字段 partialData = this._process(result); if (!result.length) // 没有数据了,则表示最后一次加载,记录下总数 this.originTotal = this.data.length; } else Eif (result instanceof Object) { // 返回 { total: boolean, data: Array<item> } 或 { last: boolean, data: Array<item> } if (result.total !== undefined) this.originTotal = result.total; else Eif (result.last) this.originTotal = this.data.length; partialData = this._process(result.data); } // 否则什么都不做 if (limit === Infinity) { this.data = partialData; this.arrange(); return partialData; } for (let i = 0; i < limit; i++) { const item = partialData[i]; if (item) this.data[offset + i] = item; } this.arrange(); return partialData; } }); }, 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() { if (!this._load || typeof this._load !== 'function') return; this.clearLocalData(); this.load(); }, page(paging) { this.paging = paging; }, sort(sorting) { this.sorting = sorting; this.params.sorting = sorting; }, filter(filtering) { this.filtering = filtering; this.params.filtering = filtering; }, // query(params) { // this.params = params; // return this; // }, prepend(item) { this.data.unshift(item); this.prependedData.unshift(item); this.arrange(); }, add(item) { this.data.push(item); this.arrange(); }, get() { // 获取某一项 }, update() { // 更新某一项 }, remove() { // 删除某一项 }, save() { // 保存 }, }, }); 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; |