-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (112 loc) · 3.41 KB
/
index.js
File metadata and controls
158 lines (112 loc) · 3.41 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
*
* AJAX Button
*
*/
var $ = require('jquery');
var Emitter = require('emitter');
var defaults = {
method: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: function() {
return window.encodeURIComponent('data');
},
callback: function (xhrResponse) {
return xhrResponse.status >= 200 && xhrResponse.status <= 399;
},
states: {
stateDefault: {
content: '<i class="fa fa-check"></i> Send data',
classes: 'button-primary',
title: 'State Default'
},
XHRrequest: {
content: '<i class="fa fa-spinner fa-spin"></i>',
classes: 'button-disabled',
title: 'Loading ...'
}
}
};
function AjaxButton(options) {
this.options = options || {};
for (var i in defaults) {
if (!(this.options[i])) this.options[i] = defaults[i];
}
this.response = {};
this.self;
this.active = 0;
// this.postData = {};
}
module.exports = AjaxButton;
Emitter(AjaxButton.prototype);
AjaxButton.prototype.init = function(self) {
var component = this;
component.self = self;
component._stateChange('XHRrequest');
};
AjaxButton.prototype._click = function(fn) {
var component = this;
$(component.self).on('click.ajaxpost', function(event) {
event.preventDefault();
fn(event);
});
};
AjaxButton.prototype._clearEvent = function() {
var component = this;
$(component.self).off('click.ajaxpost');
};
AjaxButton.prototype._stateChange = function(newStateName) {
var component = this;
// clear binded event - prevents multiple clicks
component._clearEvent();
// replace full class list
if (component.options.states[newStateName]) {
component.self.setAttribute('class', component.options.states[newStateName].classes);
component.self.setAttribute('title', component.options.states[newStateName].title);
component.self.innerHTML = component.options.states[newStateName].content;
component[newStateName]();
} else {
console.error('State: ' + newStateName + ' does not exist');
}
};
AjaxButton.prototype.stateDefault = function() {
var component = this;
component._click(function(){
component._stateChange('XHRrequest');
});
};
AjaxButton.prototype.XHRrequest = function() {
var component = this;
component._click(function(){});
component.active = 1;
component.emit('request');
component._xhr(component.self.getAttribute('href'), component.options.data(), component.options.contentType, component.options.method, function(xhr){
component.response = xhr;
component._XHRresponse();
});
};
AjaxButton.prototype._XHRresponse = function() {
var component = this;
component.active = 0;
component._stateChange('stateDefault');
if ( component.options.callback(component.response) ) {
component.emit('success');
} else {
component.emit('error');
}
component.emit('response');
};
AjaxButton.prototype._xhr = function(setUrl, data, contentType, method, resFn) {
$.ajax({
type: method,
url: setUrl,
data: data,
contentType: contentType
})
.done(function(data, textStatus, jqXHR) {
resFn(jqXHR);
})
.fail(function(jqXHR, textStatus, errorThrown) {
resFn(jqXHR);
});
};