99define ( [
1010 'jquery' ,
1111 'jquery/ui' ,
12- 'jquery/jstree/jquery.jstree'
12+ 'jquery/jstree/jquery.jstree' ,
13+ 'mage/translate' ,
1314] , function ( $ ) {
1415 'use strict' ;
1516
17+ // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
1618 $ . widget ( 'mage.rolesTree' , {
1719 options : {
1820 treeInitData : { } ,
@@ -26,9 +28,7 @@ define([
2628 this . element . jstree ( {
2729 plugins : [ 'checkbox' ] ,
2830 checkbox : {
29- // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
3031 three_state : false ,
31- // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
3232 visible : this . options . checkboxVisible ,
3333 cascade : 'undetermined'
3434 } ,
@@ -40,13 +40,64 @@ define([
4040 }
4141 } ) ;
4242 this . _bind ( ) ;
43+ this . _createButtons ( ) ;
44+ } ,
45+
46+ _createButtons : function ( ) {
47+ const $tree = $ . jstree . reference ( this . element ) ;
48+
49+ const collapseAllButton = document . createElement ( 'button' ) ;
50+ collapseAllButton . innerText = $ . mage . __ ( 'Collapse all' ) ;
51+ collapseAllButton . addEventListener ( 'click' , function ( ) {
52+ $tree . close_all ( ) ;
53+ } ) ;
54+
55+ const expandAllButton = document . createElement ( 'button' ) ;
56+ expandAllButton . innerText = $ . mage . __ ( 'Expand all' ) ;
57+ expandAllButton . addEventListener ( 'click' , function ( ) {
58+ $tree . open_all ( ) ;
59+ } ) ;
60+
61+ const expandUsedButton = document . createElement ( 'button' ) ;
62+ expandUsedButton . innerText = $ . mage . __ ( 'Expand selected' ) ;
63+ expandUsedButton . addEventListener ( 'click' , function ( ) {
64+ const hasOpened = [ ] ;
65+ $tree . get_checked ( true ) . forEach ( function ( node ) {
66+ $tree . open_node ( node ) ;
67+ hasOpened . push ( node . id ) ;
68+ for ( let i = 0 ; i < node . parents . length - 1 ; i ++ ) {
69+ const id = node . parents [ i ] ;
70+ if ( ! hasOpened . includes ( id ) ) {
71+ $tree . open_node ( $tree . get_node ( id ) ) ;
72+ hasOpened . push ( id ) ;
73+ }
74+ }
75+ } ) ;
76+ } ) ;
77+
78+ this . buttons = [
79+ collapseAllButton ,
80+ expandAllButton ,
81+ expandUsedButton ,
82+ ] ;
83+
84+ const parent = this . element [ 0 ] ;
85+ const ul = this . element . find ( 'ul' ) [ 0 ] ;
86+ this . buttons . forEach ( function ( button ) {
87+ button . type = 'button' ;
88+ parent . insertBefore ( button , ul ) ;
89+ } ) ;
4390 } ,
4491
4592 /**
4693 * @private
4794 */
4895 _destroy : function ( ) {
4996 this . element . jstree ( 'destroy' ) ;
97+
98+ this . buttons . forEach ( function ( element ) {
99+ element . parentNode . removeChild ( element ) ;
100+ } ) ;
50101 } ,
51102
52103 /**
@@ -64,7 +115,6 @@ define([
64115 * @private
65116 */
66117 _selectChildNodes : function ( event , selected ) {
67- // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
68118 selected . instance . open_node ( selected . node ) ;
69119 selected . node . children . each ( function ( id ) {
70120 var selector = '[id="' + id + '"]' ;
@@ -73,7 +123,6 @@ define([
73123 selected . instance . get_node ( $ ( selector ) , false )
74124 ) ;
75125 } ) ;
76- // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
77126 } ,
78127
79128 /**
0 commit comments