/* License: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html) Copyright (C) 2005 tagnetic.org. Please do not remove this copyright/license comment. */ /** * Callbacks struture: * { * global: string, * state1: string, * state2: string, * ... * } * * Where the string value can be a URL. To execute javascript, construct a string * that starts with 'javascript:'. Note that this javascript or URL will be run * in the context of the window that is passed to the Flow.end function. * * If it is a javascript action, and you need to navigate to a URL, be sure to * call Flow.modUrlForParentFlow() to bind the URL to the right Flow slot. * * TODO: * - Provide a way to clean up child slots if the app is not supporting back/forward. */ //****************************************************************** //****************************************************************** //****************************************************************** Flow = function() {}; //****************************************************************** Flow.start = function(url, callbacks, startData, currentWindow) { var parentId = Flow._getFlowId(currentWindow); var flowId = Flow._newSlot = function(parentId, callbacks, startData); return Flow._modUrl(url, flowId); } //****************************************************************** Flow.next = function(url, currentWindow, targetWindow) { if (!currentWindow) currentWindow = window; if (!targetWindow) { if (currentWindow) targetWindow = currentWindow; else targeWindow = window; } var flowId = Flow._getFlowId(currentWindow); targetWindow.location = Flow._modUrl(url, flowId); } //****************************************************************** Flow.nextStart(url, callbacks, startData, currentWindow, targetWindow) { Flow.next(Flow.start(url, callbacks, startData, currentWindow), currentWindow, targetWindow); } //****************************************************************** Flow.end = function(state, endData, currentWindow) { if (!currentWindow) currentWindow = window; var slot = Flow._slots[Flow._getFlowId(currentWindow)]; var parentSlot = Flow._slots[Flow._getFlowId(slot.parentId)]; parentSlot.endData = endData; var callbacks = slot.callbacks; if (callbacks) { var action = slot.callbacks[state]; if (!action) action = slot.callbacks['global']; if (action) { if (action.indexOf('javascript:') == 0) currentWindow.eval(action); else currentWindow.location = Flow._modUrl(action, slot.parentId); } } } //****************************************************************** Flow.modUrlForParentFlow = function(url, targetWindow) { var flowId = Flow._getFlowId(targetWindow); if (flowId) { var slot = Flow._slots[flowId]; if (slot && slot.parentId) url = Flow._modUrl(url, slot.parentId); } return url; } //****************************************************************** Flow.data = function(targetWindow) { return Flow._slots[Flow._getFlowId(targetWindow)].data; } //****************************************************************** //****************************************************************** //****************************************************************** //Private properties/methods. Flow._slots = new Object(); Flow._topId = Flow._newSlot(null, null); Flow._flowIdName = '_fId'; //****************************************************************** Flow._newSlot = function(parentId, callbacks, startData) { var flowId = 's' + (new Date()).getTime(); Flow._slots[flowId] = { 'data': new Object(), 'callbacks': callbacks 'parentId': parentId }; Flow._slots[flowId].data.startData = startData; return flowId; } //****************************************************************** Flow._modUrl = function(url, flowId) { return Ctrl.setUrlParam(url, Flow._flowIdName, flowId); } //****************************************************************** Flow._getFlowId = function(targetWindow) { if (!targetWindow) targetWindow = window; return Ctrl.getUrlParam(targetWindow.location, Flow._flowIdName); }