-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.data.apu.js
69 lines (54 loc) · 2.1 KB
/
jquery.data.apu.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
;(function ( $, window, undefined ) {
// APU: Ajax Place Update
var pluginName = 'dataAPU',
document = window.document,
defaults = {
propertyName: "value"
};
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
var $load_image = this.options.loadImage;
var $urlerror = this.options.urlError;
var $url404 = this.options.url404;
var $url500 = this.options.url500;
$(this.element).click(function(e) {
e.preventDefault();
var $update = $(this).data("ajax").update;
var $place = $(this).data("ajax").place;
var $url = $(this).attr("href");
$($update).html($load_image);
$($update).load($url + " " + $place, function(response, status, xhr) {
if (status === "error") {
var msg = "Sorry but there was an error: ";
console.log(msg + xhr.status + " " + xhr.statusText);
if (xhr.status == 404){
$($update).load($url404 + " " + $place);
} else if (xhr.status == 500) {
$($update).load($url500 + " " + $place);
} else {
$($update).load($urlerror + " " + $place);
}
}
});
if($url != window.location){
window.history.pushState({path: $url}, "", $url);
}
$(window).bind("popstate", function() {
$($place).load(location.href + " " + $place);
});
});
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
};
}(jQuery, window));