From 8e1e88a30a2f58339901bdfedffaa7448c254b45 Mon Sep 17 00:00:00 2001 From: Gillardo Date: Fri, 8 May 2015 12:08:43 +0100 Subject: [PATCH] CB-9180 Add correct supported check for Windows 8.1 desktop If you called show or hide, you would get an "object has no method" error as getViewStatusBar would return null.. This closes #25 --- src/windows/StatusBarProxy.js | 54 +++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/windows/StatusBarProxy.js b/src/windows/StatusBarProxy.js index 5770099..fe18108 100644 --- a/src/windows/StatusBarProxy.js +++ b/src/windows/StatusBarProxy.js @@ -1,5 +1,4 @@ /* - * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -19,20 +18,22 @@ * */ - var isSupported = true; // we assume + var _supported = null; // set to null so we can check first time + + function isSupported() { + // if not checked before, run check + if (_supported == null) { + var viewMan = Windows.UI.ViewManagement; + _supported = (viewMan.StatusBar && viewMan.StatusBar.getForCurrentView); + } + return _supported; + } function getViewStatusBar() { - if(isSupported) { - var ViewMan = Windows.UI.ViewManagement; // quick alias to save char - if( ViewMan.StatusBar && - ViewMan.StatusBar.getForCurrentView ) { - return ViewMan.StatusBar.getForCurrentView(); - } - else { - isSupported = false; // so we won't check again - } + if (!isSupported()) { + throw new Error("Status bar is not supported"); } - return null; + return Windows.UI.ViewManagement.StatusBar.getForCurrentView(); } function hexToRgb(hex) { @@ -61,40 +62,49 @@ module.exports = { styleDefault: function () { // dark text ( to be used on a light background ) - getViewStatusBar().foregroundColor = { a: 0, r: 0, g: 0, b: 0 }; + if (isSupported()) { + getViewStatusBar().foregroundColor = { a: 0, r: 0, g: 0, b: 0 }; + } }, styleLightContent: function () { // light text ( to be used on a dark background ) - getViewStatusBar().foregroundColor = { a: 0, r: 255, g: 255, b: 255 }; + if (isSupported()) { + getViewStatusBar().foregroundColor = { a: 0, r: 255, g: 255, b: 255 }; + } }, styleBlackTranslucent: function () { // #88000000 ? Apple says to use lightContent instead - return this.styleLightContent(); + return module.exports.styleLightContent(); }, styleBlackOpaque: function () { // #FF000000 ? Apple says to use lightContent instead - return this.styleLightContent(); + return module.exports.styleLightContent(); }, backgroundColorByHexString: function (win, fail, args) { var rgb = hexToRgb(args[0]); - var statusBar = getViewStatusBar(); - if(statusBar) { + if(isSupported()) { + var statusBar = getViewStatusBar(); statusBar.backgroundColor = { a: 0, r: rgb.r, g: rgb.g, b: rgb.b }; statusBar.backgroundOpacity = 1; } }, show: function (win, fail) { - getViewStatusBar().showAsync().done(win, fail); + // added support check so no error thrown, when calling this method + if (isSupported()) { + getViewStatusBar().showAsync().done(win, fail); + } }, hide: function (win, fail) { - getViewStatusBar().hideAsync().done(win, fail); + // added support check so no error thrown, when calling this method + if (isSupported()) { + getViewStatusBar().hideAsync().done(win, fail); + } } }; - -require("cordova/exec/proxy").add("StatusBar", module.exports); +require("cordova/exec/proxy").add("StatusBar", module.exports); \ No newline at end of file