UNPKG

10.8 kBJavaScriptView Raw
1 /**
2 * Module : Kero webpack entry dataTable index
3 * Author : liuyk(liuyuekai@yonyou.com)
4 * Date : 2016-08-09 15:24:46
5 */
6
7 import {extend} from 'tinper-sparrow/src/extend';
8
9import {
10 Events
11} from './indexEvents';
12
13import {
14 copyRow,
15 copyRows
16} from './copyRow';
17
18import {
19 setData,
20 setValue
21} from './data';
22
23import{
24 isEnable,
25 setEnable
26} from './enable';
27
28import{
29 getCurrentRow,
30 getCurrentIndex
31} from './getCurrent';
32
33import {
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
52import {
53 getFocusRow,
54 getFocusIndex
55} from './getFocus';
56
57import {
58 getMeta,
59 getRowMeta
60} from './getMeta';
61
62import {
63 getPage,
64 getPages
65} from './getPage';
66
67import {
68 getParam
69} from './getParam';
70
71import {
72 getSelectedIndex,
73 getSelectedIndices,
74 getSelectedIndexs,
75 getSelectedDatas,
76 getSelectedRows
77} from './getSelect';
78
79import {
80 getSimpleData
81} from './getSimpleData';
82
83import {
84 setMeta,
85 updateMeta,
86 createField
87} from './meta';
88
89import {
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
103import {
104 addParam,
105 addParams
106} from './param';
107
108import {
109 refSelectedRows,
110 ref,
111 refMeta,
112 refRowMeta,
113 refEnable,
114 refByRow
115} from './ref';
116
117import {
118 removeRowByRowId,
119 removeRow,
120 removeAllRows,
121 removeRows,
122 clear
123} from './removeRow';
124
125import {
126 setRows,
127 addRow,
128 addRows,
129 insertRow,
130 insertRows,
131 createEmptyRow
132} from './row';
133
134import {
135 updateCurrIndex
136} from './rowCurrent';
137
138import {
139 setRowDelete,
140 setAllRowsDelete,
141 setRowsDelete
142} from './rowDelete';
143
144import {
145 setAllRowsSelect,
146 setRowSelect,
147 setRowsSelect,
148 addRowSelect,
149 addRowsSelect,
150 setAllRowsUnSelect,
151 setRowUnSelect,
152 setRowsUnSelect,
153 toggleAllSelect,
154 updateSelectedIndices
155} from './rowSelect';
156
157import {
158 setRowFocus,
159 setRowUnFocus,
160 updateFocusIndex
161} from './rowFocus';
162
163import {
164 setSimpleData,
165 addSimpleData
166} from './simpleData';
167
168import {
169 isChanged
170} from './util';
171
172import {
173 on,
174 off,
175 one,
176 trigger,
177 triggerReturn,
178 getEvent
179} from './events';
180
181class DataTable{
182// class DataTable extends Events{
183 constructor(options){
184 // IE9下转化之后的代码有问题,无法获得superClass方法
185 // super();
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 = {};//ko.observable(1);
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 //dateNoconvert:true时,时间不转化,按真实走,false是,时间转换成long型
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}
226DataTable.prototype.on = on;
227DataTable.prototype.off = off;
228DataTable.prototype.one = one;
229DataTable.prototype.trigger = trigger;
230DataTable.prototype.triggerReturn = triggerReturn;
231DataTable.prototype.getEvent = getEvent;
232//copyRow
233DataTable.prototype.copyRow= copyRow;
234DataTable.prototype.copyRows= copyRows;
235
236//data
237DataTable.prototype.setData= setData;
238DataTable.prototype.setValue= setValue;
239
240//enable
241DataTable.prototype.isEnable= isEnable;
242DataTable.prototype.setEnable= setEnable;
243
244//getData
245DataTable.prototype.getData= getData;
246DataTable.prototype.getDataByRule= getDataByRule;
247DataTable.prototype.getRow= getRow;
248DataTable.prototype.getChildRow = getChildRow;
249DataTable.prototype.getRowByRowId= getRowByRowId;
250DataTable.prototype.getRowIndex= getRowIndex;
251DataTable.prototype.getRowsByField= getRowsByField;
252DataTable.prototype.getRowByField= getRowByField;
253DataTable.prototype.getAllRows= getAllRows;
254DataTable.prototype.getAllPageRows= getAllPageRows;
255DataTable.prototype.getChangedDatas= getChangedDatas;
256DataTable.prototype.getChangedRows= getChangedRows;
257DataTable.prototype.getValue= getValue;
258DataTable.prototype.getIndexByRowId= getIndexByRowId;
259DataTable.prototype.getAllDatas= getAllDatas;
260DataTable.prototype.getRowIdsByIndices= getRowIdsByIndices;
261
262//getCurrent
263DataTable.prototype.getCurrentRow= getCurrentRow;
264DataTable.prototype.getCurrentIndex= getCurrentIndex;
265
266//getFocus
267DataTable.prototype.getFocusRow= getFocusRow;
268DataTable.prototype.getFocusIndex= getFocusIndex;
269
270//getMeta
271DataTable.prototype.getMeta= getMeta;
272DataTable.prototype.getRowMeta= getRowMeta;
273
274//getPage
275DataTable.prototype.getPage= getPage;
276DataTable.prototype.getPages= getPages;
277
278//getParam
279DataTable.prototype.getParam= getParam;
280
281//getSelect
282DataTable.prototype.getSelectedIndex= getSelectedIndex;
283DataTable.prototype.getSelectedIndices= getSelectedIndices;
284DataTable.prototype.getSelectedIndexs= getSelectedIndexs;
285DataTable.prototype.getSelectedDatas= getSelectedDatas;
286DataTable.prototype.getSelectedRows= getSelectedRows;
287
288//getSimpleData
289DataTable.prototype.getSimpleData= getSimpleData;
290
291//meta
292DataTable.prototype.setMeta= setMeta;
293DataTable.prototype.updateMeta= updateMeta;
294DataTable.prototype.createField= createField;
295
296//page
297DataTable.prototype.setCurrentPage= setCurrentPage;
298DataTable.prototype.updatePages= updatePages;
299DataTable.prototype.setPages= setPages;
300DataTable.prototype.hasPage= hasPage;
301DataTable.prototype.clearCache= clearCache;
302DataTable.prototype.cacheCurrentPage= cacheCurrentPage;
303DataTable.prototype.updatePagesSelect = updatePagesSelect;
304DataTable.prototype.updatePageRows = updatePageRows;
305DataTable.prototype.updatePageSelect = updatePageSelect;
306DataTable.prototype.updatePageFocus = updatePageFocus;
307DataTable.prototype.updatePageAll = updatePageAll;
308
309
310//param
311DataTable.prototype.addParam= addParam;
312DataTable.prototype.addParams= addParams;
313
314//ref
315DataTable.prototype.refSelectedRows= refSelectedRows;
316DataTable.prototype.ref= ref;
317DataTable.prototype.refMeta= refMeta;
318DataTable.prototype.refRowMeta= refRowMeta;
319DataTable.prototype.refEnable= refEnable;
320DataTable.prototype.refByRow= refByRow;
321
322//row
323DataTable.prototype.setRows= setRows;
324DataTable.prototype.addRow= addRow;
325DataTable.prototype.addRows= addRows;
326DataTable.prototype.insertRow= insertRow;
327DataTable.prototype.insertRows= insertRows;
328DataTable.prototype.createEmptyRow= createEmptyRow;
329
330//removeRow
331DataTable.prototype.removeRowByRowId= removeRowByRowId;
332DataTable.prototype.removeRow= removeRow;
333DataTable.prototype.removeAllRows= removeAllRows;
334DataTable.prototype.removeRows= removeRows;
335DataTable.prototype.clear= clear;
336
337//rowCurrent
338DataTable.prototype.updateCurrIndex= updateCurrIndex;
339
340//rowDelete
341DataTable.prototype.setRowDelete= setRowDelete;
342DataTable.prototype.setAllRowsDelete= setAllRowsDelete;
343DataTable.prototype.setRowsDelete= setRowsDelete;
344
345//rowFocus
346DataTable.prototype.setRowFocus= setRowFocus;
347DataTable.prototype.setRowUnFocus= setRowUnFocus;
348DataTable.prototype.updateFocusIndex= updateFocusIndex;
349
350//rowSelect
351DataTable.prototype.setAllRowsSelect= setAllRowsSelect;
352DataTable.prototype.setRowSelect= setRowSelect;
353DataTable.prototype.setRowsSelect= setRowsSelect;
354DataTable.prototype.addRowSelect= addRowSelect;
355DataTable.prototype.addRowsSelect= addRowsSelect;
356DataTable.prototype.setAllRowsUnSelect= setAllRowsUnSelect;
357DataTable.prototype.setRowUnSelect= setRowUnSelect;
358DataTable.prototype.setRowsUnSelect= setRowsUnSelect;
359DataTable.prototype.toggleAllSelect= toggleAllSelect;
360DataTable.prototype.updateSelectedIndices= updateSelectedIndices;
361
362//simpleData
363DataTable.prototype.setSimpleData= setSimpleData;
364DataTable.prototype.addSimpleData= addSimpleData;
365
366//util
367DataTable.prototype.isChanged= isChanged;
368
369DataTable.DEFAULTS = {
370 pageSize: 20,
371 pageIndex: 0,
372 totalPages: 0,
373 pageCache: false,
374 enable: true
375}
376
377DataTable.META_DEFAULTS = {
378 enable: true,
379 required: false,
380 descs: {}
381}
382
383//事件类型
384DataTable.ON_ROW_SELECT = 'select'
385DataTable.ON_ROW_UNSELECT = 'unSelect'
386DataTable.ON_ROW_ALLSELECT = 'allSelect'
387DataTable.ON_ROW_ALLUNSELECT = 'allUnselect'
388DataTable.ON_VALUE_CHANGE = 'valueChange'
389DataTable.ON_BEFORE_VALUE_CHANGE = 'beforeValueCHange'
390DataTable.ON_CURRENT_VALUE_CHANGE = 'currentValueChange' //当前行变化
391// DataTable.ON_AFTER_VALUE_CHANGE = 'afterValueChange'
392// DataTable.ON_ADD_ROW = 'addRow'
393DataTable.ON_INSERT = 'insert'
394DataTable.ON_UPDATE = 'update'
395DataTable.ON_CURRENT_UPDATE = 'currentUpdate'
396DataTable.ON_DELETE = 'delete'
397DataTable.ON_DELETE_ALL = 'deleteAll'
398DataTable.ON_ROW_FOCUS = 'focus'
399DataTable.ON_ROW_UNFOCUS = 'unFocus'
400DataTable.ON_LOAD = 'load'
401DataTable.ON_ENABLE_CHANGE = 'enableChange'
402DataTable.ON_META_CHANGE = 'metaChange'
403DataTable.ON_ROW_META_CHANGE = 'rowMetaChange'
404DataTable.ON_CURRENT_META_CHANGE = 'currentMetaChange'
405DataTable.ON_CURRENT_ROW_CHANGE = 'currentRowChange'
406
407DataTable.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
419DataTable.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
431export {
432 DataTable
433}