diff options
author | Christian Schneider | 2006-10-26 13:35:12 +0000 |
---|---|---|
committer | Christian Schneider | 2006-10-26 13:35:12 +0000 |
commit | a5a19fd672bc0b8113d620669b557f17dccd343a (patch) | |
tree | 876ba4fec8362ac2e9374f61b9b7f67fcd2b8e59 /itjs/state.js | |
download | itools-a5a19fd672bc0b8113d620669b557f17dccd343a.tar.gz itools-a5a19fd672bc0b8113d620669b557f17dccd343a.tar.bz2 itools-a5a19fd672bc0b8113d620669b557f17dccd343a.zip |
Moved itools to live branch
Diffstat (limited to 'itjs/state.js')
-rw-r--r-- | itjs/state.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/itjs/state.js b/itjs/state.js new file mode 100644 index 0000000..04f8acf --- /dev/null +++ b/itjs/state.js @@ -0,0 +1,114 @@ +/** + * Generic state object (singleton), for supporting Ajax browser history + * + * $Id$ + * + */ +var it_state = +{ +it_iframe: null, +it_history_field: null, +it_store_handlers: [], +it_restore_handlers: [], + +/** + * Register a handler that gets called before creating a history entry + * @param p.object Object that contains handler method + * @param p.method Method of p.object to call + */ +register_store_handler: function(p) +{ + this.it_store_handlers[this.it_store_handlers.length] = p; +}, + + +/** + * Register a handler that gets called after restoring a history entry + * @param p.object Object that contains handler method + * @param p.method Method of p.object to call + * @param p.initial optional flag: if true, handler is called with initial empty state + */ +register_restore_handler: function(p) +{ + this.it_restore_handlers[this.it_restore_handlers.length] = p; + + // If we already have state data, call registered handler immediately. Yup this _is_ needed! + if (this.it_history_field && this.it_history_field.value) + p.object[p.method](); +}, + + +/** + * Create a new history entry, saving all values of it_state (asynchronously) + * Users must call this without parameter! + */ +new_history_entry: function(p) +{ + // ED('it_state::new_history_entry()'); + + if (!this.it_iframe && !(this.it_iframe = document.getElementById('it_state'))) + ED('it_state::new_history_entry(): it_state object not found!'); + + this.it_iframe.src = "/itjs/state.html?t="+now(); + this.it_history_field = null; +}, + + +/** + * Restore state from history, called from iframe's onload handler but in main window's context + */ +restore_history: function() +{ + if (!this.it_iframe && !(this.it_iframe = document.getElementById('it_state'))) + ED('it_state::restore_history(): it_state object not found!'); + + var idoc = it_get_iframe_document(this.it_iframe); + this.it_history_field = idoc ? idoc.getElementById('state') : {}; // Work-around IE5 not returning iframe document + + if (this.it_history_field.value) + { + var res = eval('({' + this.it_history_field.value + '})'); + // ED('it_state::restore_history(): restoring these settings:', res); + for (var key in res) + this[key] = res[key]; + } + + for (var i in this.it_restore_handlers) + { + if (this.it_history_field.value || (this.it_restore_handlers[i].initial && (!idoc || !it_get_iframe_document(this.it_iframe).location.href.match(/t=/)))) + this.it_restore_handlers[i].object[this.it_restore_handlers[i].method](); + } +}, + + +/** + * Call all store handlers and store state in it_history_field + */ +store_state: function() +{ + if (!this.it_iframe && !(this.it_iframe = document.getElementById('it_state'))) + ED('it_state::store_state(): it_state object not found!'); + + var idoc = it_get_iframe_document(this.it_iframe); + this.it_history_field = idoc ? idoc.getElementById('state') : {}; // Work-around IE5 not returning iframe document + + for (var i in this.it_store_handlers) + this.it_store_handlers[i].object[this.it_store_handlers[i].method](); + + var ser = []; + for (var key in this) + { + var value = this[key], type = typeof(value); + if (!key.match(/^it_/) && type.match(/boolean|number|string/)) + ser[ser.length] = key + ':' + ((type == 'string') ? "'" + value.replace(/([\\'])/g, '\\\1') + "'" : value); + } + + // ED('it_state::store_state()', ser); + this.it_history_field.value = ser.join(','); +}/* NO COMMA */ +} + +function it_state_restore_history() +{ + it_state.restore_history(); +} |