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
This commit is contained in:
Gillardo 2015-05-08 12:08:43 +01:00 committed by Nikhil Khandelwal
parent 47fef8450c
commit 8e1e88a30a

View File

@ -1,5 +1,4 @@
/* /*
*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information * 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() { function getViewStatusBar() {
if(isSupported) { if (!isSupported()) {
var ViewMan = Windows.UI.ViewManagement; // quick alias to save char throw new Error("Status bar is not supported");
if( ViewMan.StatusBar &&
ViewMan.StatusBar.getForCurrentView ) {
return ViewMan.StatusBar.getForCurrentView();
}
else {
isSupported = false; // so we won't check again
}
} }
return null; return Windows.UI.ViewManagement.StatusBar.getForCurrentView();
} }
function hexToRgb(hex) { function hexToRgb(hex) {
@ -61,40 +62,49 @@ module.exports = {
styleDefault: function () { styleDefault: function () {
// dark text ( to be used on a light background ) // 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 () { styleLightContent: function () {
// light text ( to be used on a dark background ) // 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 () { styleBlackTranslucent: function () {
// #88000000 ? Apple says to use lightContent instead // #88000000 ? Apple says to use lightContent instead
return this.styleLightContent(); return module.exports.styleLightContent();
}, },
styleBlackOpaque: function () { styleBlackOpaque: function () {
// #FF000000 ? Apple says to use lightContent instead // #FF000000 ? Apple says to use lightContent instead
return this.styleLightContent(); return module.exports.styleLightContent();
}, },
backgroundColorByHexString: function (win, fail, args) { backgroundColorByHexString: function (win, fail, args) {
var rgb = hexToRgb(args[0]); var rgb = hexToRgb(args[0]);
var statusBar = getViewStatusBar(); if(isSupported()) {
if(statusBar) { var statusBar = getViewStatusBar();
statusBar.backgroundColor = { a: 0, r: rgb.r, g: rgb.g, b: rgb.b }; statusBar.backgroundColor = { a: 0, r: rgb.r, g: rgb.g, b: rgb.b };
statusBar.backgroundOpacity = 1; statusBar.backgroundOpacity = 1;
} }
}, },
show: function (win, fail) { 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) { 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);