niv-openerp
2011-04-11 10:45:44 +02:00
6 changed files with 237 additions and 145 deletions
+4
View File
@@ -76,6 +76,10 @@
/** @lends openerp */
var openerp = this.openerp = {
/**
* Debug flag turns on logging
*/
debug: true,
// element_ids registry linked to all controllers on the page
// TODO rename to elements, or keep gtk naming?
screen: {},
+223 -40
View File
@@ -193,8 +193,6 @@ openerp.base.BasicController = Class.extend( /** @lends openerp.base.BasicContro
// try to call deferred methods on this return value.
return $.Deferred().done().promise();
},
on_ready: function() {
},
stop: function() {
},
log: function() {
@@ -206,20 +204,55 @@ openerp.base.BasicController = Class.extend( /** @lends openerp.base.BasicContro
this.on_log.apply(this,args);
},
on_log: function() {
if(true || window.openerp.debug || (window.location.search.indexOf('?debug') !== -1)) {
if(window.openerp.debug || (window.location.search.indexOf('?debug') !== -1)) {
var notify = false;
var body = false;
if(window.console) {
console.log(arguments);
} else {
$.each(arguments, function(i,v) {
v = v==null ? "null" : v;
$('<pre></pre>').text(v.toString()).appendTo($('body'));
});
body = true;
}
var a = Array.prototype.slice.call(arguments, 0);
for(var i = 0; i < a.length; i++) {
var v = a[i]==null ? "null" : a[i].toString();
if(i==0) {
notify = v.match(/^not/);
body = v.match(/^bod/);
}
if(body) {
$('<pre></pre>').text(v).appendTo($('body'));
}
if(notify && this.notification) {
this.notification.notify("Logging:",v);
}
}
}
}
});
openerp.base.Notification = openerp.base.BasicController.extend({
init: function(element_id) {
this._super(element_id);
this.$element.notify({
speed: 500,
expires: 1500
});
},
notify: function(title, text) {
this.$element.notify('create', {
title: title,
text: text
});
},
warn: function(title, text) {
this.$element.notify('create', 'oe_notification_alert', {
title: title,
text: text
});
}
});
openerp.base.Session = openerp.base.BasicController.extend( /** @lends openerp.base.Session# */{
/**
* @constructs
@@ -446,18 +479,89 @@ openerp.base.Session = openerp.base.BasicController.extend( /** @lends openerp.b
}
});
// A controller takes an already existing element
// new()
// start()
openerp.base.Controller = openerp.base.BasicController.extend( /** @lends openerp.base.Controller# */{
/**
* Controller manifest used to declare standard controller attributes
*/
controller_manifest: {
register: null,
template: "",
element_post_prefix: false,
},
/**
* Controller registry,
*/
controller_registry: {
},
/**
* Add a new child controller
*/
controller_get: function(key) {
return this.controller_registry[key];
// OR should contrustct it ? setting parent correctly ?
// function construct(constructor, args) {
// function F() {
// return constructor.apply(this, args);
// }
// F.prototype = constructor.prototype;
// return new F();
// }
// var obj = this.controller_registry[key];
// if(obj) {
// return construct(obj, Array.prototype.slice.call(arguments, 1));
// }
},
controller_new: function(key) {
var self;
// OR should contrustct it ? setting parent correctly ?
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
var obj = this.controller_registry[key];
if(obj) {
// TODO Prepend parent
return construct(obj, Array.prototype.slice.call(arguments, 1));
}
},
/**
* @constructs
* @extends openerp.base.BasicController
*/
init: function(session, element_id) {
init: function(parent_or_session, element_id) {
this._super(element_id);
this.session = session;
},
on_log: function() {
if(this.session)
this.session.log.apply(this.session,arguments);
this.controller_parent = null;
this.controller_children = [];
if(parent_or_session) {
if(parent_or_session.session) {
this.parent = parent_or_session;
this.session = this.parent.session;
if(this.parent.children) {
this.parent.children.push(this);
}
} else {
// TODO remove Backward compatilbility
this.session = parent_or_session;
}
}
// Apply manifest options
if(this.controller_manifest) {
var register = this.controller_manifest.register;
// TODO accept a simple string
if(register) {
for(var i=0; i<register.length; i++) {
this.controller_registry[register[i]] = this;
}
}
// TODO if post prefix
//this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
};
},
/**
* Performs a JSON-RPC call
@@ -474,6 +578,105 @@ openerp.base.Controller = openerp.base.BasicController.extend( /** @lends opener
}
});
// A widget is a controller that doesnt take an element_id
// it render its own html that you should insert into the dom
// and bind it a start()
//
// new()
// render() and insert it place it where you want
// start()
openerp.base.BaseWidget = openerp.base.Controller.extend({
/**
* The name of the QWeb template that will be used for rendering. Must be
* redefined in subclasses or the render() method can not be used.
*
* @type string
*/
template: null,
/**
* The prefix used to generate an id automatically. Should be redefined 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.
* Also initialize the identifier.
*
* @constructs
* @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 un-register 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.CrashManager = openerp.base.Controller.extend({
init: function(session, element_id) {
this._super(session, element_id);
@@ -495,11 +698,10 @@ openerp.base.CrashManager = openerp.base.Controller.extend({
}
});
openerp.base.Database = openerp.base.Controller.extend({
// Non Session Controller to manage databases
});
openerp.base.Loading = openerp.base.Controller.extend({
controller_manifest: {
register: ["Loading"],
},
init: function(session, element_id) {
this._super(session, element_id);
this.count = 0;
@@ -518,26 +720,7 @@ openerp.base.Loading = openerp.base.Controller.extend({
}
});
openerp.base.Notification = openerp.base.Controller.extend({
init: function(session, element_id) {
this._super(session, element_id);
this.$element.notify({
speed: 500,
expires: 1500
});
},
'default': function(title, text) {
this.$element.notify('create', {
title: title,
text: text
});
},
alert: function(title, text) {
this.$element.notify('create', 'oe_notification_alert', {
title: title,
text: text
});
}
openerp.base.Database = openerp.base.Controller.extend({
});
openerp.base.Login = openerp.base.Controller.extend({
@@ -630,7 +813,6 @@ openerp.base.Menu = openerp.base.Controller.extend({
});
this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
this.on_ready();
},
on_menu_click: function(ev, id) {
id = id || 0;
@@ -703,7 +885,7 @@ openerp.base.WebClient = openerp.base.Controller.extend({
this.crashmanager = new openerp.base.CrashManager(this.session);
// Do you autorize this ?
openerp.base.Controller.prototype.notification = new openerp.base.Notification(this.session, "oe_notification");
openerp.base.Controller.prototype.notification = new openerp.base.Notification("oe_notification");
this.header = new openerp.base.Header(this.session, "oe_header");
this.login = new openerp.base.Login(this.session, "oe_login");
@@ -720,7 +902,7 @@ openerp.base.WebClient = openerp.base.Controller.extend({
this.header.start();
this.login.start();
this.menu.start();
this.notification['default']("OpenERP Client", "The openerp client has been initialized.");
this.notification.notify("OpenERP Client", "The openerp client has been initialized.");
},
on_logged: function() {
this.action = new openerp.base.ActionManager(this.session, "oe_app");
@@ -739,6 +921,7 @@ openerp.base.webclient = function(element_id) {
client.start();
return client;
};
};
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
+1 -1
View File
@@ -100,7 +100,7 @@ openerp.base.DataSet = openerp.base.Controller.extend( /** @lends openerp.base.
}, callback);
},
unlink: function(ids) {
this.notification['default']("Unlink", ids);
this.notification.notify("Unlink", ids);
},
call: function (method, ids, args, callback) {
ids = ids || [];
+6 -6
View File
@@ -234,24 +234,24 @@ openerp.base.FormView = openerp.base.Controller.extend( /** @lends openerp.base
}
});
msg += "</ul>";
this.notification.alert("The following fields are invalid :", msg);
this.notification.warn("The following fields are invalid :", msg);
},
on_saved: function(r) {
if (!r.result) {
this.log("Record was not saved");
} else {
// Check response for exceptions, display error
this.notification['default']("Record saved", "The record #" + this.datarecord.id + " has been saved.");
this.notification.notify("Record saved", "The record #" + this.datarecord.id + " has been saved.");
}
},
do_search: function (domains, contexts, groupbys) {
this.notification['default']("Searching form");
this.notification.notify("Searching form");
},
on_action: function (action) {
this.notification['default']('Executing action ' + action);
this.notification.notify('Executing action ' + action);
},
do_cancel: function () {
this.notification['default']("Cancelling form");
this.notification.notify("Cancelling form");
}
});
@@ -720,7 +720,7 @@ openerp.base.form.FieldOne2ManyDatasSet = openerp.base.DataSetStatic.extend({
this._super(id, data, callback);
},
unlink: function() {
this.notification['default']('Unlinking o2m ' + this.ids);
this.notification.notify('Unlinking o2m ' + this.ids);
}
});
+1 -1
View File
@@ -215,7 +215,7 @@ openerp.base.SearchView = openerp.base.Controller.extend({
* @param {Array} errors a never-empty array of error objects
*/
on_invalid: function (errors) {
this.notification['default']("Invalid Search", "triggered from search view");
this.notification.notify("Invalid Search", "triggered from search view");
},
do_clear: function (e) {
if (e && e.preventDefault) { e.preventDefault(); }
+2 -97
View File
@@ -178,98 +178,6 @@ openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
}
});
openerp.base.BaseWidget = openerp.base.Controller.extend({
/**
* The name of the QWeb template that will be used for rendering. Must be
* redefined in subclasses or the render() method can not be used.
*
* @type string
*/
template: null,
/**
* The prefix used to generate an id automatically. Should be redefined 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.
* Also initialize the identifier.
*
* @constructs
* @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 un-register 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) {
@@ -296,8 +204,7 @@ openerp.base.Sidebar = openerp.base.BaseWidget.extend({
var i = $this.attr("data-i");
var j = $this.attr("data-j");
var action = self.sections[i].elements[j];
(new openerp.base.ExternalActionManager(self.view_manager.session, null))
.handle_action(action);
(new openerp.base.ExternalActionManager(self.view_manager.session, null)) .handle_action(action);
e.stopPropagation();
e.preventDefault();
});
@@ -317,12 +224,10 @@ openerp.base.ExternalActionManager = openerp.base.Controller.extend({
dialog.dialog({
title: action.name
});
var viewmanager = new openerp.base.ViewManagerAction(this.session
,element_id, action, false);
var viewmanager = new openerp.base.ViewManagerAction(this.session ,element_id, action, false);
viewmanager.start();
} else if (action.target == "current") {
this.rpc("/base/session/save_session_action", {the_action:action}, function(key) {
debugger;
var url = window.location.href;
//window.open();
});