-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.fmcombo-0.2.js
More file actions
140 lines (111 loc) · 4.22 KB
/
Copy pathjquery.fmcombo-0.2.js
File metadata and controls
140 lines (111 loc) · 4.22 KB
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
/*
* fmcombo - jQuery plugin 0.2
*
* Copyright (c) 2014 Fabio Michelucci <fmichelucci@gmail.com>
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function( $ ){
//Plugin namespace
$.fn.fmcombo = function(options) {
var opts = $.extend({}, $.fn.fmcombo.defaults, options);
return this.change(function () {
_self = $(this);
var v = _self.val();
if (v == opts.firstOption.Value || v == null || v == "") {
removeOptionsFrom(opts.nextElement);
removeOptionsFrom(opts.childs);
}
else {
if ($.isArray(opts.source)) {
$(opts.nextElement).html($.fn.fmcombo.format(opts.firstOption, opts.source)).focus();
} else if (typeof opts.source === "string") {
var url = opts.source + serialize(_self, opts.depElements)
self.xhr = $.ajax({
url: url,
dataType: "json",
async: false,
success: function (data) {
if (data.length > 0) {
$(opts.nextElement).html($.fn.fmcombo.format(opts.firstOption, data)).focus();
opts.onAfterDataLoad.call(this);
}
else {
opts.onEmptySource.call(this);
}
},
error: function () { debug('Error ajax load'); }
});
} else if ($.isFunction(opts.source)) opts.source.call($)
}
});
};
// Plugin defaults settings added as a property on plugin function.
$.fn.fmcombo.defaults = {
source: null,
depElements: [],
nextElement: null,
firstOption: { Value: '-1', Text: '-- select --' },
onAfterDataLoad: function() { },
onEmptySource: function () { },
childs: []
};
$.fn.fmcombo.format = function(firstOption,source){
var opt = '<option value="' + firstOption.Value + '">' + firstOption.Text + '</option>';
$.each(source,function(key,value){
opt += '<option value="'+source[key].Value+'">'+source[key].Text+'</option>';
});
return opt;
};
//private function
function dataLoad(obj, elements, input, callback) {
if ($.isArray(input)) {
optionsData = input;
} else if (typeof input === "string") {
var url = input + serialize(obj, elements)
self.xhr = $.ajax({
url: url,
dataType: "json",
async: false,
success: function (data) {
if (data.length > 0) {
optionsData = data;
}
else {
callback.call(this);
}
},
error: function () { debug('Error ajax load');}
});
} else if ($.isFunction(input)) input.call($)
}
function serialize(obj, elements) {
var qs = '?' + obj.attr("name") + '=' + encodeURIComponent(obj.val());
if ($.isArray(elements)) {
$.each(elements, function (key, value) {
$e = $(value);
qs += '&' + $e.attr("name") + '=' + encodeURIComponent($e.val());
});
} else if (typeof elements === "string") {
debug('Dep is string')
qs += '&' + $(elements).attr("name") + '=' + encodeURIComponent($(elements).val())
}
return qs;
}
function removeOptionsFrom(elements) {
if ($.isArray(elements)) {
$.each(elements, function (key, value) {
$(value).empty()
});
} else if (typeof elements === "string") {
$(elements).empty()
}
}
function debug(message) {
if (window.console && window.console.log)
window.console.log('fmCombo: ' + message);
}
})( jQuery );