-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (170 loc) · 7.72 KB
/
index.js
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
'use strict';
/**
* @mixin
*/
var finHypergridPrimaryKey = {
/**
* @summary Remove the ID'd data row object from the data store.
* @desc If data source pipeline in use, to see the deletion in the grid, you must eventually call:
* ```javascript
* this.grid.behavior.reindex();
* this.grid.behaviorChanged();
* ```
* Caveat: The row indexes of all rows following the deleted row will now be one less than they were!
* @param {object|string} keyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of 0 or more key-value pairs to search for.
* @param {*|string[]} [valOrList] - One of:
* _omitted_ - When `keyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `keyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `keyOrHash` is a string. Value to search for.
* @returns {object} The deleted row object.
*/
deleteRowById: function(keyOrHash, valOrList) {
return this.source.findRow.apply(this.source, getByIdArgs(keyOrHash, valOrList).concat([null]));
},
/**
* @summary Undefine the ID'd row object in place.
* @desc Similar to {@link rowById.deleteRowById|deleteRowById} except leave an `undefined` in place of data row object. This renders as a blank row in the grid.
*
* If data source pipeline in use, to see the deletion in the grid, you must eventually call:
* ```javascript
* this.grid.behavior.reindex();
* this.grid.behaviorChanged();
* ```
* @param {object|string} keyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of 0 or more key-value pairs to search for.
* @param {*|string[]} [valOrList] - One of:
* _omitted_ - When `keyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `keyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `keyOrHash` is a string. Value to search for.
* @returns {object} The deleted row object.
*/
eraseRowById: function(keyOrHash, valOrList) {
return this.source.findRow.apply(this.source, getByIdArgs(keyOrHash, valOrList).concat([undefined]));
},
/**
* @param {object|string} keyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of 0 or more key-value pairs to search for.
* @param {*|string[]} [valOrList] - One of:
* _omitted_ - When `keyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `keyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `keyOrHash` is a string. Value to search for.
* @returns {number}
*/
getRowIndexById: function(keyOrHash, valOrList) {
this.source.findRow.apply(this.source, arguments);
return this.source.getProperty('foundRowIndex');
},
/**
* @param {object|string} keyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of 0 or more key-value pairs to search for.
* @param {*|string[]} [valOrList] - One of:
* _omitted_ - When `keyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `keyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `keyOrHash` is a string. Value to search for.
* @returns {object}
*/
getRowById: function(keyOrHash, valOrList) {
return this.source.findRow.apply(this.source, arguments);
},
/**
* @summary Update selected columns in existing data row.
* @desc If data source pipeline in use, to see the deletion in the grid, you must eventually call:
* ```javascript
* this.grid.behavior.reindex();
* this.grid.repaint();
* ```
* @param {object|string} findKeyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of zero or more key-value pairs to search for.
* @param {*|string[]} [findValOrList] - One of:
* _omitted_ - When `findKeyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `findKeyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `findKeyOrHash` is a string. Value to search for.
* @param {object|string} modKeyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of zero or more key-value pairs to modify.
* @param {*|string[]} [modValOrList] - One of:
* _omitted_ - When `modKeyOrHash` is a hash and you want to modify all its keys.
* _string[]_ - When `modKeyOrHash` is a hash but you only want to modify certain keys.
* _otherwise_ - When `modKeyOrHash` is a string. The modified value.
* @returns {object} The modified row object.
*/
modifyRowById: function(findKeyOrHash, findValOrList, modKeyOrHash, modValOrList) {
var dataRow, keys, columnName;
if (typeof findKeyOrHash !== 'object' || findValOrList instanceof Array) {
dataRow = this.source.findRow(findKeyOrHash, findValOrList);
} else {
dataRow = this.source.findRow(findKeyOrHash);
modValOrList = modKeyOrHash; // promote
modKeyOrHash = findValOrList; // promote
}
if (dataRow) {
if (typeof modKeyOrHash !== 'object') {
dataRow[modKeyOrHash] = modValOrList;
} else {
keys = modValOrList instanceof Array ? modValOrList : Object.keys(modKeyOrHash);
for (var key in keys) {
columnName = keys[key];
dataRow[columnName] = modKeyOrHash[columnName];
}
}
}
return dataRow;
},
/**
* @summary Replace entire ID'd row object with another.
* @desc The replacement may have (but does not have to have) the same ID as the row object being replaced.
*
* If data source pipeline in use, to see the replaced row in the grid, you must eventually call:
* ```javascript
* this.grid.behavior.reindex();
* this.grid.behaviorChanged();
* ```
* @param {object|string} keyOrHash - One of:
* * _string_ - Column name.
* * _object_ - Hash of zero or more key-value pairs to search for.
* @param {*|string[]} [valOrList] - One of:
* _omitted_ - When `keyOrHash` is a hash and you want to search all its keys.
* _string[]_ - When `keyOrHash` is a hash but you only want to search certain keys.
* _otherwise_ - When `keyOrHash` is a string. Value to search for.
* @param {object} replacement
* @returns {object} The replaced row object.
*/
replaceRowById: function(keyOrHash, valOrList, replacement) {
if (typeof keyOrHash === 'object' && !(valOrList instanceof Array)) {
replacement = valOrList; // promote
}
if (typeof replacement !== 'object') {
throw 'Expected an object for replacement but found ' + typeof replacement + '.';
}
return this.source.findRow.apply(this.source, arguments);
}
};
function getByIdArgs(keyOrHash, valOrList) {
var length = typeof keyOrHash !== 'object' || valOrList instanceof Array ? 2 : 1;
return Array.prototype.slice.call(arguments, 0, length);
}
// `install` defined here as be non-enumerable to avoid being mixed in
Object.defineProperties(finHypergridPrimaryKey, {
/**
* @name install
* @summary Installer for plugin.
* @desc Required by {@link Hypergrid#installPlugins}
* @function
* @param {Hypergrid} grid -
* @param {object} target - Your data model instance or its prototype.
* @memberOf rowById
*/
install: {
value: function(grid, target) {
target = target || Object.getPrototypeOf(grid.behavior.dataModel);
target.mixIn(this);
}
}
});
module.exports = finHypergridPrimaryKey;