fde115003f86e2f33169491bd0b1a2deda3f61fc
[profile/ivi/Modello_Common.git] / css / car / car.js
1 /*global ThemeKeyColor */
2
3 /**
4  * @module CarTheme
5  */
6
7 /**
8 * This function dowloads javascript file, saves it to local cache and injects it to HTML document.
9 * @method loadScript
10 * @for window
11 * @param {string} scriptPath DOM object which to save the downloaded Javascript as Base64.
12 * @param {function(status)} callback - callback function.
13 */
14 function loadScript(path, callback) {
15     "use strict";
16     var scripts = document.getElementsByTagName("script"),
17         i = 0,
18         done = false,
19         scriptElement;
20
21     var tempPath = path;
22     if (tempPath.startsWith("./")) {
23         tempPath = tempPath.substring(1);
24     }
25
26     for (i = 0; i < scripts.length; i++) {
27         if (scripts[i].src.indexOf(tempPath) !== -1) {
28             callback(path, "ok");
29             return;
30         }
31     }
32
33     scriptElement = document.createElement('script');
34
35     function handleLoad() {
36         if (!done) {
37             done = true;
38             if (callback !== null) {
39                 callback(path, "ok");
40             }
41         }
42     }
43
44     scriptElement.onload = handleLoad;
45
46     scriptElement.onreadystatechange = function () {
47         var state;
48
49         if (!done) {
50             state = scriptElement.readyState;
51             if (state === "complete") {
52                 handleLoad();
53             }
54         }
55     };
56
57     scriptElement.onerror = function () {
58         if (!done) {
59             done = true;
60             if (callback !== null) {
61                 callback(path, "error");
62             }
63         }
64     };
65
66     scriptElement.src = path;
67     scriptElement.type = "text/javascript";
68
69     document.getElementsByTagName('head')[0].appendChild(scriptElement);
70 }
71
72 /**
73 * Function tests if string starts with substring passed as parameter.
74 * @method startsWith
75 * @param {String} str Substring to verify.
76 * @for String
77 * @return {bool} true if string starts with substring.
78 */
79 String.prototype.startsWith = function (str){
80     "use strict";
81     return this.indexOf(str) === 0;
82 };
83
84 /**
85 * Function checks if script or CSS file is included in HTML <head>.
86 * @method checkIfIncluded
87 * @param {String} file Path to file name to test.
88 * @for window
89 * @return {bool} true if script / CSS is included in html.
90 */
91 function checkIfIncluded(file) {
92     "use strict";
93     var links = document.getElementsByTagName("link"),
94         i = 0,
95         scripts = document.getElementsByTagName("script");
96
97     for (i = 0; i < links.length; i++) {
98         if (links[i].href.substr(-file.length) === file) {
99             return true;
100         }
101     }
102
103     for (i = 0; i < scripts.length; i++) {
104         if (scripts[i].src.substr(-file.length) === file) {
105             return true;
106         }
107     }
108
109     return false;
110 }
111
112 /**
113 * Function changes image background color by replacing key color with color provided as parameter.
114 * @method changeCssBgImageColor
115 * @for window
116 * @param {string} selector jQuery selector .
117 * @param {string} bgcolor New background color in HEX code.
118 */
119
120 function changeCssBgImageColor(selector, bgcolor) {
121     "use strict";
122     var imageSource = $(selector).css("background-image"),
123         patt = /\"|\'|\)|\(|url/g, //remove 'url' and '()' from background-image property
124         img, ctx, w, h;
125
126     if (imageSource !== undefined) {
127         console.log(imageSource);
128         imageSource = imageSource.replace(patt, '');
129
130         img = new Image();
131         ctx = document.createElement('canvas').getContext('2d');
132
133         img.onload = function () {
134             w = ctx.canvas.width = img.width;
135             h = ctx.canvas.height = img.height;
136             ctx.fillStyle = bgcolor || ThemeKeyColor;
137             ctx.fillRect(0, 0, w, h);
138             ctx.globalCompositeOperation = 'destination-in';
139             ctx.drawImage(img, 0, 0);
140
141             $(selector).css('background-image', 'url(' + ctx.canvas.toDataURL() + ')');
142             $(selector).css('visibility', 'visible');
143         };
144
145         img.src = imageSource;
146     }
147 }
148 /**
149 * Function loads teplate HTML code into script element with name provided as paramater.
150 * @method loadTemplate
151 * @for window
152 * @param {string} baseUrl HTLM URL to load.
153 * @param {string} name Element name to store loaded template.
154 * @param {callback} callback Callback function called when code was injected to HTML structure.
155 */
156 function loadTemplate(baseUrl, name, callback) {
157     "use strict";
158     var template = document.getElementById(name);
159
160     if (!!template) {
161         callback();
162     } else {
163         jQuery.get(baseUrl + name + '.html', function (data) {
164             var scriptTag = $('<script type="text/html" id="' + name + '"></script>');
165             scriptTag.html(data);
166             $('body').append(scriptTag);
167             callback();
168         });
169     }
170 }
171 /**
172 * Function shows loading spinner.
173 * @method showLoadingSpinner
174 * @for window
175 * @param {string} text Text to show in loading spinner.
176 */
177 function showLoadingSpinner(text) {
178     "use strict";
179     if (!$("#loadingSpinnerWrapper").length) {
180         var spinner = '';
181         spinner += '<div id="loadingSpinnerWrapper" class="loadingSpinnerWrapper">';
182         spinner += '<div id="loadingSpinner" class="loadingSpinner pageBgColorNormalTransparent">';
183         spinner += '<div id="loadingSpinnerContent" class="loading-container">';
184         spinner += '<div id="loadingSpinnerImg" class="loading"></div>';
185         spinner += '<div id="loadingSpinnerText" class="loading-text fontSizeXXSmall fontWeightBold fontColorNormal">';
186         spinner += (!!text && text !== "") ? text.toUpperCase() : "";
187         spinner += '</div>';
188         spinner += '</div>';
189         spinner += '</div>';
190         spinner += '</div>';
191         $(spinner).appendTo($("body"));
192         $("#loadingSpinnerWrapper").show();
193     } else {
194         if (!!text && text !== "") {
195             $("#loadingSpinnerText").text(text);
196         } else {
197             $("#loadingSpinnerText").text("");
198         }
199         $("#loadingSpinnerWrapper").show();
200     }
201 }
202 /**
203 * Function hides loading spinner.
204 * @method hideLoadingSpinner
205 * @for window
206 * @param {string} text Text to show in loading spinner.
207 */
208 function hideLoadingSpinner(text) {
209     "use strict";
210     if ($("#loadingSpinnerWrapper").length) {
211         if (text === undefined || text.toString().trim().toLowerCase() === $("#loadingSpinnerText").text().toString().trim().toLowerCase()) {
212             $("#loadingSpinnerWrapper").hide();
213         }
214     }
215 }
216 /**
217 * Function returns formatted phone number into unified format starting with + sign.
218 * @method formatPhoneNumber
219 * @for window
220 * @param {string} phoneNumber Unformated phone number.
221 */
222 function formatPhoneNumber(phoneNumber) {
223     "use strict";
224     var convPhoneNumber = phoneNumber;
225
226     if (!!convPhoneNumber) {
227         convPhoneNumber = convPhoneNumber.toString();
228         convPhoneNumber = convPhoneNumber.trim().replace(/^\+/, "00"); // replace leading '+' by '00'
229         convPhoneNumber = convPhoneNumber.trim().replace(/\D/g, "");  // remove all non-digit characters, ie. 00123-123(123) => 00123123123
230         convPhoneNumber = convPhoneNumber.trim().replace(/^00/, "+"); // replace leading '00' by '+'
231     }
232
233     return convPhoneNumber;
234 }