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
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'jquery/ui',
'jquery/jstree/jquery.jstree'
], function ($) {
'use strict';
$.widget('mage.folderTree', {
options: {
root: 'root',
rootName: 'Root',
url: '',
currentPath: ['root'],
tree: {
'plugins': ['themes', 'json_data', 'ui', 'hotkeys'],
'themes': {
'theme': 'default',
'dots': false,
'icons': true
}
}
},
/** @inheritdoc */
_create: function () {
var options = this.options,
treeOptions = $.extend(
true,
{},
options.tree,
{
'json_data': {
data: {
data: options.rootName,
state: 'closed',
metadata: {
node: {
id: options.root,
text: options.rootName
}
},
attr: {
'data-id': options.root,
id: options.root
}
},
ajax: {
url: options.url,
/**
* @param {Object} node
* @return {Object}
*/
data: function (node) {
return {
node: node.data('id'),
'form_key': window.FORM_KEY
};
},
success: this._convertData
}
}
}
);
this.element.jstree(treeOptions).on('loaded.jstree', $.proxy(this.treeLoaded, this));
},
/**
* Tree loaded.
*/
treeLoaded: function () {
var path = this.options.currentPath,
tree = this.element,
lastExistentFolderEl,
/**
* Recursively open folders specified in path array.
*/
recursiveOpen = function () {
var folderEl = $('[data-id="' + path.pop() + '"]');
// if folder doesn't exist, select the last opened folder
if (!folderEl.length) {
tree.jstree('select_node', lastExistentFolderEl);
return;
}
lastExistentFolderEl = folderEl;
if (path.length) {
tree.jstree('open_node', folderEl, recursiveOpen);
} else {
tree.jstree('open_node', folderEl, function () {
tree.jstree('select_node', folderEl);
});
}
};
recursiveOpen();
},
/**
* @param {*} data
* @return {*}
* @private
*/
_convertData: function (data) {
return $.map(data, function (node) {
var codeCopy = $.extend({}, node);
return {
data: node.text,
attr: {
'data-id': node.id,
id: node.id
},
metadata: {
node: codeCopy
},
state: node.state || 'closed'
};
});
}
});
return $.mage.folderTree;
});