-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
253 lines (219 loc) · 7.67 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(function() {
'use strict';
var isArray = (function() {
if (typeof Array.isArray !== 'function') {
return function(value) {
return toString.call(value) === '[object Array]';
};
}
return Array.isArray;
})();
function isDefined(val) {
return typeof val !== 'undefined';
};
function rootItems(cfg, listArg){
var list = listArg; //safeCopy(listArg);
if (!isDefined(cfg.selfKey) || !isDefined(cfg.parentKey)){
throw new Error("Both selfKey and parentKey properties of cfg parameter must be defined");
}
var listlen = list.length;
var ids = [];
for (var i = 0; i < listlen; i++){
var v = list[i][cfg.selfKey];
if (isDefined(v)) {
ids.push(v);
}
}
if (ids.length <= 0){
return [];
}
var roots = [];
for (var i = 0; i < listlen; i++){
var item = list[i];
var v = item[cfg.parentKey];
if (!isDefined(v)) {
continue;
}
if (ids.indexOf(v) < 0) {
//roots.push(item[cfg.parentKey]);
roots.push(item);
}
}
return roots;
};
function itemsWithTheseSelfVals(cfg, vals, list){
if (!list || !isArray(list)){
return [];
}
var results = [];
for (var i = 0; i < list.length; i++){
if (vals.indexOf(list[i][cfg.selfKey]) >=0 ) {
results.push(list[i]);
}
}
return results;
};
function makeArrayIfNot(val){
return isArray(val) ? val : [val];
}
function toHier(cfg, listArg){
var list = listArg; //safeCopy(listArg);
if (!isDefined(cfg.selfKey)
|| !isDefined(cfg.parentKey)
|| !isDefined(cfg.childrenKey) ){
throw new Error("selfKey, parentKey, and childrenKey properties of cfg parameter must be defined");
}
if (isDefined(cfg.rootParentValues) && isDefined(cfg.rootSelfValues)){
throw new Error("rootParentValues and rootSelfValues are mutually exclusive; please set only one of them");
}
//var iterations = 0;
var listlen = list.length;
function getChildren(parent){
var results = [];
for (var i = 0; i < listlen; i++){
var item = list[i];
//iterations++;
if (item[cfg.parentKey] === parent) {
results.push(item);
item[cfg.childrenKey] = getChildren(item[cfg.selfKey]);
}
}
return results;
};
if (!list || !list.length){
return [];
}
var results = [];
var rootItems_ = [];
var rootsDefined = isDefined(cfg.rootSelfValues) || isDefined(cfg.rootParentValues);
if (rootsDefined){
if (isDefined(cfg.rootSelfValues)) {
var selfValues = makeArrayIfNot(cfg.rootSelfValues);
rootItems_ = itemsWithTheseSelfVals(cfg, selfValues, list);
} else {
var rootValues = makeArrayIfNot(cfg.rootParentValues);
for (var i=0; i<listlen; i++){
var item = list[i];
if (rootValues.indexOf(item[cfg.parentKey]) >=0) {
rootItems_.push(item);
}
}
}
} else {
rootItems_ = rootItems(cfg, list);
//rootValues = pluck(rootItems_, cfg.selfKey);
}
var tmp = [];
for (var i =0; i < rootItems_.length ; i++){
var item = rootItems_[i];
item[cfg.childrenKey] = getChildren(rootItems_[i][cfg.selfKey]);
tmp.push(item);
}
for (var i = 0; i <tmp.length; i++){
results = results.concat(tmp[i]);
}
return results;
};
function flattenHierarchy(cfg, hierList){
if ( !isDefined(cfg.childrenKey)){
throw new Error("childrenKey property of cfg parameter must be defined");
}
//childrenKey
//
function flattenLevel(listArg, results, parentHierPosition, depth){
if (!listArg || !listArg.length) {
return;
}
parentHierPosition += parentHierPosition !== '' ? '.' : '';
for (var i = 0; i <listArg.length; i++){
var item = listArg[i];
var pos = parentHierPosition + (i+1);
results.push({
item: item, hierPos: pos, depth: depth,
childCount:item[cfg.childrenKey] ? item[cfg.childrenKey].length : 0
});
flattenLevel(item[cfg.childrenKey], results, pos, depth+1);
delete item[cfg.childrenKey];
}
}
var results = [];
flattenLevel(hierList, results, '', 0);
for (var i = 0, len = results.length; i < len; i++){
delete results[i].item[cfg.childrenKey];
}
return results;
};
function toFlatHier(cfg, hierArg){
var hier = hierArg; //safeCopy(hierArg);
//var hier = toHier(cfg, list);
return flattenHierarchy(cfg, hier);
};
function findInFlatHier(flatHier, propName, propVal){
for (var i = 0, len = flatHier.length; i < len; i++){
if (flatHier[i].item[propName] === propVal) {
return flatHier[i];
}
}
return null;
}
function getKidsAncestorsHelper(cfg, listArg, selfKeyVal, isAncestors){
var childrenKey = "children__" + (new Date()).getTime();
cfg.childrenKey = childrenKey;
var hier = toHier(cfg, listArg);
var flatHier = toFlatHier(cfg, hier);
var obj = findInFlatHier(flatHier, cfg.selfKey, selfKeyVal);
if (!obj){
return [];
}
var result =[];
for (var i = 0, len = flatHier.length; i < len; i++){
var elem = flatHier[i];
if (elem.hierPos === obj.hierPos){
continue;
}
if ((isAncestors && obj.hierPos.indexOf(elem.hierPos) === 0)
|| (!isAncestors && elem.hierPos.indexOf(obj.hierPos) === 0) ) {
result.push(elem.item);
}
};
return result;
}
function getKids(cfg, listArg, selfKeyVal){
return getKidsAncestorsHelper(cfg, listArg, selfKeyVal, false);
}
// selfKey and parentKey
function getAncestors(cfg, listArg, selfKeyVal){
return getKidsAncestorsHelper(cfg, listArg, selfKeyVal, true);
}
var selfRef = function(){
return {
toHier: toHier,
toHierarchy: toHier,
rootItems: rootItems,
getRootItems: rootItems,
toFlatHier: toFlatHier,
toFlatHierarchy: toFlatHier,
getAncestors: getAncestors,
getChildren: getKids
};
}();
//exposing
var hasModule = (typeof module !== 'undefined' && module.exports);
// CommonJS module is defined
if (hasModule) {
module.exports = selfRef;
}
/*global ender:false */
if (typeof ender === 'undefined') {
// here, `this` means `window` in the browser, or `global` on the server
// add `numeral` as a global object via a string identifier,
// for Closure Compiler 'advanced' mode
this['selfReferential'] = selfRef;
}
/*global define:false */
if (typeof define === 'function' && define.amd) {
define([], function () {
return selfRef;
});
}
}).call(this);