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

68.75% Statements 44/64
70.45% Branches 31/44
71.43% Functions 10/14
68.75% Lines 44/64

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 21421x                             21x 32x 32x     32x 32x 32x 32x 32x   32x   32x                                                                                                           33x   33x 33x         33x                                             32x 31x   32x 31x   32x 32x 1x 1x 1x                           1x     1x 1x               21x 1x   20x 20x 20x 19x   19x   1x 1x         1x 1x 1x 1x 1x                 31x 31x                                                           32x        
define(
    [
        'jquery',
        'underscore',
        'backbone',
        'routing',
        'oro/loading-mask',
        'oro/error',
        'pim/ui',
        'jquery.jstree',
        'jstree/jquery.jstree.tree_selector'
    ],
    function ($, _, Backbone, Routing, LoadingMask, OroError, UI) {
        'use strict';
 
        return function (elementId, prefixRoute) {
            var $el = $(elementId);
            Iif (!$el || !$el.length || !_.isObject($el)) {
                throw new Error('Unable to instantiate tree on this element');
            }
            var selectedNode       = $el.attr('data-node-id') || -1;
            var selectedTree       = $el.attr('data-tree-id') || -1;
            var selectedNodeOrTree = selectedNode in [0, -1] ? selectedTree : selectedNode;
            var preventFirst       = selectedNode > 0;
            var loadingMask        = new LoadingMask();
 
            loadingMask.render().$el.appendTo($('#category-tree-container'));
 
            this.config = {
                core: {
                    animation: 200
                },
                plugins: [
                    'tree_selector',
                    'themes',
                    'json_data',
                    'ui',
                    'crrm',
                    'types'
                ],
                contextmenu: {
                    items: {
                        create: {
                            label: _.__('pim_common.create')
                        },
                        ccp: false,
                        rename: false,
                        remove: false
                    }
                },
                tree_selector: {
                    ajax: {
                        url: Routing.generate(
                            prefixRoute + '_categorytree_listtree',
                            {
                                _format: 'json',
                                select_node_id: selectedNodeOrTree,
                                context: 'manage',
                                with_items_count: 0
                            }
                        )
                    },
                    auto_open_root: true,
                    node_label_field: 'label',
                    no_tree_message: _.__('jstree.no_tree'),
                    preselect_node_id: selectedNode
                },
                themes: {
                    dots: true,
                    icons: true
                },
                json_data: {
                    ajax: {
                        url: Routing.generate(
                            prefixRoute + '_categorytree_children',
                            {
                                _format: 'json',
                                context: 'manage'
                            }
                        ),
                        data: function (node) {
                            // the result is fed to the AJAX request `data` option
                            var id = null;
 
                            Eif (node && node !== -1 && node.attr) {
                                id = node.attr('id').replace('node_', '');
                            } else {
                                id = -1;
                            }
 
                            return {
                                id: id,
                                select_node_id: selectedNode,
                                with_items_count: 0
                            };
                        }
                    }
                },
                types: {
                    max_depth: -2,
                    max_children: -2,
                    valid_children: ['folder'],
                    types: {
                        'default': {
                            valid_children: 'folder'
                        }
                    }
                },
                ui: {
                    select_limit: 1,
                    select_multiple_modifier: false
                }
            };
            if ($el.attr('data-editable')) {
                this.config.plugins.push('dnd');
            }
            if ($el.attr('data-creatable')) {
                this.config.plugins.push('contextmenu');
            }
            this.init = function () {
                $el.jstree(this.config).on('move_node.jstree', function (e, data) {
                    var this_jstree = $.jstree._focused();
                    data.rslt.o.each(function (i) {
                        $.ajax({
                            async: false,
                            type: 'POST',
                            url: Routing.generate(prefixRoute + '_categorytree_movenode'),
                            data: {
                                id: $(this).attr('id').replace('node_', ''),
                                parent: data.rslt.cr === -1 ? 1 : data.rslt.np.attr('id').replace('node_', ''),
                                prev_sibling: this_jstree._get_prev(this, true) ?
                                    this_jstree._get_prev(this, true).attr('id').replace('node_', '') : null,
                                position: data.rslt.cp + i,
                                code: data.rslt.name,
                                copy: data.rslt.cy ? 1 : 0
                            },
                            success: function (r) {
                                Iif (!r.status) {
                                    this_jstree.rollback(data.rlbk);
                                } else {
                                    $(data.rslt.oc).attr('id', r.id);
                                    Iif (data.rslt.cy && $(data.rslt.oc).children('UL').length) {
                                        data.inst.refresh(data.inst._get_parent(data.rslt.oc));
                                    }
                                }
                            }
                        });
                    });
                }).on('select_node.jstree', function (e, data) {
                    if (!$el.attr('data-editable')) {
                        return;
                    }
                    var id  = data.rslt.obj.attr('id').replace('node_', '');
                    var url = Routing.generate(prefixRoute + '_categorytree_edit', { id: id });
                    if ('#' + url === Backbone.history.location.hash || preventFirst) {
                        preventFirst = false;
 
                        return;
                    }
                    loadingMask.show();
                    $.ajax({
                        async: true,
                        type: 'GET',
                        url: url + '?content=form',
                        success: function (data) {
                            Eif (data) {
                                $('#category-form').html(data);
                                Backbone.history.navigate('#' + url, {trigger: false});
                                UI($('#category-form'));
                                loadingMask.hide();
                            }
                        },
                        error: function (jqXHR) {
                            OroError.dispatch(null, jqXHR);
                            loadingMask.hide();
                        }
                    });
                }).on('loaded.jstree', function (event, data) {
                    Eif (event.namespace === 'jstree') {
                        data.inst.get_tree_select().select2({ width: '100%' });
                    }
                }).on('create.jstree', function (e, data) {
                    $.jstree._focused().lock();
                    var id       = data.rslt.parent.attr('id').replace('node_', '');
                    var url      = Routing.generate(prefixRoute + '_categorytree_create', { parent: id });
                    var position = data.rslt.position;
                    var label    = data.rslt.name;
 
                    url = url + '?label=' + label + '&position=' + position;
                    loadingMask.show();
                    $.ajax({
                        async: true,
                        type: 'GET',
                        url: url + '&content=form',
                        success: function (data) {
                            if (data) {
                                $('#category-form').html(data);
                                Backbone.history.navigate('#' + url, {trigger: false});
                                loadingMask.hide();
                            }
                        },
                        error: function (jqXHR) {
                            OroError.dispatch(null, jqXHR);
                            loadingMask.hide();
                        }
                    });
                });
            };
 
            this.init();
        };
    }
);