]> git.mxchange.org Git - mailer.git/blobdiff - js/ajax-common.js
Continued on AJAX installer to start first step (more are easily to add)
[mailer.git] / js / ajax-common.js
index 283e36d82a23defb5b0fdf38592d9a01a896e6a2..ccef02a08a3608b479660f663b851db652bddcc2 100644 (file)
@@ -28,8 +28,6 @@
 
 // Init variables
 var currentTabId       = null;
-var errorDisplayed     = false;
-var warningDisplayed   = false;
 var defaultTabId       = null;
 var footerElements     = new Array();
 var changedElements    = new Array();
@@ -49,8 +47,31 @@ function setCurrentTabId (tabId) {
        currentTabId = tabId;
 }
 
+// Checks whether a given element is visible by checking 'display: none'
+function isElementVisible (prefix, element) {
+       // Get element
+       var el = document.getElementById(prefix + '_' + element);
+
+       // Is element set?
+       if (el == null || el == undefined) {
+               throw new '"' + prefix + '_' + element + '" does not exist.';
+       }
+
+       // Default is visible
+       var isVisible = ((el.style.display == undefined) || ((el.style.display != 'none') && (el.style.display != '')));
+
+       // Return status
+       return isVisible;
+}
+
 // Marks a tab navigation entry
 function markTabNavigation (prefix, tab) {
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
        // Get all li-tags
        var li = document.getElementsByTagName('li');
 
@@ -101,7 +122,12 @@ function disableElement (element) {
 // Enables a given footer navigation element
 function enableFooterNavigationPage (element) {
        // Remove the 'disabled' class and attribute
-       enableElement('input#' + element + '_page');
+       if (element == 'finish') {
+               enableElement('input#finish');
+       } else {
+               enableElement('input#' + element + '_page');
+               disableElement('input#finish');
+       }
 }
 
 // Resets footer navigation by adding CSS class 'disabled'
@@ -152,25 +178,19 @@ function setAjaxSuccess (success) {
 
 // Set AJAX reply and decode JSON if requested
 function setAjaxReply (reply, isJson) {
-       // Copy reply to local variable
-       var localReply = reply;
-
        // Is it JSON URL-encoded content?
        if ((isJson != undefined) && (isJson == true)) {
-               // Then decode it, replace '%20' with space before because '%20' breakes JSON content
-               var obj = jQuery.parseJSON(reply.replace('%20', ' '));
+               // Decode URL-encoding (for some reason it must be here ...)
+               var localReply = decodeUrlEncoding(reply);
 
-               // Is reply_content there
-               if (obj.reply_content == undefined) {
-                       // Not defined
-                       throw new 'obj.reply_content not returned from ajax.php, please fix your scripts.';
-               } // END - if
+               // Then decode it, replace '%20' with space before because '%20' breakes JSON content
+               var obj = jQuery.parseJSON(localReply.replace('%20', ' '));
 
                // ... and set it
-               setAjaxDecodedContent(obj.reply_content);
+               setAjaxContent(obj);
        } else {
                // Handle the content over to decode it
-               setAjaxDecodedContent(localReply);
+               setAjaxDecodedContent(reply);
        }
 }
 
@@ -195,10 +215,10 @@ function sendAjaxRequest (level, doValue, extra, isJson) {
                        // Is ajax_content set?
                        if (ajax_content.reply_content == undefined) {
                                // This shall not happen
-                               throw new 'ajax_content.reply_content not returned from ajax.php, please fix your scripts.';
+                               throw new 'ajax_content.reply_content not returned from ajax.php, please fix your scripts. (1)';
                        } else if (ajax_content.reply_content == null) {
                                // This shall not happen, too
-                               throw new 'ajax_content.reply_content=null from ajax.php, please fix your scripts.';
+                               throw new 'ajax_content.reply_content=null from ajax.php, please fix your scripts. (2)';
                        }
 
                        // Set AJAX reply
@@ -212,11 +232,27 @@ function sendAjaxRequest (level, doValue, extra, isJson) {
                error: function (ajax_content) {
                        // Is ajax_content set?
                        if (ajax_content.reply_content == undefined) {
-                               // This shall not happen
-                               throw new 'ajax_content.reply_content not returned from ajax.php, please fix your scripts.';
+                               // Is 'responseText' there?
+                               if (ajax_content.responseText != undefined) {
+                                       // Then parse it
+                                       var obj = jQuery.parseJSON(ajax_content.responseText.replace('%20', ' '));
+
+                                       // Is 'reply_content' set?
+                                       if (obj.reply_content == undefined) {
+                                               // This shall not happen
+                                               throw new 'obj.reply_content not returned from ajax.php, please fix your scripts. (3)';
+                                       } // END - if
+
+                                       // Set it
+                                       setAjaxReply(obj.reply_content, false);
+                                       return false;
+                               } else {
+                                       // This shall not happen
+                                       throw new 'ajax_content.reply_content not returned from ajax.php, please fix your scripts. (4)';
+                               }
                        } else if (ajax_content.reply_content == null) {
                                // This shall not happen, too
-                               throw new 'ajax_content.reply_content=null from ajax.php, please fix your scripts.';
+                               throw new 'ajax_content.reply_content=null from ajax.php, please fix your scripts. (5)';
                        }
 
                        // Set AJAX reply
@@ -230,6 +266,12 @@ function sendAjaxRequest (level, doValue, extra, isJson) {
 
 // Enables footer navigation buttons
 function enableFooterNavigation (prefix, tabId) {
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
        // Reset both footer navigation first
        resetFooterNavigation();
 
@@ -248,6 +290,12 @@ function enableFooterNavigation (prefix, tabId) {
 
 // Requests an AJAX content
 function requestAjaxContent (prefix, htmlId, tabId, footerNavigation) {
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
        // Check if this request is disabled
        if ($('#' + prefix + '_' + tabId).hasClass('tab_disabled')) {
                // Clicked on a disabled tabId so blur it
@@ -272,7 +320,7 @@ function requestAjaxContent (prefix, htmlId, tabId, footerNavigation) {
                // Fade the old content out
                $('#' + htmlId).fadeOut('fast', function() {
                        // Send AJAX request
-                       if (sendAjaxRequest(prefix, 'request_content', '&tab=' + tabId, true) == true) {
+                       if (sendAjaxRequest(prefix, 'request_content', '&tab=' + tabId, false) == true) {
                                // Add the HTML content
                                $('#' + htmlId).html(getAjaxContent());
 
@@ -314,7 +362,7 @@ function displayTestWindow (prefix, element) {
        });
 
        // Request it from the AJAX backend
-       if (sendAjaxRequest(prefix, 'test', '') == true) {
+       if (sendAjaxRequest(prefix, 'test', '', false) == true) {
                // Transfer the returned content to the prefix_warning_content id
                $('#' + prefix + '_warning_content').html(getAjaxContent());
 
@@ -323,9 +371,6 @@ function displayTestWindow (prefix, element) {
                        // Enable element
                        enableElement(element);
                });
-
-               // Mark 'warning' as displayed
-               warningDisplayed = true;
        } else {
                // Display error message
                displayErrorWindow(prefix, getAjaxContent());
@@ -341,8 +386,11 @@ function displayChangedWarningWindow (prefix, button) {
        // Fade error out for eye-candy, if open
        closeErrorWindow(prefix);
 
+       // Fade it out for eye-candy
+       closeProcessWindow(prefix);
+
        // Abort here if warningDisplayed is still true
-       if (warningDisplayed == true) {
+       if (isElementVisible(prefix, 'warning')) {
                // Make sure this doesn't happen
                return;
        } // END - if
@@ -354,8 +402,7 @@ function displayChangedWarningWindow (prefix, button) {
 
                // Fade the warning in
                $('#' + prefix + '_warning').fadeIn('slow', function() {
-                       // Mark warning as displayed
-                       warningDisplayed = true;
+                       // Do nothing for now
                });
        } else {
                // Display error message
@@ -372,8 +419,11 @@ function displayErrorWindow (prefix, ajax_content) {
        // Fade it out for eye-candy
        closeErrorWindow(prefix);
 
+       // Fade it out for eye-candy
+       closeProcessWindow(prefix);
+
        // Abort here if errorDisplayed is still true
-       if (errorDisplayed == true) {
+       if (isElementVisible(prefix, 'error')) {
                // Make sure this doesn't happen
                return;
        } // END - if
@@ -387,15 +437,52 @@ function displayErrorWindow (prefix, ajax_content) {
 
        // Fade the error in
        $('#' + prefix + '_error').fadeIn('slow', function() {
-               // Mark error as displayed
-               errorDisplayed = true;
+               // Do nothing for now
        });
 }
 
+// Displays the process window for given prefix and content
+function displayProcessWindow (prefix, ajax_content) {
+       // Fade out warning window, if open
+       //* DEBUG: */ alert('displayProcessWindow(): prefix=' + prefix + ' - calling closeWarningWindow()');
+       closeWarningWindow(prefix);
+
+       // Fade it out for eye-candy
+       closeErrorWindow(prefix);
+
+       // Fade it out for eye-candy
+       closeProcessWindow(prefix);
+
+       // Abort here if processDisplayed is still true
+       if (isElementVisible(prefix, 'process')) {
+               // Make sure this doesn't happen
+               return;
+       } // END - if
+
+       // Copy the response text to the process variable
+       if (ajax_content.reply_content != undefined) {
+               // Set HTML content
+               setProcessContent(prefix, ajax_content.reply_content);
+       } else {
+               setProcessContent(prefix, ajax_content);
+       }
+
+       // Fade the process in
+       $('#' + prefix + '_process').fadeIn('slow', function() {
+               // Do nothing for now
+       });
+}
+
+// Sets "process content"
+function setProcessContent (prefix, content) {
+       // Set HTML content
+       $('#' + prefix + '_process_content').html(content);
+}
+
 // Waits until the window has been closed
 function closeErrorLocked () {
        // Has all been loaded?
-       if (errorDisplayed == false) {
+       if (!isElementVisible(prefix, 'error')) {
                // Then release ready()
                $.holdReady(false);
        } else {
@@ -404,10 +491,22 @@ function closeErrorLocked () {
        }
 }
 
+// Waits until the window has been closed
+function closeProcessLocked () {
+       // Has all been loaded?
+       if (!isElementVisible(prefix, 'process')) {
+               // Then release ready()
+               $.holdReady(false);
+       } else {
+               // Recursive call again
+               window.setTimeout('closeProcessLocked()', 10);
+       }
+}
+
 // Closes an error window
 function closeErrorWindow (prefix, waitClose, resetCurrentTabId) {
        // Is the error displayed?
-       if (errorDisplayed == true) {
+       if (isElementVisible(prefix, 'error')) {
                // Shall we wait ("sync") until the animation has completed?
                if (waitClose == true) {
                        // Hold the ready status
@@ -420,9 +519,6 @@ function closeErrorWindow (prefix, waitClose, resetCurrentTabId) {
                        if (resetCurrentTabId == true) {
                                setCurrentTabId(defaultTabId);
                        } // END - if
-
-                       // Mark it as closed
-                       errorDisplayed = false;
                });
 
                // Shall this animation be "synchronized"?
@@ -433,10 +529,36 @@ function closeErrorWindow (prefix, waitClose, resetCurrentTabId) {
        } // END - if
 }
 
+// Closes an process window
+function closeProcessWindow (prefix, waitClose, resetCurrentTabId) {
+       // Is the process displayed?
+       if (isElementVisible(prefix, 'process')) {
+               // Shall we wait ("sync") until the animation has completed?
+               if (waitClose == true) {
+                       // Hold the ready status
+                       $.holdReady(true);
+               } // END - if
+
+               // Yes, then fade it out
+               $('#' + prefix + '_process').fadeOut('fast', function() {
+                       // Set current tab id to default
+                       if (resetCurrentTabId == true) {
+                               setCurrentTabId(defaultTabId);
+                       } // END - if
+               });
+
+               // Shall this animation be "synchronized"?
+               if (waitClose == true) {
+                       // Wait for the window has been closed
+                       closeProcessLocked();
+               } // END - if
+       } // END - if
+}
+
 // Waits until the window has been closed
 function closeWarningLocked () {
        // Has all been loaded?
-       if (warningDisplayed == false) {
+       if (!isElementVisible(prefix, 'warning')) {
                // Then release ready()
                $.holdReady(false);
        } else {
@@ -449,7 +571,7 @@ function closeWarningLocked () {
 function closeWarningWindow (prefix, waitClose, resetCurrentTabId) {
        //* DEBUG: */ alert('prefix=' + prefix + ',waitClose=' + waitClose + ' - ENTERED!');
        // Is the warning displayed?
-       if (warningDisplayed == true) {
+       if (isElementVisible(prefix, 'warning')) {
                // Shall we wait ("sync") until the animation has completed?
                //* DEBUG: */ alert('prefix=' + prefix + ',waitClose=' + waitClose + ',warningDisplayed=true');
                if (waitClose == true) {
@@ -460,14 +582,9 @@ function closeWarningWindow (prefix, waitClose, resetCurrentTabId) {
                // Yes, then fade it out
                $('#' + prefix + '_warning').fadeOut('fast', function() {
                        // Set current tab id to default
-                       //* DEBUG: */ alert('closeWarningWindow(): prefix=' + prefix + ',waitClose=' + waitClose + ',defaultTab=' + defaultTabId + ' - Calling setCurrentTabId()');
                        if (resetCurrentTabId == true) {
                                setCurrentTabId(defaultTabId);
                        } // END - if
-
-                       // Mark it as closed
-                       warningDisplayed = false;
-                       //* DEBUG: */ alert('closeWarningWindow(): waitClose=' + waitClose + ',resetCurrentTabId=' + resetCurrentTabId + ',warningDisplayed=false');
                });
 
                // Shall this animation be "synchronized"?
@@ -491,15 +608,27 @@ function doFooterPage (prefix, htmlId, button) {
                return;
        } // END - if
 
-       // Do we have a 'next' entry?
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
+       // Is there a 'next' entry?
        //* DEBUG: */ alert('doFooterPage(): button=' + button + ',currentTabId=' + currentTabId + ',nextPage[currentTabId]=' + nextPage[currentTabId]);
        if ((button == 'next') && (nextPage[currentTabId] != null)) {
                // Then call the AJAX requester
-               requestAjaxContent(prefix, htmlId, nextPage[currentTabId]);
+               var page = nextPage[currentTabId];
        } else if ((button == 'previous') && (previousPage[currentTabId] != null)) {
                // Then call the AJAX requester
-               requestAjaxContent(prefix, htmlId, previousPage[currentTabId]);
+               var page = previousPage[currentTabId];
        }
+
+       // Request AJAX content
+       requestAjaxContent(prefix, htmlId, page);
+
+       // Change the footer navigation
+       enableFooterNavigation(prefix, page);
 }
 
 // Allows to save made changes (this will be called if the onchange event has been triggered)
@@ -589,10 +718,16 @@ function processAjaxResponseContent (prefix, ajax_content) {
 
 // Saves changes by sending the data to the AJAX backend script
 function saveChanges (prefix) {
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
        // Mark all elements as unchanged
        markAllElementsAsUnchanged();
 
-       // Do we have changed elements
+       // Is there changed elements
        if (changedElements.length == 0) {
                // This should not happen
                displayErrorWindow(prefix, '<div class="ajax_error_message">saveChanges() called with no changed elements.</div>');
@@ -624,7 +759,7 @@ function saveChanges (prefix) {
                        // Reset form
                        resetMailerAjaxForm();
                } else {
-                       // Do we have 'failed_fields' set?
+                       // Is there 'failed_fields' set?
                        if ((ajax_content.failed_fields != undefined) && (ajax_content.message != undefined)) {
                                // Mark all fields as 'failed'
                                markFormFieldsFailed(ajax_content.failed_fields);
@@ -681,6 +816,12 @@ function doSaveChangesPage (prefix, htmlId, page) {
 
 // Saves changed settings and continues with given tab
 function doSaveChangesContinue (prefix, htmlId, tab) {
+       // Is process working?
+       if (isElementVisible(prefix, 'process')) {
+               // Then exit silently
+               return;
+       } // END - if
+
        // Save the changes
        saveChanges(prefix);