/
ajaxify.js
145 lines (124 loc) · 5.24 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
/*
LimeSurvey
Copyright (C) 2007-2017 The LimeSurvey Project Team / Louis Gac
All rights reserved.
License: GNU/GPL License v2 or later, see LICENSE.php
LimeSurvey is free software. This version may have been modified pursuant
to the GNU General Public License, and as distributed it includes or
is derivative of works licensed under the GNU General Public License or
other free or open source software licenses.
See COPYRIGHT.php for copyright notices and details.
(¯`·._.·(¯`·._.· Ajax Mode ·._.·´¯)·._.·´¯)
This script deal with the new optional ajax system.
*/
//Check if we have to work on IE10 *sigh*
var isIE10 = false;
/*@cc_on
if (/^10/.test(@_jscript_version)) {
isIE10 = true;
}
@*/
console.ls.log("isIE10: ",isIE10);
// Submit the form with Ajax
var AjaxSubmitObject = function () {
var activeSubmit = false;
// First we get the value of the button clicked (movenext, submit, prev, etc)
var move = '';
var startLoadingBar = function () {
//Scroll to the top of the page
window.scrollTo(0, 0);
$('#ajax_loading_indicator').css('display','block').find('#ajax_loading_indicator_bar').css({
'width': '20%',
'display': 'block'
});
};
var endLoadingBar = function () {
$('#ajax_loading_indicator').css('opacity','0').find('#ajax_loading_indicator_bar').css('width', '100%');
setTimeout(function () {
$('#ajax_loading_indicator').css({'display': 'none', 'opacity': 1}).find('#ajax_loading_indicator_bar').css({
'width': '0%',
'display': 'none'
});
}, 1800);
};
var checkScriptNotLoaded = function(scriptNode){
if(scriptNode.src){
return ($('head').find('script[src="'+scriptNode.src+'"]').length > 0);
}
return true;
};
var appendScript = function(scriptText, scriptPosition, src){
src = src || '';
scriptPosition = scriptPosition || null;
var scriptNode = document.createElement('script');
scriptNode.type = 'text/javascript';
if(src != false){
scriptNode.src = src;
}
scriptNode.text = scriptText;
scriptNode.attributes.class = 'toRemoveOnAjax';
switch(scriptPosition) {
case 'head': if(checkScriptNotLoaded(scriptNode)){ document.head.appendChild(scriptNode); } break;
case 'body': document.body.appendChild(scriptNode); break;
case 'beginScripts': document.getElementById('beginScripts').appendChild(scriptNode); break;
case 'bottomScripts': //fallthrough
default: document.getElementById('bottomScripts').appendChild(scriptNode); break;
}
};
var bindActions = function () {
var globalPjax = new Pjax({
elements: ['form#limesurvey'], // default is "a[href], form[action]"
selectors: ['#dynamicReloadContainer', '#beginScripts', '#bottomScripts'],
debug: true,
forceRedirectOnFail: true,
reRenderCSS : true,
logObject : new ConsoleShim('PJAX-LOG', (LSvar.debugMode < 1)),
scriptloadtimeout: 1500,
});
// Always bind to document to not need to bind again
// Restrict to [type=submit]:not([data-confirmedby])
// - :submit is the default if button don't have type (reset button on slider for example),
// - confirmedby have their own javascript system
$(document).on('click', '#limesurvey [type=submit]:not([data-confirmedby])',function (e) {
$('#limesurvey').append('<input name=\''+$(this).attr('name')+'\' value=\''+$(this).attr('value')+'\' type=\'hidden\' />');
if(isIE10 || /Edge\/\d+\.\d+/.test(navigator.userAgent)) {
e.preventDefault();
$('#limesurvey').trigger('submit');
return false;
}
});
// If the user try to submit the form
// Always bind to document to not need to bind again
$(document).on('submit', '#limesurvey', function (e) {
// Prevent multiposting
//Check if there is an active submit
//If there is -> return immediately
if(activeSubmit) return;
//block further submissions
activeSubmit = true;
//start the loading animation
startLoadingBar();
$(document).on('pjax:scriptcomplete.onreload', function(){
// We end the loading animation
endLoadingBar();
//free submitting again
activeSubmit = false;
if (/<###begin###>/.test($('#beginScripts').text())) {
$('#beginScripts').text('');
}
if (/<###end###>/.test($('#bottomScripts').text())){
$('#bottomScripts').text('');
}
$(document).off('pjax:scriptcomplete.onreload');
});
});
return globalPjax;
};
return {
bindActions: bindActions,
startLoadingBar: startLoadingBar,
endLoadingBar: endLoadingBar,
unsetSubmit: function(){activeSubmit = false;},
blockSubmit: function(){activeSubmit = true;}
};
};