diff --git a/addons/web/__openerp__.py b/addons/web/__openerp__.py index 3767072ff9d..a82759af85f 100644 --- a/addons/web/__openerp__.py +++ b/addons/web/__openerp__.py @@ -36,6 +36,9 @@ "static/lib/underscore/underscore.string.js", "static/lib/labjs/LAB.src.js", "static/lib/py.parse/lib/py.js", + #"static/lib/history.js/scripts/bundled/html4+html5/jquery.history.js", + #"static/lib/history.js/scripts/uncompressed/history.adapter.jquery.js", + #"static/lib/history.js/scripts/uncompressed/history.js", "static/src/js/boot.js", "static/src/js/core.js", "static/src/js/dates.js", diff --git a/addons/web/static/src/js/chrome.js b/addons/web/static/src/js/chrome.js index 3c212616496..2bf7451c52a 100644 --- a/addons/web/static/src/js/chrome.js +++ b/addons/web/static/src/js/chrome.js @@ -1011,6 +1011,7 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie this.session.on_session_valid.add_last(this.header.do_update); this.session.on_session_invalid.add_last(this.header.do_update); this.session.on_session_valid.add_last(this.on_logged); + this.session.on_session_invalid.add_last(this.on_logged_out); this.menu = new openerp.web.Menu(this, "oe_menu", "oe_secondary_menu"); this.menu.on_action.add(this.on_menu_action); @@ -1047,37 +1048,131 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie this.action_manager.stop(); this.action_manager = new openerp.web.ActionManager(this); this.action_manager.appendTo($("#oe_app")); - this.action_manager.do_url_set_hash.add_last(this.do_url_set_hash); + //this.action_manager.do_url_set_hash.add_last(this.do_url_set_hash); // if using saved actions, load the action and give it to action manager - var parameters = jQuery.deparam(jQuery.param.querystring()); + /*var parameters = jQuery.deparam(jQuery.param.querystring()); if (parameters["s_action"] != undefined) { var key = parseInt(parameters["s_action"], 10); var self = this; this.rpc("/web/session/get_session_action", {key:key}, function(action) { self.action_manager.do_action(action); }); - } else if (openerp._modules_loaded) { // TODO: find better option than this - this.load_url_state() + } else /**/ if (openerp._modules_loaded) { // TODO: find better option than this + //this.load_url_state() + this.bind_statechange(); } else { - this.session.on_modules_loaded.add({ - callback: $.proxy(this, 'load_url_state'), + this.session.on_modules_loaded.add({ // XXX what about a $.Deferred ? + //callback: $.proxy(this, 'load_url_state'), + callback: $.proxy(this, 'bind_statechange'), unique: true, position: 'last' }) } }, + + //state_change_event: 'popstate', //statechange + state_change_event: 'hashchange', + + bind_statechange: function() { + console.log('bind', this.state_change_event); + $(window).bind(this.state_change_event, this.on_state_change); + var self = this; + + self.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'}); + return; + //var state = window.History.getState(false, false); + //if (state) { + // this.$element.trigger('statechange') + //} else + { + var ds = new openerp.web.DataSetSearch(this, 'res.users'); + ds.read_ids([this.session.uid], ['action_id'], function (users) { + var home_action = users[0].action_id; + if (!home_action) { + self.default_home(); + return; + } + self.do_action(home_action[0]); + }); + } + + }, + + on_logged_out: function() { + if(this.action_manager) + this.action_manager.stop(); + this.action_manager = null; + $(window).unbind(this.state_change_event, this.on_state_change); + }, + + on_state_change: function(event) { + // as this method is bound to a event via jQuery, the argument is the jQuery Event.. we need to get the current state + //var state = window.History.getState(); + //var state = event.getState(true); // = bbq.getState, in fact only deparam the hash + //var state = window.history.state; + //if (!state) { + // chrome + // state = event.originalEvent.state; + //} + //var h_state = window.History.getState(); + //var hash = window.History.getHash(); + //console.log('getState+Hash', h_state, hash); + //var state = $.deparam(hash, true); + var state = $.deparam.fragment(true); + console.log('onstatechange', event, state); + return this._super(state); + }, + + + + do_push_state: function(state, extend) { + if (extend) { + var hash = $.deparam.fragment(true); + state = _.extend({}, hash, state); + } + var url = '#' + $.param(state); + //console.log('pushstate', state, url); + //window.History.pushState(state, '', url); + //window.history.pushState(state, '', url); + + //var state = this.get_state(); + console.log('pushstate', state); + //$.bbq.pushState(state, 0); // will set the hash, so will call on_state_change + window.history.pushState(null, '', url); + //window.location.hash = url; + + }, + + /* + do_replace_state: function(state) { + //var hash = window.History.getHash(); + var hash = $.deparam.fragment(true); + state = _.extend({}, hash, state); + var url = '#' + $.param(state); + //window.History.replaceState(state, '', url); + //window.history.replaceState(state, '', url); + + //var state = this.get_state(); + console.log('replacestate', state); + //$.bbq.pushState(state, 0); // FIXME cannot have the "no history" behavior + window.history.replaceState(null, '', url); + //window.location.hash = url; + }, + /*/ + /** * Loads state from URL if any, or checks if there is a home action and * loads that, assuming we're at the index */ - load_url_state: function () { + /*load_url_state: function () { var self = this; // TODO: add actual loading if there is url state to unpack, test on window.location.hash // not logged in if (!this.session.uid) { return; } self.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'}); }, + */ default_home: function () { }, /** @@ -1101,12 +1196,15 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie self.action_manager.do_action(action); }); }, + /*/ do_url_set_hash: function(url) { if(!this.url_external_hashchange) { this.url_internal_hashchange = true; jQuery.bbq.pushState(url); } }, + //*/ + /*/ on_url_hashchange: function() { if(this.url_internal_hashchange) { this.url_internal_hashchange = false; @@ -1117,6 +1215,8 @@ openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClie this.url_external_hashchange = false; } }, + //*/ + on_menu_action: function(action) { this.action_manager.do_action(action); }, diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index c9255d31949..e3a32342af7 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -985,6 +985,22 @@ openerp.web.Widget = openerp.web.CallbackEnabled.extend(/** @lends openerp.web.W } return false; }, + + do_push_state: function(state) { + // create a new state + if (this.widget_parent) { + return this.widget_parent.do_push_state.apply(this,arguments); + } + //this.on_state_change(state); + return null; + }, + + on_state_change: function(state) { + _.each(this.widget_children, function(child) { + child.on_state_change(state); + }); + }, + rpc: function(url, data, success, error) { var def = $.Deferred().then(success, error); var self = this; diff --git a/addons/web/static/src/js/views.js b/addons/web/static/src/js/views.js index e11c0c20f04..13f74330a21 100644 --- a/addons/web/static/src/js/views.js +++ b/addons/web/static/src/js/views.js @@ -24,6 +24,7 @@ session.web.ActionManager = session.web.Widget.extend({ this.dialog = null; this.dialog_viewmanager = null; this.client_widget = null; + this.client_widget_name = null; }, render: function() { return "
"; @@ -44,37 +45,39 @@ session.web.ActionManager = session.web.Widget.extend({ if (this.client_widget) { this.client_widget.stop(); this.client_widget = null; + this.client_widget_name = null; } }, - url_update: function(action) { - var url = {}; - if(action.id) - url.action_id = action.id; - // this.url = { - // "model": action.res_model, - // "domain": action.domain, - // }; - // action.res_model - // action.domain - // action.context - // after - // action.views - // action.res_id - // mode - // menu - this.do_url_set_hash(url); - }, - do_url_set_hash: function(url) { - }, - on_url_hashchange: function(url) { - var self = this; - if(url && url.action_id) { - self.rpc("/web/action/load", { action_id: url.action_id }, function(result) { - self.do_action(result.result); - }); + + on_state_change: function(state) { + console.log(this.identifier_prefix, 'state change', state); + + if (state.action_id) { + var run_action = (!this.inner_viewmanager) || this.inner_viewmanager.action.id !== state.action_id; + if (run_action) { + this.null_action(); + this.do_action(state.action_id); + } } + else if (state.client_action) { + var run_client = (!this.client_widget) || this.client_widget_name === state.client_action.tag; + if (run_client) { + this.null_action(); + this.ir_actions_client(state.client_action); + } + } + + return this._super.apply(this, arguments); }, + do_action: function(action, on_close) { + if (_.isNumber(action)) { + var self = this; + self.rpc("/web/action/load", { action_id: action }, function(result) { + self.do_action(result.result, on_close); + }); + return; + } if (!action.type) { console.error("No type for action", action); return; @@ -124,14 +127,10 @@ session.web.ActionManager = session.web.Widget.extend({ this.content_stop(); this.inner_viewmanager = new session.web.ViewManagerAction(this, action); this.inner_viewmanager.appendTo(this.$element); - this.url_update(action); + if (action.id) { + this.do_push_state({action_id: action.id}); + } } - /* new window code - this.rpc("/web/session/save_session_action", { the_action : action}, function(key) { - var url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + jQuery.param({ s_action : "" + key }); - window.open(url,'_blank'); - }); - */ }, ir_actions_act_window_close: function (action, on_closed) { if (!this.dialog && on_closed) { @@ -152,6 +151,12 @@ session.web.ActionManager = session.web.Widget.extend({ this.content_stop(); var ClientWidget = session.web.client_actions.get_object(action.tag); (this.client_widget = new ClientWidget(this, action.params)).appendTo(this); + this.client_widget_name = action.tag; + + var client_action = {tag: action.tag}; + if (action.params) _.extend(client_action, {params: action.params}); + + this.do_push_state({client_action: client_action}); }, ir_actions_report_xml: function(action, on_closed) { var self = this; @@ -301,9 +306,19 @@ session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.View $.when(view_promise).then(function () { self.$element.find('.oe_view_title_text:first').text( self.display_title()); + self.do_push_state({view_type: self.active_view}, true); }); return view_promise; }, + + on_state_change: function(state) { + console.log(this.identifier_prefix, 'state change', state); + if (state.view_type && state.view_type !== this.active_view) { + this.on_mode_switch(state.view_type, true); + } + return this._super.apply(this, arguments); + }, + /** * Returns to the view preceding the caller view in this manager's * navigation history (the navigation history is appended to via