1 | |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import {extend} from 'tinper-sparrow/src/extend';
|
8 |
|
9 | import {
|
10 | Events
|
11 | } from './indexEvents';
|
12 |
|
13 | import {
|
14 | copyRow,
|
15 | copyRows
|
16 | } from './copyRow';
|
17 |
|
18 | import {
|
19 | setData,
|
20 | setValue
|
21 | } from './data';
|
22 |
|
23 | import{
|
24 | isEnable,
|
25 | setEnable
|
26 | } from './enable';
|
27 |
|
28 | import{
|
29 | getCurrentRow,
|
30 | getCurrentIndex
|
31 | } from './getCurrent';
|
32 |
|
33 | import {
|
34 | getData,
|
35 | getDataByRule,
|
36 | getRow,
|
37 | getChildRow,
|
38 | getRowByRowId,
|
39 | getRowIndex,
|
40 | getRowsByField,
|
41 | getRowByField,
|
42 | getAllRows,
|
43 | getAllPageRows,
|
44 | getChangedDatas,
|
45 | getChangedRows,
|
46 | getValue,
|
47 | getIndexByRowId,
|
48 | getAllDatas,
|
49 | getRowIdsByIndices
|
50 | } from './getData';
|
51 |
|
52 | import {
|
53 | getFocusRow,
|
54 | getFocusIndex
|
55 | } from './getFocus';
|
56 |
|
57 | import {
|
58 | getMeta,
|
59 | getRowMeta
|
60 | } from './getMeta';
|
61 |
|
62 | import {
|
63 | getPage,
|
64 | getPages
|
65 | } from './getPage';
|
66 |
|
67 | import {
|
68 | getParam
|
69 | } from './getParam';
|
70 |
|
71 | import {
|
72 | getSelectedIndex,
|
73 | getSelectedIndices,
|
74 | getSelectedIndexs,
|
75 | getSelectedDatas,
|
76 | getSelectedRows
|
77 | } from './getSelect';
|
78 |
|
79 | import {
|
80 | getSimpleData
|
81 | } from './getSimpleData';
|
82 |
|
83 | import {
|
84 | setMeta,
|
85 | updateMeta,
|
86 | createField
|
87 | } from './meta';
|
88 |
|
89 | import {
|
90 | setCurrentPage,
|
91 | updatePages,
|
92 | setPages,
|
93 | hasPage,
|
94 | clearCache,
|
95 | cacheCurrentPage,
|
96 | updatePagesSelect,
|
97 | updatePageRows,
|
98 | updatePageSelect,
|
99 | updatePageFocus,
|
100 | updatePageAll
|
101 | } from './page';
|
102 |
|
103 | import {
|
104 | addParam,
|
105 | addParams
|
106 | } from './param';
|
107 |
|
108 | import {
|
109 | refSelectedRows,
|
110 | ref,
|
111 | refMeta,
|
112 | refRowMeta,
|
113 | refEnable,
|
114 | refByRow
|
115 | } from './ref';
|
116 |
|
117 | import {
|
118 | removeRowByRowId,
|
119 | removeRow,
|
120 | removeAllRows,
|
121 | removeRows,
|
122 | clear
|
123 | } from './removeRow';
|
124 |
|
125 | import {
|
126 | setRows,
|
127 | addRow,
|
128 | addRows,
|
129 | insertRow,
|
130 | insertRows,
|
131 | createEmptyRow
|
132 | } from './row';
|
133 |
|
134 | import {
|
135 | updateCurrIndex
|
136 | } from './rowCurrent';
|
137 |
|
138 | import {
|
139 | setRowDelete,
|
140 | setAllRowsDelete,
|
141 | setRowsDelete
|
142 | } from './rowDelete';
|
143 |
|
144 | import {
|
145 | setAllRowsSelect,
|
146 | setRowSelect,
|
147 | setRowsSelect,
|
148 | addRowSelect,
|
149 | addRowsSelect,
|
150 | setAllRowsUnSelect,
|
151 | setRowUnSelect,
|
152 | setRowsUnSelect,
|
153 | toggleAllSelect,
|
154 | updateSelectedIndices
|
155 | } from './rowSelect';
|
156 |
|
157 | import {
|
158 | setRowFocus,
|
159 | setRowUnFocus,
|
160 | updateFocusIndex
|
161 | } from './rowFocus';
|
162 |
|
163 | import {
|
164 | setSimpleData,
|
165 | addSimpleData
|
166 | } from './simpleData';
|
167 |
|
168 | import {
|
169 | isChanged
|
170 | } from './util';
|
171 |
|
172 | import {
|
173 | on,
|
174 | off,
|
175 | one,
|
176 | trigger,
|
177 | triggerReturn,
|
178 | getEvent
|
179 | } from './events';
|
180 |
|
181 | class DataTable{
|
182 |
|
183 | constructor(options){
|
184 |
|
185 |
|
186 | options = options || {};
|
187 | this.id = options['id'];
|
188 | this.strict = options['strict'] || false;
|
189 | this.meta = DataTable.createMetaItems(options['meta']);
|
190 | this.enable = options['enable'] || DataTable.DEFAULTS.enable;
|
191 | this.pageSize = ko.observable(options['pageSize'] || DataTable.DEFAULTS.pageSize)
|
192 | this.pageIndex = ko.observable(options['pageIndex'] || DataTable.DEFAULTS.pageIndex)
|
193 | this.totalPages = ko.observable(options['totalPages'] || DataTable.DEFAULTS.totalPages)
|
194 | this.totalRow = ko.observable()
|
195 | this.pageCache = options['pageCache'] === undefined ? DataTable.DEFAULTS.pageCache : options['pageCache']
|
196 | this.rows = ko.observableArray([])
|
197 | this.selectedIndices = ko.observableArray([])
|
198 | this._oldCurrentIndex = -1;
|
199 | this.focusIndex = ko.observable(-1)
|
200 | this.cachedPages = []
|
201 | this.metaChange = {};
|
202 | this.valueChange = {};
|
203 | this.currentRowChange = ko.observable(1);
|
204 | this.enableChange = ko.observable(1);
|
205 | this.params = options['params'] || {};
|
206 | this.master = options['master'] || '';
|
207 | this.allSelected = ko.observable(false);
|
208 |
|
209 | this.dateNoConvert = options['dateNoConvert'];
|
210 | if (options['root']){
|
211 | this.root = options['root']
|
212 | }else{
|
213 | this.root = this;
|
214 | }
|
215 | if (options['ns']){
|
216 | this.ns = options['ns'];
|
217 | }else{
|
218 | this.ns = '';
|
219 | }
|
220 | this.newCount = 0;
|
221 |
|
222 |
|
223 |
|
224 | }
|
225 | }
|
226 | DataTable.prototype.on = on;
|
227 | DataTable.prototype.off = off;
|
228 | DataTable.prototype.one = one;
|
229 | DataTable.prototype.trigger = trigger;
|
230 | DataTable.prototype.triggerReturn = triggerReturn;
|
231 | DataTable.prototype.getEvent = getEvent;
|
232 |
|
233 | DataTable.prototype.copyRow= copyRow;
|
234 | DataTable.prototype.copyRows= copyRows;
|
235 |
|
236 |
|
237 | DataTable.prototype.setData= setData;
|
238 | DataTable.prototype.setValue= setValue;
|
239 |
|
240 |
|
241 | DataTable.prototype.isEnable= isEnable;
|
242 | DataTable.prototype.setEnable= setEnable;
|
243 |
|
244 |
|
245 | DataTable.prototype.getData= getData;
|
246 | DataTable.prototype.getDataByRule= getDataByRule;
|
247 | DataTable.prototype.getRow= getRow;
|
248 | DataTable.prototype.getChildRow = getChildRow;
|
249 | DataTable.prototype.getRowByRowId= getRowByRowId;
|
250 | DataTable.prototype.getRowIndex= getRowIndex;
|
251 | DataTable.prototype.getRowsByField= getRowsByField;
|
252 | DataTable.prototype.getRowByField= getRowByField;
|
253 | DataTable.prototype.getAllRows= getAllRows;
|
254 | DataTable.prototype.getAllPageRows= getAllPageRows;
|
255 | DataTable.prototype.getChangedDatas= getChangedDatas;
|
256 | DataTable.prototype.getChangedRows= getChangedRows;
|
257 | DataTable.prototype.getValue= getValue;
|
258 | DataTable.prototype.getIndexByRowId= getIndexByRowId;
|
259 | DataTable.prototype.getAllDatas= getAllDatas;
|
260 | DataTable.prototype.getRowIdsByIndices= getRowIdsByIndices;
|
261 |
|
262 |
|
263 | DataTable.prototype.getCurrentRow= getCurrentRow;
|
264 | DataTable.prototype.getCurrentIndex= getCurrentIndex;
|
265 |
|
266 |
|
267 | DataTable.prototype.getFocusRow= getFocusRow;
|
268 | DataTable.prototype.getFocusIndex= getFocusIndex;
|
269 |
|
270 |
|
271 | DataTable.prototype.getMeta= getMeta;
|
272 | DataTable.prototype.getRowMeta= getRowMeta;
|
273 |
|
274 |
|
275 | DataTable.prototype.getPage= getPage;
|
276 | DataTable.prototype.getPages= getPages;
|
277 |
|
278 |
|
279 | DataTable.prototype.getParam= getParam;
|
280 |
|
281 |
|
282 | DataTable.prototype.getSelectedIndex= getSelectedIndex;
|
283 | DataTable.prototype.getSelectedIndices= getSelectedIndices;
|
284 | DataTable.prototype.getSelectedIndexs= getSelectedIndexs;
|
285 | DataTable.prototype.getSelectedDatas= getSelectedDatas;
|
286 | DataTable.prototype.getSelectedRows= getSelectedRows;
|
287 |
|
288 |
|
289 | DataTable.prototype.getSimpleData= getSimpleData;
|
290 |
|
291 |
|
292 | DataTable.prototype.setMeta= setMeta;
|
293 | DataTable.prototype.updateMeta= updateMeta;
|
294 | DataTable.prototype.createField= createField;
|
295 |
|
296 |
|
297 | DataTable.prototype.setCurrentPage= setCurrentPage;
|
298 | DataTable.prototype.updatePages= updatePages;
|
299 | DataTable.prototype.setPages= setPages;
|
300 | DataTable.prototype.hasPage= hasPage;
|
301 | DataTable.prototype.clearCache= clearCache;
|
302 | DataTable.prototype.cacheCurrentPage= cacheCurrentPage;
|
303 | DataTable.prototype.updatePagesSelect = updatePagesSelect;
|
304 | DataTable.prototype.updatePageRows = updatePageRows;
|
305 | DataTable.prototype.updatePageSelect = updatePageSelect;
|
306 | DataTable.prototype.updatePageFocus = updatePageFocus;
|
307 | DataTable.prototype.updatePageAll = updatePageAll;
|
308 |
|
309 |
|
310 |
|
311 | DataTable.prototype.addParam= addParam;
|
312 | DataTable.prototype.addParams= addParams;
|
313 |
|
314 |
|
315 | DataTable.prototype.refSelectedRows= refSelectedRows;
|
316 | DataTable.prototype.ref= ref;
|
317 | DataTable.prototype.refMeta= refMeta;
|
318 | DataTable.prototype.refRowMeta= refRowMeta;
|
319 | DataTable.prototype.refEnable= refEnable;
|
320 | DataTable.prototype.refByRow= refByRow;
|
321 |
|
322 |
|
323 | DataTable.prototype.setRows= setRows;
|
324 | DataTable.prototype.addRow= addRow;
|
325 | DataTable.prototype.addRows= addRows;
|
326 | DataTable.prototype.insertRow= insertRow;
|
327 | DataTable.prototype.insertRows= insertRows;
|
328 | DataTable.prototype.createEmptyRow= createEmptyRow;
|
329 |
|
330 |
|
331 | DataTable.prototype.removeRowByRowId= removeRowByRowId;
|
332 | DataTable.prototype.removeRow= removeRow;
|
333 | DataTable.prototype.removeAllRows= removeAllRows;
|
334 | DataTable.prototype.removeRows= removeRows;
|
335 | DataTable.prototype.clear= clear;
|
336 |
|
337 |
|
338 | DataTable.prototype.updateCurrIndex= updateCurrIndex;
|
339 |
|
340 |
|
341 | DataTable.prototype.setRowDelete= setRowDelete;
|
342 | DataTable.prototype.setAllRowsDelete= setAllRowsDelete;
|
343 | DataTable.prototype.setRowsDelete= setRowsDelete;
|
344 |
|
345 |
|
346 | DataTable.prototype.setRowFocus= setRowFocus;
|
347 | DataTable.prototype.setRowUnFocus= setRowUnFocus;
|
348 | DataTable.prototype.updateFocusIndex= updateFocusIndex;
|
349 |
|
350 |
|
351 | DataTable.prototype.setAllRowsSelect= setAllRowsSelect;
|
352 | DataTable.prototype.setRowSelect= setRowSelect;
|
353 | DataTable.prototype.setRowsSelect= setRowsSelect;
|
354 | DataTable.prototype.addRowSelect= addRowSelect;
|
355 | DataTable.prototype.addRowsSelect= addRowsSelect;
|
356 | DataTable.prototype.setAllRowsUnSelect= setAllRowsUnSelect;
|
357 | DataTable.prototype.setRowUnSelect= setRowUnSelect;
|
358 | DataTable.prototype.setRowsUnSelect= setRowsUnSelect;
|
359 | DataTable.prototype.toggleAllSelect= toggleAllSelect;
|
360 | DataTable.prototype.updateSelectedIndices= updateSelectedIndices;
|
361 |
|
362 |
|
363 | DataTable.prototype.setSimpleData= setSimpleData;
|
364 | DataTable.prototype.addSimpleData= addSimpleData;
|
365 |
|
366 |
|
367 | DataTable.prototype.isChanged= isChanged;
|
368 |
|
369 | DataTable.DEFAULTS = {
|
370 | pageSize: 20,
|
371 | pageIndex: 0,
|
372 | totalPages: 0,
|
373 | pageCache: false,
|
374 | enable: true
|
375 | }
|
376 |
|
377 | DataTable.META_DEFAULTS = {
|
378 | enable: true,
|
379 | required: false,
|
380 | descs: {}
|
381 | }
|
382 |
|
383 |
|
384 | DataTable.ON_ROW_SELECT = 'select'
|
385 | DataTable.ON_ROW_UNSELECT = 'unSelect'
|
386 | DataTable.ON_ROW_ALLSELECT = 'allSelect'
|
387 | DataTable.ON_ROW_ALLUNSELECT = 'allUnselect'
|
388 | DataTable.ON_VALUE_CHANGE = 'valueChange'
|
389 | DataTable.ON_BEFORE_VALUE_CHANGE = 'beforeValueCHange'
|
390 | DataTable.ON_CURRENT_VALUE_CHANGE = 'currentValueChange'
|
391 |
|
392 |
|
393 | DataTable.ON_INSERT = 'insert'
|
394 | DataTable.ON_UPDATE = 'update'
|
395 | DataTable.ON_CURRENT_UPDATE = 'currentUpdate'
|
396 | DataTable.ON_DELETE = 'delete'
|
397 | DataTable.ON_DELETE_ALL = 'deleteAll'
|
398 | DataTable.ON_ROW_FOCUS = 'focus'
|
399 | DataTable.ON_ROW_UNFOCUS = 'unFocus'
|
400 | DataTable.ON_LOAD = 'load'
|
401 | DataTable.ON_ENABLE_CHANGE = 'enableChange'
|
402 | DataTable.ON_META_CHANGE = 'metaChange'
|
403 | DataTable.ON_ROW_META_CHANGE = 'rowMetaChange'
|
404 | DataTable.ON_CURRENT_META_CHANGE = 'currentMetaChange'
|
405 | DataTable.ON_CURRENT_ROW_CHANGE = 'currentRowChange'
|
406 |
|
407 | DataTable.SUBMIT = {
|
408 | current: 'current',
|
409 | focus: 'focus',
|
410 | all: 'all',
|
411 | select: 'select',
|
412 | change: 'change',
|
413 | empty: 'empty',
|
414 | allSelect: 'allSelect',
|
415 | allPages: 'allPages'
|
416 | }
|
417 |
|
418 |
|
419 | DataTable.createMetaItems = function (metas) {
|
420 | var newMetas = {};
|
421 | for (var key in metas) {
|
422 | var meta = metas[key]
|
423 | if (typeof meta == 'string')
|
424 | meta = {}
|
425 | newMetas[key] = extend({}, DataTable.META_DEFAULTS, meta)
|
426 | }
|
427 | return newMetas
|
428 | }
|
429 |
|
430 |
|
431 | export {
|
432 | DataTable
|
433 | }
|