1 /*---------------------------------------------------------
3 *--------------------------------------------------------*/
6 console = {log: function () {}};
9 console.debug = console.log;
12 openerp.web.core = function(openerp) {
13 openerp.web.qweb = new QWeb2.Engine();
14 openerp.web.qweb.debug = (window.location.search.indexOf('?debug') !== -1);
16 * John Resig Class with factory improvement
19 var initializing = false,
20 fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
21 // The web Class implementation (does nothing)
23 * Extended version of John Resig's Class pattern
27 openerp.web.Class = function(){};
30 * Subclass an existing class
32 * @param {Object} prop class-level properties (class attributes and instance methods) to set on the new class
34 openerp.web.Class.extend = function(prop) {
35 var _super = this.prototype;
37 // Instantiate a web class (but only create the instance,
38 // don't run the init constructor)
40 var prototype = new this();
43 // Copy the properties over onto the new prototype
44 for (var name in prop) {
45 // Check if we're overwriting an existing function
46 prototype[name] = typeof prop[name] == "function" &&
47 typeof _super[name] == "function" &&
48 fnTest.test(prop[name]) ?
51 var tmp = this._super;
53 // Add a new ._super() method that is the same
54 // method but on the super-class
55 this._super = _super[name];
57 // The method only need to be bound temporarily, so
58 // we remove it when we're done executing
59 var ret = fn.apply(this, arguments);
64 })(name, prop[name]) :
68 // The dummy class constructor
70 // All construction is actually done in the init method
71 if (!initializing && this.init) {
72 var ret = this.init.apply(this, arguments);
73 if (ret) { return ret; }
77 Class.include = function (properties) {
78 for (var name in properties) {
79 if (typeof properties[name] !== 'function'
80 || !fnTest.test(properties[name])) {
81 prototype[name] = properties[name];
82 } else if (typeof prototype[name] === 'function'
83 && prototype.hasOwnProperty(name)) {
84 prototype[name] = (function (name, fn, previous) {
86 var tmp = this._super;
87 this._super = previous;
88 var ret = fn.apply(this, arguments);
92 })(name, properties[name], prototype[name]);
93 } else if (typeof _super[name] === 'function') {
94 prototype[name] = (function (name, fn) {
96 var tmp = this._super;
97 this._super = _super[name];
98 var ret = fn.apply(this, arguments);
102 })(name, properties[name]);
107 // Populate our constructed prototype object
108 Class.prototype = prototype;
110 // Enforce the constructor to be what we expect
111 Class.constructor = Class;
113 // And make this class extendable
114 Class.extend = arguments.callee;
120 openerp.web.callback = function(obj, method) {
121 var callback = function() {
122 var args = Array.prototype.slice.call(arguments);
124 for(var i = 0; i < callback.callback_chain.length; i++) {
125 var c = callback.callback_chain[i];
127 callback.callback_chain.splice(i, 1);
130 r = c.callback.apply(c.self, c.args.concat(args));
131 // TODO special value to stop the chain
132 // openerp.web.callback_stop
136 callback.callback_chain = [];
137 callback.add = function(f) {
138 if(typeof(f) == 'function') {
139 f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
141 f.self = f.self || null;
142 f.args = f.args || [];
143 f.unique = !!f.unique;
144 if(f.position == 'last') {
145 callback.callback_chain.push(f);
147 callback.callback_chain.unshift(f);
151 callback.add_first = function(f) {
152 return callback.add.apply(null,arguments);
154 callback.add_last = function(f) {
155 return callback.add({
157 args: Array.prototype.slice.call(arguments, 1),
162 return callback.add({
165 args:Array.prototype.slice.call(arguments, 2)
170 * Generates an inherited class that replaces all the methods by null methods (methods
171 * that does nothing and always return undefined).
173 * @param {Class} claz
174 * @param {Object} add Additional functions to override.
177 openerp.web.generate_null_object_class = function(claz, add) {
179 var copy_proto = function(prototype) {
180 for (var name in prototype) {
181 if(typeof prototype[name] == "function") {
182 newer[name] = function() {};
185 if (prototype.prototype)
186 copy_proto(prototype.prototype);
188 copy_proto(claz.prototype);
189 newer.init = openerp.web.Widget.prototype.init;
190 var tmpclass = claz.extend(newer);
191 return tmpclass.extend(add || {});
195 * web error for lookup failure
199 openerp.web.NotFound = openerp.web.Class.extend( /** @lends openerp.web.NotFound# */ {
201 openerp.web.KeyNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.KeyNotFound# */ {
203 * Thrown when a key could not be found in a mapping
205 * @constructs openerp.web.KeyNotFound
206 * @extends openerp.web.NotFound
207 * @param {String} key the key which could not be found
209 init: function (key) {
212 toString: function () {
213 return "The key " + this.key + " was not found";
216 openerp.web.ObjectNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.ObjectNotFound# */ {
218 * Thrown when an object path does not designate a valid class or object
219 * in the openerp hierarchy.
221 * @constructs openerp.web.ObjectNotFound
222 * @extends openerp.web.NotFound
223 * @param {String} path the invalid object path
225 init: function (path) {
228 toString: function () {
229 return "Could not find any object of path " + this.path;
232 openerp.web.Registry = openerp.web.Class.extend( /** @lends openerp.web.Registry# */ {
234 * Stores a mapping of arbitrary key (strings) to object paths (as strings
237 * Resolves those paths at query time in order to always fetch the correct
238 * object, even if those objects have been overloaded/replaced after the
239 * registry was created.
241 * An object path is simply a dotted name from the openerp root to the
242 * object pointed to (e.g. ``"openerp.web.Session"`` for an OpenERP
245 * @constructs openerp.web.Registry
246 * @param {Object} mapping a mapping of keys to object-paths
248 init: function (mapping) {
249 this.map = mapping || {};
252 * Retrieves the object matching the provided key string.
254 * @param {String} key the key to fetch the object for
255 * @returns {Class} the stored class, to initialize
257 * @throws {openerp.web.KeyNotFound} if the object was not in the mapping
258 * @throws {openerp.web.ObjectNotFound} if the object path was invalid
260 get_object: function (key) {
261 var path_string = this.map[key];
262 if (path_string === undefined) {
263 throw new openerp.web.KeyNotFound(key);
266 var object_match = openerp;
267 var path = path_string.split('.');
268 // ignore first section
269 for(var i=1; i<path.length; ++i) {
270 object_match = object_match[path[i]];
272 if (object_match === undefined) {
273 throw new openerp.web.ObjectNotFound(path_string);
279 * Tries a number of keys, and returns the first object matching one of
282 * @param {Array} keys a sequence of keys to fetch the object for
283 * @returns {Class} the first class found matching an object
285 * @throws {openerp.web.KeyNotFound} if none of the keys was in the mapping
286 * @trows {openerp.web.ObjectNotFound} if a found object path was invalid
288 get_any: function (keys) {
289 for (var i=0; i<keys.length; ++i) {
291 if (key === undefined || !(key in this.map)) {
295 return this.get_object(key);
297 throw new openerp.web.KeyNotFound(keys.join(','));
300 * Adds a new key and value to the registry.
302 * This method can be chained.
304 * @param {String} key
305 * @param {String} object_path fully qualified dotted object path
306 * @returns {openerp.web.Registry} itself
308 add: function (key, object_path) {
309 this.map[key] = object_path;
313 * Creates and returns a copy of the current mapping, with the provided
314 * mapping argument added in (replacing existing keys if needed)
316 * @param {Object} [mapping={}] a mapping of keys to object-paths
318 clone: function (mapping) {
319 return new openerp.web.Registry(
320 _.extend({}, this.map, mapping || {}));
324 openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.CallbackEnabled# */{
326 * @constructs openerp.web.CallbackEnabled
327 * @extends openerp.web.Class
330 // Transform on_* method into openerp.web.callbacks
331 for (var name in this) {
332 if(typeof(this[name]) == "function") {
333 this[name].debug_name = name;
334 // bind ALL function to this not only on_and _do ?
335 if((/^on_|^do_/).test(name)) {
336 this[name] = openerp.web.callback(this, this[name]);
343 openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Session# */{
345 * @constructs openerp.web.Session
346 * @extends openerp.web.CallbackEnabled
348 * @param {String} [server] JSON-RPC endpoint hostname
349 * @param {String} [port] JSON-RPC endpoint port
351 init: function(server, port) {
353 this.server = (server == undefined) ? location.hostname : server;
354 this.port = (port == undefined) ? location.port : port;
355 this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp";
356 this.debug = (window.location.search.indexOf('?debug') !== -1);
357 this.session_id = false;
359 this.user_context= {};
361 this.module_list = [];
362 this.module_loaded = {"web": true};
365 this.active_id = null;
368 this.session_restore();
371 * Executes an RPC call, registering the provided callbacks.
373 * Registers a default error callback if none is provided, and handles
374 * setting the correct session id and session context in the parameter
377 * @param {String} url RPC endpoint
378 * @param {Object} params call parameters
379 * @param {Function} success_callback function to execute on RPC call success
380 * @param {Function} error_callback function to execute on RPC call failure
381 * @returns {jQuery.Deferred} jquery-provided ajax deferred
383 rpc: function(url, params, success_callback, error_callback) {
385 // Construct a JSON-RPC2 request, method is currently unused
386 params.session_id = this.session_id;
388 // Call using the rpc_mode
389 var deferred = $.Deferred();
394 id: _.uniqueId('browser-client-')
395 }).then(function () {deferred.resolve.apply(deferred, arguments);},
396 function(error) {deferred.reject(error, $.Event());});
397 return deferred.fail(function() {
398 deferred.fail(function(error, event) {
399 if (!event.isDefaultPrevented()) {
400 self.on_rpc_error(error, event);
403 }).then(success_callback, error_callback).promise();
408 * @returns {jQuery.Deferred} ajax-webd deferred object
410 rpc_ajax: function(url, payload) {
412 this.on_rpc_request();
413 // url can be an $.ajax option object
414 if (_.isString(url)) {
419 var ajax = _.extend({
423 contentType: 'application/json',
424 data: JSON.stringify(payload),
427 var deferred = $.Deferred();
428 $.ajax(ajax).done(function(response, textStatus, jqXHR) {
429 self.on_rpc_response();
430 if (!response.error) {
431 deferred.resolve(response["result"], textStatus, jqXHR);
434 if (response.error.data.type !== "session_invalid") {
435 deferred.reject(response.error);
439 self.on_session_invalid(function() {
440 self.rpc(url, payload.params,
442 deferred.resolve.apply(deferred, arguments);
444 function(error, event) {
445 event.preventDefault();
446 deferred.reject.apply(deferred, arguments);
449 }).fail(function(jqXHR, textStatus, errorThrown) {
450 self.on_rpc_response();
453 message: "XmlHttpRequestError " + errorThrown,
454 data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
456 deferred.reject(error);
458 return deferred.promise();
460 on_rpc_request: function() {
462 on_rpc_response: function() {
464 on_rpc_error: function(error) {
467 * The session is validated either by login or by restoration of a previous session
469 on_session_valid: function() {
470 if(!openerp._modules_loaded)
473 on_session_invalid: function(contination) {
475 session_is_valid: function() {
478 session_login: function(db, login, password, success_callback) {
480 var params = { db: db, login: login, password: password };
481 return this.rpc("/web/session/login", params, function(result) {
482 self.session_id = result.session_id;
483 self.uid = result.uid;
484 self.user_context = result.context;
488 }).then(success_callback);
491 * Reloads uid and session_id from local storage, if they exist
493 session_restore: function () {
495 this.session_id = this.get_cookie('session_id');
496 return this.rpc("/web/session/get_session_info", {}).then(function(result) {
497 self.uid = result.uid;
498 self.user_context = result.context;
501 self.on_session_valid();
503 self.on_session_invalid();
505 self.on_session_invalid();
509 * Saves the session id and uid locally
511 session_save: function () {
512 this.set_cookie('session_id', this.session_id);
515 this.set_cookie('session_id', '');
516 this.reload_client();
518 reload_client: function() {
519 window.location.reload();
522 * Fetches a cookie stored by an openerp session
525 * @param name the cookie's name
527 get_cookie: function (name) {
528 var nameEQ = this.element_id + '|' + name + '=';
529 var cookies = document.cookie.split(';');
530 for(var i=0; i<cookies.length; ++i) {
531 var cookie = cookies[i].replace(/^\s*/, '');
532 if(cookie.indexOf(nameEQ) === 0) {
533 return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length)));
539 * Create a new cookie with the provided name and value
542 * @param name the cookie's name
543 * @param value the cookie's value
544 * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
546 set_cookie: function (name, value, ttl) {
547 ttl = ttl || 24*60*60*365;
549 this.element_id + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)),
551 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
555 * Load additional web addons of that instance and init them
557 load_modules: function() {
559 this.rpc('/web/session/modules', {}, function(result) {
560 self.module_list = result;
561 var lang = self.user_context.lang;
562 var params = { mods: ["web"].concat(result), lang: lang};
563 self.rpc('/web/webclient/translations',params).then(function(transs) {
564 openerp.web._t.database.set_bundle(transs);
565 var modules = self.module_list.join(',');
566 var file_list = ["/web/static/lib/datejs/globalization/" +
567 self.user_context.lang.replace("_", "-") + ".js"
570 self.rpc('/web/webclient/csslist', {"mods": modules}, self.do_load_css);
571 self.rpc('/web/webclient/jslist', {"mods": modules}, function(files) {
572 self.do_load_js(file_list.concat(files));
575 self.do_load_css(["/web/webclient/css?mods="+modules]);
576 self.do_load_js(file_list.concat(["/web/webclient/js?mods="+modules]));
578 openerp._modules_loaded = true;
582 do_load_css: function (files) {
584 _.each(files, function (file) {
585 $('head').append($('<link>', {
586 'href': file + (self.debug ? '?debug=' + (new Date().getTime()) : ''),
592 do_load_js: function(files) {
594 if(files.length != 0) {
595 var file = files.shift();
596 var tag = document.createElement('script');
597 tag.type = 'text/javascript';
598 tag.src = file + (this.debug ? '?debug=' + (new Date().getTime()) : '');
599 tag.onload = tag.onreadystatechange = function() {
600 if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
602 tag.onload_done = true;
603 self.do_load_js(files);
605 document.head.appendChild(tag);
607 this.on_modules_loaded();
610 on_modules_loaded: function() {
611 for(var j=0; j<this.module_list.length; j++) {
612 var mod = this.module_list[j];
613 if(this.module_loaded[mod])
617 if(openerp._openerp[mod] != undefined) {
618 openerp._openerp[mod](openerp);
619 this.module_loaded[mod] = true;
624 * Cooperative file download implementation, for ajaxy APIs.
626 * Requires that the server side implements an httprequest correctly
627 * setting the `fileToken` cookie to the value provided as the `token`
628 * parameter. The cookie *must* be set on the `/` path and *must not* be
631 * It would probably also be a good idea for the response to use a
632 * `Content-Disposition: attachment` header, especially if the MIME is a
633 * "known" type (e.g. text/plain, or for some browsers application/json
635 * @param {Object} options
636 * @param {String} [options.url] used to dynamically create a form
637 * @param {Object} [options.data] data to add to the form submission. If can be used without a form, in which case a form is created from scratch. Otherwise, added to form data
638 * @param {HTMLFormElement} [options.form] the form to submit in order to fetch the file
639 * @param {Function} [options.success] callback in case of download success
640 * @param {Function} [options.error] callback in case of request error, provided with the error body
641 * @param {Function} [options.complete] called after both ``success`` and ``error` callbacks have executed
643 get_file: function (options) {
644 // need to detect when the file is done downloading (not used
645 // yet, but we'll need it to fix the UI e.g. with a throbber
646 // while dump is being generated), iframe load event only fires
647 // when the iframe content loads, so we need to go smarter:
648 // http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
649 var timer, token = new Date().getTime(),
650 cookie_name = 'fileToken', cookie_length = cookie_name.length,
651 CHECK_INTERVAL = 1000, id = _.uniqueId('get_file_frame'),
654 var $form, $form_data = $('<div>');
656 var complete = function () {
657 if (options.complete) { options.complete(); }
661 if (remove_form && $form) { $form.remove(); }
663 var $target = $('<iframe style="display: none;">')
664 .attr({id: id, name: id})
665 .appendTo(document.body)
667 if (options.error) { options.error(this.contentDocument.body); }
672 $form = $(options.form);
675 $form = $('<form>', {
678 }).appendTo(document.body);
681 _(_.extend({}, options.data || {},
682 {session_id: this.session_id, token: token}))
683 .each(function (value, key) {
684 $('<input type="hidden" name="' + key + '">')
686 .appendTo($form_data);
694 var waitLoop = function () {
695 var cookies = document.cookie.split(';');
697 timer = setTimeout(waitLoop, CHECK_INTERVAL);
698 for (var i=0; i<cookies.length; ++i) {
699 var cookie = cookies[i].replace(/^\s*/, '');
700 if (!cookie.indexOf(cookie_name === 0)) { continue; }
701 var cookie_val = cookie.substring(cookie_length + 1);
702 if (parseInt(cookie_val, 10) !== token) { continue; }
705 document.cookie = _.sprintf("%s=;expires=%s;path=/",
706 cookie_name, new Date().toGMTString());
707 if (options.success) { options.success(); }
712 timer = setTimeout(waitLoop, CHECK_INTERVAL);
716 openerp.web.SessionAware = openerp.web.CallbackEnabled.extend(/** @lends openerp.web.SessionAware# */{
718 * Utility class that any class is allowed to extend to easy common manipulations.
720 * It provides rpc calls, callback on all methods preceded by "on_" or "do_" and a
723 * @constructs openerp.web.SessionAware
724 * @extends openerp.web.CallbackEnabled
726 * @param {openerp.web.Session} session
728 init: function(session) {
730 this.session = session;
733 * Performs a JSON-RPC call
735 * @param {String} url endpoint url
736 * @param {Object} data RPC parameters
737 * @param {Function} success RPC call success callback
738 * @param {Function} error RPC call error callback
739 * @returns {jQuery.Deferred} deferred object for the RPC call
741 rpc: function(url, data, success, error) {
742 return this.session.rpc(url, data, success, error);
747 * Base class for all visual components. Provides a lot of functionalities helpful
748 * for the management of a part of the DOM.
751 * - Rendering with QWeb.
752 * - Life-cycle management and parenting (when a parent is destroyed, all its children are
754 * - Insertion in DOM.
756 * Widget also extends SessionAware for ease of use.
758 * Guide to create implementations of the Widget class:
759 * ==============================================
761 * Here is a sample child class:
763 * MyWidget = openerp.base.Widget.extend({
764 * // the name of the QWeb template to use for rendering
765 * template: "MyQWebTemplate",
766 * // identifier prefix, it is useful to put an obvious one for debugging
767 * identifier_prefix: 'my-id-prefix-',
769 * init: function(parent) {
770 * this._super(parent);
771 * // stuff that you want to init before the rendering
773 * start: function() {
774 * // stuff you want to make after the rendering, `this.$element` holds a correct value
775 * this.$element.find(".my_button").click(/* an example of event binding * /);
777 * // if you have some asynchronous operations, it's a good idea to return
778 * // a promise in start()
779 * var promise = this.rpc(...);
784 * Now this class can simply be used with the following syntax:
786 * var my_widget = new MyWidget(this);
787 * my_widget.appendTo($(".some-div"));
789 * With these two lines, the MyWidget instance was inited, rendered, it was inserted into the
790 * DOM inside the ".some-div" div and its events were binded.
792 * And of course, when you don't need that widget anymore, just do:
796 * That will kill the widget in a clean way and erase its content from the dom.
798 openerp.web.Widget = openerp.web.SessionAware.extend(/** @lends openerp.web.Widget# */{
800 * The name of the QWeb template that will be used for rendering. Must be
801 * redefined in subclasses or the default render() method can not be used.
807 * The prefix used to generate an id automatically. Should be redefined in
808 * subclasses. If it is not defined, a generic identifier will be used.
812 identifier_prefix: 'generic-identifier-',
814 * Construct the widget and set its parent if a parent is given.
816 * @constructs openerp.web.Widget
817 * @extends openerp.web.SessionAware
819 * @param {openerp.web.Widget} parent Binds the current instance to the given Widget instance.
820 * When that widget is destroyed by calling stop(), the current instance will be
821 * destroyed too. Can be null.
822 * @param {String} element_id Deprecated. Sets the element_id. Only useful when you want
823 * to bind the current Widget to an already existing part of the DOM, which is not compatible
824 * with the DOM insertion methods provided by the current implementation of Widget. So
825 * for new components this argument should not be provided any more.
827 init: function(parent, /** @deprecated */ element_id) {
828 this._super((parent || {}).session);
829 // if given an element_id, try to get the associated DOM element and save
830 // a reference in this.$element. Else just generate a unique identifier.
831 this.element_id = element_id;
832 this.element_id = this.element_id || _.uniqueId(this.identifier_prefix);
833 var tmp = document.getElementById(this.element_id);
834 this.$element = tmp ? $(tmp) : undefined;
836 this.widget_parent = parent;
837 this.widget_children = [];
838 if(parent && parent.widget_children) {
839 parent.widget_children.push(this);
841 // useful to know if the widget was destroyed and should not be used anymore
842 this.widget_is_stopped = false;
845 * Render the current widget and appends it to the given jQuery object or Widget.
847 * @param target A jQuery object or a Widget instance.
849 appendTo: function(target) {
851 return this._render_and_insert(function(t) {
852 self.$element.appendTo(t);
856 * Render the current widget and prepends it to the given jQuery object or Widget.
858 * @param target A jQuery object or a Widget instance.
860 prependTo: function(target) {
862 return this._render_and_insert(function(t) {
863 self.$element.prependTo(t);
867 * Render the current widget and inserts it after to the given jQuery object or Widget.
869 * @param target A jQuery object or a Widget instance.
871 insertAfter: function(target) {
873 return this._render_and_insert(function(t) {
874 self.$element.insertAfter(t);
878 * Render the current widget and inserts it before to the given jQuery object or Widget.
880 * @param target A jQuery object or a Widget instance.
882 insertBefore: function(target) {
884 return this._render_and_insert(function(t) {
885 self.$element.insertBefore(t);
888 _render_and_insert: function(insertion, target) {
889 var rendered = this.render();
890 this.$element = $(rendered);
891 if (target instanceof openerp.web.Widget)
892 target = target.$element;
894 this.on_inserted(this.$element, this);
897 on_inserted: function(element, widget) {},
899 * Renders the widget using QWeb, `this.template` must be defined.
900 * The context given to QWeb contains the "widget" key that references `this`.
902 * @param {Object} additional Additional context arguments to pass to the template.
904 render: function (additional) {
905 return openerp.web.qweb.render(this.template, _.extend({widget: this}, additional || {}));
908 * Method called after rendering. Mostly used to bind actions, perform asynchronous
911 * By convention, the method should return a promise to inform the caller when
912 * this widget has been initialized.
914 * @returns {jQuery.Deferred}
917 /* The default implementation is only useful for retro-compatibility, it is
918 not necessary to call it using _super() when using Widget for new components. */
919 if (!this.$element) {
920 var tmp = document.getElementById(this.element_id);
921 this.$element = tmp ? $(tmp) : undefined;
923 return $.Deferred().done().promise();
926 * Destroys the current widget, also destroy all its children before destroying itself.
929 _.each(_.clone(this.widget_children), function(el) {
932 if(this.$element != null) {
933 this.$element.remove();
935 if (this.widget_parent && this.widget_parent.widget_children) {
936 this.widget_parent.widget_children = _.without(this.widget_parent.widget_children, this);
938 this.widget_parent = null;
939 this.widget_is_stopped = true;
942 * Inform the action manager to do an action. Of course, this suppose that
943 * the action manager can be found amongst the ancestors of the current widget.
944 * If that's not the case this method will simply return `false`.
946 do_action: function(action, on_finished) {
947 if (this.widget_parent) {
948 return this.widget_parent.do_action(action, on_finished);
952 do_notify: function() {
953 if (this.widget_parent) {
954 return this.widget_parent.do_notify.apply(this,arguments);
958 do_warn: function() {
959 if (this.widget_parent) {
960 return this.widget_parent.do_warn.apply(this,arguments);
964 rpc: function(url, data, success, error) {
965 var def = $.Deferred().then(success, error);
967 this._super(url, data). then(function() {
968 if (!self.widget_is_stopped)
969 def.resolve.apply(def, arguments);
971 if (!self.widget_is_stopped)
972 def.reject.apply(def, arguments);
974 return def.promise();
980 * @extends openerp.web.Widget
982 * For retro compatibility only, the only difference with is that render() uses
983 * directly ``this`` instead of context with a ``widget`` key.
985 openerp.web.OldWidget = openerp.web.Widget.extend(/** @lends openerp.web.OldWidget# */{
986 render: function (additional) {
987 return openerp.web.qweb.render(this.template, _.extend(_.extend({}, this), additional || {}));
991 openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends openerp.web.TranslationDataBase# */{
993 * @constructs openerp.web.TranslationDataBase
994 * @extends openerp.web.Class
998 this.parameters = {"direction": 'ltr',
999 "date_format": '%m/%d/%Y',
1000 "time_format": '%H:%M:%S',
1002 "decimal_point": ".",
1003 "thousands_sep": ","};
1005 set_bundle: function(translation_bundle) {
1008 var modules = _.keys(translation_bundle.modules).sort();
1009 if (_.include(modules, "web")) {
1010 modules = ["web"].concat(_.without(modules, "web"));
1012 _.each(modules, function(name) {
1013 self.add_module_translation(translation_bundle.modules[name]);
1015 if (translation_bundle.lang_parameters) {
1016 this.parameters = translation_bundle.lang_parameters;
1019 add_module_translation: function(mod) {
1021 _.each(mod.messages, function(message) {
1022 if (self.db[message.id] === undefined) {
1023 self.db[message.id] = message.string;
1027 build_translation_function: function() {
1029 var fcnt = function(str) {
1030 var tmp = self.get(str);
1031 return tmp === undefined ? str : tmp;
1033 fcnt.database = this;
1036 get: function(key) {
1038 return this.db[key];
1043 openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function();
1047 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: