// setup namespace Zymonic.Transition = {}; // base transition // don't do anything here, makes it easy to establish inheritance Zymonic.Transition.Transition = function() {}; // init function Zymonic.Transition.Transition.prototype.init = function(args) { for (var key in args) { this[key] = args[key]; } if (this.ident != "") { Zymonic.add_to_lookup('Transition', this.ident, this); } // setup callbacks for when this is run this.before_run_callbacks = {}; this.run_callbacks = {}; // connect to parent process and setup handlers var parent_process = Zymonic.lookup_object('Process', this.process_ident); if (parent_process) { parent_process.registerTransition(this); } this.setupHandlers(); }; // function to set a handler for before a transition is run, handler will be passed transition and extras // if check passes then need to call transition.run(extras, { key: key, can_run: true }) Zymonic.Transition.Transition.prototype.addBeforeRunCallback = function(key, fn) { // overwrite any existing handler with that key this.before_run_callbacks[key] = fn; }; // function to set a handler for when a transition is run Zymonic.Transition.Transition.prototype.addRunCallback = function(key, fn) { // overwrite any existing handler with that key this.run_callbacks[key] = fn; }; // get the transition button Zymonic.Transition.Transition.prototype.getButton = function() { return $("#"+this.ident); }; // get the url to run the transition Zymonic.Transition.Transition.prototype.getURL = function() { if (!this.block_id) { Zymonic.log_error("No block_id found on Transition "+this.zname, true); return; } // Split base URL var url_split = split_baseurl(base_url); // get url params and add the ones we need // Split base URL var url_split = split_baseurl(base_url); var url_params = get_url_params([ "ZZwebservicemode=blockreload", "ZZwebserviceblockid="+this.block_id, "ZZno_display_attributes=false", ]); return url_split[0] + "?" + url_split[1] + url_params.join("&"); }; // function to run the transition Zymonic.Transition.Transition.prototype.run = function(extras, before_run_results) { if (!this.block_id) { Zymonic.log_error("No block_id found on Transition "+this.zname, true); return; } // if before run callbacks need to do those checks and wait for responses before running if ( Object.keys(this.before_run_callbacks).length > 0 ) { // add before run results if incoming, otherwise set up fresh if (before_run_results) { this.before_run_results[ before_run_results["key"] ] = before_run_results["can_run"]; if (!before_run_results["can_run"]) { Zymonic.log("Before run check "+before_run_results["key"]+" failed, not running Transition "+this.zname); return; } // check if all results are in and passed var checks_run = 0; var checks_passed = 0; for (var key in this.before_run_callbacks) { ++checks_run; if ( !this.before_run_results["key"] ) { ++checks_passed; } } if (checks_run != checks_passed) { Zymonic.log("Still waiting on before run checks (number="+checks_run+", results="+checks_passed+"), not running Transition "+this.zname); return; } } else { // setup results and start checks running this.before_run_results = {}; // run the before checks, passing in transition and extras so can recall this function for (var key in this.before_run_callbacks) { this.before_run_callbacks[key]({ transition: this, extras: extras }); } // stop now, will be called again with results return; } } // TODO: change the global variables below into Zymonic // attributes with accessors // set global skip validation flag to value from transition // no need to reset afterwards as that is done after block submit skip_validation = this.skip_validation; // if full reload set some global flags and stop as page will reload if (this.full_reload) { submit_clicked = this.ident; block_submitted = this.block_id; return; } var blockparams = {}; blockparams[this.ident] = this.display_name; if (extras) { for (var key in extras) { blockparams[key] = extras[key]; } } // load/reload the block to trigger the transition var transition = this; Zymonic.transition_being_run = this; var block = Zymonic.lookup_object("Block", this.block_id); if (this.open_new_block) { blockparams["ZZwebservicemode"] = "blockreload"; blockparams["ZZwebserviceblockid"] = this.block_id; blockparams["ZZno_display_attributes"] = "false"; var process = Zymonic.lookup_object('Process', this.process_ident); Zymonic.openBlock( process.zname, process.process_id, "process", block.getLocation(), blockparams, function() { Zymonic.transition_being_run = null; } ); } else { block.Load(blockparams, true, true, function(xml, block_id) { for (var key in transition.run_callbacks) { transition.run_callbacks[key](xml, block_id); } Zymonic.transition_being_run = null; }, null ); } }; // function to setup handlers on transition button Zymonic.Transition.Transition.prototype.setupHandlers = function() { // add click handler to link var transition = this; Zymonic.Utils.attach_event(this.getButton(), "click", this.ident, function(event) { transition.run.call(transition); // prevent the normal click action unless full reloading if (!transition.full_reload) { event.preventDefault(); } }); };