Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 16 | import { Inject } from '@angular/core'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 17 | import { Directive } from '@angular/core'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { FnService } from './util/fn.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | import { LogService } from './log.service'; |
| 20 | import { OnosService } from './onos.service'; |
| 21 | |
| 22 | /** |
| 23 | * ONOS GUI -- Detect Browser Directive |
| 24 | */ |
| 25 | @Directive({ |
| 26 | selector: '[onosDetectBrowser]' |
| 27 | }) |
| 28 | export class DetectBrowserDirective { |
| 29 | constructor( |
| 30 | private fs: FnService, |
| 31 | private log: LogService, |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 32 | private onos: OnosService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 33 | |
| 34 | // TODO: Change the any type to Window when https://github.com/angular/angular/issues/15640 is fixed. |
| 35 | @Inject('Window') private w: any |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 36 | ) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 37 | const body: HTMLBodyElement = document.getElementsByTagName('body')[0]; |
| 38 | // let body = d3.select('body'); |
| 39 | let browser = ''; |
| 40 | if (fs.isChrome()) { |
| 41 | browser = 'chrome'; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 42 | } else if (fs.isChromeHeadless()) { |
| 43 | browser = 'chromeheadless'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 44 | } else if (fs.isSafari()) { |
| 45 | browser = 'safari'; |
| 46 | } else if (fs.isFirefox()) { |
| 47 | browser = 'firefox'; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 48 | } else { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 49 | this.log.warn('Unknown browser. ', |
| 50 | 'Vendor:', this.w.navigator.vendor, |
| 51 | 'Agent:', this.w.navigator.userAgent); |
| 52 | return; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 53 | } |
| 54 | body.classList.add(browser); |
| 55 | // body.classed(browser, true); |
| 56 | this.onos.browser = browser; |
| 57 | |
| 58 | if (fs.isMobile()) { |
| 59 | body.classList.add('mobile'); |
| 60 | this.onos.mobile = true; |
| 61 | } |
| 62 | |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 63 | // this.log.debug('Detected browser is', fs.cap(browser)); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 64 | } |
| 65 | } |