-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
230 lines (205 loc) · 6.63 KB
/
index.js
File metadata and controls
230 lines (205 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* eslint-env browser */
'use strict';
var DatasaurBase = require('datasaur-base');
/** @typedef {object} columnSchemaObject
* @property {string} name - The required column name.
* @property {string} [header] - An override for derived header
* @property {function} [calculator] - A function for a computed column. Undefined for normal data columns.
* @property {string} [type] - Used for sorting when and only when comparator not given.
* @property {object} [comparator] - For sorting, both of following required:
* @property {function} comparator.asc - ascending comparator
* @property {function} comparator.desc - descending comparator
*/
/**
* @param {object} [options]
* @param {object[]} [options.data]
* @param {object[]} [options.schema]
* @constructor
*/
var DatasaurLocal = DatasaurBase.extend('DatasaurLocal', {
initialize: function(datasaur, options) {
this.reset();
},
reset: function() {
/**
* @summary The array of column schema objects.
* @name schema
* @type {columnSchemaObject[]}
* @memberOf DatasaurLocal#
*/
this.schema = [];
/**
* @summary The array of uniform data objects.
* @name data
* @type {object[]}
* @memberOf DatasaurLocal#
*/
this.data = [];
},
/**
* Establish new data and schema.
* If no data provided, data will be set to 0 rows.
* If no schema provided AND no previously set schema, new schema will be derived from data.
* @param {object[]} [data=[]] - Array of uniform objects containing the grid data.
* @param {columnSchemaObject[]} [schema=[]]
* @memberOf DatasaurLocal#
*/
setData: function(data, schema) {
/**
* @summary The array of uniform data objects.
* @name data
* @type {object[]}
* @memberOf DatasaurLocal#
*/
this.data = data || [];
if (schema) {
this.setSchema(schema);
} else if (this.data.length && !this.schema.length) {
this.setSchema([]);
}
this.dispatchEvent('fin-hypergrid-data-loaded');
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#getSchema}
* @memberOf DatasaurLocal#
*/
getSchema: function(){
return this.schema;
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#setSchema}
* @memberOf DatasaurLocal#
*/
setSchema: function(newSchema){
if (!newSchema.length) {
var dataRow = this.getFirstRow();
if (dataRow) {
newSchema = Object.keys(dataRow);
}
}
this.schema = newSchema;
this.dispatchEvent('fin-hypergrid-schema-loaded');
},
/**
* @summary Find first extant AND defined element.
* @desc Uses for...in to find extant rows plus a truthiness test to return only a defined row.
* @returns {dataRow|undefined} Returns undefined if there are no such rows.
*/
getFirstRow: function() {
for (var i in this.data) {
if (this.data[i]) {
return this.data[i];
}
}
},
/**
* @param y
* @returns {dataRowObject}
* @memberOf DatasaurLocal#
*/
getRow: function(y) {
return this.data[y];
},
/**
* Update or blank row in place.
*
* _Note parameter order is the reverse of `addRow`._
* @param {number} y
* @param {object} [dataRow] - if omitted or otherwise falsy, row renders as blank
* @memberOf DatasaurLocal#
*/
setRow: function(y, dataRow) {
this.data[y] = dataRow || undefined;
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#getRowMetadata}
* @memberOf DatasaurLocal#
*/
getRowMetadata: function(y, prototype) {
var dataRow = this.data[y];
return dataRow && (dataRow.__META || (prototype !== undefined && (dataRow.__META = Object.create(prototype))));
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#setRowMetadata}
* @memberOf DatasaurLocal#
*/
setRowMetadata: function(y, metadata) {
var dataRow = this.data[y];
if (dataRow) {
if (metadata) {
dataRow.__META = metadata;
} else {
delete dataRow.__META;
}
}
return !!dataRow;
},
/**
* Insert or append a new row.
*
* _Note parameter order is the reverse of `setRow`._
* @param {object} dataRow
* @param {number} [y=Infinity] - The index of the new row. If `y` >= row count, row is appended to end; otherwise row is inserted at `y` and row indexes of all remaining rows are incremented.
* @memberOf DatasaurLocal#
*/
addRow: function(y, dataRow) {
if (arguments.length === 1) {
dataRow = arguments[0];
y = undefined;
}
if (y === undefined || y >= this.getRowCount()) {
this.data.push(dataRow);
} else {
this.data.splice(y, 0, dataRow);
}
this.dispatchEvent('fin-hypergrid-data-shape-changed');
},
/**
* Rows are removed entirely and no longer render.
* Indexes of all remaining rows are decreased by `rowCount`.
* @param {number} y
* @param {number} [rowCount=1]
* @returns {dataRowObject[]}
* @memberOf DatasaurLocal#
*/
delRow: function(y, rowCount) {
var rows = this.data.splice(y, rowCount === undefined ? 1 : rowCount);
if (rows.length) {
this.dispatchEvent('fin-hypergrid-data-shape-changed');
}
return rows;
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#getValue}
* @memberOf DatasaurLocal#
*/
getValue: function(x, y) {
var row = this.data[y];
if (!row) {
return null;
}
return row[this.schema[x].name];
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#setValue}
* @memberOf DatasaurLocal#
*/
setValue: function(x, y, value) {
this.data[y][this.schema[x].name] = value;
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#getRowCount}
* @memberOf DatasaurLocal#
*/
getRowCount: function() {
return this.data.length;
},
/**
* @see {@link https://fin-hypergrid.github.io/3.0.0/doc/dataModelAPI#getColumnCount}
* @memberOf DatasaurLocal#
*/
getColumnCount: function() {
return this.schema.length;
}
});
module.exports = DatasaurLocal;