-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjquery.jcombo.js
executable file
·133 lines (133 loc) · 4.87 KB
/
jquery.jcombo.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
/*
* jQuery jCombo Plugin v3.0
* Carlos De Oliveira
* Latest Release: Dic 2013
*/
;(function ( $, window, document, undefined ) {
var pluginName = "jCombo",
version = "3.0",
defaults = {
url: null,
parent: null,
first_optval : "__jc__",
input_param: null,
data: null,
selected_value : null,
onLoad: null,
onChange: null,
initial_text: "-- Please Select --",
method: "GET",
dataType: "jsonp",
cache: true
};
function Plugin( element, options ) {
this.options = $.extend( {}, defaults, options) ;
this.element = element;
this.init();
};
Plugin.prototype = {
_addParameter: function(param,value) {
if(param==null || value == null) return false;
this.options.data = param + "=" + value;
return true;
},
_getJson: function(url,cback) {
var self = this;
var dd = (self.options.orig_data!=null) ? self.options.orig_data : "";
dd += (self.options.data!=null) ? self.options.data : "";
$.ajax({
url: url,
data: dd,
type: self.options.dataType,
dataType: self.options.dataType,
method: self.options.method,
success: cback
});
},
_onLoadCall: function() {
if(this.options.onLoad != null) {
var f = this.options.onLoad;
this.options.onLoad = null;
f();
}
},
_getData: function(url,cback) {
var self = this;
window.__jcombo_data_cache = (typeof window.__jcombo_data_cache === "undefined") ? {} : window.__jcombo_data_cache;
var cK = JSON.stringify(url + self.options.orig_data + "&" + self.options.data);
if (!window.__jcombo_data_cache[cK] || !self.options.cache ) {
self._getJson(url,function(data) {
window.__jcombo_data_cache[cK] = data;
cback(data);
});
} else setTimeout(function() {
cback(window.__jcombo_data_cache[cK]);
},0);
},
_renderOption: function(v,t,s) {
var sel = "";
if(s==true) sel = ' selected="selected"';
return '<option value="' + v + '"' + sel + '>' + t + '</option>';
},
_firstOption: function() {
if(this.initial_text == "") return "";
return this._renderOption(this.options.first_optval,this.options.initial_text,false);
},
_renderSelect: function(data, selected_value) {
var response = [];
if(typeof selected_value == "undefined") selected_value = this.options.first_optval;
response.push(this._firstOption());
for(var index in data) {
var option = data[index];
response.push(this._renderOption(option.id,option.value,option.id == selected_value));
}
return response.join("");
},
_bindSelect:function (id, value) {
var self = this;
var xurl = this.options.url;
value = (value==null) ? id : value;
if(this.options.input_param == null) xurl+= (id==null)?"":id;
else this._addParameter(this.options.input_param,value);
var xid = $(self.element).attr("id");
if(value==this.options.first_optval || id==this.options.first_optval) {
$(this.element).html(this._firstOption());
$(this.element).attr("disabled","disabled");
$(self.element).trigger("change");
self._onLoadCall();
} else {
self.options.selected_value =value;
this._getData(xurl, function(data) {
$(self.element).html(self._renderSelect(data,value));
self._onLoadCall();
if(value!=null || id ==null) {
$(self.element).trigger("change");
if(self.options.onChange != null) self.options.onChange(value);
}
if(data.length>0) $(self.element).removeAttr("disabled");
});
}
},
init: function() {
var self = this;
this.options.orig_data = this.options.data;
if(this.options.parent!=null) {
$(this.options.parent).each(function(index,elem) {
var pvalue = $(elem).val();
$(elem).bind("change",function() {
self._bindSelect($(elem).val(),self.options.selected_value);
});
});
} else this._bindSelect(null,self.options.selected_value);
}
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};
})( jQuery, window, document );