Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 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'; |
| 18 | import { FnService } from './fw/util/fn.service'; |
| 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, |
| 33 | @Inject(Window) private w: Window |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 34 | ) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 35 | const body: HTMLBodyElement = document.getElementsByTagName('body')[0]; |
| 36 | // let body = d3.select('body'); |
| 37 | let browser = ''; |
| 38 | if (fs.isChrome()) { |
| 39 | browser = 'chrome'; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 40 | } else if (fs.isChromeHeadless()) { |
| 41 | browser = 'chromeheadless'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 42 | } else if (fs.isSafari()) { |
| 43 | browser = 'safari'; |
| 44 | } else if (fs.isFirefox()) { |
| 45 | browser = 'firefox'; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 46 | } else { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 47 | this.log.warn('Unknown browser. ', |
| 48 | 'Vendor:', this.w.navigator.vendor, |
| 49 | 'Agent:', this.w.navigator.userAgent); |
| 50 | return; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 51 | } |
| 52 | body.classList.add(browser); |
| 53 | // body.classed(browser, true); |
| 54 | this.onos.browser = browser; |
| 55 | |
| 56 | if (fs.isMobile()) { |
| 57 | body.classList.add('mobile'); |
| 58 | this.onos.mobile = true; |
| 59 | } |
| 60 | |
| 61 | this.log.debug('Detected browser is', fs.cap(browser)); |
| 62 | } |
| 63 | } |