1 /*global ThemeKeyColor */
8 * This function dowloads javascript file, saves it to local cache and injects it to HTML document.
11 * @param {string} scriptPath DOM object which to save the downloaded Javascript as Base64.
12 * @param {function(status)} callback - callback function.
14 function loadScript(path, callback) {
16 var scripts = document.getElementsByTagName("script"),
22 if (tempPath.startsWith("./")) {
23 tempPath = tempPath.substring(1);
26 for (i = 0; i < scripts.length; i++) {
27 if (scripts[i].src.indexOf(tempPath) !== -1) {
33 scriptElement = document.createElement('script');
35 function handleLoad() {
38 if (callback !== null) {
44 scriptElement.onload = handleLoad;
46 scriptElement.onreadystatechange = function () {
50 state = scriptElement.readyState;
51 if (state === "complete") {
57 scriptElement.onerror = function () {
60 if (callback !== null) {
61 callback(path, "error");
66 scriptElement.src = path;
67 scriptElement.type = "text/javascript";
69 document.getElementsByTagName('head')[0].appendChild(scriptElement);
73 * Function tests if string starts with substring passed as parameter.
75 * @param {String} str Substring to verify.
77 * @return {bool} true if string starts with substring.
79 String.prototype.startsWith = function (str){
81 return this.indexOf(str) === 0;
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.
89 * @return {bool} true if script / CSS is included in html.
91 function checkIfIncluded(file) {
93 var links = document.getElementsByTagName("link"),
95 scripts = document.getElementsByTagName("script");
97 for (i = 0; i < links.length; i++) {
98 if (links[i].href.substr(-file.length) === file) {
103 for (i = 0; i < scripts.length; i++) {
104 if (scripts[i].src.substr(-file.length) === file) {
113 * Function changes image background color by replacing key color with color provided as parameter.
114 * @method changeCssBgImageColor
116 * @param {string} selector jQuery selector .
117 * @param {string} bgcolor New background color in HEX code.
120 function changeCssBgImageColor(selector, bgcolor) {
122 var imageSource = $(selector).css("background-image"),
123 patt = /\"|\'|\)|\(|url/g, //remove 'url' and '()' from background-image property
126 if (imageSource !== undefined) {
127 console.log(imageSource);
128 imageSource = imageSource.replace(patt, '');
131 ctx = document.createElement('canvas').getContext('2d');
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);
141 $(selector).css('background-image', 'url(' + ctx.canvas.toDataURL() + ')');
142 $(selector).css('visibility', 'visible');
145 img.src = imageSource;
149 * Function loads teplate HTML code into script element with name provided as paramater.
150 * @method loadTemplate
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.
156 function loadTemplate(baseUrl, name, callback) {
158 var template = document.getElementById(name);
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);
172 * Function shows loading spinner.
173 * @method showLoadingSpinner
175 * @param {string} text Text to show in loading spinner.
177 function showLoadingSpinner(text) {
179 if (!$("#loadingSpinnerWrapper").length) {
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() : "";
191 $(spinner).appendTo($("body"));
192 $("#loadingSpinnerWrapper").show();
194 if (!!text && text !== "") {
195 $("#loadingSpinnerText").text(text);
197 $("#loadingSpinnerText").text("");
199 $("#loadingSpinnerWrapper").show();
203 * Function hides loading spinner.
204 * @method hideLoadingSpinner
206 * @param {string} text Text to show in loading spinner.
208 function hideLoadingSpinner(text) {
210 if ($("#loadingSpinnerWrapper").length) {
211 if (text === undefined || text.toString().trim().toLowerCase() === $("#loadingSpinnerText").text().toString().trim().toLowerCase()) {
212 $("#loadingSpinnerWrapper").hide();
217 * Function returns formatted phone number into unified format starting with + sign.
218 * @method formatPhoneNumber
220 * @param {string} phoneNumber Unformated phone number.
222 function formatPhoneNumber(phoneNumber) {
224 var convPhoneNumber = phoneNumber;
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 '+'
233 return convPhoneNumber;