Source: modules/prompts.js

/*******************************************************************************
 * Handling of Prompts
 ******************************************************************************/

/**
 * Handler for the prompt dialog
 * @param {string}   data           URL to open
 * @param {function} finishFunction function to execute when done
 */
/* global POPUP_FUNCTION_TYPES */
/* global PROMPT */
/* global TIMEOUTBEFOREERROR */
/* global applyFormFieldFunction */
/* global debug */
/* global ensureParameterPromptKey */
/* global errorPacketHandler */
/* global getId */
/* global getTranslation */
/* global popupHandler */

/* jshint -W098 */
var promptHandling = function (data, finishFunction) {
/* jshint +W098 */

    var self = this;
    this.finishFunction = finishFunction;

    this.popupFunctions = {};
    this.popupFunctions[POPUP_FUNCTION_TYPES.FRAME_CREATED] = function( popup ) {
        window.addEvent("message", waitForPromptHello);
        self.timeoutRegistration = window.setTimeout( self.timeoutHandler, TIMEOUTBEFOREERROR); // 10s
    };

    this.popupFunctions[POPUP_FUNCTION_TYPES.POPUP_CLOSED] = function( popup ) {
        window.removeEvent("message", waitForPromptHello);
    };

    // Creates the new prompt array for displaying the popup dialog
    this.popup = new popupHandler(data, this.popupFunctions, "__promptHandling");

    this.timeoutRegistration = null;
    this.timeoutHandler = function() {
        // Timeout triggered ...
        window.clearTimeout( self.timeoutRegistration );
        new errorPacketHandler({
                errorHeader: getTranslation("prompts.promptDialogNotLoaded.subject"),
                errorMessage: getTranslation("prompts.promptDialogNotLoaded.body")
        });
    };

    // Register the Message Event for PostMessage receival
    var waitForPromptHello = function (event) {
        if (event.data != 'promptdialog_hello'+self.popup.popUpId) {
            return;
        }

        // Popup must have been loaded by now
        window.clearTimeout( self.timeoutRegistration );

        var coverFrame = getId(self.popup.popUpId + 'CoverFrame');
        if (coverFrame) {
            coverFrame.parentNode.removeChild(coverFrame);
            coverFrame = null;
        }

        // register events for values here
        window.addEvent("message", waitForPromptValues);
        window.removeEvent("message", waitForPromptHello);

        var iframe = getId(self.popup.popUpId);
        if ( iframe ) {
            iframe.contentWindow.postMessage("promptdialog_welcome"+self.popup.popUpId, "*");
            debug("Prompt Registration done.");
        } else {
            debug("Prompt Registration failed.");
        }
    };

    // Register the Message Event for PostMessage receival
    var waitForPromptValues = function (event) {
        if (event.data === undefined ||  event.data.indexOf('promptdialog_prompts'+self.popup.popUpId+'_') != 0) {
            return;
        }

        var promptValuesAsJson = event.data.substring(('promptdialog_prompts'+self.popup.popUpId+'_').length);
        var promptMap = JSON.parse(promptValuesAsJson);

        var topWindow = window.htmlviewer || window;
        topWindow.PROMPT = [];

        var promptNames = Object.keys(promptMap);
        for (var p=0; p<promptNames.length; p++) {
            var promptName = promptNames[p];
            var promptValue = promptMap[promptName];
            topWindow.PROMPT.push({
                name: encodeURIComponent('prompt'+promptName),
                value: encodeURIComponent(promptValue)
            });
        }

        topWindow.HASPROMPTS = topWindow.PROMPT.length > 0 && !topWindow.HASNOPROMPTONREFRESH;
        self.popup.hide();

        var menubar = new topWindow.MenubarActions();

        window.removeEvent("message", waitForPromptValues);
        menubar.refreshReport(null, self.finishFunction);
    };
};

/**
 * Adds the content of PROMPT to an object
 * @param {object}  object        			Object to put the PROMPTs into
 * @param {boolean} [keepPrevious=false]	if previously set parameters should be kept
 */
/* jshint -W098 */
var addPromptToObject = function (object, keepPrevious) {
/* jshint +W098 */

    applyFormFieldFunction(getPromptsField(), function (elem, i) {
        // Prompt names for subrequests might contain a #-sign which will cause trouble.
		// If the URL already contains a given parameter, keep it 
		var previous = object[ensureParameterPromptKey( elem.name )];
        object[ensureParameterPromptKey( elem.name )] = !keepPrevious ? elem.value : ( previous || elem.value );
    });
};

/**
 * Return the save prompt fields
 */
var getPromptsField = function () {
    return (window.htmlviewer ? window.htmlviewer.PROMPT : PROMPT) || [];
};

/**
 * Return the save prompt fields
 */
/* jshint -W098 */
var setPromptsField = function ( promptField ) {
/* jshint +W098 */
    if ( window.htmlviewer ) {
        window.htmlviewer.PROMPT = promptField;
    } else {
        window.PROMPT = promptField;
    }
};

/**
 * add PROMPT Values to ajax request
 * Will always be encoded
 * 
 * @param {object} ajax The AJAX Request
 */
/* jshint -W098 */
var addPromptToAjax = function (ajax) {
/* jshint +W098 */

    applyFormFieldFunction(getPromptsField(), function (elem, i) {
        // Prompt names for subrequests might contain a #-sign which will cause trouble.
        ajax.setVar( ensureParameterPromptKey( elem.name ), elem.value);
    });
};