2011-03-30 14:00:48 +00:00
|
|
|
/*---------------------------------------------------------
|
|
|
|
* OpenERP base library
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2011-03-30 14:35:56 +00:00
|
|
|
openerp.base.views = function(openerp) {
|
2011-03-30 14:00:48 +00:00
|
|
|
|
|
|
|
openerp.base.ActionManager = openerp.base.Controller.extend({
|
2011-03-30 23:57:43 +00:00
|
|
|
// process all kind of actions
|
2011-03-30 14:00:48 +00:00
|
|
|
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") {
|
2011-03-31 13:43:46 +00:00
|
|
|
if (this.viewmanager) {
|
|
|
|
this.viewmanager.stop();
|
|
|
|
}
|
2011-04-06 00:51:36 +00:00
|
|
|
this.viewmanager = new openerp.base.ViewManagerAction(this.session,this.element_id, action, false);
|
2011-03-30 14:00:48 +00:00
|
|
|
this.viewmanager.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-03-31 14:54:32 +00:00
|
|
|
/**
|
|
|
|
* Registry for all the main views
|
|
|
|
*/
|
|
|
|
openerp.base.views = new openerp.base.Registry();
|
2011-04-04 19:52:16 +00:00
|
|
|
|
2011-03-30 14:00:48 +00:00
|
|
|
openerp.base.ViewManager = openerp.base.Controller.extend({
|
2011-04-06 00:51:36 +00:00
|
|
|
init: function(session, element_id, model, views) {
|
2011-03-30 14:00:48 +00:00
|
|
|
this._super(session, element_id);
|
2011-04-06 00:51:36 +00:00
|
|
|
this.model = model;
|
|
|
|
this.dataset = new openerp.base.DataSet(this.session, model);
|
2011-03-30 14:00:48 +00:00
|
|
|
this.searchview = null;
|
2011-03-31 12:25:22 +00:00
|
|
|
this.active_view = null;
|
2011-04-06 00:51:36 +00:00
|
|
|
this.views_src = views;
|
2011-03-30 14:00:48 +00:00
|
|
|
this.views = {};
|
2011-04-05 10:10:08 +00:00
|
|
|
|
2011-04-05 10:24:40 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @returns {jQuery.Deferred} initial view loading promise
|
|
|
|
*/
|
2011-04-06 00:51:36 +00:00
|
|
|
start: function() {
|
2011-04-05 10:24:40 +00:00
|
|
|
var self = this;
|
|
|
|
this.dataset.start();
|
2011-04-06 00:51:36 +00:00
|
|
|
this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: this.views_src}));
|
2011-04-05 10:10:08 +00:00
|
|
|
this.$element.find('.oe_vm_switch button').click(function() {
|
|
|
|
self.on_mode_switch($(this).data('view-type'));
|
|
|
|
});
|
2011-04-06 00:51:36 +00:00
|
|
|
_.each(this.views_src, function(view) {
|
2011-04-05 10:10:08 +00:00
|
|
|
self.views[view[1]] = { view_id: view[0], controller: null };
|
|
|
|
});
|
2011-04-06 00:51:36 +00:00
|
|
|
// switch to the first one in sequence
|
|
|
|
return this.on_mode_switch(this.views_src[0][1]);
|
|
|
|
},
|
|
|
|
stop: function() {
|
2011-04-05 10:10:08 +00:00
|
|
|
},
|
2011-04-01 11:50:05 +00:00
|
|
|
/**
|
|
|
|
* Asks the view manager to switch visualization mode.
|
|
|
|
*
|
|
|
|
* @param {String} view_type type of view to display
|
|
|
|
* @returns {jQuery.Deferred} new view loading promise
|
|
|
|
*/
|
2011-03-30 14:00:48 +00:00
|
|
|
on_mode_switch: function(view_type) {
|
2011-04-01 11:50:05 +00:00
|
|
|
var view_promise;
|
2011-03-31 12:25:22 +00:00
|
|
|
this.active_view = view_type;
|
2011-03-31 13:20:58 +00:00
|
|
|
var view = this.views[view_type];
|
|
|
|
if (!view.controller) {
|
|
|
|
// Lazy loading of views
|
2011-04-06 00:51:36 +00:00
|
|
|
var controllerclass = openerp.base.views.get_object(view_type);
|
|
|
|
var controller = new controllerclass( this, this.session, this.element_id + "_view_" + view_type, this.dataset, view.view_id);
|
2011-04-01 11:50:05 +00:00
|
|
|
view_promise = controller.start();
|
2011-03-31 13:20:58 +00:00
|
|
|
this.views[view_type].controller = controller;
|
|
|
|
}
|
2011-04-01 09:06:53 +00:00
|
|
|
|
2011-04-05 10:24:40 +00:00
|
|
|
if(this.searchview) {
|
|
|
|
if (view.controller.searchable === false) {
|
|
|
|
this.searchview.hide();
|
|
|
|
} else {
|
|
|
|
this.searchview.show();
|
|
|
|
}
|
2011-04-01 10:45:00 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 09:06:53 +00:00
|
|
|
this.$element
|
2011-04-01 09:11:17 +00:00
|
|
|
.find('.views-switchers button').removeAttr('disabled')
|
|
|
|
.filter('[data-view-type="' + view_type + '"]')
|
2011-04-04 11:25:35 +00:00
|
|
|
.attr('disabled', true);
|
2011-04-01 09:06:53 +00:00
|
|
|
|
2011-03-30 14:00:48 +00:00
|
|
|
for (var i in this.views) {
|
2011-03-31 13:20:58 +00:00
|
|
|
if (this.views[i].controller) {
|
2011-04-04 15:34:26 +00:00
|
|
|
if (i === view_type) {
|
|
|
|
this.views[i].controller.do_show();
|
|
|
|
} else {
|
|
|
|
this.views[i].controller.do_hide();
|
|
|
|
}
|
2011-03-31 13:20:58 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
}
|
2011-04-01 11:50:05 +00:00
|
|
|
return view_promise;
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-04-06 00:51:36 +00:00
|
|
|
* Called when one of the view want to execute an action
|
2011-03-30 14:00:48 +00:00
|
|
|
*/
|
2011-04-06 00:51:36 +00:00
|
|
|
on_action: function(action) {
|
|
|
|
},
|
|
|
|
on_create: function() {
|
|
|
|
},
|
|
|
|
on_remove: function() {
|
|
|
|
},
|
|
|
|
on_edit: function() {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
|
|
|
|
init: function(session, element_id, action, sidebar) {
|
|
|
|
this._super(session, element_id, action.res_model, action.views);
|
|
|
|
this.action = action;
|
|
|
|
this.sidebar = sidebar;
|
|
|
|
if (sidebar)
|
|
|
|
this.sidebar = new openerp.base.Sidebar(null, this);
|
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
var inital_view_loaded = this._super();
|
|
|
|
if (this.sidebar) {
|
|
|
|
this.$element.find('.view-manager-main-sidebar').html(this.sidebar.render());
|
|
|
|
this.sidebar.start();
|
|
|
|
}
|
|
|
|
var searchview_loaded = this.setup_search_view(this.action);
|
|
|
|
|
|
|
|
if (this.action['auto_search']) {
|
|
|
|
$.when(searchview_loaded, inital_view_loaded)
|
|
|
|
.then(this.searchview.do_search);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
// should be replaced by automatic destruction implemented in BaseWidget
|
|
|
|
if (this.sidebar) {
|
|
|
|
this.sidebar.stop();
|
|
|
|
}
|
|
|
|
this._super();
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-03-31 14:54:32 +00:00
|
|
|
/**
|
|
|
|
* Sets up the current viewmanager's search view.
|
|
|
|
*
|
|
|
|
* @param action the action being executed
|
2011-04-01 11:50:05 +00:00
|
|
|
* @returns {jQuery.Deferred} search view startup deferred
|
2011-03-31 14:54:32 +00:00
|
|
|
*/
|
|
|
|
setup_search_view:function (action) {
|
2011-04-01 09:02:49 +00:00
|
|
|
var self = this;
|
2011-03-31 14:54:32 +00:00
|
|
|
if (this.searchview) {
|
|
|
|
this.searchview.stop();
|
|
|
|
}
|
2011-04-05 10:04:50 +00:00
|
|
|
var view_id = action.search_view_id ? action.search_view_id[0] || false : false;
|
2011-03-31 14:54:32 +00:00
|
|
|
|
2011-04-05 10:17:39 +00:00
|
|
|
this.searchview = new openerp.base.SearchView(this, this.session, this.element_id + "_search", this.dataset, view_id, this.search_defaults());
|
2011-04-05 10:04:50 +00:00
|
|
|
this.searchview.on_search.add(function() {
|
2011-04-01 09:02:49 +00:00
|
|
|
self.views[self.active_view].controller.do_search.apply(self, arguments);
|
|
|
|
});
|
2011-04-05 10:04:50 +00:00
|
|
|
return this.searchview.start();
|
2011-03-31 14:54:32 +00:00
|
|
|
},
|
2011-04-05 10:10:08 +00:00
|
|
|
/**
|
2011-04-06 00:51:36 +00:00
|
|
|
* 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}
|
2011-04-05 10:10:08 +00:00
|
|
|
*/
|
2011-04-06 00:51:36 +00:00
|
|
|
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;
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
openerp.base.BaseWidget = openerp.base.Controller.extend({
|
2011-03-30 15:04:21 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-03-30 14:00:48 +00:00
|
|
|
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',
|
|
|
|
/**
|
2011-04-04 16:08:13 +00:00
|
|
|
* Base class for widgets. Handle rendering (based on a QWeb template), identifier
|
|
|
|
* generation, parenting and destruction of the widget.
|
2011-03-30 14:00:48 +00:00
|
|
|
* Contructor. Also initialize the identifier.
|
|
|
|
*
|
|
|
|
* @params {openerp.base.search.BaseWidget} parent The parent widget.
|
|
|
|
*/
|
2011-04-04 16:08:13 +00:00
|
|
|
init: function (parent, session) {
|
|
|
|
this._super(session);
|
2011-03-30 15:04:21 +00:00
|
|
|
this.children = [];
|
2011-03-30 14:00:48 +00:00
|
|
|
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();
|
2011-03-31 08:03:35 +00:00
|
|
|
var tmp = document.getElementById(this.element_id);
|
2011-03-30 14:00:48 +00:00
|
|
|
this.$element = tmp ? $(tmp) : null;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* "Stops" the widgets. Called when the view destroys itself, this
|
|
|
|
* lets the widgets clean up after themselves.
|
|
|
|
*/
|
|
|
|
stop: function () {
|
2011-03-30 15:04:21 +00:00
|
|
|
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);
|
2011-03-30 14:00:48 +00:00
|
|
|
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) {
|
2011-03-30 15:04:21 +00:00
|
|
|
if(this.parent) {
|
|
|
|
this.parent.children = _.without(this.parent.children, this);
|
|
|
|
}
|
|
|
|
this.parent = parent;
|
|
|
|
if(this.parent) {
|
|
|
|
parent.children.push(this);
|
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 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) {
|
2011-03-30 23:55:52 +00:00
|
|
|
return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
|
2011-03-30 14:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
2011-03-30 23:55:52 +00:00
|
|
|
|
2011-04-01 12:10:42 +00:00
|
|
|
openerp.base.Sidebar = openerp.base.BaseWidget.extend({
|
2011-04-04 13:47:05 +00:00
|
|
|
template: "ViewManager.sidebar",
|
2011-04-04 16:08:13 +00:00
|
|
|
init: function(parent, view_manager) {
|
|
|
|
this._super(parent, view_manager.session);
|
|
|
|
this.view_manager = view_manager;
|
2011-04-04 13:47:05 +00:00
|
|
|
this.sections = [];
|
|
|
|
},
|
2011-04-04 16:08:13 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
},
|
2011-04-04 13:47:05 +00:00
|
|
|
refresh: function() {
|
2011-04-05 12:59:44 +00:00
|
|
|
this.$element.html(QWeb.render("ViewManager.sidebar.internal", _.extend({_:_}, this)));
|
2011-04-04 13:47:05 +00:00
|
|
|
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];
|
2011-04-05 11:02:10 +00:00
|
|
|
// I know this doesn't work, one day it will
|
|
|
|
new openerp.base.ActionManager(this.view_manager, null).do_action(action);
|
2011-04-04 13:47:05 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
this._super();
|
|
|
|
this.refresh();
|
|
|
|
}
|
2011-04-01 12:10:42 +00:00
|
|
|
});
|
|
|
|
|
2011-03-31 14:54:32 +00:00
|
|
|
openerp.base.views.add('calendar', 'openerp.base.CalendarView');
|
2011-03-30 14:00:48 +00:00
|
|
|
openerp.base.CalendarView = openerp.base.Controller.extend({
|
2011-04-01 09:40:30 +00:00
|
|
|
start: function () {
|
|
|
|
this._super();
|
|
|
|
this.$element.append('Calendar view');
|
2011-04-05 08:11:34 +00:00
|
|
|
},
|
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
2011-04-01 09:40:30 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
});
|
|
|
|
|
2011-03-31 14:54:32 +00:00
|
|
|
openerp.base.views.add('gantt', 'openerp.base.GanttView');
|
2011-03-30 14:00:48 +00:00
|
|
|
openerp.base.GanttView = openerp.base.Controller.extend({
|
2011-04-01 09:40:30 +00:00
|
|
|
start: function () {
|
|
|
|
this._super();
|
|
|
|
this.$element.append('Gantt view');
|
2011-04-05 08:11:34 +00:00
|
|
|
},
|
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
2011-04-01 09:40:30 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
});
|
|
|
|
|
2011-03-31 15:25:31 +00:00
|
|
|
openerp.base.views.add('tree', 'openerp.base.TreeView');
|
2011-04-04 15:52:09 +00:00
|
|
|
openerp.base.TreeView = openerp.base.Controller.extend({
|
2011-03-31 15:25:31 +00:00
|
|
|
/**
|
|
|
|
* Genuine tree view (the one displayed as a tree, not the list)
|
|
|
|
*/
|
2011-04-01 09:40:30 +00:00
|
|
|
start: function () {
|
|
|
|
this._super();
|
|
|
|
this.$element.append('Tree view');
|
2011-04-05 08:11:34 +00:00
|
|
|
},
|
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
2011-04-01 09:40:30 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
});
|
|
|
|
|
2011-03-31 14:54:32 +00:00
|
|
|
openerp.base.views.add('graph', 'openerp.base.GraphView');
|
2011-03-30 14:00:48 +00:00
|
|
|
openerp.base.GraphView = openerp.base.Controller.extend({
|
2011-04-01 09:40:30 +00:00
|
|
|
start: function () {
|
|
|
|
this._super();
|
|
|
|
this.$element.append('Graph view');
|
2011-04-05 08:11:34 +00:00
|
|
|
},
|
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
2011-04-01 09:40:30 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
openerp.base.ProcessView = openerp.base.Controller.extend({
|
|
|
|
});
|
|
|
|
|
|
|
|
openerp.base.HelpView = openerp.base.Controller.extend({
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|