a26328c294c8ddfed459ac0bcf2aada5a862fb96
[odoo/odoo.git] / addons / web_mobile / static / src / js / chrome_mobile.js
1 /*---------------------------------------------------------
2  * OpenERP Web Mobile chrome
3  *---------------------------------------------------------*/
4
5 openerp.web_mobile.chrome_mobile = function(openerp) {
6
7 openerp.web_mobile.mobilewebclient = function(element_id) {
8     // TODO Helper to start mobile webclient rename it openerp.web.webclient
9     var client = new openerp.web_mobile.MobileWebClient(element_id);
10     client.start();
11     return client;
12 };
13
14 openerp.web_mobile.MobileWebClient = openerp.web.Widget.extend({
15     init: function(element_id) {
16         this._super(null, element_id);
17         QWeb.add_template("xml/web_mobile.xml");
18         var params = {};
19         this.$element.html(QWeb.render("WebClient", {}));
20         this.session = new openerp.web.Session("oe_errors");
21         this.crashmanager =  new openerp.web.CrashManager(this);
22         this.login = new openerp.web_mobile.Login(this, "oe_login");
23 //        this.session.on_session_invalid.add(this.login.do_ask_login);
24     },
25     start: function() {
26         this.session.start();
27         this.login.start();
28     }
29 });
30
31 openerp.web_mobile.Login =  openerp.web.Widget.extend({
32     start: function() {
33         var self = this;
34         jQuery("#oe_header").children().remove();
35         this.rpc("/web/database/get_list", {}, function(result) {
36             var selection = new openerp.web_mobile.Selection();
37             self.db_list = result.db_list;
38             self.$element.html(QWeb.render("Login", self));
39             self.$element.find("#login_btn").click(self.on_login);
40             $.mobile.initializePage();
41         });
42         this.$element
43             .removeClass("login_invalid");
44     },
45     on_login: function(ev) {
46         ev.preventDefault();
47         var self = this;
48         var $e = this.$element;
49         var db = $e.find("div select[name=database]").val();
50         var login = $e.find("div input[name=login]").val();
51         var password = $e.find("div input[name=password]").val();
52
53         //$e.hide();
54         // Should hide then call callback
55         this.session.session_login(db, login, password, function() {
56             if(self.session.session_is_valid()) {
57                 self.on_login_valid();
58             } else {
59                 self.on_login_invalid();
60             }
61         });
62     },
63     on_login_invalid: function() {
64         this.$element
65             .removeClass("login_valid")
66             .addClass("login_invalid")
67             .show();
68     },
69     on_login_valid: function() {
70         this.$element
71             .removeClass("login_invalid")
72             .addClass("login_valid");
73             //.hide();
74
75         this.menu = new openerp.web_mobile.Menu(this, "oe_menu", "oe_secondary_menu");
76         this.menu.start();
77     },
78     do_ask_login: function(continuation) {
79         this.on_login_invalid();
80         this.on_login_valid.add({
81             position: "last",
82             unique: true,
83             callback: continuation
84         });
85     }
86 });
87 openerp.web_mobile.Header =  openerp.web.Widget.extend({
88     init: function(session, element_id) {
89         this._super(session, element_id);
90     },
91     start: function() {
92         this.$element.html(QWeb.render("Header", this));
93     }
94 });
95
96 openerp.web_mobile.Footer =  openerp.web.Widget.extend({
97     init: function(session, element_id) {
98         this._super(session, element_id);
99     },
100     start: function() {
101         this.$element.html(QWeb.render("Footer", this));
102     }
103 });
104
105 openerp.web_mobile.Shortcuts =  openerp.web.Widget.extend({
106     init: function(session, element_id) {
107         this._super(session, element_id);
108     },
109     start: function() {
110         var self = this;
111         this.rpc('/web/session/sc_list',{} ,function(res){
112             self.$element.html(QWeb.render("Shortcuts", {'sc' : res}))
113
114             self.$element.find("[data-role=header]").find('h1').html('Favourite');
115             self.$element.find("[data-role=header]").find('#home').click(function(){
116                 $.mobile.changePage($("#oe_menu"), "slide", true, true);
117             });
118             self.$element.find('#content').find("a").click(self.on_clicked);
119             self.$element.find("[data-role=footer]").find('#preference').click(function(){
120                 if(!$('#oe_options').html().length){
121                     this.options = new openerp.web_mobile.Options(self, "oe_options");
122                     this.options.start();
123                 }
124                 else{
125                     $.mobile.changePage($("#oe_options"), "slide", true, true);
126                 }
127             });
128             $.mobile.changePage($("#oe_shortcuts"), "slide", true, true);
129         });
130     },
131     on_clicked: function(ev) {
132         $shortcut = $(ev.currentTarget);
133         id = $shortcut.data('menu');
134         res_id = $shortcut.data('res');
135
136         if(!$('#oe_list').html().length){
137             this.listview = new openerp.web_mobile.ListView(this, "oe_list", res_id);
138              this.listview.start();
139         }else{
140              $('#oe_list').remove();
141              $('<div id="oe_list" data-role="page"> </div>').appendTo('#moe');
142              this.listview = new openerp.web_mobile.ListView(this, "oe_list", res_id);
143              this.listview.start();
144         }
145         jQuery("#oe_header").find("h1").html($shortcut.data('name'));
146     }
147 });
148
149 openerp.web_mobile.Menu =  openerp.web.Widget.extend({
150     init: function(session, element_id, secondary_menu_id) {
151         this._super(session, element_id);
152         this.secondary_menu_id = secondary_menu_id;
153         this.$secondary_menu = $("#" + secondary_menu_id);
154         this.menu = false;
155     },
156     start: function() {
157         this.rpc("/web/menu/load", {}, this.on_loaded);
158     },
159     on_loaded: function(data) {
160         var self = this;
161         this.data = data;
162
163         this.header = new openerp.web_mobile.Header(this, "oe_header");
164         this.header.start();
165         this.footer = new openerp.web_mobile.Footer(this, "oe_footer");
166         this.footer.start();
167
168         this.$element.html(QWeb.render("Menu", this.data));
169         this.$element.find("[data-role=header]").find('h1').html('Application');
170         this.$element.find("[data-role=footer]").find('#shrotcuts').click(function(){
171             if(!$('#oe_shortcuts').html().length){
172                 this.shortcuts = new openerp.web_mobile.Shortcuts(self, "oe_shortcuts");
173                 this.shortcuts.start();
174             }
175             else{
176                 $.mobile.changePage($("#oe_shortcuts"), "slide", true, true);
177             }
178         });
179         this.$element.find("[data-role=footer]").find('#preference').click(function(){
180             if(!$('#oe_options').html().length){
181                 this.options = new openerp.web_mobile.Options(self, "oe_options");
182                 this.options.start();
183             }
184             else{
185                 $.mobile.changePage($("#oe_options"), "slide", true, true);
186             }
187         });
188         this.$element.add(this.$secondary_menu).find("#content").find('a').click(this.on_menu_click);
189         $.mobile.changePage($("#oe_menu"), "slide", true, true);
190     },
191     on_menu_click: function(ev, id) {
192         var $menu = $(ev.currentTarget);
193         id = $menu.data('menu');
194         for (var i = 0; i < this.data.data.children.length; i++) {
195             if (this.data.data.children[i].id == id) {
196                 this.children = this.data.data.children[i];
197             }
198         }
199         this.$element
200             .removeClass("login_valid")
201             .addClass("secondary_menu");
202
203         if(!$('#oe_sec_menu').html().length){
204             this.secondary = new openerp.web_mobile.Secondary(this, "oe_sec_menu", this.children);
205             this.secondary.start();
206         }else{
207              $('#oe_sec_menu').remove();
208              $('<div id="oe_sec_menu" data-role="page"> </div>').appendTo('#moe');
209              this.secondary = new openerp.web_mobile.Secondary(this, "oe_sec_menu", this.children);
210              this.secondary.start();
211         }
212     }
213 });
214 openerp.web_mobile.Secondary =  openerp.web.Widget.extend({
215     init: function(session, element_id, secondary_menu_id) {
216         this._super(session, element_id);
217         this.data = secondary_menu_id;
218     },
219     start: function(ev, id) {
220         var self = this;
221         var v = { menu : this.data };
222
223         this.$element.html(QWeb.render("Menu.secondary", v));
224
225         this.$element.find("[data-role=header]").find("h1").html(this.data.name);
226         this.$element.add(this.$secondary_menu).find('#content').find("a").click(this.on_menu_click);
227         this.$element.find("[data-role=footer]").find('#shrotcuts').click(function(){
228             if(!$('#oe_shortcuts').html().length){
229                 this.shortcuts = new openerp.web_mobile.Shortcuts(self, "oe_shortcuts");
230                 this.shortcuts.start();
231             }
232             else{
233                 $.mobile.changePage($("#oe_shortcuts"), "slide", true, true);
234             }
235         });
236         this.$element.find("[data-role=footer]").find('#preference').click(function(){
237             if(!$('#oe_options').html().length){
238                 this.options = new openerp.web_mobile.Options(self, "oe_options");
239                 this.options.start();
240             }
241             else{
242                 $.mobile.changePage($("#oe_options"), "slide", true, true);
243             }
244         });
245         this.$element.find("[data-role=header]").find('#home').click(function(){
246             $.mobile.changePage($("#oe_menu"), "slide", true, true);
247         });
248
249         $.mobile.changePage($("#oe_sec_menu"), "slide", true, true);
250     },
251     on_menu_click: function(ev, id) {
252         var $menu = $(ev.currentTarget);
253         id = $menu.data('menu');
254         if (id) {
255             if(!$('#oe_list').html().length){
256                 this.listview = new openerp.web_mobile.ListView(this, "oe_list", id);
257                 this.listview.start();
258             }else{
259                 $('#oe_list').remove();
260                 $('<div id="oe_list" data-role="page"> </div>').appendTo('#moe');
261                 this.listview = new openerp.web_mobile.ListView(this, "oe_list", id);
262                 this.listview.start();
263             }
264         }
265         jQuery("#oe_header").find("h1").html($menu.data('name'));
266     }
267 });
268
269 openerp.web_mobile.Options =  openerp.web.Widget.extend({
270     start: function() {
271         var self = this;
272
273         this.$element.html(QWeb.render("Options", this));
274         this.$element.find("[data-role=header]").find('h1').html('Preference');
275         this.$element.find("[data-role=footer]").find('#shrotcuts').click(function(){
276             if(!$('#oe_shortcuts').html().length){
277                 this.shortcuts = new openerp.web_mobile.Shortcuts(self, "oe_shortcuts");
278                 this.shortcuts.start();
279             }
280             else{
281                 $.mobile.changePage($("#oe_shortcuts"), "slide", true, true);
282             }
283         });
284         this.$element.find("[data-role=header]").find('#home').click(function(){
285             $.mobile.changePage($("#oe_menu"), "slide", true, true);
286         });
287         this.$element.find("[data-role=content]").find('a').click(function(){
288             $('#oe_login').empty();
289             window.location.replace('/mobile');
290         });
291         $.mobile.changePage($("#oe_options"), "slide", true, true);
292     }
293 });
294
295 openerp.web_mobile.Selection = openerp.web.Widget.extend({
296     on_select_option: function(ev){
297         ev.preventDefault();
298         var $this = ev.currentTarget;
299         $($this).prev().find(".ui-btn-text").html($($this).find("option:selected").text());
300     }
301 });
302 };