Fixes TIVI-2784 - Wifi password is not empty
[profile/ivi/SettingsApp.git] / js / utils.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 function jqId(id) {
11     if (!id) return null;
12
13     /* replace colons and spaces with dash for jquery ids */
14     return '#' + id.replace(/:| |\//g, '-');
15 }
16
17 function showMsg(title, message) {
18     if (title === 'Error') {
19         console.error(message);
20         if (message.indexOf(ERROR_SETTINGSD_DISCONNECTED) >= 0) {
21             createPopupDialog(false, false, ERROR_SETTINGSD_DISCONNECTED, 'Reconnect?', 'OK', 'Cancel', function() {
22                 console.log('Reconnecting to settings daemon...');
23                 wsAPI.reconnect();
24             });
25             return;
26         }
27     } else {
28         console.log(message);
29     }
30     createPopupDialog(false, false, title, message);
31 }
32
33 function showInputDialog(title, input_message, ok_button_text, cancel_button_text, ok_callback) {
34     createPopupDialog(false, true, title, input_message, ok_button_text, cancel_button_text, ok_callback);
35 }
36
37 function createPopupDialog(dismiss, is_input, title, message, ok_button_text, cancel_button_text, ok_callback) {
38     // inject popup element
39     var $dialog = $('<div/>').popup({
40         dismissible: 'false',
41         transition: 'none'
42     }).bind('popupafterclose', function() {
43         // remove element from page
44         $(this).remove();
45     });
46
47     $('<h3/>', {
48         text: title
49     }).appendTo($dialog);
50
51     if (is_input) {
52
53     } else {
54         $('<p/>', {
55             text: message
56         }).appendTo($dialog);
57     }
58
59     $('<a>', {
60         text: ok_button_text ? ok_button_text : 'Ok'
61     }).buttonMarkup({
62         inline: true,
63     }).on('click', function() {
64         $dialog.popup('close');
65         if (ok_callback) {
66             if (is_input) {
67                 /* TODO implement getting input from field */
68                 ok_callback();
69
70             } else {
71                 ok_callback();
72             }
73         }
74     }).appendTo($dialog);
75
76     if (cancel_button_text) {
77         /* only create cancel button if requested */
78         $('<a>', {
79             text: cancel_button_text,
80         }).buttonMarkup({
81             inline: true,
82         }).on('click', function() {
83             $dialog.popup('close');
84         }).appendTo($dialog);
85     }
86
87     $dialog.popup('open').trigger('create');
88     $dialog.popup({
89         dismissible: dismiss
90     });
91 }
92
93 function showSpinner(dismiss, message) {
94     $.mobile.loading('show', {
95         text: message,
96         theme: 'a',
97         textVisible: true,
98         textonly: false
99     });
100 }
101
102 function hideSpinner() {
103     $.mobile.loading('hide');
104 }