2a6f3d400132540325921f66d18cad5fff28d4f2
[TYPO3CMS/Extensions/powermailCond.git] / Resources / Public / Js / PowermailCond.js
1 jQuery(document).ready(function() {
2 base = getBaseUrl();
3 validationFieldClasses = '.powermail_input, .powermail_textarea, .powermail_select, .powermail_radio, .powermail_checkbox';
4 clearFullSession();
5 if ($('form.powermail_form').length > 0) { // only if the powermail form is on the page (not for confirmation page)
6 checkConditions(0); // check if something should be changed
7 }
8
9 // read values from session
10 var url = base + '/index.php?eID=powermailcond_readSession&tx_powermailcond_pi1[form]=' + getFormUid();
11 $.ajax({
12 url: url, // send to this url
13 cache: false, // disable cache (for ie)
14 success: function(data) { // return values
15 if (data) { // if there is a response
16 var sets = data.split(';');
17 for (var i=0; i < sets.length; i++) { // for each field which should be filled
18 var tmp_value = sets[i].split(':');
19 fieldValue(tmp_value[0], tmp_value[1]);
20 }
21 }
22 $('form.powermail_form').fadeTo('fast', 1);
23 }
24 });
25
26 // save values via ajax to session
27 $(validationFieldClasses).bind('change', function() {
28 $this = $(this); // caching
29 var url = base + '/index.php';
30 var timestamp = Number(new Date()); // timestamp is needed for a internet explorer workarround (always change a parameter)
31 var value = $this.val(); // current value
32 if ($this.hasClass('powermail_checkbox') && !$this.is(':checked')) { // clean value if checkbox was dechecked
33 value = '';
34 }
35 var uid = $this.closest('.powermail_fieldwrap').attr('id').substr(20); // current field uid (without "uid")
36 var name = $this.prop('name');
37 var params = 'eID=' + 'powermailcond_saveToSession' + '&tx_powermailcond_pi1[form]=' + getFormUid() + '&tx_powermailcond_pi1[uid]=' + uid + '&tx_powermailcond_pi1[value]=' + value + '&ts=' + timestamp;
38
39 $.ajax({
40 type: 'GET', // type
41 url: url, // send to this url
42 data: params, // add params
43 cache: false, // disable cache (for ie)
44 success: function(data) { // return values
45 if (data != '') { // if there is a response
46 $('form.powermail_form').append('Error in powermail_cond.js in change function:' + data);
47 }
48 checkConditions(uid); // check if something should be changed
49 }
50 });
51 });
52 });
53
54 /**
55 * Fill a field with a value
56 *
57 * @param {int} fieldUid - Field Uid
58 * @param {int} fieldValue - Field Value
59 */
60 function fieldValue(fieldUid, fieldValue) {
61 var fieldWrap = $('#powermail_fieldwrap_' + fieldUid);
62 fieldWrap.find('input').not('[type="checkbox"]').val(fieldValue);
63 fieldWrap.find('input[type="checkbox"]').each(function() {
64 if ($(this).prop('value') == fieldValue) {
65 $(this).prop('checked', 'checked');
66 }
67 })
68 }
69
70 /**
71 * Main function to check conditions and do something (if necessary)
72 *
73 * @param {integer} uid: Field uid (if available)
74 * @return void
75 */
76 function checkConditions(uid) {
77 var url = base + '/index.php';
78 var params = '';
79 if (uid > 0) {
80 params += '&tx_powermailcond_pi1[uid]=' + uid;
81 }
82 $.ajax({
83 type: 'GET', // type
84 url: url, // send to this url
85 data: 'eID=' + 'powermailcond_getFieldStatus' + params + '&tx_powermailcond_pi1[formUid]=' + getFormUid(), // add params
86 cache: false, // disable cache (for ie)
87 success: function(data) {
88 if (data != 'nochange') {
89 $('.powermail_fieldwrap select option').removeClass('hide').removeProp('disabled');
90 if (data != '') { // if there is a response
91 if (data.length < 1000) { // stop if wrong result (maybe complete t3 page)
92 doAction(data); // hide all given fields
93 }
94 } else { // if there is no response
95 showAll(); // show all fields and fieldsets at the beginning
96 }
97 }
98
99 // TODO Detach Parsley
100 // Form validation
101 if ($.fn.validationEngine) {
102 $('.powermail_form').validationEngine('detach');
103 }
104 },
105 error: function() {
106 $('form.powermail_form').append('Error in PowermailCond.js in checkCondtions function by opening the given url');
107 }
108 });
109 }
110
111 /**
112 * Do some actions (hide and/or filter)
113 *
114 * @param {string} list commaseparated list with uids (1,2,3)
115 * @return void
116 */
117 function doAction(list) {
118 showAll();
119
120 var uid = list.split(',');
121 if (uid.length < 1) {
122 return false; // stop process
123 }
124 for (var i=0; i < uid.length; i++) { // one loop for every affected field
125 if (uid[i].indexOf('fieldset:') != '-1') { // fieldset part
126 hideFieldset(uid[i]);
127 } else if (uid[i].indexOf('filter:') != '-1') { // filter part
128 filterSelection(uid[i]);
129 } else { // fields part
130 hideField(uid[i]);
131 }
132 }
133 }
134
135 /**
136 * Hide a field and clear its value
137 *
138 * @param {integer} uid uid of the element
139 * @return void
140 */
141 function hideField(uid) {
142 $('.powermail_fieldwrap_' + uid).addClass('hide'); // hide current field
143 deRequiredField(uid, true);
144 if ($('.powermail_fieldwrap_' + uid + ' .powermail_field').val() != '') { // only if value is not yet empty
145 clearValue('.powermail_fieldwrap_' + uid + ' .powermail_field'); // clear value of current field
146 clearSession(uid); // clear value of current field
147 }
148 }
149
150 /**
151 * Hide some fields, which are bundled in a fieldset and clear there value
152 *
153 * @param {string} string mix of uid and values (fieldset:5:12;13;14)
154 * @return void
155 */
156 function hideFieldset(string) {
157 var params = string.split(':'); // filter / uid / values
158 var values = params[2].split(';'); // value1 / value2 / value3
159 $('.powermail_fieldset_' + params[1]).addClass('hide');
160 var fields = [];
161 for (var k=0; k < values.length; k++) {
162 clearValue('.powermail_fieldwrap_' + values[k] + ' .powermail_field');
163 deRequiredField(values[k], true);
164 fields.push(values[k]);
165 }
166
167 // save this field in session so it's no mandatory field any more
168 $.ajax({
169 url: '/index.php',
170 data: 'eID=' + 'powermailcond_deRequiredFields&tx_powermailcond_pi1[formUid]=' + getFormUid() + '&tx_powermailcond_pi1[fieldUids]=' + fields.join() + '&no_cache=1',
171 cache: false,
172 async: false
173 });
174 }
175
176 /**
177 * Hide some fields and clear there value
178 *
179 * @param {string} string mix of uid and values (filter:123:Value1;Value2;Value3)
180 * @return void
181 */
182 function filterSelection(string) {
183 var params = string.split(':'); // filter / uid / values
184 var values = params[2].split(';'); // value1 / value2 / value3
185 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option').addClass('hide').prop('disabled', 'disabled'); // disable all options
186
187 for (var j=0; j < values.length; j++) { // one loop for every option in select field
188 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option:contains(' + values[j] + ')').removeClass('hide').removeProp('disabled'); // show this option
189 }
190
191 var valueSelected = $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option:selected').val(); // give me the value of the selected option
192 if (params[2].indexOf(valueSelected) == '-1') { // if current selected value is one of the not allowed options
193 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field').get(0).selectedIndex = 0; // remove selection (because the selected option is not allowed)
194 }
195 }
196
197 /**
198 * Show all fields and fieldsets
199 *
200 * @return void
201 */
202 function showAll() {
203 reRequiredAll();
204 $('.powermail_fieldwrap, .powermail_fieldset').removeClass('hide');
205 }
206
207 /**
208 * Remove required class in Field
209 *
210 * @param {int} uid of the element
211 * @param {bool} disableAjaxRequest
212 * @return void
213 */
214 function deRequiredField(uid, disableAjaxRequest) {
215 // save this field in session so it's no mandatory field any more
216 if (disableAjaxRequest !== undefined && disableAjaxRequest === true) {
217 $.ajax({
218 url: '/index.php',
219 data: 'eID=' + 'powermailcond_deRequiredField&tx_powermailcond_pi1[formUid]=' + getFormUid() + '&tx_powermailcond_pi1[fieldUid]=' + uid + '&no_cache=1',
220 cache: false,
221 async: false
222 });
223 }
224
225 // rewrite required attributes
226 var element = $('#powermail_fieldwrap_' + uid).find('input');
227 if (element.prop('required') || element.data('parsley-required')) {
228 element.removeProp('required'); // remove required attribute
229 element.removeProp('data-parsley-required'); // remove parsley-required attribute
230 element.data('powermailcond-required', 'required'); // add own data required attribute
231 }
232 }
233
234 /**
235 * Re required Fields for JS-Validation
236 *
237 * @return void
238 */
239 function reRequiredAll() {
240 // Required Fields all
241 $.ajax({
242 url: '/index.php',
243 data: 'eID=' + 'powermailcond_requiredFields&tx_powermailcond_pi1[formUid]=' + getFormUid(),
244 cache: false,
245 async: false
246 });
247
248 $(validationFieldClasses).each(function() {
249 var $this = $(this);
250 $this.removeProp('powermailcond-required');
251 if ($this.data('powermailcond-required') === 'required') {
252 if (isHtml5ValidationActivated()) {
253 $this.prop('required', 'required');
254 } else if (isParsleyValidationActivated()) {
255 $this.prop('required', 'required');
256 }
257 }
258 });
259 }
260
261 /**
262 * Clear value of an inputfield, set selectedIndex to 0 for selection, don't clear value of submit buttons
263 *
264 * @param {string} selection selection for jQuery (e.g. "input.powermail")
265 * @return void
266 */
267 function clearValue(selection) {
268 if ($(selection).prop('type') == 'radio' || $(selection).prop('type') == 'checkbox') {
269 $(selection).prop('checked', false);
270 } else {
271 $(selection).not(':submit').val('');
272 }
273 }
274
275 /**
276 * Read BaseUrl
277 *
278 * @return {string} BaseUrl from Tag in DOM
279 */
280 function getBaseUrl() {
281 var base = $('base').prop('href');
282 if (!base || base == undefined) {
283 base = '';
284 }
285 return base;
286 }
287
288 /**
289 * Clear session of a uid
290 *
291 * @param {integer} uid of the element
292 * @return void
293 */
294 function clearSession(uid) {
295 var url = base + '/index.php';
296 var timestamp = Number(new Date()); // timestamp is needed for a internet explorer workarround (always change a parameter)
297 var params = 'eID=' + 'powermailcond_saveToSession' + '&tx_powermailcond_pi1[form]=' + getFormUid() + '&tx_powermailcond_pi1[uid]=' + uid + '&tx_powermailcond_pi1[value]=&ts=' + timestamp;
298
299 $.ajax({
300 type: 'GET', // type
301 url: url, // send to this url
302 data: params, // add params
303 cache: false, // disable cache (for ie)
304 async: false,
305 success: function(data) { // return values
306 if (data != '') { // if there is a response
307 $('form.powermail_form').append('Error in powermail_cond.js in clearSession function:' + data);
308 }
309 checkConditions(uid); // check if something should be changed
310 }
311 });
312 }
313
314 /**
315 * Clear session values if form is submitted
316 *
317 * @return void
318 */
319 function clearFullSession() {
320 if ($('.powermail_create').length || $('.powermail_frontend').length) { // if submitted Pi1 OR any Pi2
321 var url = base + '/index.php?eID=powermailcond_clearSession';
322 $.ajax({
323 url: url, // send to this url
324 cache: false,
325 async: false
326 });
327 }
328 }
329
330 /**
331 * Read Form uid from DOM
332 *
333 * @return {int} Form uid
334 */
335 function getFormUid() {
336 var formField = $('input[name="tx_powermail_pi1[mail][form]"]');
337 if (formField.length === 0) {
338 return 0;
339 }
340 return formField.val();
341 }
342
343 /**
344 * Check if Parsley is activated
345 *
346 * @return bool
347 */
348 function isParsleyValidationActivated() {
349 if ($('form.powermail_form_'+ getFormUid()).data('parsley-validate') === 'data-parsley-validate') {
350 return true;
351 }
352 return false;
353 }
354
355 /**
356 * Check if HTML5 Validation is activated
357 *
358 * @return bool
359 */
360 function isHtml5ValidationActivated() {
361 if ($('form.powermail_form_'+ getFormUid()).data('validate') === 'html5') {
362 return true;
363 }
364 return false;
365 }