/
jquery.mobileselect.js
71 lines (66 loc) · 2.21 KB
/
jquery.mobileselect.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
/**
* jQuery Mobile Select
* @Author: Jochen Vandendriessche <jochen@builtbyrobot.com>
* @Author URI: http://builtbyrobot.com
*
* @TODO:
* - create a before / after hook so we can fix some things euhm... before and
* after I suppose
**/
(function($){
"use strict";
var methods = {
init : function(config) {
var options = $.extend({
autoHide: true,
defaultOption: "Go to...",
deviceWidth: 480,
appendTo: '',
className: 'mobileselect',
useWindowWidth: false
}, config);
// we'll use the width of the device, because we stopped browsersniffing
// a long time ago. Anyway, we want to target _every_ small display
var width = (options.useWindowWidth === true) ? $(window).width() : screen.width;
if (width < options.deviceWidth){
var _o = $(this), // store the jqyuery object once
_p = (options.appendTo.length) ? $(options.appendTo) : _o.parent(), // get the parent node
_s = $("<select class=\""+ options.className +"\" />"); // create a filthy select
_s.appendTo(_p); // append it to the parent
$("<option />", {
"selected": (!$('.current', _o).length) ? 'selected' : false,
"value": "",
"text": options.defaultOption
}).appendTo(_s);
// Populate the dropdown with menu items. If there is an li.current we'll
// make this one selected
$('a', _o).each(function() {
var el = $(this),
sl = ( el.parent('li').hasClass('current') || el.hasClass('current') ) ? 'selected' : false;
$("<option />", {
"selected": sl,
"value": el.attr("href"),
"text": el.text()
}).appendTo(_s);
});
// hide the navigation ul
if (options.autoHide){
$(_o).hide();
}
// now make it work :-)
_s.change(function() {
window.location = $(this).find("option:selected").val();
});
}
}
};
$.fn.mobileSelect = function(method){
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.mobileselect' );
}
};
})(this.jQuery);