Path: blob/master/extensions/admin_ui/media/javascript/ux/PagingStore.js
1154 views
//1// Copyright (c) 2006-2025 Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//56/*7* PagingStore for Ext 3.2 - v0.58*/9Ext.ns('Ext.ux.data');10Ext.ux.data.PagingStore = Ext.extend(Ext.data.Store, {11add: function (records) {12records = [].concat(records);13if (records.length < 1) {14return;15}16for (var i = 0, len = records.length; i < len; i++) {17records[i].join(this);18}19var index = this.data.length;20this.data.addAll(records);21// *** add ***22if (this.allData) {23this.allData.addAll(records);24}25// *** end ***26if (this.snapshot) {27this.snapshot.addAll(records);28}29// *** add ***30this.totalLength += records.length;31// *** end ***32this.fireEvent('add', this, records, index);33},34remove: function (record) {35if (Ext.isArray(record)) {36Ext.each(record, function (r) {37this.remove(r);38}, this);39return;40}41// *** add ***42if (this != record.store) {43return;44}45record.join(null);46// *** end ***47var index = this.data.indexOf(record);48if (index > -1) {49// record.join(null);50this.data.removeAt(index);51}52if (this.pruneModifiedRecords) {53this.modified.remove(record);54}55// *** add ***56if (this.allData) {57this.allData.remove(record);58}59// *** end ***60if (this.snapshot) {61this.snapshot.remove(record);62}63// *** add ***64this.totalLength--;65// *** end ***66if (index > -1) {67this.fireEvent('remove', this, record, index);68}69},70removeAll: function (silent) {71// *** add ***72var items = [].concat((this.snapshot || this.allData || this.data).items);73// *** end ***74// var items = [];75// this.each(function (rec) {76// items.push(rec);77// });78this.clearData();79// if (this.snapshot) {80// this.snapshot.clear();81// }82if (this.pruneModifiedRecords) {83this.modified = [];84}85// *** add ***86this.totalLength = 0;87// *** end ***88if (silent !== true) {89this.fireEvent('clear', this, items);90}91},92insert: function (index, records) {93records = [].concat(records);94for (var i = 0, len = records.length; i < len; i++) {95this.data.insert(index, records[i]);96records[i].join(this);97}98// *** add ***99if (this.allData) {100this.allData.addAll(records);101}102// *** end ***103if (this.snapshot) {104this.snapshot.addAll(records);105}106// *** add ***107this.totalLength += records.length;108// *** end ***109this.fireEvent('add', this, records, index);110},111getById: function (id) {112// *** add ***113return (this.snapshot || this.allData || this.data).key(id);114// *** end ***115// return this.data.key(id);116},117clearData: function () {118// *** add ***119if (this.allData) {120this.data = this.allData;121delete this.allData;122}123if (this.snapshot) {124this.data = this.snapshot;125delete this.snapshot;126}127// *** end ***128this.data.each(function (rec) {129rec.join(null);130});131this.data.clear();132},133execute: function (action, rs, options, batch) {134if (!Ext.data.Api.isAction(action)) {135throw new Ext.data.Api.Error('execute', action);136}137options = Ext.applyIf(options || {}, {138params: {}139});140if (batch !== undefined) {141this.addToBatch(batch);142}143var doRequest = true;144if (action === 'read') {145doRequest = this.fireEvent('beforeload', this, options);146Ext.applyIf(options.params, this.baseParams);147}148else {149if (this.writer.listful === true && this.restful !== true) {150rs = (Ext.isArray(rs)) ? rs : [rs];151}152else if (Ext.isArray(rs) && rs.length == 1) {153rs = rs.shift();154}155if ((doRequest = this.fireEvent('beforewrite', this, action, rs, options)) !== false) {156this.writer.apply(options.params, this.baseParams, action, rs);157}158}159if (doRequest !== false) {160if (this.writer && this.proxy.url && !this.proxy.restful && !Ext.data.Api.hasUniqueUrl(this.proxy, action)) {161options.params.xaction = action;162}163// *** add ***164if (action === "read" && this.isPaging(Ext.apply({}, options.params))) {165(function () {166if (this.allData) {167this.data = this.allData;168delete this.allData;169}170this.applyPaging();171this.fireEvent("datachanged", this);172var r = [].concat(this.data.items);173this.fireEvent("load", this, r, options);174if (options.callback) {175options.callback.call(options.scope || this, r, options, true);176}177}).defer(1, this);178return true;179}180// *** end ***181this.proxy.request(Ext.data.Api.actions[action], rs, options.params, this.reader, this.createCallback(action, rs, batch), this, options);182}183return doRequest;184},185loadRecords: function (o, options, success) {186if (this.isDestroyed === true) {187return;188}189if (!o || success === false) {190if (success !== false) {191this.fireEvent('load', this, [], options);192}193if (options.callback) {194options.callback.call(options.scope || this, [], options, false, o);195}196return;197}198var r = o.records,199t = o.totalRecords || r.length;200if (!options || options.add !== true) {201if (this.pruneModifiedRecords) {202this.modified = [];203}204for (var i = 0, len = r.length; i < len; i++) {205r[i].join(this);206}207//if (this.snapshot) {208// this.data = this.snapshot;209// delete this.snapshot;210//}211this.clearData();212this.data.addAll(r);213this.totalLength = t;214this.applySort();215// *** add ***216if (!this.allData) {217this.applyPaging();218}219if (r.length > this.getCount()) {220r = [].concat(this.data.items);221}222// *** end ***223this.fireEvent('datachanged', this);224} else {225this.totalLength = Math.max(t, this.data.length + r.length);226this.add(r);227}228this.fireEvent('load', this, r, options);229if (options.callback) {230options.callback.call(options.scope || this, r, options, true);231}232},233loadData: function (o, append) {234// *** add ***235this.isPaging(Ext.apply({}, this.lastOptions ? this.lastOptions.params : null, this.baseParams));236// *** end ***237var r = this.reader.readRecords(o);238this.loadRecords(r, {239add: append240}, true);241},242getTotalCount: function () {243// *** add ***244if (this.allData) {245return this.allData.getCount();246}247// *** end ***248return this.totalLength || 0;249},250sortData: function () {251var sortInfo = this.hasMultiSort ? this.multiSortInfo : this.sortInfo,252direction = sortInfo.direction || "ASC",253sorters = sortInfo.sorters,254sortFns = [];255if (!this.hasMultiSort) {256sorters = [{257direction: direction,258field: sortInfo.field259}];260}261for (var i = 0, j = sorters.length; i < j; i++) {262sortFns.push(this.createSortFunction(sorters[i].field, sorters[i].direction));263}264if (!sortFns.length) {265return;266}267var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;268var fn = function (r1, r2) {269var result = sortFns[0].call(this, r1, r2);270if (sortFns.length > 1) {271for (var i = 1, j = sortFns.length; i < j; i++) {272result = result || sortFns[i].call(this, r1, r2);273}274}275return directionModifier * result;276};277// *** add ***278if (this.allData) {279this.data = this.allData;280delete this.allData;281}282// *** end ***283this.data.sort(direction, fn);284if (this.snapshot && this.snapshot != this.data) {285this.snapshot.sort(direction, fn);286}287// *** add ***288this.applyPaging();289// *** end ***290},291filterBy: function (fn, scope) {292// *** add ***293this.snapshot = this.snapshot || this.allData || this.data;294// *** end ***295// this.snapshot = this.snapshot || this.data;296this.data = this.queryBy(fn, scope || this);297// *** add ***298this.applyPaging();299// *** end ***300this.fireEvent('datachanged', this);301},302clearFilter: function (suppressEvent) {303if (this.isFiltered()) {304this.data = this.snapshot;305delete this.snapshot;306// *** add ***307delete this.allData;308this.applyPaging();309// *** end ***310if (suppressEvent !== true) {311this.fireEvent('datachanged', this);312}313}314},315isFiltered: function () {316// *** add ***317return !!this.snapshot && this.snapshot != (this.allData || this.data);318// *** end ***319// return !!this.snapshot && this.snapshot != this.data;320},321queryBy: function (fn, scope) {322// *** add ***323var data = this.snapshot || this.allData || this.data;324// *** end ***325// var data = this.snapshot || this.data;326return data.filterBy(fn, scope || this);327},328collect: function (dataIndex, allowNull, bypassFilter) {329// *** add ***330var d = (bypassFilter === true ? this.snapshot || this.allData || this.data : this.data).items;331// *** end ***332// var d = (bypassFilter === true && this.snapshot) ? this.snapshot.items : this.data.items;333var v, sv, r = [],334l = {};335for (var i = 0, len = d.length; i < len; i++) {336v = d[i].data[dataIndex];337sv = String(v);338if ((allowNull || !Ext.isEmpty(v)) && !l[sv]) {339l[sv] = true;340r[r.length] = v;341}342}343return r;344},345findInsertIndex : function(record){346this.suspendEvents();347var data = this.data.clone();348this.data.add(record);349this.applySort();350var index = this.data.indexOf(record);351this.data = data;352// *** add ***353this.totalLength--;354// *** end ***355this.resumeEvents();356return index;357},358// *** add ***359isPaging: function (params) {360var pn = this.paramNames,361start = params[pn.start],362limit = params[pn.limit];363if ((typeof start != 'number') || (typeof limit != 'number')) {364delete this.start;365delete this.limit;366this.lastParams = params;367return false;368}369this.start = start;370this.limit = limit;371delete params[pn.start];372delete params[pn.limit];373var lastParams = this.lastParams;374this.lastParams = params;375if (!this.proxy) {376return true;377}378if (!lastParams) {379return false;380}381for (var param in params) {382if (params.hasOwnProperty(param) && (params[param] !== lastParams[param])) {383return false;384}385}386for (param in lastParams) {387if (lastParams.hasOwnProperty(param) && (params[param] !== lastParams[param])) {388return false;389}390}391return true;392},393applyPaging: function () {394var start = this.start,395limit = this.limit;396if ((typeof start == 'number') && (typeof limit == 'number')) {397var allData = this.data,398data = new Ext.util.MixedCollection(allData.allowFunctions, allData.getKey);399data.items = allData.items.slice(start, start + limit);400data.keys = allData.keys.slice(start, start + limit);401var len = data.length = data.items.length;402var map = {};403for (var i = 0; i < len; i++) {404var item = data.items[i];405map[data.getKey(item)] = item;406}407data.map = map;408this.allData = allData;409this.data = data;410}411}412// *** end ***413});414415Ext.ux.data.PagingDirectStore = Ext.extend(Ext.ux.data.PagingStore, {416constructor: Ext.data.DirectStore.prototype.constructor417});418Ext.reg('pagingdirectstore', Ext.ux.data.PagingDirectStore);419420Ext.ux.data.PagingJsonStore = Ext.extend(Ext.ux.data.PagingStore, {421constructor: Ext.data.JsonStore.prototype.constructor422});423Ext.reg('pagingjsonstore', Ext.ux.data.PagingJsonStore);424425Ext.ux.data.PagingXmlStore = Ext.extend(Ext.ux.data.PagingStore, {426constructor: Ext.data.XmlStore.prototype.constructor427});428Ext.reg('pagingxmlstore', Ext.ux.data.PagingXmlStore);429430Ext.ux.data.PagingArrayStore = Ext.extend(Ext.ux.data.PagingStore, {431constructor: Ext.data.ArrayStore.prototype.constructor,432loadData: function (data, append) {433if (this.expandData === true) {434var r = [];435for (var i = 0, len = data.length; i < len; i++) {436r[r.length] = [data[i]];437}438data = r;439}440Ext.ux.data.PagingArrayStore.superclass.loadData.call(this, data, append);441}442});443Ext.reg('pagingarraystore', Ext.ux.data.PagingArrayStore);444445Ext.ux.data.PagingSimpleStore = Ext.ux.data.PagingArrayStore;446Ext.reg('pagingsimplestore', Ext.ux.data.PagingSimpleStore);447448Ext.ux.data.PagingGroupingStore = Ext.extend(Ext.ux.data.PagingStore, Ext.copyTo({}, Ext.data.GroupingStore.prototype, [449'constructor',450'remoteGroup',451'groupOnSort',452'groupDir',453'clearGrouping',454'groupBy',455'sort',456'applyGroupField',457'applyGrouping',458'getGroupState'459]));460Ext.reg('paginggroupingstore', Ext.ux.data.PagingGroupingStore);461462Ext.ux.PagingToolbar = Ext.extend(Ext.PagingToolbar, {463onLoad: function (store, r, o) {464if (!this.rendered) {465this.dsLoaded = [store, r, o];466return;467}468var p = this.getParams();469this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 0;470this.onChange();471// *** end ***472// var d = this.getPageData(),473// ap = d.activePage,474// ps = d.pages;475// this.afterTextItem.setText(String.format(this.afterPageText, d.pages));476// this.inputItem.setValue(ap);477// this.first.setDisabled(ap == 1);478// this.prev.setDisabled(ap == 1);479// this.next.setDisabled(ap == ps);480// this.last.setDisabled(ap == ps);481// this.refresh.enable();482// this.updateInfo();483// this.fireEvent('change', this, d);484},485onChange: function () {486// *** add ***487var t = this.store.getTotalCount(),488s = this.pageSize;489if (this.cursor >= t) {490this.cursor = Math.ceil((t + 1) / s) * s;491}492// *** end ***493var d = this.getPageData(),494ap = d.activePage,495ps = d.pages;496this.afterTextItem.setText(String.format(this.afterPageText, d.pages));497this.inputItem.setValue(ap);498this.first.setDisabled(ap == 1);499this.prev.setDisabled(ap == 1);500this.next.setDisabled(ap == ps);501this.last.setDisabled(ap == ps);502this.refresh.enable();503this.updateInfo();504this.fireEvent('change', this, d);505},506onClear: function () {507this.cursor = 0;508this.onChange();509},510doRefresh: function () {511// *** add ***512delete this.store.lastParams;513// *** end ***514this.doLoad(this.cursor);515},516bindStore: function (store, initial) {517var doLoad;518if (!initial && this.store) {519if (store !== this.store && this.store.autoDestroy) {520this.store.destroy();521} else {522this.store.un('beforeload', this.beforeLoad, this);523this.store.un('load', this.onLoad, this);524this.store.un('exception', this.onLoadError, this);525// *** add ***526this.store.un('datachanged', this.onChange, this);527this.store.un('add', this.onChange, this);528this.store.un('remove', this.onChange, this);529this.store.un('clear', this.onClear, this);530// *** end ***531}532if (!store) {533this.store = null;534}535}536if (store) {537store = Ext.StoreMgr.lookup(store);538store.on({539scope: this,540beforeload: this.beforeLoad,541load: this.onLoad,542exception: this.onLoadError,543// *** add ***544datachanged: this.onChange,545add: this.onChange,546remove: this.onChange,547clear: this.onClear548// *** end ***549});550doLoad = true;551}552this.store = store;553if (doLoad) {554this.onLoad(store, null, {});555}556}557});558Ext.reg('ux.paging', Ext.ux.PagingToolbar);559560561