All files / web/bundles/pimui/js/jstree jquery.jstree.tree_selector.js

88.64% Statements 78/88
66.67% Branches 16/24
88.89% Functions 16/18
88.64% Lines 78/88

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              336x     336x   336x   552x     552x 552x 552x 552x 552x 552x 552x 552x   552x         552x       552x   552x 1x     552x 552x   552x         551x 551x             2609x 2609x 2609x 5622x 12x 12x                                 2x   2x         554x 554x 554x   554x       554x           554x 554x   554x 554x   554x 554x     554x 9x     554x     1961x     554x     554x   554x 554x         553x       553x                       553x 821x   821x           821x 553x 553x                 821x     553x               554x 554x 554x   554x           553x     553x       554x             554x 554x 554x 554x   554x         554x 554x   554x     2x 2x 2x                     336x    
/* global jQuery */
/* jshint unused:vars */
/**
 * Allow to select on which tree to work and manage creation and deletion of trees
 * File: jstree.tree_selector.js
 *
/* Group: jstree tree_selector plugin */
(function ($) {
    'use strict';
 
    var tree_select_id = 'tree_select';
 
    $.jstree.plugin('tree_selector', {
        __init: function () {
            this.get_container()
                // Create the tree toolbar and load trees in tree selector
                .bind('init.jstree', $.proxy(function () {
                    var _this = this;
                    var settings = this._get_settings().tree_selector;
                    this.data.tree_selector.ajax = settings.ajax;
                    this.data.tree_selector.data = settings.data;
                    this.data.tree_selector.auto_open_root = settings.auto_open_root;
                    this.data.tree_selector.node_label_field = settings.node_label_field;
                    this.data.tree_selector.no_tree_message = settings.no_tree_message;
                    this.data.tree_selector.preselect_node_id = settings.preselect_node_id;
 
                    var tree_toolbar = $('<div>', {
                        id: 'tree_toolbar',
                        'class': 'jstree-tree-toolbar'
                    });
 
                    var tree_select = $('<select>', {
                        id: tree_select_id,
                        'class': 'input-large'
                    });
                    tree_select.addClass('jstree-tree-select');
 
                    tree_select.bind('change', function () {
                        _this.switch_tree();
                    });
 
                    tree_toolbar.html(tree_select);
                    this.get_container_ul().before(tree_toolbar);
 
                    this.load_trees();
 
                }, this))
                // Rewrite the root node to link it to the selected tree
                .bind('loaded.jstree', $.proxy(function (event) {
                    Eif (event.namespace === 'jstree') {
                        this.switch_tree();
                    }
                    // Select the node marked as 'toselect' from the server
 
                }, this))
                .bind('clean_node.jstree', $.proxy(function (e, data) {
                    // Switch to node clicked when requested by the data for this node
                    var _this = this;
                    Eif (data.rslt.obj) {
                        $.each(data.rslt.obj, function (index, node) {
                            if ($(node).hasClass('toselect')) {
                                _this.select_node($(node));
                                $(node).removeClass('toselect');
                            }
                        });
                    }
                }, this))
                ;
        },
        defaults: {
            ajax: false,
            data: false,
            tree_selector_buttons: false,
            no_tree_message: false,
            node_label_field: 'code',
            preselect_node_id: false
        },
        _fn: {
            refresh: function () {
                this.refresh_trees();
 
                return this.__call_old();
            },
            switch_tree: function () {
                // Create new root node, place it into the tree and
                // open it if setup to auto_open_root
                var selected_tree = this.get_tree_select().find(':selected');
                var root_node_id = $(selected_tree).prop('value');
                var root_node_code = $(selected_tree).attr('data-code');
 
                Iif (!root_node_id || (root_node_id === -1)) {
                    return null;
                }
 
                var root_node = this._prepare_node(
                    'node_' + root_node_id,
                    selected_tree.text(),
                    root_node_code
                );
 
                this.get_container_ul().empty();
                this.get_container_ul().append(root_node);
 
                this.close_node(root_node);
                this.clean_node();
 
                Eif (this.data.tree_selector.auto_open_root) {
                    this.open_node(root_node);
                }
 
                if (this.data.tree_selector.preselect_node_id === root_node_id) {
                    this.select_node(root_node);
                }
 
                this.get_container().trigger('after_tree_loaded.jstree', root_node_id);
            },
            get_tree_select: function () {
                return $('#' + tree_select_id);
            },
            load_trees: function () {
                var _this = this;
                var trees;
 
                Iif (this.data.tree_selector.data) {
                    trees = this._load_data_trees();
                } else Eif (this.data.tree_selector.ajax) {
                    trees = this._load_ajax_trees();
                } else {
                    throw 'jquery.jstree.tree_selector : Neither data nor ajax settings supplied for trees.';
                }
 
                this.get_tree_select().empty();
 
                // In case of no tree loaded, display the no_tree_message
                // if it has been set up
                Iif (trees.length === 0 && this.data.tree_selector.no_tree_message) {
                    var no_tree_option = $('<option>', {
                        text: this.data.tree_selector.no_tree_message,
                        value: -1,
                        disabled: true,
                        selected: true
                    });
                    this.get_container_ul().empty();
                    this.get_tree_select().append(no_tree_option);
 
                }
 
                $.each(trees, function (index, tree) {
                    var option_text = tree[_this.data.tree_selector.node_label_field];
 
                    var option = $('<option>', {
                        value: tree.id,
                        text: option_text,
                        'data-code': tree.code
                    });
 
                    if (tree.selected === 'true') {
                        option.prop('defaultSelected', true);
                        _this._get_settings().json_data.data = [
                            {
                                'data': option_text,
                                'state': 'closed',
                                'attr': { 'id': 'node_' + tree.id}
                            }
                        ];
                    }
 
                    _this.get_tree_select().append(option);
                });
 
                this.get_container().trigger('trees_loaded.jstree', tree_select_id);
            },
            _load_data_trees: function () {
                var trees_data = this.data.tree_selector.data;
 
                return $.parseJSON(trees_data);
            },
            _load_ajax_trees: function () {
                var trees_url = this.data.tree_selector.ajax.url;
                var trees_url_parameters = this.data.tree_selector.ajax.parameters;
                var trees = [];
 
                $.ajax({
                    url: trees_url,
                    async: false,
                    dataType: 'json',
                    data: trees_url_parameters
                }).done(function (ajax_trees) {
                    trees = ajax_trees;
                });
 
                return trees;
 
            },
            _prepare_node: function (id, node_name, node_code) {
                var node = $('<li>', {
                    id: id,
                    rel: 'folder',
                    'data-code': node_code
                });
 
                // Make the node 'openable' by switching back to initial state
                node.prepend('<ins class="jstree-icon">&#160;</ins>');
                node.removeClass('jstree-leaf');
                node.addClass('jstree-close');
                node.addClass('jstree-closed');
 
                var node_link = $('<a>', {
                    href: '#',
                    text: node_name
                });
 
                node_link.prepend('<ins class="jstree-icon">&#160;</ins>');
                node.append(node_link);
 
                return node;
            },
            refresh_trees: function () {
                this.get_tree_select().empty();
                this.load_trees();
                this.switch_tree();
            },
            get_tree_id: function () {
                var root_node = this.get_container_ul().find('li')[0];
 
                return $(root_node).attr('id');
 
            }
        }
    });
    // include the tree_selector plugin by default on available plugins list
    $.jstree.defaults.plugins.push('tree_selector');
})(jQuery);