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

92.13% Statements 82/89
81.82% Branches 36/44
86.36% Functions 19/22
92.13% Lines 82/89

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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258316x                         316x 316x 316x 316x 316x 316x 316x 316x 316x   316x 3138x 522x     2616x     316x 637x     316x 522x                       316x 520x                   316x 1044x     316x       316x 1048x 1048x         1048x 519x       316x 547x   547x     316x 520x                                   2x   2x 2x 2x 2x                                         526x                                                   316x 520x             316x 1042x     316x 522x     316x 525x 525x   525x   3x 3x 3x   522x     525x 525x       316x 621x   621x 523x     621x     316x 535x   524x   11x 11x   11x 1x 1x   10x 10x   11x     316x   520x       520x 520x 520x 520x 520x 520x 520x   520x       1902x                                          
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'routing',
        'jquery.jstree',
        'jstree/jquery.jstree.tree_selector',
        'jstree/nested_switch'
    ],
    function ($, _, __, Routing) {
        'use strict';
 
        var unclassified      = -1;
        var all               = -2;
        var selectedNode      = 0;
        var selectedTree      = 0;
        var includeSub        = true;
        var dataLocale        = null;
        var relatedEntity     = null;
        var $el               = null;
        var categoryBaseRoute = '';
 
        var getActiveNode = function (skipVirtual) {
            if (skipVirtual) {
                return selectedNode > 0 ? selectedNode : selectedTree;
            }
 
            return selectedNode !== 0 ? selectedNode : selectedTree;
        };
 
        var triggerUpdate = function () {
            $el.trigger('tree.updated');
        };
 
        var getTreeUrl = function () {
            return Routing.generate(
                getRoute('listtree'),
                {
                    _format:        'json',
                    dataLocale:     dataLocale,
                    select_node_id: getActiveNode(true),
                    include_sub:    +includeSub,
                    context:        'view'
                }
            );
        };
 
        var getChildrenUrl = function () {
            return Routing.generate(
                getRoute('children'),
                {
                    _format:    'json',
                    dataLocale: dataLocale,
                    context:    'view'
                }
            );
        };
 
        var selectNode = function (nodeId) {
            $el.jstree('select_node', '#node_' + nodeId);
        };
 
        var clearSelection = function () {
            $el.jstree('deselect_all');
        };
 
        var createNode = function (id, target, title) {
            var targetId = target !== null ? '#' + target : -1;
            $el.jstree('create', targetId, 'last', {
                attr: { 'class': 'jstree-unclassified', id: 'node_' + id },
                data: { title: __(title) }
            }, null, true);
 
            if (id === getActiveNode()) {
                selectNode(id);
            }
        };
 
        var getNodeId = function (node) {
            var nodeId = (node && node.attr && node.attr('id')) ? node.attr('id').replace('node_', '') : '';
 
            return +nodeId;
        };
 
        var getTreeConfig = function () {
            return {
                core: {
                    animation: 200,
                    strings: { loading: _.__('pim_common.loading') }
                },
                plugins: [
                    'tree_selector',
                    'nested_switch',
                    'themes',
                    'json_data',
                    'ui',
                    'crrm',
                    'types'
                ],
                nested_switch: {
                    state:    includeSub,
                    label:    __('jstree.include_sub'),
                    callback: function (state) {
                        includeSub = state;
 
                        $el.jstree('instance').data.tree_selector.ajax.url = getTreeUrl();
                        $el.jstree('refresh');
                        $el.trigger('after_tree_loaded.jstree');
                        triggerUpdate();
                    }
 
                },
                tree_selector: {
                    ajax: {
                        'url': getTreeUrl()
                    },
                    auto_open_root: true,
                    node_label_field: 'label',
                    preselect_node_id: getActiveNode()
                },
                themes: {
                    dots: true,
                    icons: true
                },
                json_data: {
                    ajax: {
                        url: getChildrenUrl(),
                        data: function (node) {
                            // the result is fed to the AJAX request `data` option
                            return {
                                id: getNodeId(node),
                                select_node_id: getActiveNode(),
                                with_items_count: 1,
                                include_sub: +includeSub
                            };
                        }
                    }
                },
                types: {
                    max_depth: -2,
                    max_children: -2,
                    valid_children: ['folder'],
                    types: {
                        'default': {
                            valid_children: 'folder'
                        }
                    }
                },
                ui: {
                    select_limit: 1,
                    select_multiple_modifier: false
                }
            };
        };
 
        var initTree = function () {
            $el.jstree(getTreeConfig())
                .on('trees_loaded.jstree', onTreesLoaded)
                .on('after_tree_loaded.jstree', afterTreeLoaded)
                .on('after_open.jstree correct_state.jstree', afterOpenNode)
                .on('select_node.jstree', onSelectNode);
        };
 
        var getRoute = function (routeName) {
            return categoryBaseRoute + '_' + routeName;
        };
 
        var onTreesLoaded = function (event, tree_select_id) {
            $('#' + tree_select_id).select2({ width: '100%' });
        };
 
        var afterTreeLoaded = function (e, root_node_id) {
            var previousTree = selectedTree;
            selectedTree = +root_node_id;
 
            if (previousTree && previousTree !== selectedTree) {
                // Tree was switched by user, select the root node
                selectedNode = 0;
                selectNode(selectedTree);
                triggerUpdate();
            } else {
                selectNode(getActiveNode());
            }
 
            Eif (!$('#node_' + all).length) {
                createNode(all, null, 'jstree.' + relatedEntity + '.all');
            }
        };
 
        var afterOpenNode = function (e, data) {
            var $node = $(data.args[0]);
 
            if ($node.attr('rel') === 'folder' && !$('#node_' + unclassified).length) {
                createNode(unclassified, $node.attr('id'), 'jstree.' + relatedEntity + '.unclassified');
            }
 
            triggerUpdate();
        };
 
        var onSelectNode = function (e, data) {
            if (data.args.length === 1) {
                // Return if the select was not user triggered
                return;
            }
            var $node = $(data.args).parent();
            var nodeId = getNodeId($node);
 
            if ($node.attr('rel') === 'folder' && !$node.hasClass('jstree-unclassified')) {
                selectedNode = 0;
                selectedTree = nodeId;
            } else {
                selectedNode = nodeId;
                selectedTree = getNodeId($el.find('li').first());
            }
            triggerUpdate();
        };
 
        return {
            init: function ($element, state, baseRoute) {
                Iif (!$element || !$element.length || !_.isObject($element)) {
                    return;
                }
 
                $el               = $element;
                dataLocale        = $el.attr('data-datalocale');
                relatedEntity     = $el.attr('data-relatedentity');
                selectedNode      = _.has(state, 'selectedNode') ? state.selectedNode : selectedNode;
                selectedTree      = _.has(state, 'selectedTree') ? state.selectedTree : selectedTree;
                includeSub        = _.has(state, 'includeSub')   ? state.includeSub   : includeSub;
                categoryBaseRoute = baseRoute;
 
                initTree();
            },
 
            getState: function () {
                return {
                    selectedNode: selectedNode,
                    selectedTree: selectedTree,
                    includeSub:   includeSub
                };
            },
 
            refresh: function () {
                initTree();
            },
 
            reset: function () {
                if ($el) {
                    clearSelection();
                    selectedNode = all;
                    selectNode(selectedNode);
                }
            }
        };
    }
);