-
Notifications
You must be signed in to change notification settings - Fork 207
/
tinynav.js
82 lines (66 loc) · 2.29 KB
/
tinynav.js
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
/*! http://tinynav.viljamis.com v1.2 by @arielsalminen */
(function ($, window, i) {
$.fn.tinyNav = function (options) {
// Default settings
var settings = $.extend({
'active' : 'selected', // String: Set the "active" class
'header' : '', // String: Specify text for "header" and show header instead of the active item
'indent' : '- ', // String: Specify text for indenting sub-items
'label' : '' // String: sets the <label> text for the <select> (if not set, no label will be added)
}, options);
return this.each(function () {
// Used for namespacing
i++;
var $nav = $(this),
// Namespacing
namespace = 'tinynav',
namespace_i = namespace + i,
l_namespace_i = '.l_' + namespace_i,
$select = $('<select/>').attr("id", namespace_i).addClass(namespace + ' ' + namespace_i);
if ($nav.is('ul,ol')) {
if (settings.header !== '') {
$select.append(
$('<option/>').text(settings.header)
);
}
// Build options
var options = '';
$nav
.addClass('l_' + namespace_i)
.find('a')
.each(function () {
options += '<option value="' + $(this).attr('href') + '">';
var j;
for (j = 0; j < $(this).parents('ul, ol').length - 1; j++) {
options += settings.indent;
}
options += $(this).text() + '</option>';
});
// Append options into a select
$select.append(options);
// Select the active item
if (!settings.header) {
$select
.find(':eq(' + $(l_namespace_i + ' li')
.index($(l_namespace_i + ' li.' + settings.active)) + ')')
.attr('selected', true);
}
// Change window location
$select.change(function () {
window.location.href = $(this).val();
});
// Inject select
$(l_namespace_i).after($select);
// Inject label
if (settings.label) {
$select.before(
$("<label/>")
.attr("for", namespace_i)
.addClass(namespace + '_label ' + namespace_i + '_label')
.append(settings.label)
);
}
}
});
};
})(jQuery, this, 0);