1/** 2 * Powermail functions 3 * 4 * @params {jQuery} $ 5 * @class PowermailForm 6 */ 7function PowermailForm($) { 8 'use strict'; 9 10 /** 11 * This class 12 * 13 * @type {PowermailForm} 14 */ 15 var that = this; 16 17 /** 18 * Initialize 19 * 20 * @returns {void} 21 */ 22 this.initialize = function() { 23 that.addTabsListener(); 24 that.addAjaxFormSubmitListener(); 25 that.getLocationAndWrite(); 26 that.addDatePicker(); 27 that.hidePasswords(); 28 that.addResetListener(); 29 that.deleteAllFilesListener(); 30 that.uploadValidationListener(); 31 }; 32 33 /** 34 * Add tabs listener 35 * 36 * @returns {void} 37 */ 38 this.addTabsListener = function() { 39 if ($.fn.powermailTabs) { 40 $('.powermail_morestep').each(function() { 41 $(this).powermailTabs(); 42 }); 43 } 44 }; 45 46 /** 47 * Add Ajax form submit listener 48 * 49 * @returns {void} 50 */ 51 this.addAjaxFormSubmitListener = function() { 52 if ($('form[data-powermail-ajax]').length) { 53 that.ajaxFormSubmit(); 54 } 55 }; 56 57 /** 58 * Getting the Location by the browser and write to inputform as address 59 * 60 * @return {void} 61 */ 62 this.getLocationAndWrite = function() { 63 if ($('.powermail_fieldwrap_location input').length && navigator.geolocation) { 64 navigator.geolocation.getCurrentPosition(function(position) { 65 var lat = position.coords.latitude; 66 var lng = position.coords.longitude; 67 var url = that.getBaseUrl() + '/index.php' + '?eID=' + 'powermailEidGetLocation'; 68 jQuery.ajax({ 69 url: url, 70 data: 'lat=' + lat + '&lng=' + lng, 71 cache: false, 72 beforeSend: function() { 73 $('body').css('cursor', 'wait'); 74 }, 75 complete: function() { 76 $('body').css('cursor', 'default'); 77 }, 78 success: function(data) { 79 if (data) { 80 $('.powermail_fieldwrap_location input').val(data); 81 } 82 } 83 }); 84 }); 85 } 86 }; 87 88 /** 89 * Add datepicker to date fields 90 * 91 * @returns {void} 92 */ 93 this.addDatePicker = function() { 94 if ($.fn.datetimepicker) { 95 $('.powermail_date').each(function() { 96 var $this = $(this); 97 // stop javascript datepicker, if browser supports type="date" or "datetime-local" or "time" 98 if ($this.prop('type') === 'date' || $this.prop('type') === 'datetime-local' || $this.prop('type') === 'time') { 99 if ($this.data('datepicker-force')) { 100 // rewrite input type 101 $this.prop('type', 'text'); 102 } else { 103 // get date in format Y-m-d H:i for html5 date fields 104 if ($(this).data('date-value')) { 105 var prefillDate = that.getDatetimeForDateFields($(this).data('date-value'), $(this).data('datepicker-format'), $this.prop('type')); 106 if (prefillDate !== null) { 107 $(this).val(prefillDate); 108 } 109 } 110 111 // stop js datepicker 112 return; 113 } 114 } 115 116 var datepickerStatus = true; 117 var timepickerStatus = true; 118 if ($this.data('datepicker-settings') === 'date') { 119 timepickerStatus = false; 120 } else if ($this.data('datepicker-settings') === 'time') { 121 datepickerStatus = false; 122 } 123 124 // create datepicker 125 $this.datetimepicker({ 126 format: $this.data('datepicker-format'), 127 timepicker: timepickerStatus, 128 datepicker: datepickerStatus, 129 lang: 'en', 130 i18n:{ 131 en:{ 132 months: $this.data('datepicker-months').split(','), 133 dayOfWeek: $this.data('datepicker-days').split(',') 134 } 135 } 136 }); 137 }); 138 } 139 }; 140 141 /** 142 * Simply change value of password fields 143 * 144 * @returns {void} 145 */ 146 this.hidePasswords = function() { 147 $('.powermail_all_type_password.powermail_all_value').html('********'); 148 }; 149 150 /** 151 * Add validation reseter on click on reset button 152 * 153 * @returns {void} 154 */ 155 this.addResetListener = function() { 156 if ($.fn.parsley) { 157 $('.powermail_reset').on('click', '', function() { 158 $('form[data-parsley-validate="data-parsley-validate"]').parsley().reset(); 159 }); 160 } 161 }; 162 163 /** 164 * Add validation for upload fields 165 * 166 * @returns {void} 167 */ 168 this.uploadValidationListener = function() { 169 if (window.ParsleyValidator) { 170 that.uploadSizeValidatorListener(); 171 that.uploadExtensionValidatorListener(); 172 } 173 }; 174 175 /** 176 * ************ INTERNAL ************* 177 */ 178 179 /** 180 * Allow AJAX Submit for powermail 181 * 182 * @returns {void} 183 */ 184 this.ajaxFormSubmit = function() { 185 var regularSubmitOnAjax = false;
186 var redirectUri; 187 188 // submit is called after parsley and html5 validation - so we don't have to check for errors 189 $(document).on('submit', 'form[data-powermail-ajax]', function(e) { 190 var $this = $(this); 191 if ($this.data('powermail-ajax-uri')) { 192 redirectUri = $this.data('powermail-ajax-uri'); 193 } 194 var formUid = $this.data('powermail-form'); 195 196 if (!regularSubmitOnAjax) { 197 $.ajax({ 198 type: 'POST', 199 url: $this.prop('action'), 200 data: new FormData($this.get(0)), 201 contentType: false, 202 processData: false, 203 beforeSend: function() { 204 $('.powermail_submit', $this).parent().append(that.getProgressbar()); 205 $('.powermail_confirmation_submit, .powermail_confirmation_form', $this).closest('.powermail_confirmation').append(that.getProgressbar()); 206 }, 207 complete: function() { 208 // remove progressbar 209 $('.powermail_fieldwrap_submit', $this).find('.powermail_progressbar').remove(); 210 that.deleteAllFilesListener(); 211 }, 212 success: function(data) { 213 var html = $('*[data-powermail-form="' + formUid + '"]:first', data); 214 if (html.length) { 215 $('*[data-powermail-form="' + formUid + '"]:first').closest('.tx-powermail').html(html); 216 // fire tabs and parsley again 217 if ($.fn.powermailTabs) { 218 $('.powermail_morestep').powermailTabs(); 219 } 220 if ($.fn.parsley) { 221 $('form[data-parsley-validate="data-parsley-validate"]').parsley(); 222 } 223 that.reloadCaptchaImages(); 224 } else { 225 // no form markup found try to redirect via javascript 226 if (redirectUri) { 227 window.location = redirectUri; 228 } else { 229 // fallback if no location found (but will effect 2x submit) 230 $this.submit(); 231 } 232 regularSubmitOnAjax = true; 233 } 234 } 235 }); 236 237 e.preventDefault(); 238 } 239 }); 240 }; 241 242 /** 243 * Add eventhandler for deleting all files button 244 * 245 * @returns {void} 246 */ 247 this.deleteAllFilesListener = function() { 248 $('.powermail_fieldwrap_file_inner').find('.deleteAllFiles').each(function() { 249 // initially hide upload fields 250 that.disableUploadField($(this).closest('.powermail_fieldwrap_file_inner').find('input[type="file"]')); 251 }); 252 $('.deleteAllFiles').click(function() { 253 that.enableUploadField($(this).closest('.powermail_fieldwrap_file_inner').children('input[type="hidden"]')); 254 $(this).closest('ul').fadeOut(function() { 255 $(this).remove(); 256 }); 257 }); 258 }; 259 260 /** 261 * Disable upload field 262 * 263 * @param {jQuery} $element 264 * @returns {void} 265 */ 266 this.disableUploadField = function($element) { 267 $element.prop('disabled', 'disabled').addClass('hide').prop('type', 'hidden'); 268 }; 269 270 /** 271 * Enable upload field 272 * 273 * @param {jQuery} $element 274 * @returns {void} 275 */ 276 this.enableUploadField = function($element) { 277 $element.prop('disabled', false).removeClass('hide').prop('type', 'file'); 278 }; 279 280 /** 281 * Reload captcha images 282 * 283 * @returns {void} 284 */ 285 this.reloadCaptchaImages = function() { 286 $('img.powermail_captchaimage').each(function() { 287 var source = that.getUriWithoutGetParam($(this).prop('src')); 288 $(this).prop('src', source + '?hash=' + that.getRandomString(5)); 289 }); 290 }; 291 292 /** 293 * Get uri without get params 294 * 295 * @param {string} uri 296 * @returns {string} 297 */ 298 this.getUriWithoutGetParam = function(uri) { 299 var parts = uri.split('?'); 300 return parts[0]; 301 }; 302 303 /** 304 * Get random string 305 * 306 * @param {int} length 307 * @returns {string} 308 */ 309 this.getRandomString = function(length) { 310 var text = ''; 311 var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 312 for (var i=0; i < length; i++) { 313 text += possible.charAt(Math.floor(Math.random() * possible.length)); 314 } 315 return text; 316 }; 317 318 /** 319 * Convert date format for html5 date fields 320 * 31.08.2014 => 2014-08-31 321 * 322 * @param {string} value 323 * @param {string} format 324 * @param {string} type 325 * @returns {string|null} 326 */ 327 this.getDatetimeForDateFields = function(value, format, type) { 328 var formatDate = Date.parseDate(value, format); 329 if (formatDate === null) { 330 return null; 331 } 332 var date = new Date(formatDate); 333 var valueDate = date.getFullYear() + '-'; 334 valueDate += ('0' + (date.getMonth() + 1)).slice(-2) + '-'; 335 valueDate += ('0' + date.getDate()).slice(-2); 336 var valueTime = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2); 337 var valueDateTime = valueDate + 'T' + valueTime; 338 339 if (type === 'date') { 340 return valueDate; 341 } 342 if (type === 'datetime-local') { 343 return valueDateTime; 344 } 345 if (type === 'time') { 346 return valueTime; 347 } 348 return null; 349 }; 350 351 /** 352 * Get markup for progressbar 353 * div.powermail_progressbar>div.powermail_progress>div.powermail_progess_inner 354 * 355 * @returns {jQuery} 356 */ 357 this.getProgressbar = function() { 358 return $('<div />').addClass('powermail_progressbar').html( 359 $('<div />').addClass('powermail_progress').html( 360 $('<div />').addClass('powermail_progess_inner') 361 ) 362 ); 363 }; 364 365 /** 366 * Get maximum filesize of files in multiple upload field 367 * 368 * @param {jQuery} $field 369 * @returns {int} 370 */ 371 this.getMaxFileSize = function($field) { 372 var field = $field.get(0); 373 var size = 0; 374 for (var i = 0; i < field.files.length; i++) { 375 var file = field.files[i]; 376 if (file.size > size) { 377 size = file.size; 378 } 379 } 380 return parseInt(size); 381 }; 382 383 /** 384 * Filesize check for upload fields 385 * 386 * @returns {void} 387 */ 388 this.uploadSizeValidatorListener = function() { 389 window.ParsleyValidator 390 .addValidator('powermailfilesize', function(value, requirement) { 391 if (requirement.indexOf(',') !== -1) { 392 var requirements = requirement.split(','); 393 var maxUploadSize = parseInt(requirements[0]); 394 var $this = $('*[name="tx_powermail_pi1[field][' + requirements[1] + '][]"]'); 395 if ($this.length) { 396 if (that.getMaxFileSize($this) > maxUploadSize) { 397 return false;
398 } 399 } 400 } 401 402 // pass test if problems 403 return true; 404 }, 32) 405 .addMessage('en', 'powermailfilesize', 'Error'); 406 }; 407 408 /** 409 * File extension check for upload fields 410 * 411 * @returns {void} 412 */ 413 this.uploadExtensionValidatorListener = function() { 414 window.ParsleyValidator 415 .addValidator('powermailfileextensions', function(value, requirement) { 416 var $this = $('*[name="tx_powermail_pi1[field][' + requirement + '][]"]'); 417 if ($this.length) { 418 return that.isFileExtensionInList(that.getExtensionFromFileName(value), $this.prop('accept')); 419 } 420 421 // pass test if problems 422 return true; 423 }, 32) 424 .addMessage('en', 'powermailfileextensions', 'Error'); 425 }; 426 427 /** 428 * Check if fileextension is allowed in dotted list 429 * "jpg" in ".jpg,.jpeg" => true 430 * "jpg" in ".gif,.png" => false 431 * 432 * @param {string} extension 433 * @param {string} list 434 * @returns {boolean} 435 */ 436 this.isFileExtensionInList = function(extension, list) { 437 return list.indexOf('.' + extension) !== -1; 438 }; 439 440 /** 441 * Get extension from filename 442 * image.jpg => jpg 443 * 444 * @param {string} fileName 445 * @returns {string} 446 */ 447 this.getExtensionFromFileName = function(fileName) { 448 return fileName.split('.').pop(); 449 }; 450 451 /** 452 * Return BaseUrl as prefix 453 * 454 * @return {string} Base Url 455 */ 456 this.getBaseUrl = function() { 457 var baseurl; 458 if ($('base').length > 0) { 459 baseurl = jQuery('base').prop('href'); 460 } else { 461 if (window.location.protocol != "https:") { 462 baseurl = 'http://' + window.location.hostname; 463 } else { 464 baseurl = 'https://' + window.location.hostname; 465 } 466 } 467 return baseurl; 468 } 469} 470 471jQuery(document).ready(function($) { 472 'use strict'; 473 var PowermailForm = new window.PowermailForm($); 474 PowermailForm.initialize(); 475});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.