1 /*---------------------------------------------------------
2 * OpenERP controller framework
3 *--------------------------------------------------------*/
5 openerp.base.core = function(openerp) {
7 * John Resig Class with factory improvement
10 var initializing = false,
11 fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
12 // The base Class implementation (does nothing)
13 openerp.base.Class = function(){};
15 // Create a new Class that inherits from this class
16 openerp.base.Class.extend = function(prop) {
17 var _super = this.prototype;
19 // Instantiate a base class (but only create the instance,
20 // don't run the init constructor)
22 var prototype = new this();
25 // Copy the properties over onto the new prototype
26 for (var name in prop) {
27 // Check if we're overwriting an existing function
28 prototype[name] = typeof prop[name] == "function" &&
29 typeof _super[name] == "function" &&
30 fnTest.test(prop[name]) ?
33 var tmp = this._super;
35 // Add a new ._super() method that is the same
36 // method but on the super-class
37 this._super = _super[name];
39 // The method only need to be bound temporarily, so
40 // we remove it when we're done executing
41 var ret = fn.apply(this, arguments);
46 })(name, prop[name]) :
50 // The dummy class constructor
52 // All construction is actually done in the init method
53 if (!initializing && this.init) {
54 var ret = this.init.apply(this, arguments);
55 if (ret) { return ret; }
59 Class.include = function (properties) {
60 for (var name in properties) {
61 if (typeof properties[name] !== 'function'
62 || !fnTest.test(properties[name])) {
63 prototype[name] = properties[name];
64 } else if (typeof prototype[name] === 'function'
65 && prototype.hasOwnProperty(name)) {
66 prototype[name] = (function (name, fn, previous) {
68 var tmp = this._super;
69 this._super = previous;
70 var ret = fn.apply(this, arguments);
74 })(name, properties[name], prototype[name]);
75 } else if (typeof _super[name] === 'function') {
76 prototype[name] = (function (name, fn) {
78 var tmp = this._super;
79 this._super = _super[name];
80 var ret = fn.apply(this, arguments);
84 })(name, properties[name]);
89 // Populate our constructed prototype object
90 Class.prototype = prototype;
92 // Enforce the constructor to be what we expect
93 Class.constructor = Class;
95 // And make this class extendable
96 Class.extend = arguments.callee;
102 openerp.base.callback = function(obj, method) {
103 var callback = function() {
104 var args = Array.prototype.slice.call(arguments);
106 for(var i = 0; i < callback.callback_chain.length; i++) {
107 var c = callback.callback_chain[i];
109 callback.callback_chain.splice(i, 1);
112 r = c.callback.apply(c.self, c.args.concat(args));
113 // TODO special value to stop the chain
114 // openerp.base.callback_stop
118 callback.callback_chain = [];
119 callback.add = function(f) {
120 if(typeof(f) == 'function') {
121 f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
123 f.self = f.self || null;
124 f.args = f.args || [];
125 f.unique = !!f.unique;
126 if(f.position == 'last') {
127 callback.callback_chain.push(f);
129 callback.callback_chain.unshift(f);
133 callback.add_first = function(f) {
134 return callback.add.apply(null,arguments);
136 callback.add_last = function(f) {
137 return callback.add({
139 args: Array.prototype.slice.call(arguments, 1),
144 return callback.add({
147 args:Array.prototype.slice.call(arguments, 2)
152 * Generates an inherited class that replaces all the methods by null methods (methods
153 * that does nothing and always return undefined).
155 * @param {Class} claz
156 * @param {dict} add Additional functions to override.
159 openerp.base.generate_null_object_class = function(claz, add) {
161 var copy_proto = function(prototype) {
162 for (var name in prototype) {
163 if(typeof prototype[name] == "function") {
164 newer[name] = function() {};
167 if (prototype.prototype)
168 copy_proto(prototype.prototype);
170 copy_proto(claz.prototype);
171 newer.init = openerp.base.Widget.prototype.init;
172 var tmpclass = claz.extend(newer);
173 return tmpclass.extend(add || {});
177 * Base error for lookup failure
181 openerp.base.NotFound = openerp.base.Class.extend( /** @lends openerp.base.NotFound# */ {
183 openerp.base.KeyNotFound = openerp.base.NotFound.extend( /** @lends openerp.base.KeyNotFound# */ {
185 * Thrown when a key could not be found in a mapping
188 * @extends openerp.base.NotFound
189 * @param {String} key the key which could not be found
191 init: function (key) {
194 toString: function () {
195 return "The key " + this.key + " was not found";
198 openerp.base.ObjectNotFound = openerp.base.NotFound.extend( /** @lends openerp.base.ObjectNotFound# */ {
200 * Thrown when an object path does not designate a valid class or object
201 * in the openerp hierarchy.
204 * @extends openerp.base.NotFound
205 * @param {String} path the invalid object path
207 init: function (path) {
210 toString: function () {
211 return "Could not find any object of path " + this.path;
214 openerp.base.Registry = openerp.base.Class.extend( /** @lends openerp.base.Registry# */ {
216 * Stores a mapping of arbitrary key (strings) to object paths (as strings
219 * Resolves those paths at query time in order to always fetch the correct
220 * object, even if those objects have been overloaded/replaced after the
221 * registry was created.
223 * An object path is simply a dotted name from the openerp root to the
224 * object pointed to (e.g. ``"openerp.base.Session"`` for an OpenERP
228 * @param {Object} mapping a mapping of keys to object-paths
230 init: function (mapping) {
231 this.map = mapping || {};
234 * Retrieves the object matching the provided key string.
236 * @param {String} key the key to fetch the object for
237 * @returns {Class} the stored class, to initialize
239 * @throws {openerp.base.KeyNotFound} if the object was not in the mapping
240 * @throws {openerp.base.ObjectNotFound} if the object path was invalid
242 get_object: function (key) {
243 var path_string = this.map[key];
244 if (path_string === undefined) {
245 throw new openerp.base.KeyNotFound(key);
248 var object_match = openerp;
249 var path = path_string.split('.');
250 // ignore first section
251 for(var i=1; i<path.length; ++i) {
252 object_match = object_match[path[i]];
254 if (object_match === undefined) {
255 throw new openerp.base.ObjectNotFound(path_string);
261 * Tries a number of keys, and returns the first object matching one of
264 * @param {Array} keys a sequence of keys to fetch the object for
265 * @returns {Class} the first class found matching an object
267 * @throws {openerp.base.KeyNotFound} if none of the keys was in the mapping
268 * @trows {openerp.base.ObjectNotFound} if a found object path was invalid
270 get_any: function (keys) {
271 for (var i=0; i<keys.length; ++i) {
273 return this.get_object(keys[i]);
275 if (e instanceof openerp.base.KeyNotFound) {
281 throw new openerp.base.KeyNotFound(keys.join(','));
284 * Adds a new key and value to the registry.
286 * This method can be chained.
288 * @param {String} key
289 * @param {String} object_path fully qualified dotted object path
290 * @returns {openerp.base.Registry} itself
292 add: function (key, object_path) {
293 this.map[key] = object_path;
297 * Creates and returns a copy of the current mapping, with the provided
298 * mapping argument added in (replacing existing keys if needed)
300 * @param {Object} [mapping={}] a mapping of keys to object-paths
302 clone: function (mapping) {
303 return new openerp.base.Registry(
304 _.extend({}, this.map, mapping || {}));
309 * Utility class that any class is allowed to extend to easy common manipulations.
311 * It provides rpc calls, callback on all methods preceded by "on_" or "do_" and a
314 openerp.base.SessionAware = openerp.base.Class.extend({
315 init: function(session) {
316 this.session = session;
318 // Transform on_* method into openerp.base.callbacks
319 for (var name in this) {
320 if(typeof(this[name]) == "function") {
321 this[name].debug_name = name;
322 // bind ALL function to this not only on_and _do ?
323 if((/^on_|^do_/).test(name)) {
324 this[name] = openerp.base.callback(this, this[name]);
330 * Performs a JSON-RPC call
332 * @param {String} url endpoint url
333 * @param {Object} data RPC parameters
334 * @param {Function} success RPC call success callback
335 * @param {Function} error RPC call error callback
336 * @returns {jQuery.Deferred} deferred object for the RPC call
338 rpc: function(url, data, success, error) {
339 return this.session.rpc(url, data, success, error);
342 var args = Array.prototype.slice.call(arguments);
343 var caller = arguments.callee.caller;
344 // TODO add support for line number using
345 // https://github.com/emwendelin/javascript-stacktrace/blob/master/stacktrace.js
346 // args.unshift("" + caller.debug_name);
347 this.on_log.apply(this,args);
350 if(window.openerp.debug || (window.location.search.indexOf('?debug') !== -1)) {
354 console.log(arguments);
358 var a = Array.prototype.slice.call(arguments, 0);
359 for(var i = 0; i < a.length; i++) {
360 var v = a[i]==null ? "null" : a[i].toString();
362 notify = v.match(/^not/);
363 body = v.match(/^bod/);
366 $('<pre></pre>').text(v).appendTo($('body'));
368 if(notify && this.notification) {
369 this.notification.notify("Logging:",v);
377 * Base class for all visual components. Provides a lot of functionalities helpful
378 * for the management of a part of the DOM.
381 * - Rendering with QWeb.
382 * - Life-cycle management and parenting (when a parent is destroyed, all its children are
384 * - Insertion in DOM.
386 * Widget also extends SessionAware for ease of use.
388 * Guide to create implementations of the Widget class:
389 * ==============================================
391 * Here is a sample child class:
393 * MyWidget = openerp.base.Widget.extend({
394 * // the name of the QWeb template to use for rendering
395 * template: "MyQWebTemplate",
396 * // identifier prefix, it is useful to put an obvious one for debugging
397 * identifier_prefix: 'my-id-prefix-',
399 * init: function(parent) {
400 * this._super(parent);
401 * // stuff that you want to init before the rendering
403 * start: function() {
405 * // stuff you want to make after the rendering, `this.$element` holds a correct value
406 * this.$element.find(".my_button").click(/* an example of event binding * /);
408 * // if you have some asynchronous operations, it's a good idea to return
409 * // a promise in start()
410 * var promise = this.rpc(...);
415 * Now this class can simply be used with the following syntax:
417 * var my_widget = new MyWidget(this);
418 * my_widget.appendTo($(".some-div"));
420 * With these two lines, the MyWidget instance was inited, rendered, it was inserted into the
421 * DOM inside the ".some-div" div and its events were binded.
423 * And of course, when you don't need that widget anymore, just do:
427 * That will kill the widget in a clean way and erase its content from the dom.
429 openerp.base.Widget = openerp.base.SessionAware.extend({
431 * The name of the QWeb template that will be used for rendering. Must be
432 * redefined in subclasses or the default render() method can not be used.
438 * The prefix used to generate an id automatically. Should be redefined in
439 * subclasses. If it is not defined, a generic identifier will be used.
443 identifier_prefix: 'generic-identifier-',
445 * Construct the widget and set its parent if a parent is given.
448 * @param {openerp.base.Widget} parent Binds the current instance to the given Widget instance.
449 * When that widget is destroyed by calling stop(), the current instance will be
450 * destroyed too. Can be null.
451 * @param {String} element_id Deprecated. Sets the element_id. Only useful when you want
452 * to bind the current Widget to an already existing part of the DOM, which is not compatible
453 * with the DOM insertion methods provided by the current implementation of Widget. So
454 * for new components this argument should not be provided any more.
456 init: function(parent, /** @deprecated */ element_id) {
457 this._super((parent || {}).session);
458 // if given an element_id, try to get the associated DOM element and save
459 // a reference in this.$element. Else just generate a unique identifier.
460 this.element_id = element_id;
461 this.element_id = this.element_id || _.uniqueId(this.identifier_prefix);
462 var tmp = document.getElementById(this.element_id);
463 this.$element = tmp ? $(tmp) : undefined;
465 this.widget_parent = parent;
466 this.widget_children = [];
467 if(parent && parent.widget_children) {
468 parent.widget_children.push(this);
470 // useful to know if the widget was destroyed and should not be used anymore
471 this.widget_is_stopped = false;
474 * Render the current widget and appends it to the given jQuery object or Widget.
476 * @param target A jQuery object or a Widget instance.
478 appendTo: function(target) {
480 return this._render_and_insert(function(t) {
481 self.$element.appendTo(t);
485 * Render the current widget and prepends it to the given jQuery object or Widget.
487 * @param target A jQuery object or a Widget instance.
489 prependTo: function(target) {
491 return this._render_and_insert(function(t) {
492 self.$element.prependTo(t);
496 * Render the current widget and inserts it after to the given jQuery object or Widget.
498 * @param target A jQuery object or a Widget instance.
500 insertAfter: function(target) {
502 return this._render_and_insert(function(t) {
503 self.$element.insertAfter(t);
507 * Render the current widget and inserts it before to the given jQuery object or Widget.
509 * @param target A jQuery object or a Widget instance.
511 insertBefore: function(target) {
513 return this._render_and_insert(function(t) {
514 self.$element.insertBefore(t);
517 _render_and_insert: function(insertion, target) {
518 var rendered = this.render();
519 this.$element = $(rendered);
520 if (target instanceof openerp.base.Widget)
521 target = target.$element;
526 * Renders the widget using QWeb, `this.template` must be defined.
527 * The context given to QWeb contains the "widget" key that references `this`.
529 * @param {Object} additional Additional context arguments to pass to the template.
531 render: function (additional) {
532 return QWeb.render(this.template, _.extend({widget: this}, additional || {}));
535 * Method called after rendering. Mostly used to bind actions, perform asynchronous
538 * By convention, the method should return a promise to inform the caller when
539 * this widget has been initialized.
541 * @returns {jQuery.Deferred}
544 if (!this.$element) {
545 var tmp = document.getElementById(this.element_id);
546 this.$element = tmp ? $(tmp) : undefined;
548 return $.Deferred().done().promise();
551 * Destroys the current widget, also destory all its children before destroying itself.
554 _.each(_.clone(this.widget_children), function(el) {
557 if(this.$element != null) {
558 this.$element.remove();
560 if (this.widget_parent && this.widget_parent.widget_children) {
561 this.widget_parent.widget_children = _.without(this.widget_parent.widget_children, this);
563 this.widget_parent = null;
564 this.widget_is_stopped = true;
567 * Inform the action manager to do an action. Of course, this suppose that
568 * the action manager can be found amongst the ancestors of the current widget.
569 * If that's not the case this method will simply return `false`.
571 do_action: function(action, on_finished) {
572 if (this.widget_parent) {
573 return this.widget_parent.do_action(action, on_finished);
577 do_notify: function() {
578 if (this.widget_parent) {
579 return this.widget_parent.do_notify.apply(this,arguments);
583 do_warn: function() {
584 if (this.widget_parent) {
585 return this.widget_parent.do_warn.apply(this,arguments);
589 rpc: function(url, data, success, error) {
590 var def = $.Deferred().then(success, error);
592 this._super(url, data). then(function() {
593 if (!self.widget_is_stopped)
594 def.resolve.apply(def, arguments);
596 if (!self.widget_is_stopped)
597 def.reject.apply(def, arguments);
599 return def.promise();
605 * For retro compatibility only, the only difference with is that render() uses
606 * directly `this` instead of context with a "widget" key.
608 openerp.base.OldWidget = openerp.base.Widget.extend({
609 render: function (additional) {
610 return QWeb.render(this.template, _.extend(_.extend({}, this), additional || {}));
614 openerp.base.Session = openerp.base.Widget.extend( /** @lends openerp.base.Session# */{
617 * @param element_id to use for exception reporting
621 init: function(parent, element_id, server, port) {
622 this._super(parent, element_id);
623 this.server = (server == undefined) ? location.hostname : server;
624 this.port = (port == undefined) ? location.port : port;
625 this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp";
631 this.session_id = false;
632 this.module_list = [];
633 this.module_loaded = {"base": true};
637 this.session_restore();
640 * Executes an RPC call, registering the provided callbacks.
642 * Registers a default error callback if none is provided, and handles
643 * setting the correct session id and session context in the parameter
646 * @param {String} url RPC endpoint
647 * @param {Object} params call parameters
648 * @param {Function} success_callback function to execute on RPC call success
649 * @param {Function} error_callback function to execute on RPC call failure
650 * @returns {jQuery.Deferred} jquery-provided ajax deferred
652 rpc: function(url, params, success_callback, error_callback) {
654 // Construct a JSON-RPC2 request, method is currently unused
655 params.session_id = this.session_id;
657 // Call using the rpc_mode
658 var deferred = $.Deferred();
664 }).then(function () {deferred.resolve.apply(deferred, arguments);},
665 function(error) {deferred.reject(error, $.Event());});
666 return deferred.fail(function() {
667 deferred.fail(function(error, event) {
668 if (!event.isDefaultPrevented()) {
669 self.on_rpc_error(error, event);
672 }).then(success_callback, error_callback).promise();
677 * @returns {jQuery.Deferred} ajax-based deferred object
679 rpc_ajax: function(url, payload) {
681 this.on_rpc_request();
682 // url can be an $.ajax option object
683 if (_.isString(url)) {
688 var ajax = _.extend({
692 contentType: 'application/json',
693 data: JSON.stringify(payload),
696 var deferred = $.Deferred();
697 $.ajax(ajax).done(function(response, textStatus, jqXHR) {
698 self.on_rpc_response();
699 if (!response.error) {
700 deferred.resolve(response["result"], textStatus, jqXHR);
703 if (response.error.data.type !== "session_invalid") {
704 deferred.reject(response.error);
708 self.on_session_invalid(function() {
709 self.rpc(url, payload.params,
711 deferred.resolve.apply(deferred, arguments);
713 function(error, event) {
714 event.preventDefault();
715 deferred.reject.apply(deferred, arguments);
718 }).fail(function(jqXHR, textStatus, errorThrown) {
719 self.on_rpc_response();
722 message: "XmlHttpRequestError " + errorThrown,
723 data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
725 deferred.reject(error);
727 return deferred.promise();
729 on_rpc_request: function() {
731 on_rpc_response: function() {
733 on_rpc_error: function(error) {
736 * The session is validated either by login or by restoration of a previous session
738 on_session_valid: function() {
739 if(!openerp._modules_loaded)
742 on_session_invalid: function(contination) {
744 session_is_valid: function() {
747 session_login: function(db, login, password, success_callback) {
751 this.password = password;
752 var params = { db: this.db, login: this.login, password: this.password };
753 this.rpc("/base/session/login", params, function(result) {
754 self.session_id = result.session_id;
755 self.uid = result.uid;
757 self.on_session_valid();
758 if (success_callback)
762 session_logout: function() {
766 * Reloads uid and session_id from local storage, if they exist
768 session_restore: function () {
769 this.uid = this.get_cookie('uid');
770 this.session_id = this.get_cookie('session_id');
771 this.db = this.get_cookie('db');
772 this.login = this.get_cookie('login');
773 // we should do an rpc to confirm that this session_id is valid and if it is retrieve the information about db and login
774 // then call on_session_valid
775 this.on_session_valid();
778 * Saves the session id and uid locally
780 session_save: function () {
781 this.set_cookie('uid', this.uid);
782 this.set_cookie('session_id', this.session_id);
783 this.set_cookie('db', this.db);
784 this.set_cookie('login', this.login);
788 delete this.session_id;
791 this.set_cookie('uid', '');
792 this.set_cookie('session_id', '');
793 this.set_cookie('db', '');
794 this.set_cookie('login', '');
795 this.on_session_invalid(function() {});
798 * Fetches a cookie stored by an openerp session
801 * @param name the cookie's name
803 get_cookie: function (name) {
804 var nameEQ = this.element_id + '|' + name + '=';
805 var cookies = document.cookie.split(';');
806 for(var i=0; i<cookies.length; ++i) {
807 var cookie = cookies[i].replace(/^\s*/, '');
808 if(cookie.indexOf(nameEQ) === 0) {
809 return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length)));
815 * Create a new cookie with the provided name and value
818 * @param name the cookie's name
819 * @param value the cookie's value
820 * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
822 set_cookie: function (name, value, ttl) {
823 ttl = ttl || 24*60*60*365;
825 this.element_id + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)),
827 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
831 * Load additional web addons of that instance and init them
833 load_modules: function() {
835 this.rpc('/base/session/modules', {}, function(result) {
836 self.module_list = result;
837 var modules = self.module_list.join(',');
838 if(self.debug || true) {
839 self.rpc('/base/webclient/csslist', {"mods": modules}, self.do_load_css);
840 self.rpc('/base/webclient/jslist', {"mods": modules}, self.do_load_js);
842 self.do_load_css(["/base/webclient/css?mods="+modules]);
843 self.do_load_js(["/base/webclient/js?mods="+modules]);
845 openerp._modules_loaded = true;
848 do_load_css: function (files) {
849 _.each(files, function (file) {
850 $('head').append($('<link>', {
857 do_load_js: function(files) {
859 if(files.length != 0) {
860 var file = files.shift();
861 var tag = document.createElement('script');
862 tag.type = 'text/javascript';
864 tag.onload = tag.onreadystatechange = function() {
865 if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
867 tag.onload_done = true;
868 self.do_load_js(files);
870 document.head.appendChild(tag);
872 this.on_modules_loaded();
875 on_modules_loaded: function() {
876 for(var j=0; j<this.module_list.length; j++) {
877 var mod = this.module_list[j];
878 if(this.module_loaded[mod])
882 if(openerp._openerp[mod] != undefined) {
883 openerp._openerp[mod](openerp);
884 this.module_loaded[mod] = true;
891 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: