-
Notifications
You must be signed in to change notification settings - Fork 1
/
depage-datalist.js
160 lines (139 loc) · 4.37 KB
/
depage-datalist.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @require framework/shared/jquery-1.8.3.js
* @require framework/shared/depage-jquery-plugins/depage-autocomplete.js
*
* @file depage-datalist
*
* Depage DataList plugin to supply user with hints or data while filling form text inputs.
*
* Adds backwards compatibility to html5 datalist using the depage-autocomplete.js plugin
*
* Provide an optional url to dynamically load via AJAX
*
* If the browser does not support datalists an unordered list of hyperlinks is built with
* functionality to mimic the datalist behaviour.
*
* copyright (c) 2006-2012 Frank Hellenkamp [[email protected]]
*
* @author Ben Wallis
*/
(function($){
if(!$.depage){
$.depage = {};
};
// shiv {{{
/**
* Shiv DataList
*
* Adds datalist element to the DOM to enable IE < 9.
*
* @return void
*/
if ($.browser.msie && $.browser.version < 9) {
$('head').append('<style>datalist{display:none}</style>');
document.createElement("datalist");
}
// }}}
/**
* datalist
*
* @param el - file input
* @param index
* @param options
*/
$.depage.datalist = function(el, index, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.datalist", base);
// HTML5 datalist element is supported
base.datalist = true;
// List element associated with input
base.$list = $(base.$el.attr('list'));
// {{{ init
/**
* Init
*
* Get the plugin options.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.datalist.defaultOptions, options);
base.datalist = (typeof(HTMLDataListElement) !== "undefined"); // && false; // DEBUG FALLBACK
// disable browser autocomplete
base.$el.attr("autocomplete", "off");
if (base.datalist) {
base.datalist();
} else {
base.fallback();
}
};
// }}}
// {{{ datalist()
/**
* datalist
*
* Binds to keypress and loads list element with options.
*
* Note that the "datalist" paramteter of the ajax request determines the format of returned HTML:
* i.e. <option> elements for datalist = true, otherwise <li> elements for setup.
*
* @return void
*/
base.autocomplete = function(){
if(base.options.url) {
base.$el.bind("keypress.datalist", function() {
var url = $("base").attr("href")
+ $('html').attr('lang')
+ base.options.url
+ "?ajax=true"
+ "&datalist=" + base.datalist
+ "&value=" + $(this).val();
$.get(url , null, function(data) {
var $data = $(data);
base.$list.empty().append($data);
base.$el.trigger("load", [$data]);
});
});
}
};
/// }}}
// {{{ fallback()
/**
* Fallback
*
* Replace <datalist> with <ul> list.
* Clicking <li> adds contents to the input element.
*
*/
base.setup = function(){
var id = base.$list.attr("id");
$ul = $("<ul class='autocomplete' />").attr("id", base.$list.attr("id")).hide();
base.$list.replaceWith($ul);
base.$el.depageAutoComplete({"url":base.options.url, "list_id": id});
};
// }}}
base.init();
};
// }}}
/**
* Default Options
*
* url - the ajax lander url
*
* TODO implement fallback for no url
*/
$.depage.autocomplete.defaultOptions = {
url : false,
};
$.fn.depageDataList = function(options){
return this.each(function(index){
(new $.depage.autocomplete(this, index, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */