-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.js
223 lines (200 loc) · 6.42 KB
/
ui.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var Bind = require('github/jxmono/bind');
var Events = require('github/jxmono/events');
module.exports = function (config) {
var self = this;
self.config = config;
// This variable stores `true` or `false` depending on whether an upload
// request is in progress or not
self.uploadRequestInProgress = false;
// Run the binds
for (var i = 0; i < self.config.binds.length; ++i) {
Bind.call(self, self.config.binds[i]);
}
Events.call(self, config);
// Set the default selectors
self.config.ui = self.config.ui || {};
self.config.ui = $.extend({
errorColor: 'red',
popupFadeAnimationDuration: 100
}, self.config.ui);
self.config.ui.selectors = self.config.ui.selectors || {};
self.config.ui.selectors = $.extend({
target: '#mc_container',
hideUi: '.close-form',
loadingIndicator: '.loading-indicator',
closeButton: '.close-btn',
uploadButton: '.upload',
listName: '.list-name'
}, self.config.ui.selectors);
// Cache some jQuery elements
self.$popup = $(self.config.ui.selectors.target);
self.$loadingIndicator = self.$popup.find(self.config.ui.selectors.
loadingIndicator);
self.$closeBtn = self.$popup.find(self.config.ui.selectors.closeButton);
self.$uploadBtn = self.$popup.find(self.config.ui.selectors.uploadButton);
self.$listName = self.$popup.find(self.config.ui.selectors.listName);
self.$titleBarCloseBtn = self.$popup.find(self.config.ui.selectors.hideUi);
// Set up the event handlers
self.on('setQuery', setQuery);
self.on('showUi', showUi);
self.on('hideUi', hideUi);
self.on('upload', upload);
// The `hideUi` selector is the close button of the MailChimp upload popup
self.$titleBarCloseBtn.add(self.$closeBtn)
.on('click', function () {
if (self.uploadRequestInProgress) { return; }
hideUi.call(self);
});
};
/**
* pad
*
* @name pad
* @function
* @param {String} x A string with the length 1 or 2.
* @return {String} `x` if it has the length 2, or `'0' + x` otherwise.
*/
function pad(x) {
return x.length === 2 ? x : ('0' + x);
}
/**
* getYYYYMMDDDateString
* Returns the current date as a string in the format YYYY-MM-DD.
*
* @name getYYYYMMDDDateString
* @function
* @return {String} The current date in the format YYYY-MM-DD.
*/
function getYYYYMMDDDateString() {
var d = new Date();
var yyyy = d.getFullYear().toString();
var mm = (d.getMonth() + 1).toString();
var dd = d.getDate().toString();
return yyyy + '.' + pad(mm) + '.' + pad(dd);
}
/**
* getAutocompletedListName
* Returns the autocompleted name of the new list. It is in the format "List
* YYYY-MM-DD".
*
* @name getAutocompletedListName
* @function
* @return {String} The autocompleted name of the new list.
*/
function getAutocompletedListName() {
return 'List ' + getYYYYMMDDDateString();
}
/**
* showUi
* Autocompletes the default list name, opens the MC popup and focuses the list
* name input.
*
* @name showUi
* @function
* @return {undefined}
*/
function showUi () {
var self = this;
self.$listName.val(getAutocompletedListName());
self.$popup.fadeIn(self.config.ui.popupFadeAnimationDuration, function () {
self.$listName.focus();
});
}
/**
* hideUi
* Hides the MC popup, resets the status of the loading indicator and of the
* upload button.
*
* @name hideUi
* @function
* @return {undefined}
*/
function hideUi () {
var self = this;
self.$popup.fadeOut(self.config.ui.popupFadeAnimationDuration);
self.$loadingIndicator.addClass('hidden')
.css('color', '');
// The following call only activates the Upload button, the
// `uploadRequestInProgress` variable is already set to false at the
// initialization of the module or at the end of the last upload request.
setUploadActive.call(self, true);
}
/**
* setUploadActive
* Depending on the `state` parameter, sets the semaphore variable
* `self.uploadRequestInProgress` and the enabled/disabled state of the upload
* button.
*
* @name setUploadActive
* @function
* @param {Boolean} state The new state of the upload button.
* @return {undefined}
*/
function setUploadActive (state) {
var self = this;
self.uploadRequestInProgress = !state;
self.$uploadBtn.prop('disabled', self.uploadRequestInProgress);
}
/**
* upload
* Does the MC upload request.
*
* @name upload
* @function
* @return {undefined}
*/
function upload () {
var self = this;
if (!self.query) {
alert('No data query set for upload');
return;
}
self.$loadingIndicator.text('Loading...').css('color', '')
.removeClass('hidden');
self.$closeBtn.prop('disabled', true);
setUploadActive.call(self, false);
var trimmedListName = self.$listName.val().trim();
self.$listName.val(trimmedListName);
self.link('uploadToMailChimp', {
data: {
query: self.query,
listName: trimmedListName
}
}, function (err) {
self.$closeBtn.prop('disabled', false);
if (err) {
self.$loadingIndicator.css('color', self.config.ui.errorColor)
.text('Error: ' + err);
setUploadActive.call(self, true);
self.$listName.focus();
} else {
self.$loadingIndicator.css('color', '')
.text('Upload under way, it will be done in about 5 minutes. ' +
'You can close this window now.');
// Here we do not call the `setUploadActive` function with the
// `true` argument because we want only a part of its behavior
// (After the upload request is done, the upload button remains
// disabled until the popup is opened again):
self.uploadRequestInProgress = false;
self.$closeBtn.focus();
}
});
}
/**
* setQuery
* Updates the database query used to make the upload request. In the Pradas CRM
* app this is an event handler connected to the changes of the filter in the
* data table (the `find` event of the `data_table` MIID) through the
* `pradas.mc_export.setFindData` function in the "/js/mc_export/handlers.js"
* file.
*
* @name setQuery
* @function
* @param {Object} query The MongoDB database query that refers to all the
* emails that should be uploaded to MailChimp.
* @return {undefined}
*/
function setQuery (query) {
var self = this;
self.query = query;
}