-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoneycomb.js
More file actions
142 lines (120 loc) · 3.91 KB
/
honeycomb.js
File metadata and controls
142 lines (120 loc) · 3.91 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
(function(window){
function parse(honey) {
if (Honeycomb.isPrimitive(honey)) {
return true;
}
if (Honeycomb.isArray(honey)) {
if (honey.every(Honeycomb.isPrimitive)) {
return true;
};
// Merges all possible properties into a single object.
return honey.reduce(function(acc, obj) {
var resultObject = acc[0];
acc[0] = doParse(obj, resultObject);
return acc;
}, [{}]);
}
// Parses into an object.
return doParse(honey, {});
}
function doParse(data, acc) {
var self = this;
return Object.keys(data).reduce(function(result, key) {
var val = data[key];
result[key] = parse(val);
return result;
}, acc);
}
function toTree(obj, callback) {
if (Honeycomb.isArray(obj)) {
// The filter for array only has one child.
return toTree(obj[0], callback);
}
var branches = [];
// For each property of the object, we create a node/branch.
// An object will become an array of nodes/branches.
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
var value = obj[key],
// Checking primitive here is simpler, since there can only
// be true/false value for each filter.
isPrimitive = (value === true || value === false);
var branch = {};
branch.text = key;
if (Honeycomb.isArray(value)) {
// When value is an array, we only convert the first child to
// tree, but we want to indicate that by the text.
// The callee can remove it if desired through the callback.
branch.text += '[]';
}
if (!isPrimitive) {
branch.children = toTree(value, callback);
}
branch.state = {
selected: value !== false,
opened: !isPrimitive
}
callback && callback(branch);
branches.push(branch);
}
return branches;
}
/**
* @typedef primitive
* @type {string|number|boolean}
*/
/**
* @typedef honey
* @type primitive|Object.<string, honey>
*/
/**
* Callback for constructing each tree node.
*
* @callback nodeCallback
* @param {json} node - A node in the tree.
*/
/**
* Creates a new Honeycomb.
* @constructor
* @param {honey|honey[]} [honey] - The data to parse.
*/
function Honeycomb(honey) {
this.honey = honey;
this.filter = this.parse(honey);
}
/**
* Parses data into a filter object
* @param {honey|honey[]} honey - The data to parse.
* @returns {json} - The filter JSON object.
*/
Honeycomb.prototype.parse = parse;
/**
* Constructs a filter object/array into a tree.
*
* This tree can be used by `jstree`.
*
* @param {json} filter - The filter object.
* @param {nodeCallback} [callback] - A callback for each node to further process.
* @returns {json} - The tree object.
*/
Honeycomb.prototype.toTree = toTree;
Honeycomb.isArray = Array.isArray;
Honeycomb.isPrimitive = function(value) {
return !value || [Number, Boolean, String].indexOf(value.constructor) > -1;
}
if (typeof module === "object" && module && typeof module.exports === "object") {
module.exports = Honeycomb;
} else {
window.Honeycomb = Honeycomb;
// Register as a named AMD module, since Honeycomb can be concatenated with
// other files that may use define, but not via a proper concatenation script
// that understands anonymous AMD modules. A named AMD is safest and most
// robust way to register. Lowercase honeycomb is used because AMD module names
// are derived from file names, and Honeycomb is normally delivered in a
// lowercase file name. Do this after creating the global so that if an AMD
// module wants to call noConflict to hide this version of Honeycomb, it will work.
if (typeof define === "function" && define.amd) {
define("honeycomb", [], function () { return Honeycomb; });
}
}
})(this);