1/* 2 * iPhorm WordPress jQuery plugin 3 */ 4;(function($) { 5 $.iPhorm = function ($form, options) { 6 var _this = this, 7 settings = {}, 8 extraData = { 9 iphorm_ajax: 1 10 }, 11 submitted = false, 12 uploaders = [], 13 uploadQueue = [], 14 swfUploadError = false, 15 supportsSwfUpload = typeof swfobject === 'object' && swfobject.hasFlashPlayerVersion('9.0.28'), 16 $successMessage = $('.iphorm-success-message', $form), 17 $loadingSpinner = $('.iphorm-loading-wrap', $form), 18 clCache = {}, 19 propFunc = !!$.fn.prop ? 'prop' : 'attr'; 20 21 _this.$form = $form; 22 23 // Load in any options 24 if (options) { 25 $.extend(settings, options); 26 } 27 28 /** 29 * Add an SWFUpload element to the form 30 * 31 * @param object element JavaScript object containing the element information 32 */ 33 _this.addUploader = function (element) { 34 if (supportsSwfUpload) { 35 // Hide the normal file element 36 $('.' + element.name + '-input-wrap', $form).hide(); 37 $('.' + element.name + '-add-another-upload', $form).hide(); 38 39 iPhorm.getSessionId().done(function (sessionId) { 40 // Show the SWFUpload element 41 $('#' + element.uniqueId + '-swfupload').show(); 42 43 var $queue = $('#' + element.uniqueId + '-file-queue'), 44 $queueErrors = $('#' + element.uniqueId + '-file-queue-errors'), 45 browseButton = getHiddenDimensions($('#' + element.uniqueId + '-browse')), swfu; 46 47 // Define SWFUpload handles 48 function fileDialogStart () { 49 $queueErrors.hide().empty(); 50 } 51 52 function fileQueued (file) { 53 var $close = $('<div class="iphorm-upload-queue-remove">X</div>').click(function () { 54 swfu.cancelUpload(file.id); 55 for (var i = 0; i < uploadQueue.length; i++) { 56 if (uploadQueue[i].file.id == file.id) { 57 uploadQueue.splice(i, 1); 58 } 59 } 60 $(this).parent().remove(); 61 62 if ($queue.children().length === 0) { 63 $queue.hide(); 64 } 65 }); 66 67 uploadQueue.push({ 68 file: file, 69 uploaderId: swfu.movieName 70 }); 71 72 $queue.append($('<div id="' + file.id + '" class="iphorm-upload-queue-file"><div class="iphorm-upload-queue-filename">' + file.name + ' (' + formatFileSize(file.size) + ')</div></div>').append($close)).show(); 73 } 74 75 function fileQueueError (file, errorCode, message) { 76 $queueErrorsList = $('<div class="iphorm-queue-errors-list iphorm-clearfix"></div>'); 77 78 switch (errorCode) { 79 case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED: 80 $queueErrorsList.append('<div class="iphorm-queue-error">' + iphormL10n.swfupload_too_many + '</div>'); 81 break; 82 case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT: 83 $queueErrorsList.append('<div class="iphorm-queue-error">' + file.name + ' - ' + iphormL10n.swfupload_file_too_big + '</div>'); 84 break; 85 case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE: 86 $queueErrorsList.append('<div class="iphorm-queue-error">' + file.name + ' - ' + iphormL10n.swfupload_file_empty + '</div>'); 87 break; 88 case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE: 89 $queueErrorsList.append('<div class="iphorm-queue-error">' + file.name + ' - ' + iphormL10n.swfupload_file_type_not_allowed + '</div>'); 90 break; 91 default: 92 $queueErrorsList.append('<div class="iphorm-queue-error">' + iphormL10n.swfupload_unknown_queue_error + '</div>'); 93 break; 94 } 95 96 $queueErrors.append($queueErrorsList).show(); 97 } 98 99 function uploadStart(file) { 100 // Show the upload progress bar 101 $('.iphom-upload-progress-wrap', $form).show(); 102 } 103 104 function uploadProgress(file, bytesLoaded, bytesTotal) { 105 var progress = Math.min(100, ((bytesLoaded / file.size) * 100)); // Limit to 100% maximum 106 $('.iphorm-upload-progress-bar', $form).css('width', progress + '%'); 107 $('.iphorm-upload-filename', $form).text(file.name); 108 } 109 110 function uploadError(file, errorCode, message) { 111 switch (errorCode) { 112 case SWFUpload.UPLOAD_ERROR.HTTP_ERROR: 113 swfUploadError = true; 114 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_upload_error).show(); 115 break; 116 case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED: 117 swfUploadError = true; 118 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_upload_failed).show(); 119 break; 120 case SWFUpload.UPLOAD_ERROR.IO_ERROR: 121 swfUploadError = true; 122 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_server_io).show(); 123 break; 124 case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR: 125 swfUploadError = true; 126 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_security_error).show(); 127 break; 128 case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED: 129 swfUploadError = true; 130 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_limit_exceeded).show(); 131 break; 132 case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED: 133 swfUploadError = true; 134 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_validation_failed).show(); 135 break; 136 case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED: 137 break; 138 case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED: 139 swfUploadError = true; 140 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_upload_stopped).show(); 141 break; 142 default: 143 swfUploadError = true; 144 $('.iphorm-upload-error', $form).text(iphormL10n.swfupload_unknown_upload_error).show(); 145 break; 146 } 147 } 148 149 function uploadSuccess(file, response) { 150 if (response) { 151 response = $.parseJSON(response); 152 153 if (typeof response == 'object' && response.type == 'error') { 154 swfUploadError = true; 155 $('.iphorm-upload-error', $form).text(file.name + ' - ' + response.data[0]).show(); 156 $('#' + file.id, $form).remove(); 157 } 158 } 159 } 160 161 function uploadComplete(file) { 162 if (!swfUploadError) { 163 // Show the file as uploaded successfully 164 $('#' + file.id, $form).find('.iphorm-upload-queue-remove').removeClass('iphorm-upload-queue-remove').addClass('iphorm-upload-queue-success').unbind('click'); 165 166 if (uploadQueue.length > 0) { 167 var next = uploadQueue.shift(); 168 getUploader(next.uploaderId).startUpload(next.file.id); 169 } else { 170 // Nothing left in the queue so this time we submit the form 171 $('.iphom-upload-progress-wrap', $form).hide(); 172 _this.submit(); 173 } 174 } else { 175 // Hide the loading spinner 176 $loadingSpinner.hide(); 177 resetSWFUpload(); 178 submitted = false;
179 } 180 } 181 182 swfu = new SWFUpload({ 183 button_height: browseButton.outerHeight, 184 button_width: browseButton.outerWidth, 185 button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT, 186 button_placeholder_id: element.uniqueId + '-object', 187 button_cursor: SWFUpload.CURSOR.HAND, 188 189 flash_url: iphormL10n.swfupload_flash_url, 190 upload_url: iphormL10n.swfupload_upload_url, 191 file_post_name: element.name, 192 193 file_dialog_start_handler: fileDialogStart, 194 file_queued_handler: fileQueued, 195 file_queue_error_handler: fileQueueError, 196 upload_start_handler: uploadStart, 197 upload_progress_handler: uploadProgress, 198 upload_error_handler: uploadError, 199 upload_success_handler: uploadSuccess, 200 upload_complete_handler: uploadComplete, 201 202 post_params: { 203 iphorm_id: settings.id, 204 iphorm_form_uniq_id: settings.uniqueId, 205 iphorm_element_id: element.id, 206 iphorm_element_name: element.name, 207 PHPSESSID: sessionId 208 }, 209 210 prevent_swf_caching: true, 211 212 file_types : element.fileTypes, 213 file_types_description: element.fileTypesDescription, 214 file_size_limit : element.fileSizeLimit, 215 file_upload_limit : element.fileUploadLimit, 216 217 debug: false 218 }); 219 220 uploaders.push(swfu); 221 }) 222 .fail(function () { 223 // The request to get the session ID failed, so show normal file upload fields 224 $('.' + element.name + '-input-wrap', $form).show(); 225 $('.' + element.name + '-add-another-upload', $form).show(); 226 }); 227 } 228 }; // End addUploader 229 230 /** 231 * Bind to the submit event of the form 232 */ 233 if (settings.useAjax) { 234 $form.bind('submit', function(event) { 235 // Can't submit during preview 236 if (settings.preview === true) { 237 alert(iphormL10n.preview_no_submit); 238 return false; 239 } 240 241 // Prevent double submit 242 if (submitted) { 243 return false; 244 } else { 245 submitted = true; 246 } 247 248 // Show loading image 249 $loadingSpinner.fadeIn('slow'); 250 251 // Prevent the browser submitting the form normally 252 event.preventDefault(); 253 254 // Remove any previous upload error 255 $('.iphorm-upload-error', $form).hide().text(''); 256 257 // Detect if there are any SWFUpload files in the queue and upload them first 258 if (uploadQueue.length > 0) { 259 extraData.iphorm_swfu = 1; 260 var next = uploadQueue.shift(); 261 getUploader(next.uploaderId).startUpload(next.file.id); 262 } else { 263 // There are no uploads in the queue, submit the form normally 264 _this.submit(); 265 } 266 }); // End bind 267 } // if settings.useAjax 268 269 // Prepares the form to be submitted again 270 var prepareForm = function () { 271 // Hide the loading spinner 272 $loadingSpinner.hide(); 273 274 // Hide any previous errors or success messages 275 $('.iphorm-errors-wrap', $form).hide(); 276 $('.iphorm-errors-list, .iphorm-error', $form).remove(); 277 $('.iphorm-queue-errors', $form).hide().empty(); 278 $successMessage.hide(); 279 $('.iphorm-element-error', $form).removeClass('iphorm-element-error'); 280 281 // Allow the form to be submitted again 282 submitted = false; 283 }; 284 285 /** 286 * Submits the form 287 */ 288 _this.submit = function () { 289 // Bind the form submit to use the ajax form plugin 290 $form.ajaxSubmit({ 291 async: false, 292 type: 'post', 293 dataType: 'json', 294 data: extraData, 295 iframe: true, 296 url: '', 297 success: function(response) { 298 // Reset reCAPTCHAs - they can't be used twice 299 if (!!window.grecaptcha) { 300 $('.iphorm-recaptcha', $form).each(function () { 301 try { 302 window.grecaptcha.reset($(this).data('iphorm-recaptcha-id')); 303 } catch (e) {} 304 }); 305 } 306 307 // Check if the form submission was successful 308 if (response === null || response === undefined) { 309 // Hide the loading spinner 310 $loadingSpinner.hide(); 311 312 // Allow resubmitting 313 submitted = false; 314 315 alert(iphormL10n.error_submitting_form); 316 } else if (typeof response === 'object') { 317 if (response.type == 'success') { 318 _this.resetForm(); 319 320 if (typeof response.redirect === 'string') { 321 if (response.redirect === '') { 322 window.location.reload(); 323 } else { 324 window.location = response.redirect; 325 } 326 } else { 327 // Then fade in the success message 328 $successMessage.html(response.data).fadeIn('slow').show(0, function() { 329 // Set timeout 330 if (settings.successMessageTimeout > 0) { 331 setTimeout(function () { 332 $successMessage.fadeOut(400); 333 }, (settings.successMessageTimeout * 1000)); 334 } 335 336 // Custom success callback 337 if (typeof settings.success === 'function') { 338 settings.success(); 339 } 340 }); 341 342 // Scroll to the success message if it's not in view 343 if (!isScrolledIntoView($successMessage) && $.isFunction($.smoothScroll)) { 344 $.smoothScroll({ 345 scrollTarget: $successMessage, 346 offset: -50, 347 speed: 500 348 }); 349 } 350 } 351 } else if (response.type == 'error') { 352 prepareForm(); 353 354 var $errors = $(); 355 356 // Go through each element containing errors 357 $.each(response.data, function(index, info) { 358 // If there are errors for this element 359 if ($.isArray(info.errors) && info.errors.length > 0) { 360 // Save a reference to this element 361 var $elementWrap = $("." + index + "-element-wrap", $form), 362 $errorsWrap = $elementWrap.find('.iphorm-errors-wrap'); 363 364 // If the returned element exists 365 if ($elementWrap.length && $errorsWrap.length) { 366 // Create a blank error list 367 var $errorList = $('<div class="iphorm-errors-list iphorm-clearfix"></div>'); 368 369 // Go through each error for this field 370 $.each(info.errors, function(i, e) { 371 // Append the error to the list as a list item
372 $errorList.append('<div class="iphorm-error">' + e + '</div>'); 373 return false; // Just display one error per element 374 }); 375 376 $errors = $errors.add($elementWrap); 377 378 // Add the error list after the element's wrapper 379 $errorsWrap.append($errorList); 380 381 // Add an error class to the element wrapper 382 $elementWrap.addClass('iphorm-element-error'); 383 } 384 } 385 }); 386 387 // Fade all errors in 388 $('.iphorm-errors-wrap', $form).fadeIn(1000).show(); 389 390 // Scroll to the first error 391 if ($errors.size()) { 392 var $targetError = $errors.get(0); 393 if (!isScrolledIntoView($targetError) && $.isFunction($.smoothScroll)) { 394 $.smoothScroll({ 395 scrollTarget: $targetError, 396 offset: -50, 397 speed: 700 398 }); 399 } 400 } 401 402 // Custom error callback 403 if (typeof settings.error === 'function') { 404 settings.error(); 405 } 406 } // End reponse.type == error 407 } // End typeof response == object 408 }, // End success callback 409 error: function () { 410 // Hide the loading spinner 411 $loadingSpinner.hide(); 412 413 // Allow resubmitting 414 submitted = false; 415 416 alert(iphormL10n.error_submitting_form); 417 } 418 }); // End ajaxSubmit() 419 }; // End submit() 420 421 /** 422 * Reset the form to its original state 423 */ 424 _this.resetForm = function (resetFormValues) { 425 prepareForm(); 426 427 // Reset the captcha 428 $('.iphorm-captcha-image', $form).trigger('click'); 429 430 resetFormValues = resetFormValues || settings.resetFormValues; 431 switch (resetFormValues) { 432 default: 433 case '': 434 $form.resetForm(); 435 break; 436 case 'clear': 437 $form.clearForm(); 438 $('select', $form).each(function () { 439 $(this)[propFunc]('selectedIndex', 0); 440 }); 441 break; 442 case 'keep': 443 /* Do nothing */ 444 break; 445 } 446 447 // Call blur on element to reset inline labels 448 $('input[type="text"], textarea', $form).blur(); 449 450 // Hide dynamically added file inputs 451 $('.iphorm-add-another-file-wrap', $form).remove(); 452 453 // Reset file upload fields properly in IE10+ 454 $('input[type="file"]', $form).each(function () { 455 var $fileInput = $(this), 456 uniformed = !!$fileInput.data('uniformed'); 457 458 if (uniformed) { 459 $.uniform.restore($fileInput); 460 } 461 462 var $fileInputClone = $fileInput.val('').clone(true); 463 464 $fileInput.replaceWith($fileInputClone); 465 466 if (uniformed) { 467 $fileInputClone.uniform(); 468 } 469 }); 470 471 // Reset conditional logic 472 _this.applyAllLogic(); 473 474 // Sync uniform with underlying elements 475 if (typeof $.uniform === 'object' && typeof $.uniform.update === 'function') { 476 $.uniform.update(); 477 } 478 479 // Reset SWF upload stats 480 resetSWFUpload(); 481 482 // Hide any tooltips 483 $('.qtip').hide(); 484 }; 485 486 /** 487 * Adds a datepicker to the element with the given unique ID 488 * 489 * @param string uniqueId 490 */ 491 _this.addDatepicker = function (uniqueId, options) { 492 if ($.isFunction($.fn.datepicker)) { 493 var $daySelect = $('#' + uniqueId + '_day'), 494 $monthSelect = $('#' + uniqueId + '_month'), 495 $yearSelect = $('#' + uniqueId + '_year'), 496 $datePicker = $('.iphorm-datepicker', '#' + uniqueId).datepicker($.extend({}, { 497 onSelect: function (dateText, inst) { 498 $daySelect.val(inst.selectedDay).change(); 499 $monthSelect.val(inst.selectedMonth + 1).change(); 500 $yearSelect.val(inst.selectedYear).change(); 501 }, 502 beforeShow: function (input, inst) { 503 var currentTime = new Date(), 504 dayToSet = ($daySelect.val().length > 0) ? $daySelect.val() : currentTime.getDate(), 505 monthToSet = ($monthSelect.val().length > 0) ? $monthSelect.val()-1 : currentTime.getMonth(), 506 yearToSet = ($yearSelect.val().length > 0) ? $yearSelect.val() : currentTime.getFullYear(); 507 508 $datePicker.datepicker('setDate', new Date(yearToSet, monthToSet, dayToSet)); 509 }}, options) 510 ); 511 512 $('.iphorm-datepicker-icon', '#' + uniqueId).click(function () { 513 $datePicker.datepicker('show'); 514 }).show(); 515 } 516 }; 517 518 /** 519 * Applies the the logic to all elements 520 * 521 * If loading is true, bind the logic triggers and do not animate the logic 522 * 523 * @param boolean loading 524 */ 525 _this.applyAllLogic = function (loading) { 526 clCache = {}; 527 _this.applyLogic(settings.clElementIds, loading); 528 529 if (loading) { 530 _this.applyDependentLogic(settings.clDependentElementIds); 531 } 532 }; 533 534 /** 535 * Applies logic to show or hide the elements with the given IDs 536 * 537 * @param array elementIds The element IDs to apply the logic to 538 * @param boolean loading True if we are applying initial logic (to skip animating) 539 */ 540 _this.applyLogic = function (elementIds, loading) { 541 var i = 0, length = elementIds.length; 542 for ( ; i < length; i++) { 543 _this.applyElementLogic(elementIds[i], loading); 544 } 545 }; 546 547 /** 548 * Apply logic to the given element ID 549 * 550 * @param int elementId 551 * @param boolean loading True if we are applying initial logic (to skip animating) 552 */ 553 _this.applyElementLogic = function (elementId, loading) { 554 if (iPhorm.logic[settings.id] && iPhorm.logic[settings.id].logic) { 555 var logic = iPhorm.logic[settings.id].logic[elementId]; 556 557 if (logic && logic.rules && logic.rules.length) { 558 var matchedValues = 0, elementUniqueIdPrefix = 'iphorm_' + settings.id + '_', action; 559 560 for (var i = 0; i < logic.rules.length; i++) { 561 var rule = logic.rules[i];
562 if ((rule.operator == 'eq' && _this.elementHasValue(elementUniqueIdPrefix + rule.element_id, rule.value)) || (rule.operator == 'neq' && !_this.elementHasValue(elementUniqueIdPrefix + rule.element_id, rule.value))) { 563 matchedValues++; 564 } 565 } 566 567 if ((logic.match == 'any' && matchedValues > 0) || (logic.match == 'all' && matchedValues == logic.rules.length)) { 568 action = logic.action; 569 } else { 570 action = logic.action === 'show' ? 'hide' : 'show'; 571 } 572 573 var $element = $('.iphorm_'+settings.id+'_'+elementId+'-element-wrap', $form); 574 575 if ($element.length === 0) { 576 $element = $('.iphorm_'+settings.id+'_'+elementId+'-group-wrap'); 577 } 578 579 if (!loading && iPhorm.logic[settings.id].animate) { 580 if (action == 'show') { 581 $element.slideDown(400, function () { 582 centerFancybox(); 583 }); 584 } else { 585 $element.slideUp(400, function () { 586 centerFancybox(); 587 }); 588 } 589 } else { 590 if (action == 'show') { 591 $element.show(); 592 } else { 593 $element.hide(); 594 } 595 596 if (!loading) { 597 centerFancybox(); 598 } 599 } 600 } 601 } 602 }; 603 604 /** 605 * Binds the conditional logic events to the elements 606 * 607 * @param array elementIds 608 */ 609 _this.applyDependentLogic = function (elementIds) { 610 if (iPhorm.logic[settings.id] && iPhorm.logic[settings.id].dependents) { 611 var i = 0, 612 length = elementIds.length, 613 bindHandler = function ($input, bind, elementUniqueId, dependentElementIds) { 614 $input.bind(bind, function () { 615 clCache[elementUniqueId] = []; 616 setTimeout(function () { _this.applyLogic(dependentElementIds); }, 0); // setTimeout here to prevent a small delay closing the select menu in Chrome 617 }); 618 }; 619 620 for ( ; i < length; i++) { 621 var dependentElementIds = iPhorm.logic[settings.id].dependents[elementIds[i]], 622 elementUniqueId = 'iphorm_' + settings.id + '_' + elementIds[i], 623 $input = $('.' + elementUniqueId, $form); 624 625 if ($input.length) { 626 var bind; 627 if ($input.is('select')) { 628 bind = 'change.iphorm'; 629 } else if ($input.is('input[type=checkbox]') || $input.is('input[type=radio]')) { 630 bind = 'click.iphorm'; 631 } 632 633 if (bind) { 634 bindHandler($input, bind, elementUniqueId, dependentElementIds); 635 } 636 } 637 } 638 } 639 }; 640 641 /** 642 * Does the element of the given ID has the given value? 643 * 644 * @param {string} elementUniqueId The unique ID of the element e.g. iphorm_1_1 645 * @param {string} value Check if the element has this value 646 * @return {boolean} 647 */ 648 _this.elementHasValue = function (elementUniqueId, value) { 649 // Check the cache first 650 if ($.isArray(clCache[elementUniqueId])) { 651 if (clCache[elementUniqueId].length) { 652 var i = 0, 653 length = clCache[elementUniqueId].length; 654 655 for ( ; i < length; i++) { 656 if (clCache[elementUniqueId][i].value === value) { 657 return clCache[elementUniqueId][i].result; 658 } 659 } 660 } 661 } else { 662 clCache[elementUniqueId] = []; 663 } 664 665 // Look up the value in the DOM and save the result to cache 666 var $input = $('.' + elementUniqueId, $form); 667 668 if ($input.length) { 669 if ($input.is('select')) { 670 if ($input.val() == value) { 671 clCache[elementUniqueId].push({ value: value, result: true }); 672 return true; 673 } 674 } else if ($input.is('input[type=checkbox]') || $input.is('input[type=radio]')) { 675 var hasValue = false; 676 $.each($input, function () { 677 if ($(this).is(':checked') && $(this).val() == value) { 678 hasValue = true; 679 return false; 680 } 681 }); 682 clCache[elementUniqueId].push({ value: value, result: hasValue }); 683 return hasValue; 684 } 685 } 686 687 clCache[elementUniqueId].push({ value: value, result: false }); 688 return false; 689 }; 690 691 /** 692 * Clears the default value and saves it and unbind the focus event, 693 * if reset is true a blur event is bound to show the default value 694 * again on blur if left empty. 695 * 696 * @param boolean reset 697 */ 698 _this.clearDefaultValue = function (reset) { 699 $(this).data('iphorm-default-value', $(this).val()).val('').unbind('focus'); 700 701 if (reset) { 702 $(this).bind('blur', function () { 703 _this.resetDefaultValue.call(this); 704 }); 705 } 706 }; 707 708 /** 709 * Resets the default value of the element 710 */ 711 _this.resetDefaultValue = function () { 712 if ($(this).val() === '') { 713 $(this).val($(this).data('iphorm-default-value')).unbind('blur').bind('focus', function () { 714 _this.clearDefaultValue.call(this, true); 715 }); 716 } 717 }; 718 719 /** 720 * Center the fancybox inside the viewport 721 */ 722 function centerFancybox() 723 { 724 if (settings.centerFancybox && typeof $.fancybox === 'function' && typeof $.fancybox.center === 'function' && $('#fancybox-wrap').length && $('#fancybox-wrap').is(':visible')) { 725 $.fancybox.center(settings.centerFancyboxSpeed); 726 } 727 } 728 729 /** 730 * Format a file size given in bytes to a human readable value 731 * 732 * @param int File size in bytes 733 * @return string 734 */ 735 function formatFileSize(size) { 736 if (size >= 1073741824) { 737 size = (Math.round((size / 1073741824) * 10) / 10) + ' GB'; 738 } else if (size >= 1048576) { 739 size = (Math.round((size / 1048576) * 10) / 10) + ' MB'; 740 } else if (size >= 1024) { 741 size = (Math.round((size / 1024) * 10) / 10) + ' KB'; 742 } else { 743 size = size + ' bytes'; 744 } 745 746 return size; 747 } 748 749 /** 750 * Get the SWFUploader with the given ID 751 * 752 * @param string The uploader movie ID 753 * @return object|null 754 */ 755 function getUploader(uploaderId) 756 { 757 for (var i = 0; i < uploaders.length; i++) { 758 if (uploaders[i].movieName == uploaderId) { 759 return uploaders[i]; 760 } 761 } 762 763 return null; 764 } 765 766 /** 767 * Is the element in or scrolled out of the current viewport 768 * 769 * @param DOMElement element 770 * @return boolean 771 */ 772 function isScrolledIntoView(elem) { 773 var docViewTop = $(window).scrollTop(); 774 var docViewBottom = docViewTop + $(window).height(); 775 776 var elemTop = $(elem).offset().top; 777 var elemBottom = elemTop + $(elem).height(); 778 779 return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) && (elemTop >= docViewTop)); 780 } 781 782 /** 783 * Reset all the SWFUpload fields and stats 784 */ 785 function resetSWFUpload() 786 { 787 // Reset the SWFUpload stats and queues 788 $('.iphorm-file-queue').hide().empty(); 789 for (var i = 0; i < uploaders.length; i++) { 790 try { 791 uploaders[i].setStats({ 792 in_progress: 0, 793 files_queued : 0, 794 successful_uploads : 0, 795 upload_errors : 0, 796 upload_cancelled : 0, 797 queue_errors : 0 798 }); 799 } catch (e) {} 800 } 801 } 802 803 /** 804 * Get the dimensions of the given element even if it is hidden 805 * 806 * @param DOMElement element 807 * @param boolean includeMargin Include margin in outerWidth? 808 * @return object Object with all dimensions 809 */ 810 function getHiddenDimensions(element, includeMargin) { 811 var $item = $(element), 812 props = { position: 'absolute', visibility: 'hidden', display: 'block' }, 813 dim = { width:0, height:0, innerWidth: 0, innerHeight: 0,outerWidth: 0,outerHeight: 0 }, 814 $hiddenParents = $item.parents()[!!$.fn.addBack ? 'addBack' : 'andSelf']().not(':visible'); 815 816 includeMargin = (includeMargin === undefined) ? false : includeMargin; 817 818 var oldProps = []; 819 $hiddenParents.each(function() { 820 var old = {}; 821 822 for ( var name in props ) { 823 old[ name ] = this.style[ name ]; 824 this.style[ name ] = props[ name ]; 825 } 826 827 oldProps.push(old); 828 }); 829 830 dim.width = $item.width(); 831 dim.outerWidth = $item.outerWidth(includeMargin); 832 dim.innerWidth = $item.innerWidth(); 833 dim.height = $item.height();
834 dim.innerHeight = $item.innerHeight(); 835 dim.outerHeight = $item.outerHeight(includeMargin); 836 837 $hiddenParents.each(function(i) { 838 var old = oldProps[i]; 839 for ( var name in props ) { 840 this.style[ name ] = old[ name ]; 841 } 842 }); 843 844 return dim; 845 } 846 }; // End $.iPhorm() 847 848 $.fn.iPhorm = function(options) { 849 return this.each(function () { 850 var $this = $(this); 851 if (!$this.data('quform')) { 852 var form = new $.iPhorm($this, options); 853 iPhorm.instance = form; 854 $this.data('quform', form); 855 } 856 }); 857 }; // End $.fn.iPhorm 858 859 // Preload the images in the base theme 860 $(window).load(function () { 861 window.iPhorm.preload([ 862 'file-upload-tick.png', 863 'captcha-refresh-icon.png', 864 'default-loading.gif', 865 'error.png', 866 'success.png' 867 ], iphormL10n.plugin_url + '/images/'); 868 }); 869})(jQuery); // End jQuery wrapper
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.