/
jquery.touchdown.js
54 lines (38 loc) · 1.42 KB
/
jquery.touchdown.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
/**
* jQuery Touchdown Plugin <https://github.com/samuelcotterall/Touchdown>
*
* By Samuel Cotterall <http://samuelcotterall.com>
*/
(function($) {
$.fn.Touchdown = function() {
return this.each(function() {
$this = $(this);
var listDepth = $this.parents().length,
anchor = $this.find('a'),
title = 'Navigate',
optionList;
// Create a default `<option>` for the list - If this is missing, fall back to 'Select'
if ($this.attr('title')) {
title = $this.attr('title');
}
optionList += '<option value="">' + title + '</option>';
// Convert each anchor to an `<option>`
for (var i=0; i < anchor.length; i++) {
var a = $(anchor[i]), // Current <a>
linkDepth = ((a.parents().length - listDepth) / 2) - 1, // Current <a>'s depth minus main list's depth divided by 2 (account for both <ul> and <li> parents) minus 1
indent = ''; // Reset indent
while (linkDepth > 0){ // Append a space for each level
indent += '\u00a0 ';
linkDepth--;
}
optionList += '<option value="' + a.attr('href') + '">' + indent + a.text() + '</option>';
}
// DOM manipulation
$this.addClass('touchdown-list').after('<select class="touchdown"> ' + optionList +'</select>');
// Event handler
$this.next('select').change(function(){
window.location = $(this).val();
});
});
};
})(jQuery);