odoo/addons/base/static/src/js/views.js

391 lines
12 KiB
JavaScript
Raw Normal View History

/*---------------------------------------------------------
* OpenERP base library
*---------------------------------------------------------*/
openerp.base.views = function(openerp) {
openerp.base.ActionManager = openerp.base.Controller.extend({
// process all kind of actions
init: function(session, element_id) {
this._super(session, element_id);
this.viewmanager = null;
},
/**
* Process an action
* Supported actions: act_window
*/
do_action: function(action) {
// instantiate the right controllers by understanding the action
if(action.type == "ir.actions.act_window") {
if (this.viewmanager) {
this.viewmanager.stop();
}
this.viewmanager = new openerp.base.ViewManager(this.session,this.element_id, false);
this.viewmanager.do_action_window(action);
this.viewmanager.start();
}
}
});
/**
* Registry for all the main views
*/
openerp.base.views = new openerp.base.Registry();
openerp.base.ViewManager = openerp.base.Controller.extend({
init: function(session, element_id, desactivate_sidebar) {
this._super(session, element_id);
this.action = null;
this.dataset = null;
this.searchview = null;
this.active_view = null;
this.views = {};
if (desactivate_sidebar)
this.sidebar = null;
else
this.sidebar = new openerp.base.Sidebar(null, this);
},
start: function() {
if (this.sidebar) {
this.$element.find('.view-manager-main-sidebar').html(this.sidebar.render());
this.sidebar.start();
}
},
stop: function() {
// should be replaced by automatic destruction implemented in BaseWidget
if (this.sidebar) {
this.sidebar.stop();
}
this._super();
},
do_action_window: function(action) {
var self = this;
this.action = action;
// switch to the first one in sequence
var inital_view_loaded = this.setup_initial_view(action.res_model,action.views);
var searchview_loaded = this.setup_search_view(action);
if (action['auto_search']) {
$.when(searchview_loaded, inital_view_loaded)
.then(this.searchview.do_search);
}
},
/**
* @returns {jQuery.Deferred} initial view loading promise
*/
setup_initial_view: function(model,views) {
var self = this;
this.dataset = new openerp.base.DataSet(this.session, model);
this.dataset.start();
this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: views}));
this.$element.find('.oe_vm_switch button').click(function() {
self.on_mode_switch($(this).data('view-type'));
});
_.each(views, function(view) {
self.views[view[1]] = { view_id: view[0], controller: null };
});
return this.on_mode_switch(views[0][1]);
},
/**
* Asks the view manager to switch visualization mode.
*
* @param {String} view_type type of view to display
* @returns {jQuery.Deferred} new view loading promise
*/
on_mode_switch: function(view_type) {
var view_promise;
this.active_view = view_type;
var view = this.views[view_type];
if (!view.controller) {
// Lazy loading of views
var controller = new (openerp.base.views.get_object(view_type))(
this, this.session, this.element_id + "_view_" + view_type, this.dataset, view.view_id);
view_promise = controller.start();
this.views[view_type].controller = controller;
}
if(this.searchview) {
if (view.controller.searchable === false) {
this.searchview.hide();
} else {
this.searchview.show();
}
}
this.$element
.find('.views-switchers button').removeAttr('disabled')
.filter('[data-view-type="' + view_type + '"]')
.attr('disabled', true);
for (var i in this.views) {
if (this.views[i].controller) {
if (i === view_type) {
this.views[i].controller.do_show();
} else {
this.views[i].controller.do_hide();
}
}
}
return view_promise;
},
/**
* Extract search view defaults from the current action's context.
*
* These defaults are of the form {search_default_*: value}
*
* @returns {Object} a clean defaults mapping of {field_name: value}
*/
search_defaults: function () {
var defaults = {};
_.each(this.action.context, function (value, key) {
var match = /^search_default_(.*)$/.exec(key);
if (match) {
defaults[match[1]] = value;
}
});
return defaults;
},
/**
* Sets up the current viewmanager's search view.
*
* @param action the action being executed
* @returns {jQuery.Deferred} search view startup deferred
*/
setup_search_view:function (action) {
var self = this;
if (this.searchview) {
this.searchview.stop();
}
var view_id = action.search_view_id ? action.search_view_id[0] || false : false;
this.searchview = new openerp.base.SearchView(this, this.session, this.element_id + "_search", this.dataset, view_id, this.search_defaults());
this.searchview.on_search.add(function() {
self.views[self.active_view].controller.do_search.apply(self, arguments);
});
return this.searchview.start();
},
/**
* Called when one of the view want to execute an action
*/
on_action: function(action) {
},
on_create: function() {
},
on_remove: function() {
},
on_edit: function() {
}
});
openerp.base.ViewManagerRoot = openerp.base.ViewManager.extend({
// Extends view manager
});
openerp.base.ViewManagerUsedAsAMany2One = openerp.base.ViewManager.extend({
// Extends view manager
});
openerp.base.BaseWidget = openerp.base.Controller.extend({
/**
* The name of the QWeb template that will be used for rendering. Must be redifined
* in subclasses or the render() method can not be used.
*
* @type string
*/
template: null,
/**
* The prefix used to generate an id automatically. Should be redifined in subclasses.
* If it is not defined, a default identifier will be used.
*
* @type string
*/
identifier_prefix: 'generic-identifier',
/**
* Base class for widgets. Handle rendering (based on a QWeb template), identifier
* generation, parenting and destruction of the widget.
* Contructor. Also initialize the identifier.
*
* @params {openerp.base.search.BaseWidget} parent The parent widget.
*/
init: function (parent, session) {
this._super(session);
this.children = [];
this.parent = null;
this.set_parent(parent);
this.make_id(this.identifier_prefix);
},
/**
* Sets and returns a globally unique identifier for the widget.
*
* If a prefix is appended, the identifier will be appended to it.
*
* @params sections prefix sections, empty/falsy sections will be removed
*/
make_id: function () {
this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
return this.element_id;
},
/**
* "Starts" the widgets. Called at the end of the rendering, this allows
* to get a jQuery object referring to the DOM ($element attribute).
*/
start: function () {
this._super();
var tmp = document.getElementById(this.element_id);
this.$element = tmp ? $(tmp) : null;
},
/**
* "Stops" the widgets. Called when the view destroys itself, this
* lets the widgets clean up after themselves.
*/
stop: function () {
var tmp_children = this.children;
this.children = [];
_.each(tmp_children, function(x) {
x.stop();
});
if(this.$element != null) {
this.$element.remove();
}
this.set_parent(null);
this._super();
},
/**
* Set the parent of this component, also unregister the previous parent if there
* was one.
*
* @param {openerp.base.BaseWidget} parent The new parent.
*/
set_parent: function(parent) {
if(this.parent) {
this.parent.children = _.without(this.parent.children, this);
}
this.parent = parent;
if(this.parent) {
parent.children.push(this);
}
},
/**
* Render the widget. This.template must be defined.
* The content of the current object is passed as context to the template.
*
* @param {object} additional Additional context arguments to pass to the template.
*/
render: function (additional) {
return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
}
});
openerp.base.Sidebar = openerp.base.BaseWidget.extend({
template: "ViewManager.sidebar",
init: function(parent, view_manager) {
this._super(parent, view_manager.session);
this.view_manager = view_manager;
this.sections = [];
},
load_multi_actions: function() {
if (_.detect(this.sections, function(x) {return x.type=="multi_actions";}) != undefined)
return;
var self = this;
this.rpc("/base/sidebar/get_actions",
{"model": this.view_manager.dataset.model}, function(result) {
self.sections.push({type: "multi_actions", elements:
_.map(result, function(x) {return {text:x[2].name, action:x}; })});
self.refresh();
});
},
refresh: function() {
this.$element.html(QWeb.render("ViewManager.sidebar.internal", _.extend({_:_}, this)));
var self = this;
this.$element.find("a").click(function(e) {
$this = jQuery(this);
var i = $this.attr("data-i");
var j = $this.attr("data-i");
var action = self.sections[i].elements[j];
// I know this doesn't work, one day it will
new openerp.base.ActionManager(this.view_manager, null).do_action(action);
e.stopPropagation();
e.preventDefault();
});
},
start: function() {
this._super();
this.refresh();
}
});
openerp.base.views.add('calendar', 'openerp.base.CalendarView');
openerp.base.CalendarView = openerp.base.Controller.extend({
start: function () {
this._super();
this.$element.append('Calendar view');
},
do_show: function () {
this.$element.show();
},
do_hide: function () {
this.$element.hide();
}
});
openerp.base.views.add('gantt', 'openerp.base.GanttView');
openerp.base.GanttView = openerp.base.Controller.extend({
start: function () {
this._super();
this.$element.append('Gantt view');
},
do_show: function () {
this.$element.show();
},
do_hide: function () {
this.$element.hide();
}
});
openerp.base.views.add('tree', 'openerp.base.TreeView');
openerp.base.TreeView = openerp.base.Controller.extend({
/**
* Genuine tree view (the one displayed as a tree, not the list)
*/
start: function () {
this._super();
this.$element.append('Tree view');
},
do_show: function () {
this.$element.show();
},
do_hide: function () {
this.$element.hide();
}
});
openerp.base.views.add('graph', 'openerp.base.GraphView');
openerp.base.GraphView = openerp.base.Controller.extend({
start: function () {
this._super();
this.$element.append('Graph view');
},
do_show: function () {
this.$element.show();
},
do_hide: function () {
this.$element.hide();
}
});
openerp.base.ProcessView = openerp.base.Controller.extend({
});
openerp.base.HelpView = openerp.base.Controller.extend({
});
};
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: