Completed
Push — master ( 91c77a...12ca8b )
by Yannick
08:33
created

$.fn.sidebar   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 24
rs 8.9713
1
/**
2
 * Create a new sidebar on this jQuery object.
3
 *
4
 * @example
5
 * var sidebar = $('#sidebar').sidebar();
6
 *
7
 * @param {Object} [options] - Optional options object
0 ignored issues
show
Documentation Bug introduced by
The parameter [options] does not exist. Did you maybe mean options instead?
Loading history...
8
 * @param {string} [options.position=left] - Position of the sidebar: 'left' or 'right'
0 ignored issues
show
Documentation Bug introduced by
The parameter [options.position=left] does not exist. Did you maybe mean options instead?
Loading history...
9
 * @returns {jQuery}
10
 */
11
$.fn.sidebar = function(options) {
12
    var $sidebar = this;
13
    var $tabs = $sidebar.find('ul.sidebar-tabs, .sidebar-tabs > ul');
14
    var $container = $sidebar.children('.sidebar-content').first();
15
16
    options = $.extend({
17
        position: 'left'
18
    }, options || {});
19
20
    $sidebar.addClass('sidebar-' + options.position);
21
22
    $tabs.children('li').children('a').on('click', function(e) {
23
        e.preventDefault();
24
        var $tab = $(this).closest('li');
25
26
        if ($tab.hasClass('active'))
27
            $sidebar.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
        else if (!$tab.hasClass('disabled') && this.hash.slice(1) != '')
29
            $sidebar.open(this.hash.slice(1), $tab);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30
    });
31
32
    $sidebar.find('.sidebar-close').on('click', function() {
33
        $sidebar.close();
34
    });
35
36
    /**
37
     * Open sidebar (if necessary) and show the specified tab.
38
     *
39
     * @param {string} id - The id of the tab to show (without the # character)
40
     * @param {jQuery} [$tab] - The jQuery object representing the tab node (used internally for efficiency)
0 ignored issues
show
Documentation introduced by
The parameter [$tab] does not exist. Did you maybe forget to remove this comment?
Loading history...
41
     */
42
    $sidebar.open = function(id, $tab) {
43
        if (typeof $tab === 'undefined')
44
            $tab = $tabs.find('li > a[href="#' + id + '"]').parent();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
45
46
        // hide old active contents
47
        $container.children('.sidebar-pane.active').removeClass('active');
48
49
        // show new content
50
         $container.children('#' + id).addClass('active');
51
52
        // remove old active highlights
53
        $tabs.children('li.active').removeClass('active');
54
55
        // set new highlight
56
        $tab.addClass('active');
57
58
        $sidebar.trigger('content', { 'id': id });
59
60
        if ($sidebar.hasClass('collapsed')) {
61
            // open sidebar
62
            $sidebar.trigger('opening');
63
            $sidebar.removeClass('collapsed');
64
        }
65
    };
66
67
    /**
68
     * Close the sidebar (if necessary).
69
     */
70
    $sidebar.close = function() {
71
        // remove old active highlights
72
        $tabs.children('li.active').removeClass('active');
73
74
        if (!$sidebar.hasClass('collapsed')) {
75
            // close sidebar
76
            $sidebar.trigger('closing');
77
            $sidebar.addClass('collapsed');
78
        }
79
    };
80
81
    return $sidebar;
82
};
83