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 // This should NOT be used, like callbackenable it's too hackish not enough javasish
78 Class.include = function (properties) {
79 for (var name in properties) {
80 if (typeof properties[name] !== 'function'
81 || !fnTest.test(properties[name])) {
82 prototype[name] = properties[name];
83 } else if (typeof prototype[name] === 'function'
84 && prototype.hasOwnProperty(name)) {
85 prototype[name] = (function (name, fn, previous) {
87 var tmp = this._super;
88 this._super = previous;
89 var ret = fn.apply(this, arguments);
93 })(name, properties[name], prototype[name]);
94 } else if (typeof _super[name] === 'function') {
95 prototype[name] = (function (name, fn) {
97 var tmp = this._super;
98 this._super = _super[name];
99 var ret = fn.apply(this, arguments);
103 })(name, properties[name]);
108 // Populate our constructed prototype object
109 Class.prototype = prototype;
111 // Enforce the constructor to be what we expect
112 Class.constructor = Class;
114 // And make this class extendable
115 Class.extend = arguments.callee;
121 openerp.web.callback = function(obj, method) {
122 var callback = function() {
123 var args = Array.prototype.slice.call(arguments);
125 for(var i = 0; i < callback.callback_chain.length; i++) {
126 var c = callback.callback_chain[i];
128 callback.callback_chain.splice(i, 1);
131 r = c.callback.apply(c.self, c.args.concat(args));
132 // TODO special value to stop the chain
133 // openerp.web.callback_stop
137 callback.callback_chain = [];
138 callback.add = function(f) {
139 if(typeof(f) == 'function') {
140 f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
142 f.self = f.self || null;
143 f.args = f.args || [];
144 f.unique = !!f.unique;
145 if(f.position == 'last') {
146 callback.callback_chain.push(f);
148 callback.callback_chain.unshift(f);
152 callback.add_first = function(f) {
153 return callback.add.apply(null,arguments);
155 callback.add_last = function(f) {
156 return callback.add({
158 args: Array.prototype.slice.call(arguments, 1),
163 return callback.add({
166 args:Array.prototype.slice.call(arguments, 2)
171 * Generates an inherited class that replaces all the methods by null methods (methods
172 * that does nothing and always return undefined).
174 * @param {Class} claz
175 * @param {Object} add Additional functions to override.
178 openerp.web.generate_null_object_class = function(claz, add) {
180 var copy_proto = function(prototype) {
181 for (var name in prototype) {
182 if(typeof prototype[name] == "function") {
183 newer[name] = function() {};
186 if (prototype.prototype)
187 copy_proto(prototype.prototype);
189 copy_proto(claz.prototype);
190 newer.init = openerp.web.Widget.prototype.init;
191 var tmpclass = claz.extend(newer);
192 return tmpclass.extend(add || {});
196 * web error for lookup failure
200 openerp.web.NotFound = openerp.web.Class.extend( /** @lends openerp.web.NotFound# */ {
202 openerp.web.KeyNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.KeyNotFound# */ {
204 * Thrown when a key could not be found in a mapping
206 * @constructs openerp.web.KeyNotFound
207 * @extends openerp.web.NotFound
208 * @param {String} key the key which could not be found
210 init: function (key) {
213 toString: function () {
214 return "The key " + this.key + " was not found";
217 openerp.web.ObjectNotFound = openerp.web.NotFound.extend( /** @lends openerp.web.ObjectNotFound# */ {
219 * Thrown when an object path does not designate a valid class or object
220 * in the openerp hierarchy.
222 * @constructs openerp.web.ObjectNotFound
223 * @extends openerp.web.NotFound
224 * @param {String} path the invalid object path
226 init: function (path) {
229 toString: function () {
230 return "Could not find any object of path " + this.path;
233 openerp.web.Registry = openerp.web.Class.extend( /** @lends openerp.web.Registry# */ {
235 * Stores a mapping of arbitrary key (strings) to object paths (as strings
238 * Resolves those paths at query time in order to always fetch the correct
239 * object, even if those objects have been overloaded/replaced after the
240 * registry was created.
242 * An object path is simply a dotted name from the openerp root to the
243 * object pointed to (e.g. ``"openerp.web.Session"`` for an OpenERP
246 * @constructs openerp.web.Registry
247 * @param {Object} mapping a mapping of keys to object-paths
249 init: function (mapping) {
250 this.map = mapping || {};
253 * Retrieves the object matching the provided key string.
255 * @param {String} key the key to fetch the object for
256 * @returns {Class} the stored class, to initialize
258 * @throws {openerp.web.KeyNotFound} if the object was not in the mapping
259 * @throws {openerp.web.ObjectNotFound} if the object path was invalid
261 get_object: function (key) {
262 var path_string = this.map[key];
263 if (path_string === undefined) {
264 throw new openerp.web.KeyNotFound(key);
267 var object_match = openerp;
268 var path = path_string.split('.');
269 // ignore first section
270 for(var i=1; i<path.length; ++i) {
271 object_match = object_match[path[i]];
273 if (object_match === undefined) {
274 throw new openerp.web.ObjectNotFound(path_string);
280 * Tries a number of keys, and returns the first object matching one of
283 * @param {Array} keys a sequence of keys to fetch the object for
284 * @returns {Class} the first class found matching an object
286 * @throws {openerp.web.KeyNotFound} if none of the keys was in the mapping
287 * @trows {openerp.web.ObjectNotFound} if a found object path was invalid
289 get_any: function (keys) {
290 for (var i=0; i<keys.length; ++i) {
292 return this.get_object(keys[i]);
294 if (e instanceof openerp.web.KeyNotFound) {
300 throw new openerp.web.KeyNotFound(keys.join(','));
303 * Adds a new key and value to the registry.
305 * This method can be chained.
307 * @param {String} key
308 * @param {String} object_path fully qualified dotted object path
309 * @returns {openerp.web.Registry} itself
311 add: function (key, object_path) {
312 this.map[key] = object_path;
316 * Creates and returns a copy of the current mapping, with the provided
317 * mapping argument added in (replacing existing keys if needed)
319 * @param {Object} [mapping={}] a mapping of keys to object-paths
321 clone: function (mapping) {
322 return new openerp.web.Registry(
323 _.extend({}, this.map, mapping || {}));
327 openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.CallbackEnabled# */{
329 * @constructs openerp.web.CallbackEnabled
330 * @extends openerp.web.Class
333 // Transform on_* method into openerp.web.callbacks
334 for (var name in this) {
335 if(typeof(this[name]) == "function") {
336 this[name].debug_name = name;
337 // bind ALL function to this not only on_and _do ?
338 if((/^on_|^do_/).test(name)) {
339 this[name] = openerp.web.callback(this, this[name]);
346 openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Session# */{
348 * @constructs openerp.web.Session
349 * @extends openerp.web.CallbackEnabled
351 * @param {String} [server] JSON-RPC endpoint hostname
352 * @param {String} [port] JSON-RPC endpoint port
354 init: function(server, port) {
356 this.server = (server == undefined) ? location.hostname : server;
357 this.port = (port == undefined) ? location.port : port;
358 this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp";
359 this.debug = (window.location.search.indexOf('?debug') !== -1);
360 this.session_id = false;
362 this.user_context= {};
364 this.module_list = [];
365 this.module_loaded = {"web": true};
368 this.active_id = null;
371 this.session_restore();
374 * Executes an RPC call, registering the provided callbacks.
376 * Registers a default error callback if none is provided, and handles
377 * setting the correct session id and session context in the parameter
380 * @param {String} url RPC endpoint
381 * @param {Object} params call parameters
382 * @param {Function} success_callback function to execute on RPC call success
383 * @param {Function} error_callback function to execute on RPC call failure
384 * @returns {jQuery.Deferred} jquery-provided ajax deferred
386 rpc: function(url, params, success_callback, error_callback) {
388 // Construct a JSON-RPC2 request, method is currently unused
389 params.session_id = this.session_id;
391 // Call using the rpc_mode
392 var deferred = $.Deferred();
397 id: _.uniqueId('browser-client-')
398 }).then(function () {deferred.resolve.apply(deferred, arguments);},
399 function(error) {deferred.reject(error, $.Event());});
400 return deferred.fail(function() {
401 deferred.fail(function(error, event) {
402 if (!event.isDefaultPrevented()) {
403 self.on_rpc_error(error, event);
406 }).then(success_callback, error_callback).promise();
411 * @returns {jQuery.Deferred} ajax-webd deferred object
413 rpc_ajax: function(url, payload) {
415 this.on_rpc_request();
416 // url can be an $.ajax option object
417 if (_.isString(url)) {
422 var ajax = _.extend({
426 contentType: 'application/json',
427 data: JSON.stringify(payload),
430 var deferred = $.Deferred();
431 $.ajax(ajax).done(function(response, textStatus, jqXHR) {
432 self.on_rpc_response();
433 if (!response.error) {
434 deferred.resolve(response["result"], textStatus, jqXHR);
437 if (response.error.data.type !== "session_invalid") {
438 deferred.reject(response.error);
442 self.on_session_invalid(function() {
443 self.rpc(url, payload.params,
445 deferred.resolve.apply(deferred, arguments);
447 function(error, event) {
448 event.preventDefault();
449 deferred.reject.apply(deferred, arguments);
452 }).fail(function(jqXHR, textStatus, errorThrown) {
453 self.on_rpc_response();
456 message: "XmlHttpRequestError " + errorThrown,
457 data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
459 deferred.reject(error);
461 return deferred.promise();
463 on_rpc_request: function() {
465 on_rpc_response: function() {
467 on_rpc_error: function(error) {
470 * The session is validated either by login or by restoration of a previous session
472 on_session_valid: function() {
473 if(!openerp._modules_loaded)
476 on_session_invalid: function(contination) {
478 session_is_valid: function() {
481 session_login: function(db, login, password, success_callback) {
483 var params = { db: db, login: login, password: password };
484 return this.rpc("/web/session/login", params, function(result) {
485 self.session_id = result.session_id;
486 self.uid = result.uid;
487 self.user_context = result.context;
491 }).then(success_callback);
494 * Reloads uid and session_id from local storage, if they exist
496 session_restore: function () {
498 this.session_id = this.get_cookie('session_id');
499 return this.rpc("/web/session/get_session_info", {}).then(function(result) {
500 self.uid = result.uid;
501 self.user_context = result.context;
504 self.on_session_valid();
506 self.on_session_invalid();
508 self.on_session_invalid();
512 * Saves the session id and uid locally
514 session_save: function () {
515 this.set_cookie('session_id', this.session_id);
518 this.set_cookie('session_id', '');
519 this.reload_client();
521 reload_client: function() {
522 window.location.reload();
525 * Fetches a cookie stored by an openerp session
528 * @param name the cookie's name
530 get_cookie: function (name) {
531 var nameEQ = this.element_id + '|' + name + '=';
532 var cookies = document.cookie.split(';');
533 for(var i=0; i<cookies.length; ++i) {
534 var cookie = cookies[i].replace(/^\s*/, '');
535 if(cookie.indexOf(nameEQ) === 0) {
536 return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length)));
542 * Create a new cookie with the provided name and value
545 * @param name the cookie's name
546 * @param value the cookie's value
547 * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
549 set_cookie: function (name, value, ttl) {
550 ttl = ttl || 24*60*60*365;
552 this.element_id + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)),
554 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
558 * Load additional web addons of that instance and init them
560 load_modules: function() {
562 this.rpc('/web/session/modules', {}, function(result) {
563 self.module_list = result;
564 var lang = self.user_context.lang;
565 var params = { mods: ["web"].concat(result), lang: lang};
566 self.rpc('/web/webclient/translations',params).then(function(transs) {
567 openerp.web._t.database.set_bundle(transs);
568 var modules = self.module_list.join(',');
569 var file_list = ["/web/static/lib/datejs/globalization/" +
570 self.user_context.lang.replace("_", "-") + ".js"
573 self.rpc('/web/webclient/csslist', {"mods": modules}, self.do_load_css);
574 self.rpc('/web/webclient/jslist', {"mods": modules}, function(files) {
575 self.do_load_js(file_list.concat(files));
578 self.do_load_css(["/web/webclient/css?mods="+modules]);
579 self.do_load_js(file_list.concat(["/web/webclient/js?mods="+modules]));
581 openerp._modules_loaded = true;
585 do_load_css: function (files) {
587 _.each(files, function (file) {
588 $('head').append($('<link>', {
589 'href': file + (self.debug ? '?debug=' + (new Date().getTime()) : ''),
595 do_load_js: function(files) {
597 if(files.length != 0) {
598 var file = files.shift();
599 var tag = document.createElement('script');
600 tag.type = 'text/javascript';
601 tag.src = file + (this.debug ? '?debug=' + (new Date().getTime()) : '');
602 tag.onload = tag.onreadystatechange = function() {
603 if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
605 tag.onload_done = true;
606 self.do_load_js(files);
608 $('head').append(tag);
609 self.do_load_js(files);
611 this.on_modules_loaded();
614 on_modules_loaded: function() {
615 for(var j=0; j<this.module_list.length; j++) {
616 var mod = this.module_list[j];
617 if(this.module_loaded[mod])
621 if(openerp._openerp[mod] != undefined) {
622 openerp._openerp[mod](openerp);
623 this.module_loaded[mod] = true;
628 * Cooperative file download implementation, for ajaxy APIs.
630 * Requires that the server side implements an httprequest correctly
631 * setting the `fileToken` cookie to the value provided as the `token`
632 * parameter. The cookie *must* be set on the `/` path and *must not* be
635 * It would probably also be a good idea for the response to use a
636 * `Content-Disposition: attachment` header, especially if the MIME is a
637 * "known" type (e.g. text/plain, or for some browsers application/json
639 * @param {Object} options
640 * @param {String} [options.url] used to dynamically create a form
641 * @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
642 * @param {HTMLFormElement} [options.form] the form to submit in order to fetch the file
643 * @param {Function} [options.success] callback in case of download success
644 * @param {Function} [options.error] callback in case of request error, provided with the error body
645 * @param {Function} [options.complete] called after both ``success`` and ``error` callbacks have executed
647 get_file: function (options) {
648 // need to detect when the file is done downloading (not used
649 // yet, but we'll need it to fix the UI e.g. with a throbber
650 // while dump is being generated), iframe load event only fires
651 // when the iframe content loads, so we need to go smarter:
652 // http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
653 var timer, token = new Date().getTime(),
654 cookie_name = 'fileToken', cookie_length = cookie_name.length,
655 CHECK_INTERVAL = 1000, id = _.uniqueId('get_file_frame'),
658 var $form, $form_data = $('<div>');
660 var complete = function () {
661 if (options.complete) { options.complete(); }
665 if (remove_form && $form) { $form.remove(); }
667 var $target = $('<iframe style="display: none;">')
668 .attr({id: id, name: id})
669 .appendTo(document.body)
671 if (options.error) { options.error(this.contentDocument.body); }
676 $form = $(options.form);
679 $form = $('<form>', {
682 }).appendTo(document.body);
685 _(_.extend({}, options.data || {},
686 {session_id: this.session_id, token: token}))
687 .each(function (value, key) {
688 $('<input type="hidden" name="' + key + '">')
690 .appendTo($form_data);
698 var waitLoop = function () {
699 var cookies = document.cookie.split(';');
701 timer = setTimeout(waitLoop, CHECK_INTERVAL);
702 for (var i=0; i<cookies.length; ++i) {
703 var cookie = cookies[i].replace(/^\s*/, '');
704 if (!cookie.indexOf(cookie_name === 0)) { continue; }
705 var cookie_val = cookie.substring(cookie_length + 1);
706 if (parseInt(cookie_val, 10) !== token) { continue; }
709 document.cookie = _.sprintf("%s=;expires=%s;path=/",
710 cookie_name, new Date().toGMTString());
711 if (options.success) { options.success(); }
716 timer = setTimeout(waitLoop, CHECK_INTERVAL);
720 openerp.web.SessionAware = openerp.web.CallbackEnabled.extend(/** @lends openerp.web.SessionAware# */{
722 * Utility class that any class is allowed to extend to easy common manipulations.
724 * It provides rpc calls, callback on all methods preceded by "on_" or "do_" and a
727 * @constructs openerp.web.SessionAware
728 * @extends openerp.web.CallbackEnabled
730 * @param {openerp.web.Session} session
732 init: function(session) {
734 this.session = session;
737 * Performs a JSON-RPC call
739 * @param {String} url endpoint url
740 * @param {Object} data RPC parameters
741 * @param {Function} success RPC call success callback
742 * @param {Function} error RPC call error callback
743 * @returns {jQuery.Deferred} deferred object for the RPC call
745 rpc: function(url, data, success, error) {
746 return this.session.rpc(url, data, success, error);
751 * Base class for all visual components. Provides a lot of functionalities helpful
752 * for the management of a part of the DOM.
755 * - Rendering with QWeb.
756 * - Life-cycle management and parenting (when a parent is destroyed, all its children are
758 * - Insertion in DOM.
760 * Widget also extends SessionAware for ease of use.
762 * Guide to create implementations of the Widget class:
763 * ==============================================
765 * Here is a sample child class:
767 * MyWidget = openerp.base.Widget.extend({
768 * // the name of the QWeb template to use for rendering
769 * template: "MyQWebTemplate",
770 * // identifier prefix, it is useful to put an obvious one for debugging
771 * identifier_prefix: 'my-id-prefix-',
773 * init: function(parent) {
774 * this._super(parent);
775 * // stuff that you want to init before the rendering
777 * start: function() {
778 * // stuff you want to make after the rendering, `this.$element` holds a correct value
779 * this.$element.find(".my_button").click(/* an example of event binding * /);
781 * // if you have some asynchronous operations, it's a good idea to return
782 * // a promise in start()
783 * var promise = this.rpc(...);
788 * Now this class can simply be used with the following syntax:
790 * var my_widget = new MyWidget(this);
791 * my_widget.appendTo($(".some-div"));
793 * With these two lines, the MyWidget instance was inited, rendered, it was inserted into the
794 * DOM inside the ".some-div" div and its events were binded.
796 * And of course, when you don't need that widget anymore, just do:
800 * That will kill the widget in a clean way and erase its content from the dom.
802 openerp.web.Widget = openerp.web.SessionAware.extend(/** @lends openerp.web.Widget# */{
804 * The name of the QWeb template that will be used for rendering. Must be
805 * redefined in subclasses or the default render() method can not be used.
811 * The prefix used to generate an id automatically. Should be redefined in
812 * subclasses. If it is not defined, a generic identifier will be used.
816 identifier_prefix: 'generic-identifier-',
818 * Construct the widget and set its parent if a parent is given.
820 * @constructs openerp.web.Widget
821 * @extends openerp.web.SessionAware
823 * @param {openerp.web.Widget} parent Binds the current instance to the given Widget instance.
824 * When that widget is destroyed by calling stop(), the current instance will be
825 * destroyed too. Can be null.
826 * @param {String} element_id Deprecated. Sets the element_id. Only useful when you want
827 * to bind the current Widget to an already existing part of the DOM, which is not compatible
828 * with the DOM insertion methods provided by the current implementation of Widget. So
829 * for new components this argument should not be provided any more.
831 init: function(parent, /** @deprecated */ element_id) {
832 this._super((parent || {}).session);
833 // if given an element_id, try to get the associated DOM element and save
834 // a reference in this.$element. Else just generate a unique identifier.
835 this.element_id = element_id;
836 this.element_id = this.element_id || _.uniqueId(this.identifier_prefix);
837 var tmp = document.getElementById(this.element_id);
838 this.$element = tmp ? $(tmp) : undefined;
840 this.widget_parent = parent;
841 this.widget_children = [];
842 if(parent && parent.widget_children) {
843 parent.widget_children.push(this);
845 // useful to know if the widget was destroyed and should not be used anymore
846 this.widget_is_stopped = false;
849 * Render the current widget and appends it to the given jQuery object or Widget.
851 * @param target A jQuery object or a Widget instance.
853 appendTo: function(target) {
855 return this._render_and_insert(function(t) {
856 self.$element.appendTo(t);
860 * Render the current widget and prepends it to the given jQuery object or Widget.
862 * @param target A jQuery object or a Widget instance.
864 prependTo: function(target) {
866 return this._render_and_insert(function(t) {
867 self.$element.prependTo(t);
871 * Render the current widget and inserts it after to the given jQuery object or Widget.
873 * @param target A jQuery object or a Widget instance.
875 insertAfter: function(target) {
877 return this._render_and_insert(function(t) {
878 self.$element.insertAfter(t);
882 * Render the current widget and inserts it before to the given jQuery object or Widget.
884 * @param target A jQuery object or a Widget instance.
886 insertBefore: function(target) {
888 return this._render_and_insert(function(t) {
889 self.$element.insertBefore(t);
892 _render_and_insert: function(insertion, target) {
893 var rendered = this.render();
894 this.$element = $(rendered);
895 if (target instanceof openerp.web.Widget)
896 target = target.$element;
898 this.on_inserted(this.$element, this);
901 on_inserted: function(element, widget) {},
903 * Renders the widget using QWeb, `this.template` must be defined.
904 * The context given to QWeb contains the "widget" key that references `this`.
906 * @param {Object} additional Additional context arguments to pass to the template.
908 render: function (additional) {
909 return openerp.web.qweb.render(this.template, _.extend({widget: this}, additional || {}));
912 * Method called after rendering. Mostly used to bind actions, perform asynchronous
915 * By convention, the method should return a promise to inform the caller when
916 * this widget has been initialized.
918 * @returns {jQuery.Deferred}
921 /* The default implementation is only useful for retro-compatibility, it is
922 not necessary to call it using _super() when using Widget for new components. */
923 if (!this.$element) {
924 var tmp = document.getElementById(this.element_id);
925 this.$element = tmp ? $(tmp) : undefined;
927 return $.Deferred().done().promise();
930 * Destroys the current widget, also destroy all its children before destroying itself.
933 _.each(_.clone(this.widget_children), function(el) {
936 if(this.$element != null) {
937 this.$element.remove();
939 if (this.widget_parent && this.widget_parent.widget_children) {
940 this.widget_parent.widget_children = _.without(this.widget_parent.widget_children, this);
942 this.widget_parent = null;
943 this.widget_is_stopped = true;
946 * Inform the action manager to do an action. Of course, this suppose that
947 * the action manager can be found amongst the ancestors of the current widget.
948 * If that's not the case this method will simply return `false`.
950 do_action: function(action, on_finished) {
951 if (this.widget_parent) {
952 return this.widget_parent.do_action(action, on_finished);
956 do_notify: function() {
957 if (this.widget_parent) {
958 return this.widget_parent.do_notify.apply(this,arguments);
962 do_warn: function() {
963 if (this.widget_parent) {
964 return this.widget_parent.do_warn.apply(this,arguments);
968 rpc: function(url, data, success, error) {
969 var def = $.Deferred().then(success, error);
971 this._super(url, data). then(function() {
972 if (!self.widget_is_stopped)
973 def.resolve.apply(def, arguments);
975 if (!self.widget_is_stopped)
976 def.reject.apply(def, arguments);
978 return def.promise();
984 * @extends openerp.web.Widget
986 * For retro compatibility only, the only difference with is that render() uses
987 * directly ``this`` instead of context with a ``widget`` key.
989 openerp.web.OldWidget = openerp.web.Widget.extend(/** @lends openerp.web.OldWidget# */{
990 render: function (additional) {
991 return openerp.web.qweb.render(this.template, _.extend(_.extend({}, this), additional || {}));
995 openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends openerp.web.TranslationDataBase# */{
997 * @constructs openerp.web.TranslationDataBase
998 * @extends openerp.web.Class
1002 this.parameters = {"direction": 'ltr',
1003 "date_format": '%m/%d/%Y',
1004 "time_format": '%H:%M:%S',
1006 "decimal_point": ".",
1007 "thousands_sep": ","};
1009 set_bundle: function(translation_bundle) {
1012 var modules = _.keys(translation_bundle.modules).sort();
1013 if (_.include(modules, "web")) {
1014 modules = ["web"].concat(_.without(modules, "web"));
1016 _.each(modules, function(name) {
1017 self.add_module_translation(translation_bundle.modules[name]);
1019 if (translation_bundle.lang_parameters) {
1020 this.parameters = translation_bundle.lang_parameters;
1023 add_module_translation: function(mod) {
1025 _.each(mod.messages, function(message) {
1026 if (self.db[message.id] === undefined) {
1027 self.db[message.id] = message.string;
1031 build_translation_function: function() {
1033 var fcnt = function(str) {
1034 var tmp = self.get(str);
1035 return tmp === undefined ? str : tmp;
1037 fcnt.database = this;
1040 get: function(key) {
1042 return this.db[key];
1047 openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function();
1051 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: