X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Fajax-common.js;h=dd2f5c8f580fa9e3f91e836b7aae831164b9c7fa;hb=f85404739552dd9519025e870ca09d2a435f94c2;hp=3686444aed6a2cf21094a1f843d069078af2eaed;hpb=1d22c70e65e858422ee0d17a7612f4b5c0757a42;p=mailer.git diff --git a/js/ajax-common.js b/js/ajax-common.js index 3686444aed..dd2f5c8f58 100644 --- a/js/ajax-common.js +++ b/js/ajax-common.js @@ -7,7 +7,7 @@ * $Author:: $ * -------------------------------------------------------------------- * Copyright (c) 2003 - 2009 by Roland Haeder - * Copyright (c) 2009 - 2012 by Mailer Developer Team + * Copyright (c) 2009 - 2013 by Mailer Developer Team * For more information visit: http://mxchange.org * * This program is free software; you can redistribute it and/or modify @@ -37,6 +37,9 @@ var saveChangesId = null; var lastErrorMessage = null; var saveChangesPending = false; +// Counter for success steps +var counterSuccess = 0; + // Add all footer navigation elements footerElements[0] = 'next'; footerElements[1] = 'previous'; @@ -50,14 +53,17 @@ function setCurrentTabId (tabId) { // Checks whether a given element is visible by checking 'display: none' function isElementVisible (prefix, element) { + //* DEBUG: */ alert('isElementVisible(' + prefix + ',' + element + ') Called!'); + // Get element var el = document.getElementById(prefix + '_' + element); + //* DEBUG: */ alert('isElementVisible(): el=' + el); // Is element set? - if ((el == null) || (el == undefined)) { - throw new '"' + prefix + '_' + element + '" does not exist.'; + if ((el === null) || (el == undefined)) { + throw new ('prefix=' + prefix + ',element=' + element + ' does not exist.'); } else if ((el.style.display == undefined) || (el.style.display == '')) { - throw new '"' + prefix + '_' + element + '" has no style.display element.'; + throw new ('prefix=' + prefix + ',element=' + element + ' has no style.display element.'); } // Default is visible @@ -154,7 +160,7 @@ function getAjaxContent () { // Is it defined? if (data['ajax_content'] == undefined) { // Not set - throw new 'ajax_content requested but not set.'; + throw new('ajax_content requested but not set.'); } // END - if // Return it @@ -189,7 +195,7 @@ function setAjaxSuccess (success) { function setAjaxReply (reply, isJson) { // Is it JSON URL-encoded content? //* DEBUG: */ alert('setAjaxReply(): reply=' + reply + ',isJson=' + isJson); - if ((isJson != undefined) && (isJson == true)) { + if ((isJson != undefined) && (isJson === true)) { // Decode URL-encoding (for some reason it must be here ...) var localReply = decodeUrlEncoding(reply); @@ -226,10 +232,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. (1)'; - } else if (ajax_content.reply_content == null) { + 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. (2)'; + throw new('ajax_content.reply_content=null from ajax.php, please fix your scripts. (2)'); } // Set AJAX reply @@ -237,6 +243,13 @@ function sendAjaxRequest (level, doValue, extra, isJson) { // Mark it as success setAjaxSuccess(true); + + // Was there a redirect? + if ((ajax_content.redirect != undefined) && (ajax_content.redirect != null)) { + // Redirect URL detected + // @TODO Need this be secured? + document.location.href = ajax_content.redirect; + } // END - if }, // Called in case of an error (e.g. HTTP response status not '200 OK') @@ -251,7 +264,7 @@ function sendAjaxRequest (level, doValue, extra, isJson) { // 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)'; + throw new('obj.reply_content not returned from ajax.php, please fix your scripts. (3)'); } // END - if // Set it @@ -259,15 +272,22 @@ function sendAjaxRequest (level, doValue, extra, isJson) { return false; } else { // This shall not happen - throw new 'ajax_content.reply_content not returned from ajax.php, please fix your scripts. (4)'; + throw new('ajax_content.reply_content not returned from ajax.php, please fix your scripts. (4)'); } - } else if (ajax_content.reply_content == null) { + } 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. (5)'; + throw new('ajax_content.reply_content=null from ajax.php, please fix your scripts. (5)'); } // Set AJAX reply setAjaxReply(ajax_content.reply_content, isJson); + + // Was there a redirect? + if ((ajax_content.redirect != undefined) && (ajax_content.redirect != null)) { + // Redirect URL detected + // @TODO Need this be secured? + document.location.href = ajax_content.redirect; + } // END - if } }); @@ -288,7 +308,7 @@ function enableFooterNavigation (prefix, tabId) { resetFooterNavigation(); // Do the AJAX request (JSON as content is enabled) - if (sendAjaxRequest(prefix, 'footer_navigation', '&tab=' + tabId, true) == true) { + if (sendAjaxRequest(prefix, 'footer_navigation', '&tab=' + tabId, true) === true) { // Parse the content $.each(getAjaxContent(), function (i, value) { // Enable current element @@ -313,7 +333,7 @@ function requestAjaxContent (prefix, htmlId, tabId, footerNavigation) { // Clicked on a disabled tabId so blur it //* DEBUG: */ alert('requestAjaxContent(): prefix=' + prefix + ',htmlId=' + htmlId + ',tabId=' + tabId + ' - DISABLED!'); return; - } else if (formChanged == true) { + } else if (formChanged === true) { // Has changed form , so output message to browser //* DEBUG: */ alert('requestAjaxContent(): prefix=' + prefix + ',htmlId=' + htmlId + ',tabId=' + tabId + ' - FORM CHANGED!'); displayChangedWarningWindow(prefix, tabId); @@ -332,7 +352,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, false) == true) { + if (sendAjaxRequest(prefix, 'request_content', '&tab=' + tabId, false) === true) { // Add the HTML content $('#' + htmlId).html(getAjaxContent()); @@ -342,7 +362,7 @@ function requestAjaxContent (prefix, htmlId, tabId, footerNavigation) { markTabNavigation(prefix, tabId); // Is the footer navigation enabled? - if (footerNavigation == true) { + if (footerNavigation === true) { // Change footer navigation as well enableFooterNavigation(prefix, tabId); } // END - if @@ -374,9 +394,9 @@ function displayTestWindow (prefix, element) { }); // Request it from the AJAX backend - if (sendAjaxRequest(prefix, 'test', '', false) == true) { + if (sendAjaxRequest(prefix, 'test', '', false) === true) { // Transfer the returned content to the prefix_warning_content id - $('#' + prefix + '_warning_content').html(getAjaxContent()); + setWarningContent(prefix, getAjaxContent()); // Fade the warning in $('#' + prefix + '_warning').fadeIn('slow', function() { @@ -389,33 +409,36 @@ function displayTestWindow (prefix, element) { } } +// Fades slowly in given window +function fadeInWindow (prefix, id) { + // Do the fade-in + $('#' + prefix + '_' + id).fadeIn('slow', function() { + // Do nothing for now + }); +} + // Displays a warning window above the form to warn about changed&unsafed fields function displayChangedWarningWindow (prefix, button) { - // Fade out warning window, if open //* DEBUG: */ alert('displayChangedWarningWindow(): prefix=' + prefix + ',button=' + button + ' - calling closeWarningWindow()'); - closeWarningWindow(prefix, true, false); - - // Fade error out for eye-candy, if open - closeErrorWindow(prefix, true, false); - - // Fade it out for eye-candy - closeProgressWindow(prefix, true, false); + // Fade all windows out + closeAllWindows(prefix); // Abort here if warningDisplayed is still true + //* DEBUG: */ alert('displayChangedWarningWindow(): Calling isElementVisible(' + prefix + ', warning)'); if (isElementVisible(prefix, 'warning')) { // Make sure this doesn't happen + //* DEBUG: */ alert('displayChangedWarningWindow(): isElementVisible(' + prefix + ', warning)=true'); return; } // END - if // Request it from the AJAX backend - if (sendAjaxRequest(prefix, 'change_warning', '&button=' + button + '&elements=' + changedElements.join(':')) == true) { + //* DEBUG: */ alert('displayChangedWarningWindow(): Calling sendAjaxRequest(' + prefix + ', button=' + button + ',changedElements()=' + changedElements.join(':') + ')'); + if (sendAjaxRequest(prefix, 'change_warning', '&button=' + button + '&elements=' + changedElements.join(':')) === true) { // Transfer the returned content to the prefix_warning_content id - $('#' + prefix + '_warning_content').html(getAjaxContent()); + setWarningContent(prefix, getAjaxContent()); // Fade the warning in - $('#' + prefix + '_warning').fadeIn('slow', function() { - // Do nothing for now - }); + fadeInWindow(prefix, 'warning'); } else { // Display error message displayErrorWindow(prefix, getAjaxContent()); @@ -424,15 +447,9 @@ function displayChangedWarningWindow (prefix, button) { // Displays the error window for given prefix and content function displayErrorWindow (prefix, ajax_content) { - // Fade out warning window, if open - //* DEBUG: */ alert('displayErrorWindow(): prefix=' + prefix + ',ajax_content=' + ajax_content.reply_content + ' - calling closeWarningWindow()'); - closeWarningWindow(prefix, true, false); - - // Fade it out for eye-candy - closeErrorWindow(prefix, true, false); - - // Fade it out for eye-candy - closeProgressWindow(prefix, true, false); + //* DEBUG: */ alert('displayErrorWindow(): prefix=' + prefix + ',ajax_content=' + ajax_content + ' - calling closeWarningWindow()'); + // Fade all windows out + closeAllWindows(prefix); // Abort here if errorDisplayed is still true if (isElementVisible(prefix, 'error')) { @@ -442,28 +459,20 @@ function displayErrorWindow (prefix, ajax_content) { // Copy the response text to the error variable if (ajax_content.reply_content != undefined) { - $('#' + prefix + '_error_content').html(ajax_content.reply_content); + setErrorContent(prefix, ajax_content.reply_content); } else { - $('#' + prefix + '_error_content').html(ajax_content); + setErrorContent(prefix, ajax_content); } // Fade the error in - $('#' + prefix + '_error').fadeIn('slow', function() { - // Do nothing for now - }); + fadeInWindow(prefix, 'error'); } // Displays the progress window for given prefix and content function displayProgressWindow (prefix, ajax_content) { - // Fade out warning window, if open //* DEBUG: */ alert('displayProgressWindow(): prefix=' + prefix + ' - calling closeWarningWindow()'); - closeWarningWindow(prefix, true, false); - - // Fade it out for eye-candy - closeErrorWindow(prefix, true, false); - - // Fade it out for eye-candy - closeProgressWindow(prefix, true, false); + // Fade all windows out + closeAllWindows(prefix); // Abort here if progressDisplayed is still true if (isElementVisible(prefix, 'progress')) { @@ -480,9 +489,43 @@ function displayProgressWindow (prefix, ajax_content) { } // Fade the progress in - $('#' + prefix + '_progress').fadeIn('slow', function() { - // Do nothing for now - }); + fadeInWindow(prefix, 'progress'); +} + +// Displays the success window for given prefix and content +function displaySuccessWindow (prefix, ajax_content) { + //* DEBUG: */ alert('displaySuccessWindow(): prefix=' + prefix + ' - calling closeWarningWindow()'); + // Fade all windows out + closeAllWindows(prefix); + + // Abort here if successDisplayed is still true + if (isElementVisible(prefix, 'success')) { + // Make sure this doesn't happen + return; + } // END - if + + // Copy the response text to the success variable + if (ajax_content.reply_content != undefined) { + // Set HTML content + setSuccessContent(prefix, ajax_content.reply_content); + } else { + setSuccessContent(prefix, ajax_content); + } + + // Fade the success in + fadeInWindow(prefix, 'success'); +} + +// Sets "warning content" +function setWarningContent (prefix, content) { + // Set HTML content + $('#' + prefix + '_warning_content').html(content); +} + +// Sets "error content" +function setErrorContent (prefix, content) { + // Set HTML content + $('#' + prefix + '_error_content').html(content); } // Sets "progress content" @@ -491,27 +534,34 @@ function setProgressContent (prefix, content) { $('#' + prefix + '_progress_content').html(content); } -// Waits until the window has been closed -function closeErrorLocked (prefix) { - // Has all been loaded? - if (!isElementVisible(prefix, 'error')) { - // Then release ready() - $.holdReady(false); - } else { - // Recursive call again - window.setTimeout('closeErrorLocked(' + prefix + ')', 10); - } +// Sets "success content" +function setSuccessContent (prefix, content) { + // Set HTML content + $('#' + prefix + '_success_content').html(content); +} + +// Close all windows +function closeAllWindows (prefix) { + //* DEBUG: */ alert('closeAllWindows(): Calling closeWarningWindow(' + prefix + ', true, false)'); + closeWarningWindow(prefix, true, false); + //* DEBUG: */ alert('closeAllWindows(): Calling closeErrorWindow(' + prefix + ', true, false)'); + closeErrorWindow(prefix, true, false); + //* DEBUG: */ alert('closeAllWindows(): Calling closeProgressWindow(' + prefix + ', true, false)'); + closeProgressWindow(prefix, true, false); + //* DEBUG: */ alert('closeAllWindows(): Calling closeSuccessWindow(' + prefix + ', true, false)'); + closeSuccessWindow(prefix, true, false); + //* DEBUG: */ alert('closeAllWindows(): EXIT!'); } // Waits until the window has been closed -function closeProgressLocked (prefix) { +function closeErrorLocked (prefix) { // Has all been loaded? - if (!isElementVisible(prefix, 'progress')) { + if (!isElementVisible(prefix, 'error')) { // Then release ready() $.holdReady(false); } else { // Recursive call again - window.setTimeout('closeProgressLocked(' + prefix + ')', 10); + window.setTimeout('closeErrorLocked("' + prefix + '")', 10); } } @@ -520,7 +570,7 @@ function closeErrorWindow (prefix, waitClose, resetCurrentTabId) { // Is the error displayed? if (isElementVisible(prefix, 'error')) { // Shall we wait ("sync") until the animation has completed? - if (waitClose == true) { + if (waitClose === true) { // Hold the ready status $.holdReady(true); } // END - if @@ -528,25 +578,37 @@ function closeErrorWindow (prefix, waitClose, resetCurrentTabId) { // Yes, then fade it out $('#' + prefix + '_error').fadeOut('fast', function() { // Set current tab id to default - if (resetCurrentTabId == true) { + if (resetCurrentTabId === true) { setCurrentTabId(defaultTabId); } // END - if }); // Shall this animation be "synchronized"? - if (waitClose == true) { + if (waitClose === true) { // Wait for the window has been closed closeErrorLocked(prefix); } // END - if } // END - if } +// Waits until the window has been closed +function closeProgressLocked (prefix) { + // Has all been loaded? + if (!isElementVisible(prefix, 'progress')) { + // Then release ready() + $.holdReady(false); + } else { + // Recursive call again + window.setTimeout('closeProgressLocked("' + prefix + '")', 10); + } +} + // Closes an progress window function closeProgressWindow (prefix, waitClose, resetCurrentTabId) { // Is the progress displayed? if (isElementVisible(prefix, 'progress')) { // Shall we wait ("sync") until the animation has completed? - if (waitClose == true) { + if (waitClose === true) { // Hold the ready status $.holdReady(true); } // END - if @@ -554,19 +616,59 @@ function closeProgressWindow (prefix, waitClose, resetCurrentTabId) { // Yes, then fade it out $('#' + prefix + '_progress').fadeOut('fast', function() { // Set current tab id to default - if (resetCurrentTabId == true) { + if (resetCurrentTabId === true) { setCurrentTabId(defaultTabId); } // END - if }); // Shall this animation be "synchronized"? - if (waitClose == true) { + if (waitClose === true) { // Wait for the window has been closed closeProgressLocked(prefix); } // END - if } // END - if } +// Waits until the window has been closed +function closeSuccessLocked (prefix) { + // Has all been loaded? + if (!isElementVisible(prefix, 'success')) { + // Then release ready() + $.holdReady(false); + } else { + // Recursive call again + window.setTimeout('closeSuccessLocked("' + prefix + '")', 10); + } +} + +// Closes an success window +function closeSuccessWindow (prefix, waitClose, resetCurrentTabId) { + // Is the success displayed? + //* DEBUG: */ alert('closeSuccessWindow(): prefix=' + prefix + ',waitClose=' + waitClose + ',resetCurrentTabId=' + resetCurrentTabId + ' - ENTERED!'); + if (isElementVisible(prefix, 'success')) { + // 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 + '_success').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 + closeSuccessLocked(prefix); + } // END - if + } // END - if + //* DEBUG: */ alert('closeSuccessWindow(): prefix=' + prefix + ',waitClose=' + waitClose + ',resetCurrentTabId=' + resetCurrentTabId + ' - EXIT!'); +} + // Waits until the window has been closed function closeWarningLocked (prefix) { // Has all been loaded? @@ -575,7 +677,7 @@ function closeWarningLocked (prefix) { $.holdReady(false); } else { // Recursive call again - window.setTimeout('closeWarningLocked(' + prefix + ')', 10); + window.setTimeout('closeWarningLocked("' + prefix + '")', 10); } } @@ -586,7 +688,7 @@ function closeWarningWindow (prefix, waitClose, resetCurrentTabId) { if (isElementVisible(prefix, 'warning')) { // Shall we wait ("sync") until the animation has completed? //* DEBUG: */ alert('prefix=' + prefix + ',waitClose=' + waitClose + ',warningDisplayed=true'); - if (waitClose == true) { + if (waitClose === true) { // Hold the ready status $.holdReady(true); } // END - if @@ -594,13 +696,13 @@ function closeWarningWindow (prefix, waitClose, resetCurrentTabId) { // Yes, then fade it out $('#' + prefix + '_warning').fadeOut('fast', function() { // Set current tab id to default - if (resetCurrentTabId == true) { + if (resetCurrentTabId === true) { setCurrentTabId(defaultTabId); } // END - if }); // Shall this animation be "synchronized"? - if (waitClose == true) { + if (waitClose === true) { // Wait for the window has been closed //* DEBUG: */ alert('prefix=' + prefix + ',waitClose=' + waitClose + ' - LOCKED!'); closeWarningLocked(prefix); @@ -612,7 +714,7 @@ function closeWarningWindow (prefix, waitClose, resetCurrentTabId) { function doFooterPage (prefix, htmlId, button) { //* DEBUG: */ alert('doFooterPage(): prefix=' + prefix + ',htmlId=' + htmlId + ',button=' + button + ' - ENTERED!'); // Has something being changed? - if (formChanged == true) { + if (formChanged === true) { // Output message to browser displayChangedWarningWindow(prefix, button); @@ -628,10 +730,10 @@ function doFooterPage (prefix, htmlId, button) { // Is there a 'next' entry? //* DEBUG: */ alert('doFooterPage(): button=' + button + ',currentTabId=' + currentTabId + ',nextPage[currentTabId]=' + nextPage[currentTabId]); - if ((button == 'next') && (nextPage[currentTabId] != null)) { + if ((button == 'next') && (nextPage[currentTabId] !== null)) { // Then call the AJAX requester var page = nextPage[currentTabId]; - } else if ((button == 'previous') && (previousPage[currentTabId] != null)) { + } else if ((button == 'previous') && (previousPage[currentTabId] !== null)) { // Then call the AJAX requester var page = previousPage[currentTabId]; } @@ -743,7 +845,7 @@ function saveChanges (prefix) { if (changedElements.length == 0) { // This should not happen displayErrorWindow(prefix, '
saveChanges() called with no changed elements.
'); - } else if (saveChangesId == null) { + } else if (saveChangesId === null) { // saveChangesId is not det displayErrorWindow(prefix, '
saveChangesId is not set. Please add saveChanges = \'foo_bar\'; to your code.
'); } @@ -759,12 +861,12 @@ function saveChanges (prefix) { * Send the request to the AJAX backend, it doesn't matter from which page * this was requested. */ - if (sendAjaxRequest(prefix, 'save_changes', '&tab=' + currentTabId + '&' + serializedData, true) == true) { + if (sendAjaxRequest(prefix, 'save_changes', '&tab=' + currentTabId + '&' + serializedData, true) === true) { // Get the content var ajax_content = getAjaxContent(); // Progress the returned content - if (progressAjaxResponseContent(prefix, ajax_content) == true) { + if (progressAjaxResponseContent(prefix, ajax_content) === true) { // Mark all elements as unchanged markAllElementsAsUnchanged(); @@ -804,7 +906,7 @@ function saveChanges (prefix) { // Waiting for resources being loaded function saveChangesLocked () { // Has all been loaded? - if (saveChangesPending == false) { + if (saveChangesPending === false) { // Then release ready() $.holdReady(false); } else { @@ -838,9 +940,78 @@ function doSaveChangesContinue (prefix, htmlId, tab) { saveChanges(prefix); // Close the window - //* DEBUG: */ alert('doSaveChangesPage(): prefix=' + prefix + ',htmlId=' + htmlId + ',tab=' + tab + ' - calling closeWarningWindow()'); + //* DEBUG: */ alert('doSaveChangesContinue(): prefix=' + prefix + ',htmlId=' + htmlId + ',tab=' + tab + ' - calling closeWarningWindow()'); closeWarningWindow(prefix, true, false); // Load requested content requestAjaxContent(prefix, htmlId, tab); } + +// Registers common things (close button, drap&drop) +function registerCommons (prefix) { + //----------------------------------------- + // Close buttons + //----------------------------------------- + $('#' + prefix + '_error_close').click(function () { + // Close the window + closeErrorWindow(prefix); + }); + + $('#' + prefix + '_warning_close').click(function () { + // Close the window + closeWarningWindow(prefix); + }); + + $('#' + prefix + '_success_close').click(function () { + // Close the window + closeSuccessWindow(prefix); + }); + + //----------------------------------------- + // Drag'N'Drop + //----------------------------------------- + $('#' + prefix + '_progress').draggable({ + opacity: 0.85 + }); + + $('#' + prefix + '_warning').draggable({ + opacity: 0.85 + }); + + $('#' + prefix + '_success').draggable({ + opacity: 0.85 + }); + + $('#' + prefix + '_error').draggable({ + opacity: 0.85 + }); +} + +// Update progress bar +function updateProgressBar (maxValue) { + // Increment counter + counterSuccess++; + + // Do only update <= 100% values + if (counterSuccess <= maxValue) { + // Update progress bar + $('#progressbar').progressbar({ + value: (counterSuccess / maxValue * 100) + }); + } // END - if +} + +// Updates a given "status" field +function updateStatusField (id, cssClass, statusMessage) { + // Set message + $('#' + id).html(statusMessage); + + // Is a cssClass set? + if (cssClass != '') { + // Add it + $('#' + id).addClass(cssClass); + } else { + // Remove all classes + $('#' + id).removeClass(); + } +}