|
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 |
|
|
|
|
|
|
8
|
|
|
* @param {string} [options.position=left] - Position of the sidebar: 'left' or 'right' |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
28
|
|
|
else if (!$tab.hasClass('disabled') && this.hash.slice(1) != '') |
|
29
|
|
|
$sidebar.open(this.hash.slice(1), $tab); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
$sidebar.open = function(id, $tab) { |
|
43
|
|
|
if (typeof $tab === 'undefined') |
|
44
|
|
|
$tab = $tabs.find('li > a[href="#' + id + '"]').parent(); |
|
|
|
|
|
|
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
|
|
|
|