[TASK] Register event handler for changes in fields and concatenate hide/unhide code...
authorOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 10 Sep 2015 11:00:50 +0000 (13:00 +0200)
committerOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 10 Sep 2015 11:00:50 +0000 (13:00 +0200)
Resources/Public/JavaScript/PowermailCondition.js

index 10af72a..9452345 100644 (file)
@@ -50,17 +50,29 @@ function PowermailCondition($formElement) {
                        processData: false,
                        success: function(data) {
                                console.log(data);
-                               if (data.todo_field !== undefined) {
-                                       for (var formUid in data.todo_field) {
+                               if (data.loops === 100) {
+                                       console.log('DFAAFUQ');
+                               }
+                               if (data.todo !== undefined) {
+                                       for (var formUid in data.todo) {
                                                var form = $('.powermail_form_' + formUid)
-                                               for (var pageUid in data.todo_field[formUid]) {
-                                                       for (var fieldMarker in data.todo_field[formUid][pageUid]) {
+                                               for (var pageUid in data.todo[formUid]) {
+
+                                                       var page = form.find('.powermail_fieldset_' + pageUid);
+                                                       if (data.todo[formUid][pageUid]['action'] === 'hide') {
+                                                               page.hide();
+                                                       }
+                                                       if (data.todo[formUid][pageUid]['action'] === 'un_hide') {
+                                                               page.show();
+                                                       }
+
+                                                       for (var fieldMarker in data.todo[formUid][pageUid]) {
                                                                var fields = form.find('[id^=powermail_field_' + fieldMarker + ']');
-                                                               if (data.todo_field[formUid][pageUid][fieldMarker]['action'] === 'hide') {
+                                                               if (data.todo[formUid][pageUid][fieldMarker]['action'] === 'hide') {
                                                                        fields.prop('disabled', true);
                                                                        fields.closest('.powermail_fieldwrap').hide();
                                                                }
-                                                               if (data.todo_field[formUid][pageUid][fieldMarker]['action'] === 'un_hide') {
+                                                               if (data.todo[formUid][pageUid][fieldMarker]['action'] === 'un_hide') {
                                                                        fields.removeProp('disabled');
                                                                        fields.closest('.powermail_fieldwrap').show();
                                                                }
@@ -68,20 +80,6 @@ function PowermailCondition($formElement) {
                                                }
                                        }
                                }
-                               if (data.todo_page !== undefined) {
-                                       for (var formUid in data.todo_page) {
-                                               var form = $('.powermail_form_' + formUid)
-                                               for (var pageUid in data.todo_page[formUid]) {
-                                                       var page = form.find('.powermail_fieldset_' + pageUid);
-                                                       if (data.todo_page[formUid][pageUid]['action'] === 'hide') {
-                                                               page.hide();
-                                                       }
-                                                       if (data.todo_page[formUid][pageUid]['action'] === 'un_hide') {
-                                                               page.show();
-                                                       }
-                                               }
-                                       }
-                               }
 
                        }
                });
@@ -104,5 +102,20 @@ jQuery(document).ready(function() {
        $('form.powermail_form').each(function() {
                var PowermailCondition = new window.PowermailCondition($(this));
                PowermailCondition.sendFormValuesToPowermailCond();
+               $(this).find('input').each(function() {
+                       $(this).on('change', function() {
+                               PowermailCondition.sendFormValuesToPowermailCond();
+                       });
+               });
+               $(this).find('select').each(function() {
+                       $(this).on('change', function() {
+                               PowermailCondition.sendFormValuesToPowermailCond();
+                       });
+               });
+               $(this).find('textarea').each(function() {
+                       $(this).on('change', function() {
+                               PowermailCondition.sendFormValuesToPowermailCond();
+                       });
+               });
        });
 });