UNPKG

jpush-ui

Version:

极光大数据前端Angular组件(^6.1.0)

456 lines 15.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var cache_helper_1 = require("./cache.helper"); var DEFAULT_PAGE_SIZE = 10; var PAGE_NUMBER_COUNT_SHOWING = 5; exports.LEAST_FETCHING_PERCENT = 1 / 3; var OPTIONS_TO_UPDATE = [ 'curPage', 'pageSize', 'total', 'pageSizeOpts', 'delAware', 'autoStart', 'doRenewList', ]; var PageData = /** @class */ (function () { function PageData(option) { var _this = this; this.pageSizeOpts = [5, 10, 20, 50]; this.start = 0; this.end = DEFAULT_PAGE_SIZE; // 是否对删除有感知,即当列表有删除时,是否帮用户补充数据 this.delAware = false; this.autoStart = true; this.totalPage = 0; this.hasTotalInited = false; this.pageNos = []; this.allPageNos = []; this.lacks = { start: 0, end: this.end }; this.list = []; this.doRenewList = false; this.cache = new cache_helper_1.PageCache(); this._pageSize = DEFAULT_PAGE_SIZE; this._curPage = 0; this._total = 0; this.leastFetch = Math.ceil(exports.LEAST_FETCHING_PERCENT * this._pageSize); this.deleting = []; if (option) { OPTIONS_TO_UPDATE.forEach(function (opt) { if (option[opt] !== undefined) { _this[opt] = option[opt]; } }); } this.detectChanges(); } Object.defineProperty(PageData.prototype, "pageSize", { get: function () { return this._pageSize; }, set: function (pageSize) { if (this._pageSize === pageSize) { return; } this.start = 0; this._pageSize = pageSize; this.end = this.checkEnd(this._pageSize); if (this.updatePageCount()) { this.updatePageNos(); } this.lacks = this.cache.checkCache(this.start, this.end, this.leastFetch); if (this.pageSizeChange) { this.pageSizeChange.emit(pageSize); } this.updateList(); // 这里有detectChanges }, enumerable: true, configurable: true }); Object.defineProperty(PageData.prototype, "curPage", { get: function () { return this._curPage; }, set: function (curPage) { if (this._curPage === curPage) { return; } this.start = curPage * this._pageSize; this.end = this.checkEnd(this.start + this._pageSize); this._curPage = curPage; if (this.pageChange) { this.pageChange.emit(curPage); } this.lacks = this.cache.checkCache(this.start, this.end, this.leastFetch); this.listChanged(); this.updatePageNos(); }, enumerable: true, configurable: true }); Object.defineProperty(PageData.prototype, "total", { get: function () { return this._total; }, set: function (total) { if (this._total === total) { return; } this.hasTotalInited = true; this._total = total; if (!this.end) { this.end = this._pageSize; } if (this.end > total) { this.end = total; } this.cache.setTotal(total); if (this.updatePageCount()) { this.updatePageNos(); // 自带detectChanges } else { this.detectChanges(); } }, enumerable: true, configurable: true }); PageData.prototype.init = function () { if (this.autoStart) { this.listChanged(); } }; PageData.prototype.goto = function (pageNo) { this.curPage = pageNo - 1; }; PageData.prototype.goFirstPage = function () { if (this._curPage === 0) { return; } this.curPage = 0; this.updatePageNos(); }; PageData.prototype.goLastPage = function () { if (this.totalPage <= 0) { return; } var lastPage = this.totalPage - 1; if (this._curPage === lastPage) { return; } this.curPage = lastPage; this.updatePageNos(); }; PageData.prototype.goPrevPage = function () { if (this._curPage > 0) { this.curPage--; } }; PageData.prototype.goNextPage = function () { if (this._total <= 0) { return; } if (!this.isLastPage()) { this.curPage++; } }; PageData.prototype.listChanged = function () { if (this.listChange) { var data = this.getList(); this.listChange.emit({ lacks: this.lacks, data: data, }); } // 当this.lacks为空时,此时不需要网络请求 if (!this.lacks && this.cache.hasData) { // 需要使用内置数据容器时触发 this.updateList(); this.detectChanges(); } }; PageData.prototype.isLastPage = function () { return !this.totalPage || this._curPage === this.totalPage - 1; }; // 更新当前可见的页面(PAGE_NUMBER_COUNT_SHOWING 个) // 更新当前可见的页面(PAGE_NUMBER_COUNT_SHOWING 个) PageData.prototype.updatePageNos = // 更新当前可见的页面(PAGE_NUMBER_COUNT_SHOWING 个) function (noTrigger) { var pages = []; var start = this._curPage - 1; // start数字从1开始 start = start > 0 ? start : 1; var end = start + PAGE_NUMBER_COUNT_SHOWING; // 不包含end if (end > this.totalPage + 1) { var diff = end - this.totalPage; end = this.totalPage + 1; // 因为end不包含 start -= diff; if (start <= 0) { start = 1; } } for (var index = start; index < end; index++) { pages.push(index); } this.pageNos = pages; this.detectChanges(); }; PageData.prototype.setChangeDetector = function (changeDetectionRef) { this.changeDetectionRef = changeDetectionRef; }; PageData.prototype.setPageChanger = function (pageChange) { this.pageChange = pageChange; }; PageData.prototype.setPageSizeChanger = function (pageSizeChange) { this.pageSizeChange = pageSizeChange; }; PageData.prototype.setListChanger = function (listChange) { this.listChange = listChange; }; PageData.prototype.hasData = function () { return this.cache.hasData; }; PageData.prototype.addToList = function (rows, start) { if (!start) { start = 0; } var isFirstAdd = false; // 是不是第一次添加 var list = this.cache.getList(); if (!list || list.length <= 0) { isFirstAdd = true; if (this._total === 0 && this.end === 0) { this.end = this._pageSize; // 临时设置,让list至少有数据 } } this.cache.addToList(rows, start); if (!isFirstAdd && (this.end <= start || this.start >= start + rows.length)) { // 当前页没受影响 } else { this.realEnd = this.end; this.updateList(); } }; PageData.prototype.prepend = function (arr) { if (!arr || arr.length <= 0) { return; } this.cache.prepend(arr); var isFirstPage = this._curPage === 0; this.total = this._total + arr.length; // 如果需要填补数据 if (this.cache.hasData && this.delAware) { if (isFirstPage) { // 在第一页才更新 // 如果第一页没有填满 if (this.end % this._pageSize !== 0) { this.end = this._pageSize; } this.updateList(); } } }; PageData.prototype.append = function (arr) { if (!arr || arr.length <= 0) { return; } this.cache.append(arr); var isEmpty = !this._total; var isLastPage = this.isLastPage(); this.total = this._total + arr.length; // 如果需要填补数据 if (this.cache.hasData && this.delAware) { if (isLastPage) { // 在最后一页 // 如果最后一页没有填满 if (this.end % this._pageSize !== 0 || isEmpty) { this.end = this.start + this._pageSize; this.updateList(); } } } }; PageData.prototype.delFromList = function (opt) { // console.log('deleting...', this.end); // 如果用到了组件的缓存机制 if (this.cache.hasData) { // console.log('deleting', opt); var result = this.cache.delFromList(opt); var index = result.index; if (index < 0) { return; // 没删除成功 } // this.updatePageCount(); var delData = result.data; var delCnt = result.data.length; this._total -= delCnt; if (this.delAware) { // 需要尽量帮用户维持一整页满数据 var end = this.end; // let start = this.start; if (delData && delData.length > 0) { end -= delCnt; // end要往前减 if (!this.realEnd) { this.realEnd = this.end - delCnt; } else { this.realEnd -= delCnt; } // this.updateList(); } this.deleting.push(index); var item = void 0; var list = this.cache.getList(); if (this.isLastPage()) { // 最后一页,直接删除 this.end -= delCnt; console.log('end: ', this.end); if (this.list.length === 0) { // 最后一条 this.goPrevPage(); } this.updateList(); // this.detectChanges(); } else { // 不是最后一页,则往后取 item = list[end]; // end不用再加1 if (!this.cache.isExist(end)) { // console.log(end, this.end, this.realEnd); // this.realEnd = end; var start = end - 1; if (end !== this.realEnd // 用户有快删的行为 || end === this._total) { // 或者本次是最后一次加载了 // console.log('用户有暴力倾向'); // 暴力从头取,而且依然多取一页,让用户随便删 start = this.start; } // 多取一页,节省请求 end = this.checkEnd(this.end + this._pageSize); // 后面再没请求了,直接全部重取, // 这是为了避免快速删除的情况下,后面再没请求修复数据 if (end === this._total) { start = this.start; } this.lacks = { start: start, end: end }; // console.log(this.lacks); // this.end -= delCnt; this.listChanged(); // 需要加载,但暂不需要更新list } else { this.updateList(); this.detectChanges(); } } } else { // 不需要帮用户补充数据 if (delData && delData.length > 0) { this.end -= delCnt; // end要往前减 this.updateList(); } // 如果删除的这条是当前页中的最后一条,则要切换页面 if (this.list.length === 0) { // 最后一条 if (this.isLastPage()) { // 最后一页就往前进 // this.curPage --; this.goPrevPage(); } else { // 否则就往下一页 this.end = this.checkEnd(this.start + this._pageSize); this.lacks = this.cache.checkCache(this.start, this.end, this.leastFetch); // console.log(this.lacks); this.listChanged(); } } } // 每次都检查 if (this.updatePageCount()) { this.updatePageNos(); } // this.updateList(); } }; // 获取特定的范围的数据,这里不校验是否为空 // 获取特定的范围的数据,这里不校验是否为空 PageData.prototype.getList = // 获取特定的范围的数据,这里不校验是否为空 function (startEnd) { // 没指定则默认当前页 if (!startEnd) { startEnd = { start: this.start, end: this.end, }; } return this.cache.getListOf(startEnd); }; PageData.prototype.reset = function () { this._curPage = this._total = 0; this.start = this.end = 0; this.totalPage = 0; this.hasTotalInited = false; this.allPageNos = []; this.pageNos = []; this.list = []; this.lacks = null; this.cache.reset(); }; PageData.prototype.updateList = function () { var list = this.getList(); if (this.doRenewList) { this.list = list; } else { var len = this.list.length; (_a = this.list).splice.apply(_a, [0, len].concat(list)); } this.detectChanges(); var _a; }; // 更新总页数,及页码选择框的数据 // 更新总页数,及页码选择框的数据 PageData.prototype.updatePageCount = // 更新总页数,及页码选择框的数据 function () { var totalPage = Math.ceil(1.0 * this._total / this._pageSize); if (totalPage === this.totalPage) { return false; } this.totalPage = totalPage; var pages = []; for (var index = 1; index <= this.totalPage; index++) { pages.push(index); } this.allPageNos = pages; return true; }; // 检查end是否溢出 // 检查end是否溢出 PageData.prototype.checkEnd = // 检查end是否溢出 function (end) { if (this._total <= 0) { return end; } if (end > this._total) { return this._total; } return end; }; PageData.prototype.detectChanges = function () { if (this.changeDetectionRef) { this.changeDetectionRef.detectChanges(); } }; return PageData; }()); exports.PageData = PageData; //# sourceMappingURL=pager.model.js.map