diff --git a/res/app/components/stf/native-url/index.js b/res/app/components/stf/native-url/index.js new file mode 100644 index 00000000..5e335208 --- /dev/null +++ b/res/app/components/stf/native-url/index.js @@ -0,0 +1,4 @@ +module.exports = angular.module('stf.native-url', [ + +]) + .factory('NativeUrlService', require('./native-url-service')) diff --git a/res/app/components/stf/native-url/native-url-service.js b/res/app/components/stf/native-url/native-url-service.js new file mode 100644 index 00000000..68fe2785 --- /dev/null +++ b/res/app/components/stf/native-url/native-url-service.js @@ -0,0 +1,89 @@ +module.exports = function NativeUrlServiceFactory($window, $timeout) { + var service = {} + + // Ways of opening native URLs: + // - window.open + // - window.location.href + + // Ways of detecting failure: + // - new window timeout + // - window onblur timeout + // - javascript single thread time elapsed + + // Browser Behaviours: + // - Chrome Mac: 25ms timeout OK + // - Firefox Mac: 250ms timeout OK, 1000ms timeout better + // - Safari Mac: no working fallback, may need 2 html pages + // navigator.userAgent.match() + // TODO: Find which method works well in every browser + + var fallbackMethod = 'USE_ON_BLUR' + + var wasBlured = false + var windowOpened + + // TODO: use a central on-blur event + var cachedWindowOnBlur = $window.onblur + + service.open = function (options) { + + switch (fallbackMethod) { + case 'USE_NEW_WINDOW': + // Doesn't work well on Chrome + windowOpened = $window.open(options.nativeUrl) + + $timeout(function () { + if (windowOpened) { + windowOpened.close() + } + }, 500) + + $window.location.href = options.webUrl + break; + case 'USE_ON_BLUR': + // Doesn't work on Safari + + $window.onblur = function () { + wasBlured = true + } + + $window.location.href = options.nativeUrl + + $timeout(function () { + if (wasBlured) { + wasBlured = false + } else { + $window.open(options.webUrl, '_blank') + } + + $window.onblur = cachedWindowOnBlur + }, 250) + + break; + case 'USE_TIME_ELAPSED': + // Doesn't work on Chrome + + var start, end, elapsed + start = new Date().getTime() + + console.log(' window.performance.webkitNow()', window.performance.now()) + + // This depends on the fact that JS is single-thread + document.location = options.nativeUrl + + end = new Date().getTime() + + elapsed = (end - start) + + if (elapsed < 1) { + document.location = options.webUrl + } + + break; + default: + } + + } + + return service +} diff --git a/res/app/components/stf/native-url/native-url-spec.js b/res/app/components/stf/native-url/native-url-spec.js new file mode 100644 index 00000000..39b7baf1 --- /dev/null +++ b/res/app/components/stf/native-url/native-url-spec.js @@ -0,0 +1,11 @@ +describe('NativeUrlService', function() { + + beforeEach(angular.mock.module(require('./').name)) + + it('should ...', inject(function(NativeUrlService) { + + //expect(NativeUrlService.doSomething()).toEqual('something') + + })) + +}) diff --git a/res/app/menu/index.js b/res/app/menu/index.js index 6a930dde..bfe85e93 100644 --- a/res/app/menu/index.js +++ b/res/app/menu/index.js @@ -3,7 +3,8 @@ require('./menu.css') module.exports = angular.module('stf.menu', [ require('stf/nav-menu').name, require('stf/settings').name, - require('stf/common-ui/modals/external-url-modal').name + require('stf/common-ui/modals/external-url-modal').name, + require('stf/native-url').name ]) .controller('MenuCtrl', require('./menu-controller')) .run(["$templateCache", function ($templateCache) { diff --git a/res/app/menu/menu-controller.js b/res/app/menu/menu-controller.js index 7e891be2..7a6d16db 100644 --- a/res/app/menu/menu-controller.js +++ b/res/app/menu/menu-controller.js @@ -1,5 +1,5 @@ module.exports = function MenuCtrl($scope, $rootScope, SettingsService, - $location, ExternalUrlModalService) { + $location, ExternalUrlModalService, NativeUrlService) { SettingsService.bind($scope, { target: 'lastUsedDevice' @@ -15,8 +15,16 @@ module.exports = function MenuCtrl($scope, $rootScope, SettingsService, }) $scope.openChat = function () { - var hipChatUrl = 'https://cyberagent.hipchat.com/chat?focus_jid=' + + var hipChatNativeUrl = 'hipchat://cyberagent.hipchat.com/room/stf' + + var hipChatWebUrl = 'https://cyberagent.hipchat.com/chat?focus_jid=' + '44808_stf@conf.hipchat.com&minimal=true' - ExternalUrlModalService.open(hipChatUrl, 'HipChat #STF', 'fa-comment') + + NativeUrlService.open({ + nativeUrl: hipChatNativeUrl, + webUrl: hipChatWebUrl + }) + + //ExternalUrlModalService.open(hipChatUrl, 'HipChat #STF', 'fa-comment') } } diff --git a/res/app/menu/menu.jade b/res/app/menu/menu.jade index 4876384e..677193cb 100644 --- a/res/app/menu/menu.jade +++ b/res/app/menu/menu.jade @@ -17,7 +17,7 @@ .btn-group button(type='button', ng-model='$root.platform', btn-radio="'web'", translate).btn.btn-sm.btn-default-outline Web button(type='button', ng-model='$root.platform', btn-radio="'native'", translate).btn.btn-sm.btn-default-outline Native - //li(ng-if='!$root.basicMode') + li(ng-if='!$root.basicMode') a(ng-click='openChat()').pointer i.fa.fa-comment.fa-fw | {{ "Chat" | translate }}