c5d0a93fe86e1d256419f058b11cbfb0411ff0c6
[odoo/odoo.git] / addons / base / static / src / js / chrome.js
1 /*---------------------------------------------------------
2  * OpenERP base library
3  *---------------------------------------------------------*/
4
5 openerp.base.chrome = function(openerp) {
6
7 openerp.base.callback = function(obj, method) {
8     var callback = function() {
9         var args = Array.prototype.slice.call(arguments);
10         var r;
11         for(var i = 0; i < callback.callback_chain.length; i++)  {
12             var c = callback.callback_chain[i];
13             if(c.unique) {
14                 callback.callback_chain.splice(i, 1);
15                 i -= 1;
16             }
17             r = c.callback.apply(c.self, c.args.concat(args));
18             // TODO special value to stop the chain
19             // openerp.base.callback_stop
20         }
21         return r;
22     };
23     callback.callback_chain = [];
24     callback.add = function(f) {
25         if(typeof(f) == 'function') {
26             f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
27         }
28         f.self = f.self || null;
29         f.args = f.args || [];
30         f.unique = !!f.unique;
31         if(f.position == 'last') {
32             callback.callback_chain.push(f);
33         } else {
34             callback.callback_chain.unshift(f);
35         }
36         return callback;
37     };
38     callback.add_first = function(f) {
39         return callback.add.apply(null,arguments);
40     };
41     callback.add_last = function(f) {
42         return callback.add({
43             callback: f,
44             args: Array.prototype.slice.call(arguments, 1),
45             position: "last"
46         });
47     };
48
49     return callback.add({
50         callback: method,
51         self:obj,
52         args:Array.prototype.slice.call(arguments, 2)
53     });
54 };
55
56 /**
57  * Base error for lookup failure
58  *
59  * @class
60  */
61 openerp.base.NotFound = Class.extend( /** @lends openerp.base.NotFound# */ {
62 });
63 openerp.base.KeyNotFound = openerp.base.NotFound.extend( /** @lends openerp.base.KeyNotFound# */ {
64     /**
65      * Thrown when a key could not be found in a mapping
66      *
67      * @constructs
68      * @extends openerp.base.NotFound
69      * @param {String} key the key which could not be found
70      */
71     init: function (key) {
72         this.key = key;
73     },
74     toString: function () {
75         return "The key " + this.key + " was not found";
76     }
77 });
78 openerp.base.ObjectNotFound = openerp.base.NotFound.extend( /** @lends openerp.base.ObjectNotFound# */ {
79     /**
80      * Thrown when an object path does not designate a valid class or object
81      * in the openerp hierarchy.
82      *
83      * @constructs
84      * @extends openerp.base.NotFound
85      * @param {String} path the invalid object path
86      */
87     init: function (path) {
88         this.path = path;
89     },
90     toString: function () {
91         return "Could not find any object of path " + this.path;
92     }
93 });
94 openerp.base.Registry = Class.extend( /** @lends openerp.base.Registry# */ {
95     /**
96      * Stores a mapping of arbitrary key (strings) to object paths (as strings
97      * as well).
98      *
99      * Resolves those paths at query time in order to always fetch the correct
100      * object, even if those objects have been overloaded/replaced after the
101      * registry was created.
102      *
103      * An object path is simply a dotted name from the openerp root to the
104      * object pointed to (e.g. ``"openerp.base.Session"`` for an OpenERP
105      * session object).
106      *
107      * @constructs
108      * @param {Object} mapping a mapping of keys to object-paths
109      */
110     init: function (mapping) {
111         this.map = mapping || {};
112     },
113     /**
114      * Retrieves the object matching the provided key string.
115      *
116      * @param {String} key the key to fetch the object for
117      * @returns {Class} the stored class, to initialize
118      *
119      * @throws {openerp.base.KeyNotFound} if the object was not in the mapping
120      * @throws {openerp.base.ObjectNotFound} if the object path was invalid
121      */
122     get_object: function (key) {
123         var path_string = this.map[key];
124         if (path_string === undefined) {
125             throw new openerp.base.KeyNotFound(key);
126         }
127
128         var object_match = openerp;
129         var path = path_string.split('.');
130         // ignore first section
131         for(var i=1; i<path.length; ++i) {
132             object_match = object_match[path[i]];
133
134             if (object_match === undefined) {
135                 throw new openerp.base.ObjectNotFound(path_string);
136             }
137         }
138         return object_match;
139     },
140     /**
141      * Tries a number of keys, and returns the first object matching one of
142      * the keys.
143      *
144      * @param {Array} keys a sequence of keys to fetch the object for
145      * @returns {Class} the first class found matching an object
146      *
147      * @throws {openerp.base.KeyNotFound} if none of the keys was in the mapping
148      * @trows {openerp.base.ObjectNotFound} if a found object path was invalid
149      */
150     get_any: function (keys) {
151         for (var i=0; i<keys.length; ++i) {
152             try {
153                 return this.get_object(keys[i]);
154             } catch (e) {
155                 if (e instanceof openerp.base.KeyNotFound) {
156                     continue;
157                 }
158                 throw e;
159             }
160         }
161         throw new openerp.base.KeyNotFound(keys.join(','));
162     },
163     /**
164      * Adds a new key and value to the registry.
165      *
166      * This method can be chained.
167      *
168      * @param {String} key
169      * @param {String} object_path fully qualified dotted object path
170      * @returns {openerp.base.Registry} itself
171      */
172     add: function (key, object_path) {
173         this.map[key] = object_path;
174         return this;
175     },
176     /**
177      * Creates and returns a copy of the current mapping, with the provided
178      * mapping argument added in (replacing existing keys if needed)
179      *
180      * @param {Object} [mapping={}] a mapping of keys to object-paths
181      */
182     clone: function (mapping) {
183         return new openerp.base.Registry(
184             _.extend({}, this.map, mapping || {}));
185     }
186 });
187
188 openerp.base.BasicController = Class.extend( /** @lends openerp.base.BasicController# */{
189     /**
190      * rpc operations, event binding and callback calling should be done in
191      * start() instead of init so that event can be hooked in between.
192      *
193      *  @constructs
194      */
195     init: function(element_id) {
196         this.element_id = element_id;
197         this.$element = $('#' + element_id);
198         if (element_id) {
199             openerp.screen[element_id] = this;
200         }
201
202         // Transform on_* method into openerp.base.callbacks
203         for (var name in this) {
204             if(typeof(this[name]) == "function") {
205                 this[name].debug_name = name;
206                 // bind ALL function to this not only on_and _do ?
207                 if((/^on_|^do_/).test(name)) {
208                     this[name] = openerp.base.callback(this, this[name]);
209                 }
210             }
211         }
212     },
213     /**
214      * Controller start
215      * event binding, rpc and callback calling required to initialize the
216      * object can happen here
217      *
218      * Returns a promise object letting callers (subclasses and direct callers)
219      * know when this component is done starting
220      *
221      * @returns {jQuery.Deferred}
222      */
223     start: function() {
224         // returns an already fulfilled promise. Maybe we could return nothing?
225         // $.when can take non-deferred and in that case it simply considers
226         // them all as fulfilled promises.
227         // But in thise case we *have* to ensure callers use $.when and don't
228         // try to call deferred methods on this return value.
229         return $.Deferred().done().promise();
230     },
231     stop: function() {
232     },
233     log: function() {
234         var args = Array.prototype.slice.call(arguments);
235         var caller = arguments.callee.caller;
236         // TODO add support for line number using
237         // https://github.com/emwendelin/javascript-stacktrace/blob/master/stacktrace.js
238         // args.unshift("" + caller.debug_name);
239         this.on_log.apply(this,args);
240     },
241     on_log: function() {
242         if(window.openerp.debug || (window.location.search.indexOf('?debug') !== -1)) {
243             var notify = false;
244             var body = false;
245             if(window.console) {
246                 console.log(arguments);
247             } else {
248                 body = true;
249             }
250             var a = Array.prototype.slice.call(arguments, 0);
251             for(var i = 0; i < a.length; i++) {
252                 var v = a[i]==null ? "null" : a[i].toString();
253                 if(i==0) {
254                     notify = v.match(/^not/);
255                     body = v.match(/^bod/);
256                 }
257                 if(body) {
258                     $('<pre></pre>').text(v).appendTo($('body'));
259                 }
260                 if(notify && this.notification) {
261                     this.notification.notify("Logging:",v);
262                 }
263             }
264         }
265
266     }
267 });
268
269 /**
270  * Generates an inherited class that replaces all the methods by null methods (methods
271  * that does nothing and always return undefined).
272  * 
273  * @param {Class} claz
274  * @param {dict} add Additional functions to override.
275  * @return {Class}
276  */
277 openerp.base.generate_null_object_class = function(claz, add) {
278     var newer = {};
279     var copy_proto = function(prototype) {
280         for (var name in prototype) {
281             if(typeof prototype[name] == "function") {
282                 newer[name] = function() {};
283             }
284         }
285         if (prototype.prototype)
286             copy_proto(prototype.prototype);
287     };
288     copy_proto(claz.prototype);
289     newer.init = openerp.base.BasicController.prototype.init;
290     var tmpclass = claz.extend(newer);
291     return tmpclass.extend(add || {});
292 };
293
294 openerp.base.Notification =  openerp.base.BasicController.extend({
295     init: function(element_id) {
296         this._super(element_id);
297         this.$element.notify({
298             speed: 500,
299             expires: 1500
300         });
301     },
302     notify: function(title, text) {
303         this.$element.notify('create', {
304             title: title,
305             text: text
306         });
307     },
308     warn: function(title, text) {
309         this.$element.notify('create', 'oe_notification_alert', {
310             title: title,
311             text: text
312         });
313     }
314 });
315
316 openerp.base.Session = openerp.base.BasicController.extend( /** @lends openerp.base.Session# */{
317     /**
318      * @constructs
319      * @extends openerp.base.BasicController
320      * @param element_id to use for exception reporting
321      * @param server
322      * @param port
323      */
324     init: function(element_id, server, port) {
325         this._super(element_id);
326         this.server = (server == undefined) ? location.hostname : server;
327         this.port = (port == undefined) ? location.port : port;
328         this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp";
329         this.debug = true;
330         this.db = "";
331         this.login = "";
332         this.password = "";
333         this.uid = false;
334         this.session_id = false;
335         this.module_list = [];
336         this.module_loaded = {"base": true};
337         this.context = {};
338     },
339     start: function() {
340         this.session_restore();
341     },
342     /**
343      * Executes an RPC call, registering the provided callbacks.
344      *
345      * Registers a default error callback if none is provided, and handles
346      * setting the correct session id and session context in the parameter
347      * objects
348      *
349      * @param {String} url RPC endpoint
350      * @param {Object} params call parameters
351      * @param {Function} success_callback function to execute on RPC call success
352      * @param {Function} error_callback function to execute on RPC call failure
353      * one
354      * @returns {jQuery.Deferred} jquery-provided ajax deferred
355      */
356     rpc: function(url, params, success_callback, error_callback) {
357         var self = this;
358         // Construct a JSON-RPC2 request, method is currently unused
359         params.session_id = this.session_id;
360
361         // Call using the rpc_mode
362         var deferred = $.Deferred();
363         this.rpc_ajax(url, {
364             jsonrpc: "2.0",
365             method: "call",
366             params: params,
367             id:null
368         }).then(function () {deferred.resolve.apply(deferred, arguments);},
369         function(error) {deferred.reject(error, $.Event());});
370         return deferred.fail(function() {
371             deferred.fail(function(error, event) {
372                 if (!event.isDefaultPrevented()) {
373                     self.on_rpc_error(error, event);
374                 }
375             });
376         }).then(success_callback, error_callback).promise();
377     },
378     /**
379      * Raw JSON-RPC call
380      *
381      * @returns {jQuery.Deferred} ajax-based deferred object
382      */
383     rpc_ajax: function(url, payload) {
384         var self = this;
385         this.on_rpc_request();
386         // url can be an $.ajax option object
387         if (_.isString(url)) {
388             url = {
389                 url: url
390             }
391         }
392         var ajax = _.extend({
393             type: "POST",
394             url: url,
395             dataType: 'json',
396             contentType: 'application/json',
397             data: JSON.stringify(payload),
398             processData: false
399         }, url);
400         var deferred = $.Deferred();
401         $.ajax(ajax).done(function(response, textStatus, jqXHR) {
402                 self.on_rpc_response();
403                 if (response.error) {
404                     if (response.error.data.type == "session_invalid") {
405                         self.uid = false;
406                         self.on_session_invalid(function() {
407                             self.rpc(url, payload.params,
408                                 function() {deferred.resolve.apply(deferred, arguments);},
409                                 function(error, event) {event.preventDefault();
410                                     deferred.reject.apply(deferred, arguments);});
411                         });
412                     } else {
413                         deferred.reject(response.error);
414                     }
415                 } else {
416                     deferred.resolve(response["result"], textStatus, jqXHR);
417                 }
418             }).fail(function(jqXHR, textStatus, errorThrown) {
419                 self.on_rpc_response();
420                 var error = {
421                     code: -32098,
422                     message: "XmlHttpRequestError " + errorThrown,
423                     data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
424                 };
425                 deferred.reject(error);
426             });
427         return deferred.promise();
428     },
429     on_rpc_request: function() {
430     },
431     on_rpc_response: function() {
432     },
433     on_rpc_error: function(error) {
434     },
435     /**
436      * The session is validated either by login or by restoration of a previous session
437      */
438     on_session_valid: function() {
439         if(!openerp._modules_loaded)
440             this.load_modules();
441     },
442     on_session_invalid: function(contination) {
443     },
444     session_is_valid: function() {
445         return this.uid;
446     },
447     session_login: function(db, login, password, success_callback) {
448         var self = this;
449         this.db = db;
450         this.login = login;
451         this.password = password;
452         var params = { db: this.db, login: this.login, password: this.password };
453         this.rpc("/base/session/login", params, function(result) {
454             self.session_id = result.session_id;
455             self.uid = result.uid;
456             self.session_save();
457             self.on_session_valid();
458             if (success_callback)
459                 success_callback();
460         });
461     },
462     session_logout: function() {
463         this.uid = false;
464     },
465     /**
466      * Reloads uid and session_id from local storage, if they exist
467      */
468     session_restore: function () {
469         this.uid = this.get_cookie('uid');
470         this.session_id = this.get_cookie('session_id');
471         // we should do an rpc to confirm that this session_id is valid and if it is retrieve the information about db and login
472         // then call on_session_valid
473         this.on_session_valid();
474     },
475     /**
476      * Saves the session id and uid locally
477      */
478     session_save: function () {
479         this.set_cookie('uid', this.uid);
480         this.set_cookie('session_id', this.session_id);
481     },
482     logout: function() {
483         this.uid = this.get_cookie('uid');
484         this.session_id = this.get_cookie('session_id');
485         this.set_cookie('uid', '');
486         this.set_cookie('session_id', '');
487         this.on_session_invalid(function() {});
488     },
489     /**
490      * Fetches a cookie stored by an openerp session
491      *
492      * @private
493      * @param name the cookie's name
494      */
495     get_cookie: function (name) {
496         var nameEQ = this.element_id + '|' + name + '=';
497         var cookies = document.cookie.split(';');
498         for(var i=0; i<cookies.length; ++i) {
499             var cookie = cookies[i].replace(/^\s*/, '');
500             if(cookie.indexOf(nameEQ) === 0) {
501                 return decodeURIComponent(cookie.substring(nameEQ.length));
502             }
503         }
504         return null;
505     },
506     /**
507      * Create a new cookie with the provided name and value
508      *
509      * @private
510      * @param name the cookie's name
511      * @param value the cookie's value
512      * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
513      */
514     set_cookie: function (name, value, ttl) {
515         ttl = ttl || 24*60*60*365;
516         document.cookie = [
517             this.element_id + '|' + name + '=' + encodeURIComponent(value),
518             'max-age=' + ttl,
519             'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
520         ].join(';');
521     },
522     /**
523      * Load additional web addons of that instance and init them
524      */
525     load_modules: function() {
526         var self = this;
527         this.rpc('/base/session/modules', {}, function(result) {
528             self.module_list = result['modules'];
529             var modules = self.module_list.join(',');
530             self.rpc('/base/session/csslist', {mods: modules}, self.do_load_css);
531             self.rpc('/base/session/jslist', {"mods": modules}, self.debug ? self.do_load_modules_debug : self.do_load_modules_prod);
532             openerp._modules_loaded = true;
533         });
534     },
535     do_load_css: function (result) {
536         _.each(result.files, function (file) {
537             $('head').append($('<link>', {
538                 'href': file,
539                 'rel': 'stylesheet',
540                 'type': 'text/css'
541             }));
542         });
543     },
544     do_load_modules_debug: function(result) {
545         $LAB.setOptions({AlwaysPreserveOrder: true})
546             .script(result.files)
547             .wait(this.on_modules_loaded);
548     },
549     do_load_modules_prod: function() {
550         // load merged ones
551         // /base/session/css?mod=mod1,mod2,mod3
552         // /base/session/js?mod=mod1,mod2,mod3
553         // use $.getScript(‘your_3rd_party-script.js’); ? i want to keep lineno !
554     },
555     on_modules_loaded: function() {
556         for(var j=0; j<this.module_list.length; j++) {
557             var mod = this.module_list[j];
558             if(this.module_loaded[mod])
559                 continue;
560             openerp[mod] = {};
561             // init module mod
562             if(openerp._openerp[mod] != undefined) {
563                 openerp._openerp[mod](openerp);
564                 this.module_loaded[mod] = true;
565             }
566         }
567     }
568 });
569
570 // A controller takes an already existing element
571 // new()
572 // start()
573 openerp.base.Controller = openerp.base.BasicController.extend( /** @lends openerp.base.Controller# */{
574     /**
575      * Controller manifest used to declare standard controller attributes
576      */
577     controller_manifest: {
578         register: null,
579         template: "",
580         element_post_prefix: false
581     },
582     /**
583      * Controller registry, 
584      */
585     controller_registry: {
586     },
587     /**
588      * Add a new child controller
589      */
590     controller_get: function(key) {
591         return this.controller_registry[key];
592         // OR should build it ? setting parent correctly ?
593         // function construct(constructor, args) {
594         //     function F() {
595         //         return constructor.apply(this, args);
596         //     }
597         //     F.prototype = constructor.prototype;
598         //     return new F();
599         // }
600         // var obj = this.controller_registry[key];
601         // if(obj) {
602         //     return construct(obj, Array.prototype.slice.call(arguments, 1));
603         // }
604     },
605     controller_new: function(key) {
606         var self;
607         // OR should contrustct it ? setting parent correctly ?
608         function construct(constructor, args) {
609             function F() {
610                 return constructor.apply(this, args);
611             }
612             F.prototype = constructor.prototype;
613             return new F();
614         }
615         var obj = this.controller_registry[key];
616         if(obj) {
617             // TODO Prepend parent
618             return construct(obj, Array.prototype.slice.call(arguments, 1));
619         }
620     },
621     /**
622      * @constructs
623      * @extends openerp.base.BasicController
624      */
625     init: function(parent_or_session, element_id) {
626         this._super(element_id);
627         this.controller_parent = null;
628         this.controller_children = [];
629         if(parent_or_session) {
630             if(parent_or_session.session) {
631                 this.parent = parent_or_session;
632                 this.session = this.parent.session;
633                 if(this.parent.children) {
634                     this.parent.children.push(this);
635                 }
636             } else {
637                 // TODO remove Backward compatilbility
638                 this.session = parent_or_session;
639             }
640         }
641         // Apply manifest options
642         if(this.controller_manifest) {
643             var register = this.controller_manifest.register;
644             // TODO accept a simple string
645             if(register) {
646                 for(var i=0; i<register.length; i++) {
647                     this.controller_registry[register[i]] = this;
648                 }
649             }
650             // TODO if post prefix
651             //this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
652         }
653     },
654     /**
655      * Performs a JSON-RPC call
656      *
657      * @param {String} url endpoint url
658      * @param {Object} data RPC parameters
659      * @param {Function} success RPC call success callback
660      * @param {Function} error RPC call error callback
661      * @returns {jQuery.Deferred} deferred object for the RPC call
662      */
663     rpc: function(url, data, success, error) {
664         // TODO: support additional arguments ?
665         return this.session.rpc(url, data, success, error);
666     }
667 });
668
669 // A widget is a controller that doesnt take an element_id
670 // it render its own html that you should insert into the dom
671 // and bind it a start()
672 //
673 // new()
674 // render() and insert it place it where you want
675 // start()
676 openerp.base.BaseWidget = openerp.base.Controller.extend({
677     /**
678      * The name of the QWeb template that will be used for rendering. Must be
679      * redefined in subclasses or the render() method can not be used.
680      * 
681      * @type string
682      */
683     template: null,
684     /**
685      * The prefix used to generate an id automatically. Should be redefined in
686      * subclasses. If it is not defined, a default identifier will be used.
687      * 
688      * @type string
689      */
690     identifier_prefix: 'generic-identifier',
691     /**
692      * Base class for widgets. Handle rendering (based on a QWeb template),
693      * identifier generation, parenting and destruction of the widget.
694      * Also initialize the identifier.
695      *
696      * @constructs
697      * @params {openerp.base.search.BaseWidget} parent The parent widget.
698      */
699     init: function (parent, session) {
700         this._super(session);
701         this.children = [];
702         this.parent = null;
703         this.set_parent(parent);
704         this.make_id(this.identifier_prefix);
705     },
706     /**
707      * Sets and returns a globally unique identifier for the widget.
708      *
709      * If a prefix is appended, the identifier will be appended to it.
710      *
711      * @params sections prefix sections, empty/falsy sections will be removed
712      */
713     make_id: function () {
714         this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
715         return this.element_id;
716     },
717     /**
718      * "Starts" the widgets. Called at the end of the rendering, this allows
719      * to get a jQuery object referring to the DOM ($element attribute).
720      */
721     start: function () {
722         this._super();
723         var tmp = document.getElementById(this.element_id);
724         this.$element = tmp ? $(tmp) : null;
725     },
726     /**
727      * "Stops" the widgets. Called when the view destroys itself, this
728      * lets the widgets clean up after themselves.
729      */
730     stop: function () {
731         var tmp_children = this.children;
732         this.children = [];
733         _.each(tmp_children, function(x) {
734             x.stop();
735         });
736         if(this.$element != null) {
737             this.$element.remove();
738         }
739         this.set_parent(null);
740         this._super();
741     },
742     /**
743      * Set the parent of this component, also un-register the previous parent
744      * if there was one.
745      * 
746      * @param {openerp.base.BaseWidget} parent The new parent.
747      */
748     set_parent: function(parent) {
749         if(this.parent) {
750             this.parent.children = _.without(this.parent.children, this);
751         }
752         this.parent = parent;
753         if(this.parent) {
754             parent.children.push(this);
755         }
756     },
757     /**
758      * Render the widget. This.template must be defined.
759      * The content of the current object is passed as context to the template.
760      * 
761      * @param {object} additional Additional context arguments to pass to the template.
762      */
763     render: function (additional) {
764         return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
765     }
766 });
767
768 openerp.base.Dialog = openerp.base.BaseWidget.extend({
769     dialog_title: "",
770     identifier_prefix: 'dialog',
771     init: function (session, options) {
772         this._super(null, session);
773         this.options = {
774             modal: true,
775             width: 'auto',
776             min_width: 0,
777             max_width: '100%',
778             height: 'auto',
779             min_height: 0,
780             max_height: '100%',
781             autoOpen: false,
782             buttons: {}
783         };
784         for (var f in this) {
785             if (f.substr(0, 10) == 'on_button_') {
786                 this.options.buttons[f.substr(10)] = this[f];
787             }
788         }
789         if (options) {
790             this.set_options(options);
791         }
792     },
793     set_options: function(options) {
794         options = options || {};
795         options.width = this.get_width(options.width || this.options.width);
796         options.min_width = this.get_width(options.min_width || this.options.min_width);
797         options.max_width = this.get_width(options.max_width || this.options.max_width);
798         options.height = this.get_height(options.height || this.options.height);
799         options.min_height = this.get_height(options.min_height || this.options.min_height);
800         options.max_height = this.get_height(options.max_height || this.options.max_width);
801
802         if (options.width !== 'auto') {
803             if (options.width > options.max_width) options.width = options.max_width;
804             if (options.width < options.min_width) options.width = options.min_width;
805         }
806         if (options.height !== 'auto') {
807             if (options.height > options.max_height) options.height = options.max_height;
808             if (options.height < options.min_height) options.height = options.min_height;
809         }
810         if (!options.title && this.dialog_title) {
811             options.title = this.dialog_title;
812         }
813         _.extend(this.options, options);
814     },
815     get_width: function(val) {
816         return this.get_size(val.toString(), $(window.top).width());
817     },
818     get_height: function(val) {
819         return this.get_size(val.toString(), $(window.top).height());
820     },
821     get_size: function(val, available_size) {
822         if (val === 'auto') {
823             return val;
824         } else if (val.slice(-1) == "%") {
825             return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
826         } else {
827             return parseInt(val, 10);
828         }
829     },
830     start: function (auto_open) {
831         this.$dialog = $('<div id="' + this.element_id + '"></div>').dialog(this.options);
832         if (auto_open !== false) {
833             this.open();
834         }
835         this._super();
836     },
837     open: function(options) {
838         // TODO fme: bind window on resize
839         if (this.template) {
840             this.$element.html(this.render());
841         }
842         this.set_options(options);
843         this.$dialog.dialog(this.options).dialog('open');
844     },
845     close: function(options) {
846         this.$dialog.dialog('close');
847     },
848     stop: function () {
849         this.$dialog.dialog('destroy');
850     }
851 });
852
853 openerp.base.CrashManager = openerp.base.Dialog.extend({
854     identifier_prefix: 'dialog_crash',
855     init: function(session) {
856         this._super(session);
857         this.session.on_rpc_error.add(this.on_rpc_error);
858     },
859     on_button_Ok: function() {
860         this.close();
861     },
862     on_rpc_error: function(error) {
863         this.error = error;
864         if (error.data.fault_code) {
865             var split = error.data.fault_code.split('\n')[0].split(' -- ');
866             if (split.length > 1) {
867                 error.type = split.shift();
868                 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
869             }
870         }
871         if (error.code === 200 && error.type) {
872             this.dialog_title = "OpenERP " + _.capitalize(error.type);
873             this.template = 'DialogWarning';
874             this.open({
875                 width: 'auto',
876                 height: 'auto'
877             });
878         } else {
879             this.dialog_title = "OpenERP Error";
880             this.template = 'DialogTraceback';
881             this.open({
882                 width: '80%',
883                 height: '80%'
884             });
885         }
886     }
887 });
888
889 openerp.base.Loading =  openerp.base.Controller.extend({
890     controller_manifest: {
891         register: ["Loading"]
892     },
893     init: function(session, element_id) {
894         this._super(session, element_id);
895         this.count = 0;
896         this.session.on_rpc_request.add_first(this.on_rpc_event, 1);
897         this.session.on_rpc_response.add_last(this.on_rpc_event, -1);
898     },
899     on_rpc_event : function(increment) {
900         this.count += increment;
901         if (this.count) {
902             //this.$element.html(QWeb.render("Loading", {}));
903             this.$element.html("Loading ("+this.count+")");
904             this.$element.show();
905         } else {
906             this.$element.fadeOut();
907         }
908     }
909 });
910
911 openerp.base.Database = openerp.base.Controller.extend({
912 });
913
914 openerp.base.Login =  openerp.base.Controller.extend({
915     init: function(session, element_id) {
916         this._super(session, element_id);
917         this.has_local_storage = typeof(localStorage) != 'undefined';
918         this.selected_db = null;
919         this.selected_login = null;
920         this.selected_password = null;
921         this.remember = false;
922         if (this.has_local_storage && localStorage.getItem('remember_creditentials') === 'true') {
923             this.remember = true;
924             this.selected_db = localStorage.getItem('last_db_login_success');
925             this.selected_login = localStorage.getItem('last_login_login_success');
926             this.selected_password = localStorage.getItem('last_password_login_success');
927         }
928     },
929     start: function() {
930         var self = this;
931         this.rpc("/base/session/get_databases_list", {}, function(result) {
932             self.db_list = result.db_list;
933             self.display();
934         }, function() {
935             self.display();
936         });
937     },
938     display: function() {
939         this.$element.html(QWeb.render("Login", this));
940         this.$element.find("form").submit(this.on_submit);
941     },
942     on_login_invalid: function() {
943         this.$element.closest(".openerp").addClass("login-mode");
944     },
945     on_login_valid: function() {
946         this.$element.closest(".openerp").removeClass("login-mode");
947     },
948     on_submit: function(ev) {
949         ev.preventDefault();
950         var self = this;
951         var $e = this.$element;
952         var db = $e.find("form [name=db]").val();
953         var login = $e.find("form input[name=login]").val();
954         var password = $e.find("form input[name=password]").val();
955         var remember = $e.find("form input[name=remember]").attr('checked');
956         //$e.hide();
957         // Should hide then call callback
958         this.session.session_login(db, login, password, function() {
959             if(self.session.session_is_valid()) {
960                 if (self.has_local_storage) {
961                     if(remember) {
962                         localStorage.setItem('remember_creditentials', 'true');
963                         localStorage.setItem('last_db_login_success', db);
964                         localStorage.setItem('last_login_login_success', login);
965                         localStorage.setItem('last_password_login_success', password);
966                     } else {
967                         localStorage.setItem('remember_creditentials', '');
968                         localStorage.setItem('last_db_login_success', '');
969                         localStorage.setItem('last_login_login_success', '');
970                         localStorage.setItem('last_password_login_success', '');
971                     }
972                 }
973                 self.on_login_valid();
974             } else {
975                 self.$element.addClass("login_invalid");
976                 self.on_login_invalid();
977             }
978         });
979     },
980     do_ask_login: function(continuation) {
981         this.on_login_invalid();
982         this.$element
983             .removeClass("login_invalid");
984         this.on_login_valid.add({
985             position: "last",
986             unique: true,
987             callback: continuation
988         });
989     },
990     on_logout: function() {
991         this.session.logout();
992     }
993 });
994
995 openerp.base.Header =  openerp.base.Controller.extend({
996     init: function(session, element_id) {
997         this._super(session, element_id);
998     },
999     start: function() {
1000         this.do_update();
1001     },
1002     do_update: function() {
1003         this.$element.html(QWeb.render("Header", this));
1004         this.$element.find(".logout").click(this.on_logout);
1005     },
1006     on_logout: function() {}
1007 });
1008
1009 openerp.base.Menu =  openerp.base.Controller.extend({
1010     init: function(session, element_id, secondary_menu_id) {
1011         this._super(session, element_id);
1012         this.secondary_menu_id = secondary_menu_id;
1013         this.$secondary_menu = $("#" + secondary_menu_id);
1014         this.menu = false;
1015     },
1016     start: function() {
1017         this.rpc("/base/menu/load", {}, this.on_loaded);
1018     },
1019     on_loaded: function(data) {
1020         this.data = data;
1021         this.$element.html(QWeb.render("Menu", this.data));
1022         for (var i = 0; i < this.data.data.children.length; i++) {
1023             var v = { menu : this.data.data.children[i] };
1024             this.$secondary_menu.append(QWeb.render("Menu.secondary", v));
1025         }
1026         this.$secondary_menu.find("div.menu_accordion").accordion({
1027             animated : false,
1028             autoHeight : false,
1029             icons : false
1030         });
1031         this.$secondary_menu.find("div.submenu_accordion").accordion({
1032             animated : false,
1033             autoHeight : false,
1034             active: false,
1035             collapsible: true,
1036             header: 'h4'
1037         });
1038
1039         this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
1040     },
1041     on_menu_click: function(ev, id) {
1042         id = id || 0;
1043         var $menu, $parent, $secondary;
1044
1045         if (id) {
1046             // We can manually activate a menu with it's id (for hash url mapping)
1047             $menu = this.$element.find('a[data-menu=' + id + ']');
1048             if (!$menu.length) {
1049                 $menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
1050             }
1051         } else {
1052             $menu = $(ev.currentTarget);
1053             id = $menu.data('menu');
1054         }
1055         if (this.$secondary_menu.has($menu).length) {
1056             $secondary = $menu.parents('.menu_accordion');
1057             $parent = this.$element.find('a[data-menu=' + $secondary.data('menu-parent') + ']');
1058         } else {
1059             $parent = $menu;
1060             $secondary = this.$secondary_menu.find('.menu_accordion[data-menu-parent=' + $menu.attr('data-menu') + ']');
1061         }
1062
1063         this.$secondary_menu.find('.menu_accordion').hide();
1064         // TODO: ui-accordion : collapse submenus and expand the good one
1065         $secondary.show();
1066
1067         if (id) {
1068             this.rpc('/base/menu/action', {'menu_id': id},
1069                     this.on_menu_action_loaded);
1070         }
1071
1072         $('.active', this.$element.add(this.$secondary_menu)).removeClass('active');
1073         $parent.addClass('active');
1074         $menu.addClass('active');
1075         $menu.parent('h4').addClass('active');
1076
1077         return !$menu.is(".leaf");
1078     },
1079     on_menu_action_loaded: function(data) {
1080         var self = this;
1081         if (data.action.length) {
1082             var action = data.action[0][2];
1083             self.on_action(action);
1084         }
1085     },
1086     on_action: function(action) {
1087     }
1088 });
1089
1090 openerp.base.Homepage = openerp.base.Controller.extend({
1091 });
1092
1093 openerp.base.Preferences = openerp.base.Controller.extend({
1094 });
1095
1096 openerp.base.ImportExport = openerp.base.Controller.extend({
1097 });
1098
1099 openerp.base.WebClient = openerp.base.Controller.extend({
1100     init: function(element_id) {
1101         var self = this;
1102         this._super(null, element_id);
1103
1104         QWeb.add_template("xml/base.xml");
1105         var params = {};
1106         if(jQuery.param != undefined &&
1107                 jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
1108             this.$element.addClass("kitten-mode-activated");
1109         }
1110         this.$element.html(QWeb.render("Interface", params));
1111
1112         this.session = new openerp.base.Session("oe_errors");
1113         this.loading = new openerp.base.Loading(this.session, "oe_loading");
1114         this.crashmanager =  new openerp.base.CrashManager(this.session);
1115         this.crashmanager.start(false);
1116
1117         // Do you autorize this ?
1118         openerp.base.Controller.prototype.notification = new openerp.base.Notification("oe_notification");
1119
1120         this.header = new openerp.base.Header(this.session, "oe_header");
1121         this.login = new openerp.base.Login(this.session, "oe_login");
1122         this.header.on_logout.add(this.login.on_logout);
1123
1124         this.session.on_session_invalid.add(this.login.do_ask_login);
1125         this.session.on_session_valid.add_last(this.header.do_update);
1126         this.session.on_session_valid.add_last(this.on_logged);
1127
1128         this.menu = new openerp.base.Menu(this.session, "oe_menu", "oe_secondary_menu");
1129         this.menu.on_action.add(this.on_menu_action);
1130     },
1131     start: function() {
1132         this.session.start();
1133         this.header.start();
1134         this.login.start();
1135         this.menu.start();
1136         this.notification.notify("OpenERP Client", "The openerp client has been initialized.");
1137     },
1138     on_logged: function() {
1139         this.action_manager =  new openerp.base.ActionManager(this.session, "oe_app");
1140         this.action_manager.start();
1141         
1142         // if using saved actions, load the action and give it to action manager
1143         var parameters = jQuery.deparam(jQuery.param.querystring());
1144         if(parameters["s_action"] != undefined) {
1145             var key = parseInt(parameters["s_action"]);
1146             var self = this;
1147             this.rpc("/base/session/get_session_action", {key:key}, function(action) {
1148                 self.action_manager.do_action(action);
1149             });
1150         }
1151     },
1152     on_menu_action: function(action) {
1153         this.action_manager.do_action(action);
1154     },
1155     do_about: function() {
1156     }
1157 });
1158
1159 openerp.base.webclient = function(element_id) {
1160     // TODO Helper to start webclient rename it openerp.base.webclient
1161     var client = new openerp.base.WebClient(element_id);
1162     client.start();
1163     return client;
1164 };
1165
1166 };
1167
1168 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: