Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit c4329be

Browse files
authored
Merge pull request #112 from symfony-cmf/root_path
Fix input binding with a configured root node
2 parents b3a5f14 + b243c8b commit c4329be

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ sudo: false
1111
cache:
1212
directories:
1313
- $HOME/.composer/cache/files
14-
- node_modules
14+
# node_modules are not cached, because we don't shrinkwrap the deps.
15+
# - node_modules
1516

1617
env: LANG=php
1718

Resources/assets/js/adapter/fancytree.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,20 @@ export class FancytreeAdapter {
147147
// transform the JSON response into a data structure that's supported by FancyTree
148148
postProcess: function (event, data) {
149149
if (null == data.error) {
150-
data.result = requestNodeToFancytreeNode(data.response).children;
151-
if (data.result.length == 1) {
152-
data.result[0].expanded = true;
150+
var result = requestNodeToFancytreeNode(data.response);
151+
if ("" === result.key) {
152+
result = result.children;
153+
} else {
154+
result = [result];
155+
}
156+
157+
if (result.length == 1) {
158+
result[0].expanded = true;
153159
}
154160

161+
data.result = result;
155162
if (useCache) {
156-
cache.set(data.node.getKeyPath(), data.result);
163+
cache.set(data.node.getKeyPath(), result);
157164
}
158165
} else {
159166
data.result = {
@@ -182,29 +189,41 @@ export class FancytreeAdapter {
182189
}
183190

184191
bindToInput($input) {
192+
var root = this.rootNode;
193+
if (root.substr(-1) == '/') {
194+
var root = this.rootNode.substr(0, -1);
195+
}
196+
var rootParent = root.substr(0, root.lastIndexOf('/'));
197+
185198
// output active node to input field
186-
this.$tree.fancytree('option', 'activate', function(event, data) {
187-
$input.val(data.node.getKeyPath());
199+
this.$tree.fancytree('option', 'activate', (event, data) => {
200+
$input.val(rootParent + data.node.getKeyPath());
188201
});
189202

190-
var tree = this.tree;
191-
var showKey = function (key) {
192-
tree.loadKeyPath(key, function (node, status) {
203+
var showKey = (key) => {
204+
this.tree.loadKeyPath(key, function (node, status) {
193205
if ('ok' == status) {
194206
node.setExpanded();
195207
node.setActive();
196208
}
197209
});
198210
};
211+
var removeRoot = (path) => {
212+
if (0 === path.indexOf(rootParent + '/')) {
213+
return path.substr(rootParent.length + 1);
214+
}
215+
216+
return path;
217+
};
199218

200219
// use initial input value as active node
201220
this.$tree.bind('fancytreeinit', function (event, data) {
202-
showKey($input.val());
221+
showKey(removeRoot($input.val()));
203222
});
204223

205224
// change active node when the value of the input field changed
206225
$input.on('change', function (e) {
207-
showKey($(this).val());
226+
showKey(removeRoot($(this).val()));
208227
});
209228
}
210229

Resources/public/js/cmf_tree_browser.fancytree.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/js/adapter/fancytreeSpec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,52 @@ describe('The Fancytree adapter', function() {
208208
expect(tree.getNodeByKey('cms').isActive()).toBe(true);
209209
});
210210

211+
it('prefixes the root node to path output when configured', function () {
212+
jasmine.Ajax.stubRequest(/^\/root_api/).andReturn({
213+
responseText: JSON.stringify({
214+
node_name: 'cms',
215+
label: 'cms',
216+
path: '\/cms',
217+
children: {
218+
content: {
219+
node_name: 'content',
220+
label: 'Content',
221+
path: '/cms/content',
222+
children: {
223+
some_article: [],
224+
other_article: []
225+
}
226+
},
227+
routes: {
228+
node_name: 'routes',
229+
label: 'Routes',
230+
path: '/cms/routes',
231+
children: {}
232+
}
233+
}
234+
})
235+
});
236+
237+
var adapter = new FancytreeAdapter({
238+
request: {
239+
load: function (path) {
240+
return {
241+
url: '/root_api'
242+
};
243+
},
244+
},
245+
root_node: '/cms'
246+
});
247+
248+
adapter.bindToElement(this.$tree);
249+
250+
var $input = $('<input type=text value="/cms/routes"/>');
251+
adapter.bindToInput($input);
252+
253+
var tree = this.$tree.fancytree('getTree');
254+
255+
tree.getNodeByKey('content').setActive();
256+
expect($input).toHaveValue('/cms/content');
257+
});
258+
211259
});

0 commit comments

Comments
 (0)