Skip to content
Merged
61 changes: 56 additions & 5 deletions app/code/Magento/User/view/adminhtml/web/js/roles-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
define([
'jquery',
'jquery/ui',
'jquery/jstree/jquery.jstree'
'jquery/jstree/jquery.jstree',
'mage/translate'
], function ($) {
'use strict';

// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
$.widget('mage.rolesTree', {
options: {
treeInitData: {},
Expand All @@ -26,9 +28,7 @@ define([
this.element.jstree({
plugins: ['checkbox'],
checkbox: {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
three_state: false,
// jscs:enable requireCamelCaseOrUpperCaseIdentifiers
visible: this.options.checkboxVisible,
cascade: 'undetermined'
},
Expand All @@ -40,13 +40,66 @@ define([
}
});
this._bind();
this._createButtons();
},

_createButtons: function () {
const $tree = $.jstree.reference(this.element),
collapseAllButton = document.createElement('button'),
expandAllButton = document.createElement('button'),
expandUsedButton = document.createElement('button'),
parent = this.element[0],
ul = this.element.find('ul')[0];

collapseAllButton.innerText = $.mage.__('Collapse all');
collapseAllButton.addEventListener('click', function () {
$tree.close_all();
});

expandAllButton.innerText = $.mage.__('Expand all');
expandAllButton.addEventListener('click', function () {
$tree.open_all();
});

expandUsedButton.innerText = $.mage.__('Expand selected');
expandUsedButton.addEventListener('click', function () {
const hasOpened = [];

$tree.get_checked(true).forEach(function (node) {
$tree.open_node(node);
hasOpened.push(node.id);
for (let i = 0; i < node.parents.length - 1; i++) {
const id = node.parents[i];

if (!hasOpened.includes(id)) {
$tree.open_node($tree.get_node(id));
hasOpened.push(id);
}
}
});
});

this.buttons = [
collapseAllButton,
expandAllButton,
expandUsedButton
];

this.buttons.forEach(function (button) {
button.type = 'button';
parent.insertBefore(button, ul);
});
},

/**
* @private
*/
_destroy: function () {
this.element.jstree('destroy');

this.buttons.forEach(function (element) {
element.parentNode.removeChild(element);
});
},

/**
Expand All @@ -64,7 +117,6 @@ define([
* @private
*/
_selectChildNodes: function (event, selected) {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
selected.instance.open_node(selected.node);
selected.node.children.each(function (id) {
var selector = '[id="' + id + '"]';
Expand All @@ -73,7 +125,6 @@ define([
selected.instance.get_node($(selector), false)
);
});
// jscs:enable requireCamelCaseOrUpperCaseIdentifiers
},

/**
Expand Down