1 | |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import {
|
8 | extend
|
9 | } from 'tinper-sparrow/src/extend';
|
10 |
|
11 |
|
12 | import {
|
13 | copyRowFunObj
|
14 | } from './copyRow';
|
15 |
|
16 | import {
|
17 | dataFunObj
|
18 | } from './data';
|
19 |
|
20 | import {
|
21 | enableFunObj
|
22 | } from './enable';
|
23 |
|
24 | import {
|
25 | getCurrentFunObj
|
26 | } from './getCurrent';
|
27 |
|
28 | import {
|
29 | getDataFunObj
|
30 | } from './getData';
|
31 |
|
32 | import {
|
33 | getFocusFunObj
|
34 | } from './getFocus';
|
35 |
|
36 | import {
|
37 | getMetaFunObj
|
38 | } from './getMeta';
|
39 |
|
40 | import {
|
41 | getPageFunObj
|
42 | } from './getPage';
|
43 |
|
44 | import {
|
45 | getParamFunObj
|
46 | } from './getParam';
|
47 |
|
48 | import {
|
49 | getSelectFunObj
|
50 | } from './getSelect';
|
51 |
|
52 | import {
|
53 | getSimpleDataFunObj
|
54 | } from './getSimpleData';
|
55 |
|
56 | import {
|
57 | metaFunObj
|
58 | } from './meta';
|
59 |
|
60 | import {
|
61 | pageFunObj
|
62 | } from './page';
|
63 |
|
64 | import {
|
65 | paramFunObj
|
66 | } from './param';
|
67 |
|
68 | import {
|
69 | refFunObj
|
70 | } from './ref';
|
71 |
|
72 | import {
|
73 | removeRowFunObj
|
74 | } from './removeRow';
|
75 |
|
76 | import {
|
77 | rowFunObj
|
78 | } from './row';
|
79 |
|
80 | import {
|
81 | rowCurrentFunObj
|
82 | } from './rowCurrent';
|
83 |
|
84 | import {
|
85 | rowDeleteFunObj
|
86 | } from './rowDelete';
|
87 |
|
88 | import {
|
89 | rowSelectFunObj
|
90 | } from './rowSelect';
|
91 |
|
92 | import {
|
93 | rowFocusFunObj
|
94 | } from './rowFocus';
|
95 |
|
96 | import {
|
97 | simpleDataFunObj
|
98 | } from './simpleData';
|
99 |
|
100 | import {
|
101 | utilFunObj
|
102 | } from './util';
|
103 |
|
104 | import {
|
105 | eventsFunObj
|
106 | } from './events';
|
107 |
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | class DataTable {
|
115 | constructor(options) {
|
116 | options = options || {};
|
117 | |
118 |
|
119 |
|
120 |
|
121 | this.id = options['id'];
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 | this.strict = options['strict'] || false;
|
128 | |
129 |
|
130 |
|
131 |
|
132 | this.meta = DataTable.createMetaItems(options['meta']);
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 | this.enable = options['enable'] || DataTable.DEFAULTS.enable;
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 | this.pageSize = ko.observable(options['pageSize'] || DataTable.DEFAULTS.pageSize);
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 | this.pageIndex = ko.observable(options['pageIndex'] || DataTable.DEFAULTS.pageIndex);
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | this.totalPages = ko.observable(options['totalPages'] || DataTable.DEFAULTS.totalPages);
|
157 |
|
158 | this.totalRow = ko.observable();
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | this.pageCache = options['pageCache'] === undefined ? DataTable.DEFAULTS.pageCache : options['pageCache'];
|
165 |
|
166 | this.rows = ko.observableArray([])
|
167 |
|
168 | this.selectedIndices = ko.observableArray([])
|
169 |
|
170 | this._oldCurrentIndex = -1;
|
171 |
|
172 | this.focusIndex = ko.observable(-1)
|
173 |
|
174 | this.cachedPages = []
|
175 |
|
176 | this.metaChange = {};
|
177 |
|
178 | this.valueChange = {};
|
179 |
|
180 | this.currentRowChange = ko.observable(1);
|
181 |
|
182 | this.enableChange = ko.observable(1);
|
183 | |
184 |
|
185 |
|
186 |
|
187 | this.params = options['params'] || {};
|
188 | |
189 |
|
190 |
|
191 |
|
192 | this.master = options['master'] || '';
|
193 |
|
194 | this.allSelected = ko.observable(false);
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 | this.dateNoConvert = options['dateNoConvert'] || false;
|
201 |
|
202 | if (options['root']) {
|
203 | this.root = options['root']
|
204 | } else {
|
205 | this.root = this;
|
206 | }
|
207 |
|
208 | if (options['ns']) {
|
209 | this.ns = options['ns'];
|
210 | } else {
|
211 | this.ns = '';
|
212 | }
|
213 |
|
214 | this.newCount = 0;
|
215 | }
|
216 | }
|
217 |
|
218 | var DataTableProto = DataTable.prototype;
|
219 | Object.assign(DataTableProto, copyRowFunObj);
|
220 | Object.assign(DataTableProto, dataFunObj);
|
221 | Object.assign(DataTableProto, enableFunObj);
|
222 | Object.assign(DataTableProto, getCurrentFunObj);
|
223 | Object.assign(DataTableProto, getDataFunObj);
|
224 | Object.assign(DataTableProto, getFocusFunObj);
|
225 | Object.assign(DataTableProto, getMetaFunObj);
|
226 | Object.assign(DataTableProto, getPageFunObj);
|
227 | Object.assign(DataTableProto, getParamFunObj);
|
228 | Object.assign(DataTableProto, getSelectFunObj);
|
229 | Object.assign(DataTableProto, getSimpleDataFunObj);
|
230 | Object.assign(DataTableProto, pageFunObj);
|
231 | Object.assign(DataTableProto, metaFunObj);
|
232 | Object.assign(DataTableProto, refFunObj);
|
233 | Object.assign(DataTableProto, paramFunObj);
|
234 | Object.assign(DataTableProto, rowFunObj);
|
235 | Object.assign(DataTableProto, removeRowFunObj);
|
236 | Object.assign(DataTableProto, rowCurrentFunObj);
|
237 | Object.assign(DataTableProto, simpleDataFunObj);
|
238 | Object.assign(DataTableProto, rowFocusFunObj);
|
239 | Object.assign(DataTableProto, eventsFunObj);
|
240 | Object.assign(DataTableProto, utilFunObj);
|
241 | Object.assign(DataTableProto, rowSelectFunObj);
|
242 | Object.assign(DataTableProto, rowDeleteFunObj);
|
243 |
|
244 | DataTable.DEFAULTS = {
|
245 | pageSize: 20,
|
246 | pageIndex: 0,
|
247 | totalPages: 0,
|
248 | pageCache: false,
|
249 | enable: true
|
250 | }
|
251 |
|
252 | DataTable.META_DEFAULTS = {
|
253 | enable: true,
|
254 | required: false,
|
255 | descs: {}
|
256 | }
|
257 |
|
258 |
|
259 | DataTable.ON_ROW_SELECT = 'select'
|
260 | DataTable.ON_ROW_UNSELECT = 'unSelect'
|
261 | DataTable.ON_ROW_ALLSELECT = 'allSelect'
|
262 | DataTable.ON_ROW_ALLUNSELECT = 'allUnselect'
|
263 | DataTable.ON_VALUE_CHANGE = 'valueChange'
|
264 | DataTable.ON_BEFORE_VALUE_CHANGE = 'beforeValueCHange'
|
265 | DataTable.ON_CURRENT_VALUE_CHANGE = 'currentValueChange'
|
266 |
|
267 |
|
268 | DataTable.ON_INSERT = 'insert'
|
269 | DataTable.ON_UPDATE = 'update'
|
270 | DataTable.ON_CURRENT_UPDATE = 'currentUpdate'
|
271 | DataTable.ON_DELETE = 'delete'
|
272 | DataTable.ON_DELETE_ALL = 'deleteAll'
|
273 | DataTable.ON_ROW_FOCUS = 'focus'
|
274 | DataTable.ON_ROW_UNFOCUS = 'unFocus'
|
275 | DataTable.ON_LOAD = 'load'
|
276 | DataTable.ON_ENABLE_CHANGE = 'enableChange'
|
277 | DataTable.ON_META_CHANGE = 'metaChange'
|
278 | DataTable.ON_ROW_META_CHANGE = 'rowMetaChange'
|
279 | DataTable.ON_CURRENT_META_CHANGE = 'currentMetaChange'
|
280 | DataTable.ON_CURRENT_ROW_CHANGE = 'currentRowChange'
|
281 |
|
282 | DataTable.SUBMIT = {
|
283 | current: 'current',
|
284 | focus: 'focus',
|
285 | all: 'all',
|
286 | select: 'select',
|
287 | change: 'change',
|
288 | empty: 'empty',
|
289 | allSelect: 'allSelect',
|
290 | allPages: 'allPages'
|
291 | }
|
292 |
|
293 |
|
294 | DataTable.createMetaItems = function(metas) {
|
295 | var newMetas = {};
|
296 | for (var key in metas) {
|
297 | var meta = metas[key]
|
298 | if (typeof meta == 'string')
|
299 | meta = {}
|
300 | newMetas[key] = extend({}, DataTable.META_DEFAULTS, meta)
|
301 | }
|
302 | return newMetas
|
303 | }
|
304 |
|
305 |
|
306 | export {
|
307 | DataTable
|
308 | }
|