-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.es6
198 lines (162 loc) · 6.75 KB
/
utils.es6
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
import {ID_PREFIX_BOX, ID_PREFIX_PAGE, ID_PREFIX_SECTION, ID_PREFIX_SORTABLE_BOX,
ID_PREFIX_CONTAINED_VIEW, ID_PREFIX_SORTABLE_CONTAINER, PAGE_TYPES} from './constants';
export default {
//This would be a good post to explore if we don't want to use JSON Stringify: http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object
deepClone: function (myObj) {
return JSON.parse(JSON.stringify(myObj));
}
};
export function isView(id) {
return isPage(id) || isSection(id);
}
export function isPage(id) {
return id.length && id.indexOf(ID_PREFIX_PAGE) !== -1;
}
export function isSlide(type) {
return type === PAGE_TYPES.SLIDE;
}
export function isDocument(type) {
return type === PAGE_TYPES.DOCUMENT;
}
export function isSection(id) {
return id.length && id.indexOf(ID_PREFIX_SECTION) !== -1;
}
export function isBox(id) {
return id.length && id.indexOf(ID_PREFIX_BOX) !== -1;
}
export function isSortableBox(id) {
return id.length && id.indexOf(ID_PREFIX_SORTABLE_BOX) !== -1;
}
export function isContainedView(id) {
return id.length && id.indexOf(ID_PREFIX_CONTAINED_VIEW) !== -1;
}
export function isSortableContainer(id){
return id.length && id.indexOf(ID_PREFIX_SORTABLE_CONTAINER) !== -1;
}
export function changeProps(object, keys, values) {
if (Array.isArray(keys) && Array.isArray(values) && keys.length === values.length) {
/* jshint ignore:start */
let temp = {...object};
for (let i = 0; i < keys.length; i++) {
temp = changeProp(temp, keys[i], values[i]);
}
return temp;
/* jshint ignore:end */
}
console.error("Incorrect parameters");
return;
}
export function changeProp(object, key, value) {
// This is based in object spread notation
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
// ...object -> add rest of properties (in this case, all of them)
// If property "keys" is found in object, replace value with "values"; otherwise, add it
// return new object -> state is not mutated!!
// ---------------------------------------------------------------------------
// ORDER IS IMPORTANT!!
// return {...object, [keys]: values}; !== return {[keys]: values, ...object};
// First one works, second does not!
// ---------------------------------------------------------------------------
/* jshint ignore:start */
return {
...object,
[key]: value
};
/* jshint ignore:end */
}
export function deleteProps(object, keys) {
if (Array.isArray(keys)) {
/* jshint ignore:start */
let temp = {...object};
for (let i = 0; i < keys.length; i++) {
temp = deleteProp(temp, keys[i]);
}
return temp;
/* jshint ignore:end */
}
console.error("Parameter is not an array");
return;
}
export function deleteProp(object, key) {
// This is based in object spread notation
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
// split object into new ones using it's properties
// to the properties we're interesting in deleting, we assign whatever (omit is nothing)
// rest of properties are stored in a new object (rest) -> state is not mutated!!
let {
[key]: omit,
...rest
} = object;
return rest;
}
export function findDescendantNavItems(state, element) {
let family = [element];
state[element].children.forEach(child => {
family = family.concat(findDescendantNavItems(state, child));
});
return family;
}
export function calculateNewIdOrder(oldArray, newChildren, newParent, itemMoved, navItems) {
let itemsToChange = findDescendantNavItems(navItems, itemMoved);
let oldArrayFiltered = oldArray.filter(id => itemsToChange.indexOf(id) === -1);
// This is the index where we split the old array to add the items we're moving
let splitIndex = oldArrayFiltered.length;
let indexInNewChildren = newChildren.indexOf(itemMoved);
// If we didn't move to last position of new children, we split by the position of the following one
if (indexInNewChildren !== newChildren.length - 1) {
splitIndex = oldArrayFiltered.indexOf(newChildren[indexInNewChildren + 1]);
// We have to look for the next item that has a lower or equal level
// If none is found, it means we were dragging to last position, so default value for splitIndex is not changed
} else {
for(let i = oldArrayFiltered.indexOf(newParent) + 1; i < oldArrayFiltered.length; i++){
if(navItems[oldArrayFiltered[i]].level <= navItems[newParent].level){
splitIndex = i;
break;
}
}
}
let newArray = oldArrayFiltered.slice(0, splitIndex);
newArray = newArray.concat(itemsToChange);
newArray = newArray.concat(oldArrayFiltered.slice(splitIndex));
return newArray;
}
/**
* Replaces all occurences of needle (interpreted as a regular expression with replacement and returns the new object.
*
* @param entity The object on which the replacements should be applied to
* @param needle The search phrase (as a regular expression)
* @param replacement Replacement value
* @param affectsKeys[optional=true] Whether keys should be replaced
* @param affectsValues[optional=true] Whether values should be replaced
*/
Object.replaceAll = function (entity, needle, replacement, affectsKeys, affectsValues) {
affectsKeys = typeof affectsKeys === "undefined" ? true : affectsKeys;
affectsValues = typeof affectsValues === "undefined" ? true : affectsValues;
var newEntity = {},
regExp = new RegExp(needle, 'g');
for (var property in entity) {
if (!entity.hasOwnProperty(property)) {
continue;
}
var value = entity[property],
newProperty = property;
if (affectsKeys) {
newProperty = property.replace(regExp, replacement);
}
if (affectsValues) {
if (value === null || (value instanceof Array && value.length === 0) || ( value instanceof Object && Object.keys(value).length === 0)) {
} else if (value instanceof Array) {
var obj = Object.replaceAll(value, needle, replacement, affectsKeys, affectsValues);
value = Object.keys(obj).map(function (k) {
return obj[k];
});
} else if (typeof value === "object") {
value = Object.replaceAll(value, needle, replacement, affectsKeys, affectsValues);
} else if (typeof value === "string") {
value = value.replace(regExp, replacement);
}
}
newEntity[newProperty] = value;
}
return newEntity;
};