/
jquery.metabox.js
executable file
·163 lines (139 loc) · 5.38 KB
/
jquery.metabox.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
161
162
163
/**
* Metabox javascript class.
*
* @author Yannis Fragkoulis <yannis.fragkoulis@gmail.com>
* @version 0.4
*/
!function( $ ){
"use strict";
var Metabox = function ( element, options ) {
this.options = $.extend({}, $.fn.metabox.defaults, options);
this.$element = $(element);
this.$content = $('.' + this.options.cssClass + '-content', element);
this.$header = $('.' + this.options.cssClass + '-header', element);
this.$footer = $('.' + this.options.cssClass + '-footer', element);
this.options.errorText = this.options.errorText.replace('{url}', this.options.url);
if(this.options.loadingContainer)
this.options.loadingContainer = $(this.options.loadingContainer);
else
this.options.loadingContainer = this.$content;
if(this.options.refreshTimeout>0)
this.createTimeout();
else if(this.options.refreshInterval>0)
this.createInterval();
if(this.options.refreshOnInit)
this.refresh();
};
Metabox.prototype = {
constructor: Metabox
,
createTimeout : function() {
var $this = this;
setTimeout(function(){
$this.refresh();
},this.options.refreshTimeout);
}
,
createInterval : function() {
var $this = this;
setInterval(function(){
$this.refresh();
},this.options.refreshInterval);
}
,
refresh: function (options) {
var customError;
if (options && options.error !== undefined) {
customError = options.error;
delete options.error;
}
var $box = this;
options = $.extend({
type: this.options.type,
url : this.options.url,
data : this.options.data,
beforeSend : function() {
$box.$element.addClass($box.options.loadingClass);
$box.options.loadingContainer.html($box.options.loadingText);
$box.options.beforeRefresh.apply($box);
},
success : function(data) {
$box.$element.removeClass($box.options.loadingClass);
$box.options.loadingContainer.html('');
$box.options.handleResponse.apply($box, [data]);
$box.options.afterRefresh.apply($box, [data]);
},
error: function (XHR, textStatus, errorThrown) {
var ret, err;
$box.$element.removeClass($box.options.loadingClass);
$box.options.loadingContainer.html($box.options.errorText);
if (XHR.readyState === 0 || XHR.status === 0) {
return;
}
if (customError !== undefined) {
ret = customError(XHR);
if (ret !== undefined && !ret) {
return;
}
}
switch (textStatus) {
case 'timeout':
err = 'The request timed out!';
break;
case 'parsererror':
err = 'Parser error!';
break;
case 'error':
if (XHR.status && !/^\s*$/.test(XHR.status)) {
err = 'Error ' + XHR.status;
} else {
err = 'Error';
}
if (XHR.responseText && !/^\s*$/.test(XHR.responseText)) {
err = err + ': ' + XHR.responseText;
}
break;
}
if (err)
alert(err);
}
}, options || {});
if (options.data !== undefined && options.type === 'GET') {
options.url = $.param.querystring(options.url, options.data);
options.data = {};
}
$.ajax(options);
}
};
/* METABOX PLUGIN DEFINITION
* ======================== */
$.fn.metabox = function ( option, ajaxoptions ) {
return this.each(function () {
var $this = $(this)
, data = $this.data('metabox')
, options = typeof option == 'object' && option;
if (!data) $this.data('metabox', (data = new Metabox(this, options)));
if (option == 'refresh') data.refresh(ajaxoptions);
})
};
$.fn.metabox.defaults = {
loadingText: 'loading...',
loadingClass: 'metabox-loading',
loadingContainer: null,
errorText: 'Error loading {url}',
cssClass: 'metabox',
url: null,
data : {},
type : 'GET',
refreshOnInit : false,
refreshTimeout : 0,
refreshInterval : 0,
beforeRefresh: function() {},
handleResponse: function(data) {
this.$content.html(data);
},
afterRefresh: function(data) {},
debug: true
};
$.fn.metabox.Constructor = Metabox
}( window.jQuery );