UNPKG

8.52 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 {
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 * DataTable
111 * @namespace
112 * @description 前端数据模型对象
113 */
114 class DataTable {
115 constructor(options) {
116 options = options || {};
117 /**
118 * DataTable对应的唯一标识
119 * @type {string}
120 */
121 this.id = options['id'];
122 /**
123 * 在设置数据时是否自动创建对应字段,如果为true则不自动创建,如果为false则自动创建缺失的字段
124 * @type {boolean}
125 * @default false
126 */
127 this.strict = options['strict'] || false;
128 /**
129 * DataTable的所有字段属性信息
130 * @type {object}
131 */
132 this.meta = DataTable.createMetaItems(options['meta']);
133 /**
134 * DataTable的是否支持编辑功能
135 * @type {boolean}
136 * @default true
137 */
138 this.enable = options['enable'] || DataTable.DEFAULTS.enable;
139 /**
140 * DataTable支持翻页功能时每页显示数据条数
141 * @type {number}
142 * @default 20
143 */
144 this.pageSize = ko.observable(options['pageSize'] || DataTable.DEFAULTS.pageSize);
145 /**
146 * DataTable支持翻页功能时当前页码
147 * @type {number}
148 * @default 0
149 */
150 this.pageIndex = ko.observable(options['pageIndex'] || DataTable.DEFAULTS.pageIndex);
151 /**
152 * DataTable支持翻页功能时总页数
153 * @type {number}
154 * @default 0
155 */
156 this.totalPages = ko.observable(options['totalPages'] || DataTable.DEFAULTS.totalPages);
157 // 存储所有行对象
158 this.totalRow = ko.observable();
159 /**
160 * DataTable的是否支持前端缓存,支持前端缓存则前端会存储所有页的数据信息,否则只保存当前页的数据信息。如果使用前端缓存则需要使用框架封装的fire方法来与后台进行交互
161 * @type {boolean}
162 * @default false
163 */
164 this.pageCache = options['pageCache'] === undefined ? DataTable.DEFAULTS.pageCache : options['pageCache'];
165 // 存储所有row对象
166 this.rows = ko.observableArray([])
167 // 存储所有的选中行的index
168 this.selectedIndices = ko.observableArray([])
169 // 原有的当前行,用于判断当前行是否发生变化
170 this._oldCurrentIndex = -1;
171 // 当前focus行
172 this.focusIndex = ko.observable(-1)
173 // 存储所有页对象
174 this.cachedPages = []
175 // 存储meta改变信息
176 this.metaChange = {};
177 // 存储valuecahnge改变信息
178 this.valueChange = {}; //ko.observable(1);
179 // 监听当前行改变
180 this.currentRowChange = ko.observable(1);
181 // 监听是否可修改属性的改变
182 this.enableChange = ko.observable(1);
183 /**
184 * 使用者自定义的属性合集,框架内部不会针对此属性进行特殊处理,仅用于设置及获取
185 * @type {object}
186 */
187 this.params = options['params'] || {};
188 /**
189 * 使用者自定义的属性,框架内部不会针对此属性进行特殊处理,仅用于设置及获取。
190 * @type {string}
191 */
192 this.master = options['master'] || '';
193 // 监听是否全部选中
194 this.allSelected = ko.observable(false);
195 /**
196 * 通过getSimpleData获取数据时,日期字段是否转化为long型,如果为true时不进行转化,为false时转化为long型
197 * @type {boolean}
198 * @default false
199 */
200 this.dateNoConvert = options['dateNoConvert'] || false;
201 // 对于子表通过root字段存储根datatable对象
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 // DataTable.ON_AFTER_VALUE_CHANGE = 'afterValueChange'
267 // DataTable.ON_ADD_ROW = 'addRow'
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 }