/
cintlink.js
373 lines (325 loc) · 10.6 KB
/
cintlink.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* @since 2016-07-14
* @author Olle Haerstedt
*/
// Namespace
var LS = LS || {};
LS.plugin = LS.plugin || {};
LS.plugin.cintlink = LS.plugin.cintlink || {};
$(document).ready(function() {
function showLoader() {
$('.ajaxLoading').show();
}
function hideLoader() {
$('.ajaxLoading').hide();
}
/**
* Run when user click "Login"
* @param {object} ev - Event
* @return
*/
function onLoginClick(ev) {
ev.preventDefault();
var formValues = $('#cintlink-login-form').serialize();
showLoader();
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=login&' + formValues
}).done(function(response) {
console.log(response);
hideLoader();
var response = JSON.parse(response);
if (response.error) {
$('#error-modal .modal-body-text').html(response.error);
$('#error-modal').modal();
}
else if (response.result) {
// Login OK
showDashboard();
}
else {
$('#error-modal .modal-body-text').html(LS.plugin.cintlink.lang.couldNotLogin);
$('#error-modal').modal();
}
})
}
/**
* Show the login form if user is not already logged in
* @return
*/
function showLoginForm() {
hideLoader();
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=getLoginForm'
}).done(function(response) {
$('#cintlink-container').html(response);
$('#cintlink-login-submit').on('click', function(ev) {
onLoginClick(ev);
});
});
}
/**
* CintLink widget supports a couple of languages.
* Get one of those. Default to English.
* @param {string} surveyLanguage
* @return {string}
*/
function getWidgetLanguage(surveyLanguage) {
var supportedLanguages = [
'en',
'de',
'es',
'fi',
'fr',
'ja',
'ru',
'sv'
];
if (supportedLanguages.indexOf(surveyLanguage) === -1) {
return 'en';
}
else {
return surveyLanguage;
}
}
/**
* Fetch dashboard HTMl from server and inject it
* @return
*/
function showDashboard() {
showLoader();
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=getDashboard',
surveyId: LS.plugin.cintlink.surveyId
}).done(function(response) {
console.log('response', response);
hideLoader();
$('#cintlink-container').html(response);
doToolTip();
// It's possible that dashboard will add a notification, e.g. tutorial.
// Make sure notifications are updated even if we are using Ajax.
LS.updateNotificationWidget(LS.plugin.cintlink.notificationUpdateUrl, false);
// If surveyId is defined we have survey specific dashboard
if (LS.plugin.cintlink.surveyId) {
if (LS.plugin.cintlink.tour) {
LS.plugin.cintlink.tour.init();
LS.plugin.cintlink.tour.start();
}
/*
var useLangWizard = $('#cint-lang-wizard input[name="lang"]').length > 1 ;
var shouldShowWidget = $('#cintlink-gridview table tr').length == 2;
if (shouldShowWidget) {
if (useLangWizard) {
showLangWizard();
}
else {
showWidget();
}
}
*/
}
});
}
/**
* Show the CintLink widget
* @param {string|undefined} lang Language to put in the survey link
* @return
*/
function showWidget(lang) {
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=getSurvey&surveyId=' + LS.plugin.cintlink.surveyId
}).done(function(response) {
console.log(response);
var response = JSON.parse(response);
console.log('response', response);
var survey = JSON.parse(response.result);
console.log('survey', survey);
var completeLink;
if (lang === undefined) {
completeLink = response.link + '&lang=' + response.language;
}
else {
completeLink = response.link + '&lang=' + lang;
}
console.log('completeLink', completeLink);
console.log('response.numberOfQuestions', response.numberOfQuestions);
var options = {
locale: getWidgetLanguage(survey.surveyls_language),
introText: response.loggedIn ? 'LimeSurvey' : response.warningMessage,
surveyLink: {
value: completeLink,
readOnly: true
},
surveyTitle: {
value: survey.surveyls_title,
readOnly: true
},
contactName: {
value: response.name,
readOnly: false
},
contactEmail: {
value: response.email,
readOnly: false
},
numberOfQuestions: {
value: response.numberOfQuestions,
readOnly: true
}
};
CintLink.show(options, function(hold, release) {
// An order was made, and we're going to POST the hold URL back to ourselves
$.ajax({
url: LS.plugin.cintlink.pluginBaseUrl + '&method=purchaseRequest',
data: {
purchaseRequest: hold,
surveyId: LS.plugin.cintlink.surveyId
},
type: "POST",
dataType: "json",
success: function(data) {
console.log('here');
console.log(data);
$('#order').text(data.text);
orderUrl = data.id;
$('#release-order').show();
CintLink.close();
if (data.error) {
showErrorOrSuccessModal(data);
}
else {
showErrorOrSuccessModal({
result: LS.plugin.cintlink.lang.orderPlacedOnHold
});
}
}
});
});
});
}
/**
* After e.g. cancellation, depending on response,
* show either error modal or success modal.
* Reloads the dashboard.
* @param {object} response Expected to have response.error or response.result
* @return
*/
function showErrorOrSuccessModal(response) {
if (response.error) {
$('#error-modal .modal-body-text').html(response.error);
$('#error-modal').modal();
}
else if (response.result) {
$('#success-modal .modal-body-text').html(response.result);
$('#success-modal').modal();
}
showDashboard();
}
/**
* Cancel an order at Cint
* @param {string} orderUrl
* @return
*/
function cancelOrder(orderUrl) {
showLoader();
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=cancelOrder',
data: {orderUrl: orderUrl}
}).done(function(response) {
hideLoader();
console.log(response);
var response = JSON.parse(response);
console.log(response);
showErrorOrSuccessModal(response);
});
}
/**
* Soft deletes an order so it no longer shows up
* in dashboard.
* @param {string} orderUrl
* @return
*/
function softDeleteOrder(orderUrl) {
showLoader();
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=softDeleteOrder',
data: {
orderUrl: orderUrl,
surveyId: LS.plugin.cintlink.surveyId
}
}).done(function(response) {
hideLoader();
console.log(response);
var response = JSON.parse(response);
console.log(response);
showErrorOrSuccessModal(response);
});
}
/**
* Run when user clicks 'Pay now'
* @return
*/
function userTriedToPay() {
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=userTriedToPay',
data: {
surveyId: LS.plugin.cintlink.surveyId
}
});
}
/**
* When survey has more than one language, show a
* popup to let user choose lang before ordering
* participants.
* @return
*/
function showLangWizard() {
$('#cint-lang-wizard').modal();
}
/**
* Fired when user clicks OK in language wizard
* @return
*/
function langWizardOK() {
var lang = $('#cint-lang-wizard input[name="lang"]:checked').val();
showWidget(lang);
}
// Needs to be accessed from the outside (dashboard view)
LS.plugin.cintlink.showWidget = showWidget;
LS.plugin.cintlink.showLoginForm = showLoginForm;
LS.plugin.cintlink.cancelOrder = cancelOrder;
LS.plugin.cintlink.softDeleteOrder = softDeleteOrder;
LS.plugin.cintlink.userTriedToPay = userTriedToPay;
LS.plugin.cintlink.showDashboard = showDashboard;
LS.plugin.cintlink.showLangWizard = showLangWizard;
LS.plugin.cintlink.langWizardOK = langWizardOK;
// Check if user is logged in on limesurvey.org
// If yes, show widget
// If no, show login form
$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&method=checkIfUserIsLoggedInOnLimesurveyorg'
}).done(function(response) {
console.log(response);
var response = JSON.parse(response);
showDashboard();
/*
if (response.result)
{
// User logged in
showDashboard();
}
else
{
// User not logged in, show user form
showLoginForm();
}
*/
});
});