All files / web/bundles/pimui/js tree-associate.jstree.js

76.34% Statements 71/93
72.97% Branches 27/37
68.42% Functions 13/19
76.34% Lines 71/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222384x         384x 43x 43x 23x   20x 20x 20x 20x 20x 20x   20x                                   109x                   24x   24x   6x   6x                         18x                   24x 24x   24x 3x   21x 15x   21x     24x       23x   23x                                           20x 31x 31x   31x 31x 31x   31x 31x 31x     31x 21x       20x 21x 21x   21x 20x     21x 16x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x         21x                               20x                         20x         20x         20x 20x     20x 20x           20x 20x   20x     20x 20x 20x     20x        
define(
    ['jquery', 'underscore', 'routing', 'oro/mediator', 'jquery.jstree'],
    function ($, _, Routing, mediator) {
        'use strict';
 
        return function (elementId, hiddenCategoryId, routes) {
            var $el = $(elementId);
            if (!$el || !$el.length || !_.isObject($el)) {
                return;
            }
            var self         = this;
            var currentTree  = -1;
            var id           = $el.attr('data-id');
            var selectedTree = $el.attr('data-selected-tree');
            var dataLocale   = $el.attr('data-datalocale');
            var locked       = false;
 
            this.config = {
                core: {
                    animation: 200,
                    html_titles: true,
                    strings: { loading:  _.__('pim_common.loading') }
                },
                plugins: [
                    'themes',
                    'json_data',
                    'ui',
                    'types',
                    'checkbox'
                ],
                checkbox: {
                    two_state: true,
                    real_checkboxes: true,
                    override_ui: true,
                    real_checkboxes_names: function (n) {
                        return ['category_' + n[0].id, 1];
                    }
                },
                themes: {
                    dots: true,
                    icons: true
                },
                json_data: {
                    ajax: {
                        url: function (node) {
                            var treeHasItem = $('#tree-link-' + currentTree).hasClass('tree-has-item');
 
                            if ((!node || (node === -1)) && treeHasItem) {
                                // First load of the tree: get the checked categories
                                var selected = this.parseHiddenCategories();
 
                                return Routing.generate(
                                    routes.list_categories,
                                    {
                                        id: id,
                                        categoryId: currentTree,
                                        _format: 'json',
                                        dataLocale: dataLocale,
                                        context: 'associate',
                                        selected: selected
                                    }
                                );
                            }
 
                            return Routing.generate(
                                routes.children,
                                {
                                    _format: 'json',
                                    dataLocale: dataLocale,
                                    context: 'associate'
                                }
                            );
                        }.bind(this),
                        data: function (node) {
                            var data           = {};
                            var treeHasItem = $('#tree-link-' + currentTree).hasClass('tree-has-item');
 
                            if (node && node !== -1 && node.attr) {
                                data.id = node.attr('id').replace('node_', '');
                            } else {
                                if (!treeHasItem) {
                                    data.id = currentTree;
                                }
                                data.include_parent = 'true';
                            }
 
                            return data;
                        },
                        complete: function () {
                            // Disable the root checkbox
                            $('.jstree-root>input.jstree-real-checkbox').attr('disabled', 'disabled');
                            // Lock the loaded tree if the state is locked
                            Iif (locked) {
                                this.lock();
                            }
                        }
                    }
                },
                types: {
                    max_depth: -2,
                    max_children: -2,
                    valid_children: ['folder'],
                    types: {
                        'default': {
                            valid_children: 'folder'
                        }
                    }
                },
                ui: {
                    select_limit: 1,
                    select_multiple_modifier: false
                }
            };
 
            this.switchTree = function (treeId) {
                currentTree = treeId;
                var $tree = $('#tree-' + treeId);
 
                $('#trees').find('> div').hide();
                $('#trees-list').find('li').removeClass('active');
                $('#tree-link-' + treeId).parent().addClass('active');
 
                $('.tree[data-tree-id=' + treeId + ']').show();
                $tree.show();
                $('#tree-link-' + treeId).trigger('shown');
 
                // If empty, load the associated jstree
                if ($tree.children('ul').length === 0) {
                    self.initTree(treeId);
                }
            };
 
            this.initTree = function (treeId) {
                var $tree = $('#tree-' + treeId);
                $tree.jstree(self.config);
 
                $tree.on('loaded.jstree', function () {
                    mediator.trigger('jstree:loaded');
                });
 
                $tree.on('check_node.jstree', function (e, d) {
                    if (d.inst.get_checked() && $(d.rslt.obj[0]).hasClass('jstree-root') === false) {
                        var selected = this.parseHiddenCategories();
                        var id = d.rslt.obj[0].id.replace('node_', '');
                        Eif ($.inArray(id, selected) < 0) {
                            selected.push(id);
                            selected = $.unique(selected);
                            selected = selected.join(',');
                            $(hiddenCategoryId).val(selected).trigger('change');
                            var treeId = e.target.id;
                            var treeLinkId = treeId.replace('-', '-link-');
                            $('#' + treeLinkId + ' i').addClass('AknAcl-icon--granted');
                        }
                    }
                }.bind(this));
 
                $tree.on('uncheck_node.jstree', function (e, d) {
                    if (d.inst.get_checked()) {
                        var selected = this.parseHiddenCategories();
                        var id = d.rslt.obj[0].id.replace('node_', '');
                        selected.splice($.inArray(id, selected), 1);
                        selected = selected.join(',');
                        $(hiddenCategoryId).val(selected).trigger('change');
                        var treeId = e.target.id;
                        if ($('#' + treeId).jstree('get_checked').length === 0) {
                            var treeLinkId = treeId.replace('-', '-link-');
                            $('#' + treeLinkId + ' i').removeClass('AknAcl-icon--granted');
                        }
                    }
                }.bind(this));
            };
 
            var setLocked = function () {
                $('#trees-list').find('a').each(function () {
                    var ref = $.jstree._reference(this.id.replace('tree-link-', '#tree-'));
                    if (ref) {
                        if (locked) {
                            ref.lock();
                        } else {
                            ref.unlock();
                        }
                    }
                });
            };
 
            this.lock = function () {
                locked = true;
                setLocked();
            };
 
            this.unlock = function () {
                locked = false;
                setLocked();
            };
 
            this.bindEvents = function () {
                $('#trees-list').on('click', 'a', function () {
                    self.switchTree(this.id.replace('tree-link-', ''));
                });
                mediator.on('jstree:lock', this.lock);
                mediator.on('jstree:unlock', this.unlock);
            };
 
            /**
             * @return {Array}
             */
            this.parseHiddenCategories = function () {
                var hiddenValue = $(hiddenCategoryId).val();
 
                return hiddenValue.length > 0 ? hiddenValue.split(',') : [];
            };
 
            this.init = function () {
                self.switchTree(selectedTree);
                self.bindEvents();
            };
 
            this.init();
        };
    }
);