blob: a2ec16df0f803a9b9a6f382836e5772c5af5c397 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 Condonfd6d11b2018-06-02 20:29:49 +010016import { Inject } from '@angular/core';
Sean Condon83fc39f2018-04-19 18:56:13 +010017import { Directive } from '@angular/core';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { FnService } from './util/fn.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010019import { LogService } from './log.service';
20import { OnosService } from './onos.service';
21
22/**
23 * ONOS GUI -- Detect Browser Directive
24 */
25@Directive({
26 selector: '[onosDetectBrowser]'
27})
28export class DetectBrowserDirective {
29 constructor(
30 private fs: FnService,
31 private log: LogService,
Sean Condonfd6d11b2018-06-02 20:29:49 +010032 private onos: OnosService,
Sean Condon5ca00262018-09-06 17:55:25 +010033
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 Condon83fc39f2018-04-19 18:56:13 +010036 ) {
Sean Condon83fc39f2018-04-19 18:56:13 +010037 const body: HTMLBodyElement = document.getElementsByTagName('body')[0];
38// let body = d3.select('body');
39 let browser = '';
40 if (fs.isChrome()) {
41 browser = 'chrome';
Sean Condon49e15be2018-05-16 16:58:29 +010042 } else if (fs.isChromeHeadless()) {
43 browser = 'chromeheadless';
Sean Condon83fc39f2018-04-19 18:56:13 +010044 } else if (fs.isSafari()) {
45 browser = 'safari';
46 } else if (fs.isFirefox()) {
47 browser = 'firefox';
Sean Condon49e15be2018-05-16 16:58:29 +010048 } else {
Sean Condonfd6d11b2018-06-02 20:29:49 +010049 this.log.warn('Unknown browser. ',
50 'Vendor:', this.w.navigator.vendor,
51 'Agent:', this.w.navigator.userAgent);
52 return;
Sean Condon83fc39f2018-04-19 18:56:13 +010053 }
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 Condon5ca00262018-09-06 17:55:25 +010063// this.log.debug('Detected browser is', fs.cap(browser));
Sean Condon83fc39f2018-04-19 18:56:13 +010064 }
65}