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

96.3% Statements 26/27
50% Branches 1/2
85.71% Functions 6/7
96.3% Lines 26/27

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            316x     316x 316x   316x   520x     520x 520x 520x 520x 520x 520x   520x         520x             520x   520x                 520x           520x   2x 2x 2x 2x         520x 520x 520x 520x                               316x      
/* global jQuery */
/**
 * Allow to select on which tree to work and manage creation and deletion of trees
 * File: jstree.nested_switch.js
 *
/* Group: jstree nested_switch plugin */
(function ($) {
    'use strict';
 
    require(['bootstrap.bootstrapswitch'], function () {
        var nested_switch_id = 'nested_switch_input';
 
        $.jstree.plugin('nested_switch', {
            __init: function () {
                var container = this.get_container();
 
                // Create the tree toolbar and load trees in tree selector
                container.bind('init.jstree', $.proxy(function () {
                    var settings = this._get_settings().nested_switch;
                    this.data.nested_switch.state = settings.state;
                    this.data.nested_switch.label = settings.label;
                    this.data.nested_switch.callback = settings.callback;
                    var _this = this;
 
                    var nested_switch_bar = $('<div>', {
                        id: 'nested-switch',
                        class: 'jstree-footer'
                    });
 
                    var nested_switch = $('<input>', {
                        type: 'checkbox',
                        id: nested_switch_id,
                        'class': 'input-large jstree-tree-select',
                        checked: !!this.data.nested_switch.state
                    });
 
                    var __ = require('oro/translator');
 
                    var switch_wrapper = $('<div>', {
                        'class': 'switch switch-small pull-right',
                        'attr': {
                            'data-on-label': __('pim_common.yes'),
                            'data-off-label': __('pim_common.no'),
                            'data-animated': false
                        }
                    }).html(nested_switch);
 
                    var nested_switch_label = $('<label>', {
                        'for': nested_switch_id,
                        'html': this.data.nested_switch.label,
                        'class': 'control-label pull-left'
                    });
 
                    switch_wrapper.on('switch-change', function (e, data) {
                        // Execute callback with a timeout to give bootstrapSwitch time to change the switch
                        setTimeout(function () {
                            var callback = _this.data.nested_switch.callback;
                            Eif (callback) {
                                callback(data.value);
                            }
                        }, 25);
                    });
 
                    nested_switch_bar.html(nested_switch_label);
                    nested_switch_bar.append(switch_wrapper.bootstrapSwitch());
                    container.append(nested_switch_bar);
                    container.closest('.sidebar-content').css('height', 'calc(100% - 60px)');
 
                }, this));
            },
            defaults: {
                state: true,
                label: null,
                callback: null
            },
            _fn: {
                get_nested_switch: function () {
                    return $('#' + nested_switch_id);
                }
            }
        });
        // include the nested_switch plugin by default on available plugins list
        $.jstree.defaults.plugins.push('nested_switch');
    });
})(jQuery);