openEventsPlatformApp/plugins/cordova-plugin-splashscreen/src/browser/SplashScreenProxy.js
plugins/cordova-plugin-splashscreen/src/browser/SplashScreenProxy.jsRaw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22// Default parameter values including image size can be changed in `config.xml`
23var splashImageWidth = 170;
24var splashImageHeight = 200;
25var position = { x: 0, y: 0, width: splashImageWidth, height: splashImageHeight };
26var localSplash; // the image to display
27var localSplashImage;
28var bgColor = "#464646";
29var imageSrc = '/img/logo.png';
30var splashScreenDelay = 3000; // in milliseconds
31var showSplashScreen = true; // show splashcreen by default
32var cordova = require('cordova');
33var configHelper = cordova.require('cordova/confighelper');
34
35function updateImageLocation() {
36 position.width = Math.min(splashImageWidth, window.innerWidth);
37 position.height = position.width * (splashImageHeight / splashImageWidth);
38
39 localSplash.style.width = window.innerWidth + "px";
40 localSplash.style.height = window.innerHeight + "px";
41 localSplash.style.top = "0px";
42 localSplash.style.left = "0px";
43
44 localSplashImage.style.top = "50%";
45 localSplashImage.style.left = "50%";
46 localSplashImage.style.height = position.height + "px";
47 localSplashImage.style.width = position.width + "px";
48 localSplashImage.style.marginTop = (-position.height / 2) + "px";
49 localSplashImage.style.marginLeft = (-position.width / 2) + "px";
50}
51
52function onResize() {
53 updateImageLocation();
54}
55
56var SplashScreen = {
57 setBGColor: function (cssBGColor) {
58 bgColor = cssBGColor;
59 if (localSplash) {
60 localSplash.style.backgroundColor = bgColor;
61 }
62 },
63 show: function () {
64 if(!localSplash) {
65 window.addEventListener("resize", onResize, false);
66 localSplash = document.createElement("div");
67 localSplash.style.backgroundColor = bgColor;
68 localSplash.style.position = "absolute";
69
70 localSplashImage = document.createElement("img");
71 localSplashImage.src = imageSrc;
72 localSplashImage.style.position = "absolute";
73
74 updateImageLocation();
75
76 localSplash.appendChild(localSplashImage);
77 document.body.appendChild(localSplash);
78 }
79 },
80 hide: function () {
81 if(localSplash) {
82 window.removeEventListener("resize", onResize, false);
83 document.body.removeChild(localSplash);
84 localSplash = null;
85 }
86 }
87};
88
89/**
90 * Reads preferences via ConfigHelper and substitutes default parameters.
91 */
92function readPreferencesFromCfg(cfg) {
93 try {
94 var value = cfg.getPreferenceValue('ShowSplashScreen');
95 if(typeof value != 'undefined') {
96 showSplashScreen = value === 'true';
97 }
98
99 splashScreenDelay = cfg.getPreferenceValue('SplashScreenDelay') || splashScreenDelay;
100 imageSrc = cfg.getPreferenceValue('SplashScreen') || imageSrc;
101 bgColor = cfg.getPreferenceValue('SplashScreenBackgroundColor') || bgColor;
102 splashImageWidth = cfg.getPreferenceValue('SplashScreenWidth') || splashImageWidth;
103 splashImageHeight = cfg.getPreferenceValue('SplashScreenHeight') || splashImageHeight;
104 } catch(e) {
105 var msg = '[Browser][SplashScreen] Error occured on loading preferences from config.xml: ' + JSON.stringify(e);
106 console.error(msg);
107 }
108}
109
110/**
111 * Shows and hides splashscreen if it is enabled, with a delay according the current preferences.
112 */
113function showAndHide() {
114 if(showSplashScreen) {
115 SplashScreen.show();
116
117 window.setTimeout(function() {
118 SplashScreen.hide();
119 }, splashScreenDelay);
120 }
121}
122
123/**
124 * Tries to read config.xml and override default properties and then shows and hides splashcreen if it is enabled.
125 */
126(function initAndShow() {
127 configHelper.readConfig(function(config) {
128 readPreferencesFromCfg(config);
129 showAndHide();
130 }, function(err) {
131 console.error(err);
132 });
133})();
134
135module.exports = SplashScreen;
136
137require("cordova/exec/proxy").add("SplashScreen", SplashScreen);
138