This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajaxify.js
148 lines (134 loc) · 6.14 KB
/
ajaxify.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
/*!
* jQuery Ajaxify
*
* Copyright (c) 2010
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Author: Jonathan Tropper
*/
(function($) {
var base = $.fn.ajaxify = function(event, ajaxOptions) {
return this.bind(event, function() {
return base.callbackFor(this).call(this, ajaxOptions);
}).data('ajaxified', true);
};
$.extend(base, {
selectors: {
'a': function(options) {
$.extend(options, {
url: $(this).attr('href')
});
$.ajax(options);
return false;
},
'form': function(options) {
$.extend(options, {
type: $(this).attr('method').toUpperCase(),
url: $(this).attr('action'),
data: $(this).serialize()
});
$.ajax(options);
return false;
},
'form[enctype=multipart/form-data]': function(options) {
var uuid = '';
for (i = 0; i < 32; i++) { uuid += Math.floor(Math.random() * 16).toString(16); }
var unsupported = [];
var supported = ['beforeSend', 'success', 'error'];
$.each(options, function(key, value) {
if ($.inArray(key, supported) == -1) { unsupported.push(key); }
});
if (options.beforeSend) {
if (options.beforeSend() == false) { return false; };
}
var target_name = 'ajax_target_' + uuid;
var target = $('<iframe name="' + target_name + '" src="about:blank" style="display: none"></iframe>').appendTo('body');
var url = $(this).attr('action');
$(this).attr({
action: url + (url.split('?').length > 1 ? '&' : '?') + 'X-Progress-ID=' + uuid,
target: target_name,
// Set encoding for IE
encoding: 'multipart/form-data'
});
if (options.progress) {
function getStatus() {
$.ajax({
type: 'GET',
url: '/file-progress',
data: { 'X-Progress-ID': uuid },
dataType: 'json',
success: function(upload) {
if (upload.state == 'starting') {
// Nothing
} else if (upload.state == 'uploading') {
upload.percent = Math.floor((upload.received / upload.size) * 1000) / 10;
options.progress(upload);
} else {
window.clearTimeout(options.timer);
}
}
});
}
options.timer = window.setInterval(getStatus, 1000);
}
target.load(function() {
window.clearTimeout(options.timer);
var element = $(this).contents().find('body textarea[name="content"]');
var html = element.val();
if (options.complete) { options.complete(); }
if (html) {
var status = parseInt(element.dattr('status'), 10);
var successful = (status >= 200 && status < 300) || status == 304 || status == 1223;
if ((successful) && (options.success)) { options.success(html); }
if ((!successful) && (options.error)) { options.error({ responseText: html }); }
} else {
html = $(this).contents().find('body').html();
if (options.success) { options.success(html); }
}
target.remove();
});
}
},
callbackFor: function(element) {
element = $(element);
var callback;
var specificity = 0;
$.each(base.selectors, function(selector, tempCallback) {
var tempSpecificity = base.specificityFor(selector);
if ((element.is(selector)) && (tempSpecificity > specificity)) {
callback = tempCallback;
specificity = tempSpecificity;
}
});
return callback;
},
specificityFor: function(selector) {
// W3C CSS Specificity: http://www.w3.org/TR/CSS21/cascade.html#specificity
// Implemented slightly different.
// But it is much simpler, and you will not notice anything different unless you nest 11 or more selectors,
// in which there would be much bigger problems with your code.
var specificity = 0;
var items = selector.split(' ');
for (var i = items.length - 1; i >= 0; i--) {
var item = items[i];
if (base.hasSelector('ID', item)) { specificity += 100; }
if (base.hasSelector('CLASS', item)) { specificity += 10; }
if (base.hasSelector('ATTR', item)) { specificity += 10; }
if (base.hasSelector('PSEUDO', item)) { specificity += 10; }
if (base.hasSelector('TAG', item)) { specificity += 1; }
};
return specificity;
},
hasSelector: function(sizzleType, selector) {
return $.expr.match[sizzleType].test(selector);
}
});
$.fn.ajax = function(event, ajaxOptions) {
var handler = function(event) {
event.stopPropagation();
return base.callbackFor(this).call(this, ajaxOptions);
};
return this.one(event, handler).trigger(event);
};
})(jQuery);