Skip to content

Commit eab0af9

Browse files
committed
refactor per review comments
1 parent 65b11c3 commit eab0af9

File tree

1 file changed

+64
-41
lines changed

1 file changed

+64
-41
lines changed

app/code/Magento/Cms/view/adminhtml/web/js/folder-tree.js

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -80,61 +80,84 @@ define([
8080
tree = this.element,
8181
lastExistentFolderEl,
8282
pathId,
83-
encodedPath,
8483

85-
/**
86-
* Recursively open folders specified in path array.
87-
*/
88-
recursiveOpen = function () {
89-
var folderEl = $('[data-id="' + path.pop() + '"]');
84+
/**
85+
* Recursively open folders specified in path array.
86+
*/
87+
recursiveOpen = function () {
88+
var folderEl = $('[data-id="' + path.pop() + '"]');
9089

91-
// if folder doesn't exist, select the last opened folder
92-
if (!folderEl.length) {
93-
tree.jstree('select_node', lastExistentFolderEl);
90+
// if folder doesn't exist, select the last opened folder
91+
if (!folderEl.length) {
92+
tree.jstree('select_node', lastExistentFolderEl);
9493

95-
return;
96-
}
94+
return;
95+
}
9796

98-
lastExistentFolderEl = folderEl;
97+
lastExistentFolderEl = folderEl;
9998

100-
if (path.length) {
101-
tree.jstree('open_node', folderEl, recursiveOpen);
99+
if (path.length) {
100+
tree.jstree('open_node', folderEl, recursiveOpen);
101+
} else {
102+
tree.jstree('open_node', folderEl, function () {
103+
tree.jstree('select_node', folderEl);
104+
});
105+
}
106+
},
107+
108+
/**
109+
* Encode path value
110+
*
111+
* @param {String} val
112+
*/
113+
_encodePathId = function (val) {
114+
return Base64.encode(val)
115+
.replace(/\+/g, ':')
116+
.replace(/\//g, '_')
117+
.replace(/=/g, '-');
118+
},
119+
120+
/**
121+
* Decode path value
122+
*
123+
* @param {String} val
124+
*/
125+
_decodePathId = function (val) {
126+
return Base64.decode(val)
127+
.replace(/\:/g, '+')
128+
.replace(/\_/g, '/')
129+
.replace(/-/g, '=');
130+
},
131+
132+
/**
133+
* Get currentPath decode it returns new path array
134+
*/
135+
_parseCurrentPath = function () {
136+
var paths = [],
137+
decodedPath = _decodePathId(window.MediabrowserUtility.pathId.replace(/--|,,/, '==')).split('/');
138+
139+
$.each(decodedPath, function (i, val) {
140+
var isLastElement = i === decodedPath.length - 1;
141+
142+
if (isLastElement) {
143+
paths[i] = window.MediabrowserUtility.pathId.replace(',,', '--');
102144
} else {
103-
tree.jstree('open_node', folderEl, function () {
104-
tree.jstree('select_node', folderEl);
105-
});
145+
paths[i] = _encodePathId(val);
106146
}
107-
},
108-
109-
/**
110-
* Get currentPath decode it returns new path array
111-
*/
112-
_parseCurrentPath = function () {
113-
var paths = [];
147+
});
148+
paths.unshift('root');
149+
paths.reverse();
114150

115-
$.each(encodedPath, function (i, val) {
116-
var isLastElement = i === encodedPath.length - 1;
117-
118-
if (isLastElement) {
119-
paths[i] = window.MediabrowserUtility.pathId.replace(',,', '--');
120-
} else {
121-
paths[i] = Base64.encode(val).replace('==', '--');
122-
}
123-
});
124-
paths.unshift('root');
125-
paths.reverse();
126-
127-
return paths;
128-
};
151+
return paths;
152+
};
129153

130154
$(window).on('reload.MediaGallery', function () {
131-
encodedPath = Base64.decode(window.MediabrowserUtility.pathId.replace(/--|,,/, '==')).split('/');
132155
pathId = window.MediabrowserUtility.pathId.replace(',,', '--');
156+
path = _parseCurrentPath();
133157

134158
tree.jstree('deselect_all');
135159

136-
if (encodedPath.length > 1) {
137-
path = _parseCurrentPath(encodedPath);
160+
if (path.length > 1) {
138161
recursiveOpen();
139162
} else {
140163
tree.jstree('open_node', $('[data-id="' + pathId + '"]'), function () {

0 commit comments

Comments
 (0)