-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyTreeLooper.jsx
More file actions
79 lines (61 loc) · 2.3 KB
/
PropertyTreeLooper.jsx
File metadata and controls
79 lines (61 loc) · 2.3 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
/**
* @description class
*/
var PropertyTreeLooper = function () {
var rootClass = this;
/**
* @description callbacks
* @param {function} onAny - callback for each time a property, group or layer is found (regardless of type)
* @param {function} onPropertyFound - callback for each time a property of type PropertyType.PROPERTY is found
* @param {function} onGroupFound - callback for each time a property of type PropertyType.PROPERTY_GROUP is found
* @param {function} onEnd - callback for when the loop is finished
* @param {boolean} skipUnmodified - skip properties, groups or layers that have not been modified
*/
this.onAny = null;
this.onPropertyFound = null;
this.onGroupFound = null;
this.onEnd = null;
this.skipUnmodified = false;
this.loopIndex = -1;
this.loop = function (prop) {
rootClass.loopIndex++;
var thisLoopIndex = rootClass.loopIndex; // cache inside to access after recursive calls
// prepare
if (!prop) {
return;
}
// ⤵skip unmodified properties
if (rootClass.skipUnmodified && prop.isModified === false) {
return;
}
// ⤵call onAny callback
if (rootClass.onAny) {
rootClass.onAny(prop);
}
// ⤵call onPropertyFound or onLayerFound callbacks
var type = prop.propertyType;
if (type == PropertyType.PROPERTY && rootClass.onPropertyFound) {
rootClass.onPropertyFound(prop);
} else if (type == PropertyType.NAMED_GROUP && prop.containingComp && rootClass.onLayerFound) {
rootClass.onLayerFound(prop);
}
// call onGroupFound callback and go deeper
if (prop.numProperties > 0) {
if (rootClass.onGroupFound) {
rootClass.onGroupFound(prop);
}
// ...loop through its properties
for (var i = 1; i <= prop.numProperties; i++) {
rootClass.loop(prop.property(i));
}
};
// ⤵call onEnd callback
if (thisLoopIndex === 0) {
// reset the loop index of the looper to its initial value
rootClass.loopIndex = -1;
if (rootClass.onEnd) {
rootClass.onEnd(prop);
}
}
};
};