[TASK] Add private functions to javascript
authorAlex Kellner <alexander.kellner@in2code.de>
Mon, 15 Feb 2016 13:00:21 +0000 (14:00 +0100)
committerAlex Kellner <alexander.kellner@in2code.de>
Mon, 15 Feb 2016 13:00:21 +0000 (14:00 +0100)
Resources/Public/JavaScript/PowermailCondition.js

index ed24aac..213d2b7 100644 (file)
-/**
- * PowermailCondition functions
- *
- * @class PowermailCondition
- * @param {jQuery} $formElement powermail form
- * @constructor
- */
-function PowermailCondition($, $formElement) {
-       'use strict';
-
+(function($) {
        /**
-        * This class
+        * PowermailCondition functions
         *
-        * @type {PowermailCondition}
+        * @class PowermailCondition
+        * @param {HTMLFormElement} formElement powermail form
+        * @constructor
         */
-       var that = this;
+       function PowermailCondition(formElement) {
+               'use strict';
 
-       /**
-        * Classnames of all default fields
-        *
-        * @type {array}
-        */
-       this.defaultFieldClassNames = [
-               'powermail_input',
-               'powermail_textarea',
-               'powermail_select',
-               'powermail_radio',
-               'powermail_checkbox'
-       ];
+               /**
+                * Closure for $(formElement)
+                *
+                * @type {jQuery}
+                */
+               var $formElement = $(formElement);
 
-       /**
-        * @type {jQuery}
-        */
-       this.$formElement = $formElement;
+               /**
+                * Classnames of all default fields (used via closure)
+                *
+                * @type {array}
+                */
+               var defaultFieldClassNames = [
+                       'powermail_input',
+                       'powermail_textarea',
+                       'powermail_select',
+                       'powermail_radio',
+                       'powermail_checkbox'
+               ];
 
-       /**
-        * Listening for changes
-        *
-        * @returns {void}
-        */
-       this.ajaxListener = function() {
-               // initially send form values
-               that.sendFormValuesToPowermailCond();
+               /**
+                * Listening for changes
+                *
+                * @returns {void}
+                */
+               this.ajaxListener = function() {
+                       // initially send form values
+                       sendFormValuesToPowermailCond();
 
-               // send form values on a change
-               $(that.getDefaultFieldClassNamesList()).on('change', function() {
-                       that.sendFormValuesToPowermailCond();
-               });
-       };
+                       // send form values on a change
+                       $(getDefaultFieldClassNamesList()).on('change', function() {
+                               sendFormValuesToPowermailCond();
+                       });
+               };
 
-       /**
-        * ************* Internal ***************
-        */
+               /**
+                * do actions with fields or fieldsets (private)
+                *
+                * @param {array} data
+                * @returns {void}
+                */
+               var processActions = function(data) {
+                       if (data.todo !== undefined) {
+                               for (var formUid in data.todo) {
+                                       var $form = $('.powermail_form_' + formUid)
+                                       for (var pageUid in data.todo[formUid]) {
 
-       /**
-        * Send form values
-        *
-        * @returns {void}
-        */
-       this.sendFormValuesToPowermailCond = function() {
-               var formToSend = $(that.$formElement.get(0));
-               var tempEnabledFields = formToSend.find(':disabled').prop('disabled', false);
-               var dataToSend = new FormData(that.$formElement.get(0));
-               tempEnabledFields.prop('disabled', true);
-               jQuery.ajax({
-                       type: 'POST',
-                       url: that.getAjaxUri(),
-                       data: dataToSend,
-                       contentType: false,
-                       processData: false,
-                       success: function(data) {
-                               if (data.loops === 100) {
-                                       that.log('100 loops reached by parsing conditions and rules. Maybe there are conflicting conditions.');
+                                               // do actions with whole pages
+                                               var $page = $form.find('.powermail_fieldset_' + pageUid);
+                                               if (data.todo[formUid][pageUid]['#action'] === 'hide') {
+                                                       hidePage(getFieldsetByUid(pageUid, $form));
+                                               }
+                                               if (data.todo[formUid][pageUid]['#action'] === 'un_hide') {
+                                                       showPage(getFieldsetByUid(pageUid, $form));
+                                               }
+
+                                               // do actions with single fields
+                                               for (var fieldMarker in data.todo[formUid][pageUid]) {
+                                                       if (data.todo[formUid][pageUid][fieldMarker]['#action'] === 'hide') {
+                                                               hideField(getFieldByMarker(fieldMarker, $form));
+                                                       }
+                                                       if (data.todo[formUid][pageUid][fieldMarker]['#action'] === 'un_hide') {
+                                                               showField(getFieldByMarker(fieldMarker, $form));
+                                                       }
+                                               }
+                                       }
                                }
-                               that.processActions(data);
                        }
-               });
-       };
+               };
 
-       /**
-        * do actions with fields or fieldsets
-        *
-        * @param {array} data
-        * @returns {void}
-        */
-       this.processActions = function(data) {
-               if (data.todo !== undefined) {
-                       for (var formUid in data.todo) {
-                               var $form = $('.powermail_form_' + formUid)
-                               for (var pageUid in data.todo[formUid]) {
-
-                                       // do actions with whole pages
-                                       var $page = $form.find('.powermail_fieldset_' + pageUid);
-                                       if (data.todo[formUid][pageUid]['#action'] === 'hide') {
-                                               that.hidePage(that.getFieldsetByUid(pageUid, $form));
-                                       }
-                                       if (data.todo[formUid][pageUid]['#action'] === 'un_hide') {
-                                               that.showPage(that.getFieldsetByUid(pageUid, $form));
+               /**
+                * Send form values (private)
+                *
+                * @returns {void}
+                */
+               var sendFormValuesToPowermailCond = function() {
+                       var formToSend = $($formElement.get(0));
+                       var tempEnabledFields = formToSend.find(':disabled').prop('disabled', false);
+                       var dataToSend = new FormData($formElement.get(0));
+                       tempEnabledFields.prop('disabled', true);
+                       $.ajax({
+                               type: 'POST',
+                               url: getAjaxUri(),
+                               data: dataToSend,
+                               contentType: false,
+                               processData: false,
+                               success: function(data) {
+                                       if (data.loops === 100) {
+                                               log('100 loops reached by parsing conditions and rules. Maybe there are conflicting conditions.');
                                        }
+                                       processActions(data);
+                               }
+                       });
+               };
 
-                                       // do actions with single fields
-                                       for (var fieldMarker in data.todo[formUid][pageUid]) {
-                                               if (data.todo[formUid][pageUid][fieldMarker]['#action'] === 'hide') {
-                                                       that.hideField(that.getFieldByMarker(fieldMarker, $form));
-                                               }
-                                               if (data.todo[formUid][pageUid][fieldMarker]['#action'] === 'un_hide') {
-                                                       that.showField(that.getFieldByMarker(fieldMarker, $form));
-                                               }
-                                       }
+               /**
+                * Make a required field to a non-required field (private)
+                *
+                * @param {jQuery} $field
+                * @returns {void}
+                */
+               var derequireField = function($field) {
+                       if ($field.prop('required') || $field.data('parsley-required')) {
+                               $field.prop('required', false);
+                               $field.removeAttr('data-parsley-required');
+                               $field.data('powermailcond-required', 'required');
+                       }
+                       reInitializeParsleyValidation();
+               };
+
+               /**
+                * Make a inactive required field required again
+                *
+                * @param {jQuery} $field
+                * @returns {void}
+                */
+               var rerequireField = function($field) {
+                       if ($field.data('powermailcond-required') === 'required') {
+                               if (isHtml5ValidationActivated()) {
+                                       $field.prop('required', 'required');
+                               } else if (isParsleyValidationActivated()) {
+                                       $field.prop('required', 'required');
                                }
                        }
-               }
-       };
+                       $field.removeData('powermailcond-required');
+                       reInitializeParsleyValidation();
+               };
 
-       /**
-        * Show a powermail field
-        *
-        * @param {jQuery} $field
-        * @returns {void}
-        */
-       this.showField = function($field) {
-               $field.prop('disabled', false);
-               $field.closest('.powermail_fieldwrap').show();
-               that.rerequireField($field);
-       };
+               /**
+                * Show a powermail field (private)
+                *
+                * @param {jQuery} $field
+                * @returns {void}
+                */
+               var showField = function($field) {
+                       $field.prop('disabled', false);
+                       $field.closest('.powermail_fieldwrap').show();
+                       rerequireField($field);
+               };
 
-       /**
-        * Hide a powermail field
-        *
-        * @param {jQuery} $field
-        * @returns {void}
-        */
-       this.hideField = function($field) {
-               $field.prop('disabled', true);
-               $field.closest('.powermail_fieldwrap').hide();
-               that.derequireField($field);
-       };
+               /**
+                * Hide a powermail field (private)
+                *
+                * @param {jQuery} $field
+                * @returns {void}
+                */
+               var hideField = function($field) {
+                       $field.prop('disabled', true);
+                       $field.closest('.powermail_fieldwrap').hide();
+                       derequireField($field);
+               };
 
-       /**
-        * Show a powermail page (fieldset)
-        *
-        * @param {jQuery} $page
-        * @returns {void}
-        */
-       this.showPage = function($page) {
-               $page.show();
-       };
+               /**
+                * Show a powermail page (fieldset)
+                *
+                * @param {jQuery} $page
+                * @returns {void}
+                */
+               var showPage = function($page) {
+                       $page.show();
+               };
 
-       /**
-        * Hide a powermail page (fieldset)
-        *
-        * @param {jQuery} $page
-        * @returns {void}
-        */
-       this.hidePage = function($page) {
-               $page.hide();
-       };
-
-       /**
-        * Make a required field to a non-required field
-        *
-        * @param {jQuery} $field
-        * @returns {void}
-        */
-       this.derequireField = function($field) {
-               if ($field.prop('required') || $field.data('parsley-required')) {
-                       $field.prop('required', false);
-                       $field.removeAttr('data-parsley-required');
-                       $field.data('powermailcond-required', 'required');
-               }
-               that.reInitializeParsleyValidation();
-       };
+               /**
+                * Hide a powermail page (fieldset)
+                *
+                * @param {jQuery} $page
+                * @returns {void}
+                */
+               var hidePage = function($page) {
+                       $page.hide();
+               };
 
-       /**
-        * Make a inactive required field required again
-        *
-        * @param {jQuery} $field
-        * @returns {void}
-        */
-       this.rerequireField = function($field) {
-               if ($field.data('powermailcond-required') === 'required') {
-                       if (that.isHtml5ValidationActivated()) {
-                               $field.prop('required', 'required');
-                       } else if (that.isParsleyValidationActivated()) {
-                               $field.prop('required', 'required');
+               /**
+                * get page id
+                *
+                * @returns {int}
+                */
+               var getAjaxUri = function() {
+                       var uri = $('*[data-condition-uri]').data('condition-uri');
+                       if (uri === undefined) {
+                               log('Tag with data-condition-uri not found. Maybe TypoScript was not included.');
                        }
-               }
-               $field.removeData('powermailcond-required');
-               that.reInitializeParsleyValidation();
-       };
+                       return uri;
+               };
 
-       /**
-        * get page id
-        *
-        * @returns {int}
-        */
-       this.getAjaxUri = function() {
-               var uri = $('*[data-condition-uri]').data('condition-uri');
-               if (uri === undefined) {
-                       that.log('Tag with data-condition-uri not found. Maybe TypoScript was not included.');
-               }
-               return uri;
-       };
+               /**
+                * Select a powermail field by its name (private)
+                *         name="tx_powermail_pi1[field][fieldMarker]"
+                *         or
+                *         name="tx_powermail_pi1[field][fieldMarker][]"
+                *
+                * @param {string} fieldMarker
+                * @param {jQuery} $form
+                * @returns {jQuery}
+                */
+               var getFieldByMarker = function(fieldMarker, $form) {
+                       return $form.find('[name^="tx_powermail_pi1[field][' + fieldMarker + ']"]').not('[type="hidden"]');
+               };
 
-       /**
-        * Select a powermail field by its name
-        *              name="tx_powermail_pi1[field][fieldMarker]"
-        *              or
-        *              name="tx_powermail_pi1[field][fieldMarker][]"
-        *
-        * @param {string} fieldMarker
-        * @param {jQuery} $form
-        * @returns {jQuery}
-        */
-       this.getFieldByMarker = function(fieldMarker, $form) {
-               return $form.find('[name^="tx_powermail_pi1[field][' + fieldMarker + ']"]').not('[type="hidden"]');
-       };
-
-       /**
-        * Select a powermail fieldset
-        *              class="powermail_fieldset_[pageUid]"
-        *
-        * @param {string} pageUid
-        * @param {jQuery} $form
-        * @returns {jQuery}
-        */
-       this.getFieldsetByUid = function(pageUid, $form) {
-               return $form.find('.powermail_fieldset_' + pageUid);
-       };
+               /**
+                * Select a powermail fieldset (private)
+                *         class="powermail_fieldset_[pageUid]"
+                *
+                * @param {string} pageUid
+                * @param {jQuery} $form
+                * @returns {jQuery}
+                */
+               var getFieldsetByUid = function(pageUid, $form) {
+                       return $form.find('.powermail_fieldset_' + pageUid);
+               };
 
-       /**
-        * get default field class names as string
-        *
-        * @returns {string}
-        */
-       this.getDefaultFieldClassNamesList = function() {
-               return that.listFromArray(that.defaultFieldClassNames, '.');
-       };
+               /**
+                * Convert array to a string list (private)
+                *
+                * @param {array} array
+                * @param {string} itemPrefix
+                * @param {string} glue
+                * @returns {string}
+                */
+               var listFromArray = function(array, itemPrefix, glue) {
+                       itemPrefix = typeof itemPrefix !== 'undefined' ? itemPrefix : '';
+                       glue = typeof glue !== 'undefined' ? glue : ',';
 
-       /**
-        * Turn off and on parsley validation for a reinitialization
-        *
-        * @returns {void}
-        */
-       this.reInitializeParsleyValidation = function() {
-               if (that.isParsleyValidationActivated()) {
-                       that.$formElement.parsley().destroy();
-                       that.$formElement.parsley();
-               }
-       };
+                       var string = '';
+                       for (var i = 0; i < array.length; i++) {
+                               if (i > 0) {
+                                       string += glue;
+                               }
+                               string += itemPrefix + array[i];
+                       }
+                       return string;
+               };
 
-       /**
-        * Check if parsley validation is activated
-        *
-        * @returns {boolean}
-        */
-       this.isParsleyValidationActivated = function() {
-               return that.$formElement.data('parsley-validate') === 'data-parsley-validate';
-       };
+               /**
+                * get default field class names as string
+                *
+                * @returns {string}
+                */
+               var getDefaultFieldClassNamesList = function() {
+                       return listFromArray(defaultFieldClassNames, '.');
+               };
 
-       /**
-        * Check if html5 validation is activated
-        *
-        * @returns {boolean}
-        */
-       this.isHtml5ValidationActivated = function() {
-               return that.$formElement.data('validate') === 'html5';
-       };
+               /**
+                * Check if parsley validation is activated (private)
+                *
+                * @returns {boolean}
+                */
+               var isParsleyValidationActivated = function() {
+                       return $formElement.data('parsley-validate') === 'data-parsley-validate';
+               };
 
-       /**
-        * Convert array to a string list
-        *
-        * @param {array} array
-        * @param {string} itemPrefix
-        * @param {string} glue
-        * @returns {string}
-        */
-       this.listFromArray = function(array, itemPrefix, glue) {
-               itemPrefix = typeof itemPrefix !== 'undefined' ? itemPrefix : '';
-               glue = typeof glue !== 'undefined' ? glue : ',';
+               /**
+                * Check if html5 validation is activated (private)
+                *
+                * @returns {boolean}
+                */
+               var isHtml5ValidationActivated = function() {
+                       return $formElement.data('validate') === 'html5';
+               };
 
-               var string = '';
-               for (var i = 0; i < array.length; i++) {
-                       if (i > 0) {
-                               string += glue;
+               /**
+                * Turn off and on parsley validation for a reinitialization (private)
+                *
+                * @returns {void}
+                */
+               var reInitializeParsleyValidation = function() {
+                       if (isParsleyValidationActivated()) {
+                               $formElement.parsley().destroy();
+                               $formElement.parsley();
                        }
-                       string += itemPrefix + array[i];
-               }
-               return string;
-       };
+               };
 
-       /**
-        * write message to console
-        *
-        * @param {string|object} message
-        * @return {void}
-        */
-       this.log = function(message) {
-               if (typeof console == 'object') {
-                       if (typeof message === 'string') {
-                               message = 'powermail_cond: ' + message;
+               /**
+                * write message to console
+                *
+                * @param {string|object} message
+                * @return {void}
+                */
+               var log = function(message) {
+                       if (typeof console == 'object') {
+                               if (typeof message === 'string') {
+                                       message = 'powermail_cond: ' + message;
+                               }
+                               console.log(message);
                        }
-                       console.log(message);
-               }
-       };
-
-       // make global
-       window.PowermailCondition = PowermailCondition;
-}
+               };
+       }
 
-jQuery(document).ready(function() {
-       jQuery('form.powermail_form').each(function() {
-               var PowermailCondition = new window.PowermailCondition(jQuery, $(this));
-               PowermailCondition.ajaxListener();
+       $(document).ready(function() {
+               $('form.powermail_form').each(function() {
+                       (new PowermailCondition(this)).ajaxListener();
+               });
        });
-});
+
+})(jQuery);