Skip to content

Commit 1681020

Browse files
committed
DOM: Add deep argument to getItems
Previously the function only returned the topmost items in a folder, this was different from how the function behaves when no argument is given, where all items in the project is returned. If no arg is given, it returns all items in the project. If the folder is not found (when the argument is a string) an empty arrayEx is returned. If deep is not disabled, it returns all items in subfolders as well, else it returns only the items in the folder.
1 parent b83d6a4 commit 1681020

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

lib/dom.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,57 @@ aeq.extend({
55
* Gets all the item in a folder or project.
66
* @method
77
* @memberof aeq
8-
* @memberof aeq
98
* @param {FolderItem} [folder=app.project] The Folder to get items from.
10-
* @return {aeq.arrayEx} Array of Item objects
9+
* @param {boolean} [deep=true] When `true`, gets items from
10+
* subfolders as well.
11+
* @return {aeq.arrayEx} Array of Item objects
1112
*/
12-
getItems: function( folder ) {
13-
folder = aeq.project.getFolder(folder) || app.project;
14-
var items = aeq.normalizeCollection( folder.items );
13+
getItems: function(folder, deep) {
14+
// If no arguments are given, just return all items in project.
15+
if (folder === undefined) {
16+
return aeq.normalizeCollection(app.project.items);
17+
}
18+
19+
deep = setDefault(deep, true);
20+
folder = aeq.project.getFolder(folder);
21+
if (folder === null) {
22+
return aeq.arrayEx();
23+
}
24+
25+
if (deep) {
26+
return aeq.getItemsDeep(folder);
27+
}
28+
29+
return aeq.normalizeCollection(folder.items);
30+
},
1531

16-
return aeq.arrayEx( items );
32+
/**
33+
* Returns an {@link aeq.arrayEx} with all items in a folder, and items in
34+
* subfolders.
35+
* @method
36+
* @param {FolderItem} folder The folder to flatten.
37+
* @return {aeq.arrayEx} ArrayEx with Item objects.
38+
*/
39+
getItemsDeep: function(folder, returnArrayEx) {
40+
// The returnArrayEx param is so we can skip the converting to arrayEx when
41+
// recursing. It is not meant to be used outside of this function.
42+
var item,
43+
items = [],
44+
len = folder.items.length;
45+
46+
for (var i=1; i <= len; i++) {
47+
item = folder.items[i];
48+
if (aeq.isFolderItem(item)) {
49+
// Add all items in subfolder to the `items` array.
50+
items.push.apply(items, aeq.getItemsDeep(item, false));
51+
}
52+
items.push(item);
53+
}
54+
// Skip converting to arrayEx when function is called by it self.
55+
if (returnArrayEx === false) {
56+
return items;
57+
}
58+
return aeq.arrayEx(items);
1759
},
1860

1961
/**

0 commit comments

Comments
 (0)