1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const setCurrentPage = function (pageIndex, notCacheCurrentPage) {
|
9 | var nowTotalRow = this.totalRow();
|
10 | if (pageIndex != this.pageIndex() && notCacheCurrentPage != true)
|
11 | this.cacheCurrentPage();
|
12 | this.pageIndex(pageIndex)
|
13 | var cachedPage = this.cachedPages[this.pageIndex()]
|
14 | if (cachedPage) {
|
15 |
|
16 | var selectedIndices = cachedPage.selectedIndices;
|
17 | this.removeAllRows()
|
18 | this.setRows(cachedPage.rows)
|
19 | this.setRowsSelect(selectedIndices)
|
20 | }
|
21 | this.totalRow(nowTotalRow);
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const updatePages = function (pages) {
|
29 | var pageSize = this.pageSize(), pageIndex = 0, page, r, row;
|
30 | var page, index, i, rows, focus, selectIndices, status, j, row, originRow;
|
31 | for (i = 0; i < pages.length; i++) {
|
32 | index = pages[i].index
|
33 | rows = pages[i].rows
|
34 | focus = pages[i].current
|
35 | selectIndices = pages[i].select
|
36 | status = pages[i].status
|
37 | if (status === 'del') {
|
38 | this.cachedPages[index] = null;
|
39 | continue;
|
40 | }
|
41 | if (!this.cachedPages[index]) {
|
42 | page = new Page({parent: this})
|
43 | page.rows = rows;
|
44 | for (var j = 0; j < page.rows.length; j++) {
|
45 | page.rows[j].rowId = page.rows[j].id
|
46 | delete page.rows[j].id
|
47 | }
|
48 | this.cachedPages[index] = page
|
49 | page.selectedIndices = selectIndices;
|
50 | page.focus = focus;
|
51 | } else {
|
52 | page = this.cachedPages[index]
|
53 | page.selectedIndices = selectIndices;
|
54 | page.focus = focus;
|
55 | for (var j = 0; j < rows.length; j++) {
|
56 | r = rows[j];
|
57 | if (!r.id)
|
58 | r.id = Row.getRandomRowId()
|
59 | if (r.status == Row.STATUS.DELETE) {
|
60 |
|
61 | var row = page.getRowByRowId(r.id)
|
62 | if(row){
|
63 |
|
64 | var oldTotalRow = this.totalRow();
|
65 | var newTotalRow = oldTotalRow - 1;
|
66 | this.totalRow(newTotalRow);
|
67 | if(row.status == Row.STATUS.NEW){
|
68 | this.newCount -= 1;
|
69 | if(this.newCount < 0)
|
70 | this.newCount = 0;
|
71 | }
|
72 | }
|
73 | this.removeRowByRowId(r.id)
|
74 | page.removeRowByRowId(r.id)
|
75 |
|
76 | } else {
|
77 | row = page.getRowByRowId(r.id)
|
78 | if (row) {
|
79 | page.updateRow(row, r);
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | if(row.status == Row.STATUS.NEW && r.status != Row.STATUS.NEW){
|
87 | this.newCount -= 1;
|
88 | if(this.newCount < 0)
|
89 | this.newCount = 0;
|
90 | }
|
91 | row.status = Row.STATUS.NORMAL
|
92 | if(r.status == Row.STATUS.NEW){
|
93 | row.status = Row.STATUS.NEW
|
94 | }
|
95 | } else {
|
96 | r.rowId = r.id
|
97 | delete r.id
|
98 | page.rows.push(r);
|
99 | if(r.status != Row.STATUS.NEW){
|
100 | r.status = Row.STATUS.NORMAL;
|
101 | }else{
|
102 | this.newCount += 1;
|
103 | }
|
104 |
|
105 | var oldTotalRow = this.totalRow();
|
106 | var newTotalRow = oldTotalRow + 1;
|
107 | this.totalRow(newTotalRow);
|
108 | }
|
109 | }
|
110 | }
|
111 | }
|
112 |
|
113 | }
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | const setPages = function (allRows) {
|
121 | var pageSize = this.pageSize(), pageIndex = 0, page;
|
122 | this.cachedPages = [];
|
123 | for (var i = 0; i < allRows.length; i++) {
|
124 | pageIndex = Math.floor(i / pageSize);
|
125 | if (!this.cachedPages[pageIndex]) {
|
126 | page = new Page({parent: this})
|
127 | this.cachedPages[pageIndex] = page
|
128 | }
|
129 | page.rows.push(allRows[i])
|
130 | }
|
131 | if (this.pageIndex() > -1)
|
132 | this.setCurrentPage(this.pageIndex());
|
133 | this.totalRow(allRows.length);
|
134 | this.totalPages(pageIndex + 1);
|
135 | }
|
136 |
|
137 | const hasPage = function (pageIndex) {
|
138 | return (this.pageCache && this.cachedPages[pageIndex]) ? true : false
|
139 | }
|
140 |
|
141 | const clearCache = function () {
|
142 | this.cachedPages = []
|
143 | }
|
144 |
|
145 | const cacheCurrentPage = function () {
|
146 | if (this.pageCache && this.pageIndex() > -1) {
|
147 | var page = new Page({parent: this});
|
148 | page.focus = this.getFocusIndex();
|
149 | page.selectedIndices = this.selectedIndices().slice();
|
150 | var rows = this.rows.peek();
|
151 | for (var i = 0; i < rows.length; i++) {
|
152 | var r = rows[i].getData();
|
153 | r.rowId = r.id;
|
154 | delete r.id;
|
155 | page.rows.push(r)
|
156 | }
|
157 | this.cachedPages[this.pageIndex()] = page
|
158 | }
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | const updatePagesSelect = function(){
|
165 | var selectRows = this.getSelectedRows();
|
166 | var pages = this.getPages();
|
167 | for(var i = 0; i < pages.length; i++){
|
168 | var rows = pages[i].rows;
|
169 | var selectedIndices = [];
|
170 | for(var j = 0; j < selectRows.length; j++){
|
171 | var nowSelectRow = selectRows[j]
|
172 | for(var k = 0; k < rows.length; k++){
|
173 | var row = rows[k];
|
174 | if(nowSelectRow == row){
|
175 | selectedIndices.push(k);
|
176 | break;
|
177 | }
|
178 | }
|
179 | }
|
180 | pages[i].selectedIndices = selectedIndices;
|
181 | }
|
182 |
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | const updatePageRows = function(){
|
190 | if(this.pageCache){
|
191 | var pageIndex = this.pageIndex();
|
192 | var page = this.getPages()[pageIndex];
|
193 | if(page){
|
194 | page.rows = this.rows();
|
195 | }
|
196 | }
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | const updatePageSelect = function(){
|
203 | if(this.pageCache){
|
204 | var pageIndex = this.pageIndex();
|
205 | var page = this.getPages()[pageIndex];
|
206 | if(page){
|
207 | var selectedIndices = this.selectedIndices().slice()
|
208 | page.selectedIndices = selectedIndices;
|
209 | }
|
210 | }
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | const updatePageFocus = function(){
|
217 | if(this.pageCache){
|
218 | var pageIndex = this.pageIndex();
|
219 | var page = this.getPages()[pageIndex];
|
220 | if(page){
|
221 | page.focus = this.getFocusIndex();
|
222 | }
|
223 | }
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | const updatePageAll = function(){
|
230 | this.updatePageRows();
|
231 | this.updatePageSelect();
|
232 | this.updatePageFocus();
|
233 | }
|
234 |
|
235 |
|
236 | export {
|
237 | setCurrentPage,
|
238 | updatePages,
|
239 | setPages,
|
240 | hasPage,
|
241 | clearCache,
|
242 | cacheCurrentPage,
|
243 | updatePagesSelect,
|
244 | updatePageRows,
|
245 | updatePageSelect,
|
246 | updatePageFocus,
|
247 | updatePageAll
|
248 | } |
\ | No newline at end of file |