Moved ZoomDirective to gui2-fw-lib (from gui2-topo-lib)

Change-Id: Idc8bfefea7c84d6611242a9562d84ca6cc53a7b3
diff --git a/web/gui2-fw-lib/package.json b/web/gui2-fw-lib/package.json
index d7da490..d26e07b 100644
--- a/web/gui2-fw-lib/package.json
+++ b/web/gui2-fw-lib/package.json
@@ -1,6 +1,6 @@
 {
     "name": "gui2-fw-lib-app",
-    "version": "2.3.0",
+    "version": "2.3.3",
     "license": "MIT",
     "scripts": {
         "ng": "ng",
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/package.json b/web/gui2-fw-lib/projects/gui2-fw-lib/package.json
index fd1249a..402e4d9 100644
--- a/web/gui2-fw-lib/projects/gui2-fw-lib/package.json
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/package.json
@@ -1,6 +1,6 @@
 {
     "name": "gui2-fw-lib",
-    "version": "2.3.2",
+    "version": "2.3.3",
     "author": {
         "name": "Sean Condon",
         "email": "sean@opennetworking.org"
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/gui2-fw-lib.module.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/gui2-fw-lib.module.ts
index 2027b14..4a59988 100644
--- a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/gui2-fw-lib.module.ts
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/gui2-fw-lib.module.ts
@@ -13,46 +13,53 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-import { NgModule } from '@angular/core';
-import { CommonModule } from '@angular/common';
-import { DetectBrowserDirective } from './detectbrowser.directive';
-import { IconComponent } from './svg/icon/icon.component';
-import { VeilComponent } from './layer/veil/veil.component';
-import { FlashComponent } from './layer/flash/flash.component';
-import { ConfirmComponent } from './layer/confirm/confirm.component';
-import { MastComponent } from './mast/mast/mast.component';
-import { TableFilterPipe } from './widget/tablefilter.pipe';
-import { TableResizeDirective } from './widget/tableresize.directive';
-import { QuickhelpComponent } from './layer/quickhelp/quickhelp.component';
-import { LoadingComponent } from './layer/loading/loading.component';
+import {NgModule} from '@angular/core';
+import {CommonModule} from '@angular/common';
+import {DetectBrowserDirective} from './detectbrowser.directive';
+import {IconComponent} from './svg/icon/icon.component';
+import {VeilComponent} from './layer/veil/veil.component';
+import {FlashComponent} from './layer/flash/flash.component';
+import {ConfirmComponent} from './layer/confirm/confirm.component';
+import {MastComponent} from './mast/mast/mast.component';
+import {TableFilterPipe} from './widget/tablefilter.pipe';
+import {TableResizeDirective} from './widget/tableresize.directive';
+import {QuickhelpComponent} from './layer/quickhelp/quickhelp.component';
+import {LoadingComponent} from './layer/loading/loading.component';
+import {ZoomableDirective} from './svg/zoomable.directive';
+import {NameInputComponent} from './util/name-input/name-input.component';
 
 @NgModule({
-  imports: [
-    CommonModule
-  ],
-  declarations: [
-    DetectBrowserDirective,
-    TableResizeDirective,
-    IconComponent,
-    VeilComponent,
-    FlashComponent,
-    ConfirmComponent,
-    QuickhelpComponent,
-    MastComponent,
-    TableFilterPipe,
-    LoadingComponent
-  ],
-  exports: [
-    DetectBrowserDirective,
-    TableResizeDirective,
-    IconComponent,
-    VeilComponent,
-    FlashComponent,
-    ConfirmComponent,
-    QuickhelpComponent,
-    MastComponent,
-    TableFilterPipe,
-    LoadingComponent
-  ]
+    imports: [
+        CommonModule
+    ],
+    declarations: [
+        DetectBrowserDirective,
+        TableResizeDirective,
+        IconComponent,
+        VeilComponent,
+        FlashComponent,
+        ConfirmComponent,
+        QuickhelpComponent,
+        MastComponent,
+        TableFilterPipe,
+        LoadingComponent,
+        ZoomableDirective,
+        NameInputComponent
+    ],
+    exports: [
+        DetectBrowserDirective,
+        TableResizeDirective,
+        IconComponent,
+        VeilComponent,
+        FlashComponent,
+        ConfirmComponent,
+        QuickhelpComponent,
+        MastComponent,
+        TableFilterPipe,
+        LoadingComponent,
+        ZoomableDirective,
+        NameInputComponent
+    ]
 })
-export class Gui2FwLibModule { }
+export class Gui2FwLibModule {
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.spec.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.spec.ts
new file mode 100644
index 0000000..36b6e37
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.spec.ts
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { ZoomableDirective } from './zoomable.directive';
+import {inject, TestBed} from '@angular/core/testing';
+import {ElementRef} from '@angular/core';
+import {ActivatedRoute, Params} from '@angular/router';
+import {of} from 'rxjs';
+import {FnService} from '../util/fn.service';
+import {LogService} from '../log.service';
+import {ConsoleLoggerService} from '../consolelogger.service';
+
+class MockActivatedRoute extends ActivatedRoute {
+    constructor(params: Params) {
+        super();
+        this.queryParams = of(params);
+    }
+}
+
+describe('ZoomableDirective', () => {
+    let fs: FnService;
+    let ar: MockActivatedRoute;
+    let log: LogService;
+    let mockWindow: Window;
+
+    beforeEach(() => {
+        log = new ConsoleLoggerService();
+        ar = new MockActivatedRoute({ 'debug': 'txrx' });
+
+        mockWindow = <any>{
+            navigator: {
+                userAgent: 'HeadlessChrome',
+                vendor: 'Google Inc.'
+            },
+            location: <any>{
+                hostname: 'foo',
+                host: 'foo',
+                port: '80',
+                protocol: 'http',
+                search: { debug: 'true' },
+                href: 'ws://foo:123/onos/ui/websock/path',
+                absUrl: 'ws://foo:123/onos/ui/websock/path'
+            }
+        };
+
+        fs = new FnService(ar, log, mockWindow);
+
+        TestBed.configureTestingModule({
+            providers: [ZoomableDirective,
+                { provide: FnService, useValue: fs },
+                { provide: LogService, useValue: log },
+                { provide: 'Window', useValue: mockWindow },
+                { provide: ElementRef, useValue: mockWindow }
+            ]
+        });
+    });
+
+    it('should create an instance', inject([ZoomableDirective], (directive: ZoomableDirective) => {
+
+        expect(directive).toBeTruthy();
+    }));
+});
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.ts
new file mode 100644
index 0000000..96fbbfb
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/svg/zoomable.directive.ts
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import {
+    Directive,
+    ElementRef,
+    Input,
+    OnChanges,
+    OnInit,
+    SimpleChanges
+} from '@angular/core';
+import * as d3 from 'd3';
+import {TopoZoomPrefs} from './zoomutils';
+import {LogService} from '../log.service';
+import {PrefsService} from '../util/prefs.service';
+
+const TOPO_ZOOM_PREFS = 'topo_zoom';
+
+const ZOOM_PREFS_DEFAULT: TopoZoomPrefs = <TopoZoomPrefs>{
+    tx: 0, ty: 0, sc: 1.0
+};
+
+/**
+ * A directive that takes care of Zooming and Panning the Topology view
+ *
+ * It wraps the D3 Pan and Zoom functionality
+ * See https://github.com/d3/d3-zoom/blob/master/README.md
+ */
+@Directive({
+  selector: '[onosZoomableOf]'
+})
+export class ZoomableDirective implements OnChanges, OnInit {
+    @Input() zoomableOf: ElementRef;
+
+    zoom: any; // The d3 zoom behaviour
+    zoomCached: TopoZoomPrefs = <TopoZoomPrefs>{tx: 0, ty: 0, sc: 1.0};
+
+    constructor(
+        private _element: ElementRef,
+        private log: LogService,
+        private ps: PrefsService
+    ) {
+        const container = d3.select(this._element.nativeElement);
+
+        const zoomed = () => {
+            const transform = d3.event.transform;
+            container.attr('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.k + ')');
+            this.updateZoomState(<TopoZoomPrefs>{tx: transform.x, ty: transform.y, sc: transform.k});
+        };
+
+        this.zoom = d3.zoom().on('zoom', zoomed);
+    }
+
+    ngOnInit() {
+        this.zoomCached = this.ps.getPrefs(TOPO_ZOOM_PREFS, ZOOM_PREFS_DEFAULT);
+        const svg = d3.select(this.zoomableOf);
+
+        svg.call(this.zoom);
+
+        svg.transition().call(this.zoom.transform,
+            d3.zoomIdentity.translate(this.zoomCached.tx, this.zoomCached.ty).scale(this.zoomCached.sc));
+        this.log.debug('Loaded topo_zoom_prefs',
+            this.zoomCached.tx, this.zoomCached.ty, this.zoomCached.sc);
+
+    }
+
+    /**
+     * Updates the cache of zoom preferences locally and onwards to the PrefsService
+     */
+    updateZoomState(zoomPrefs: TopoZoomPrefs): void {
+        this.zoomCached = zoomPrefs;
+        this.ps.setPrefs(TOPO_ZOOM_PREFS, zoomPrefs);
+    }
+
+    /**
+     * If the input object is changed then re-establish the zoom
+     */
+    ngOnChanges(changes: SimpleChanges): void {
+        if (changes['zoomableOf']) {
+            const svg = d3.select(changes['zoomableOf'].currentValue);
+            svg.call(this.zoom);
+            this.log.debug('Applying zoomable behaviour on', this.zoomableOf, this._element.nativeElement);
+        }
+    }
+
+    /**
+     * Change the zoom level when a map is chosen in Topology view
+     *
+     * Animated to run over 750ms
+     */
+    changeZoomLevel(zoomState: TopoZoomPrefs, fast?: boolean): void {
+        const svg = d3.select(this.zoomableOf);
+        svg.transition().duration(fast ? 0 : 750).call(this.zoom.transform,
+            d3.zoomIdentity.translate(zoomState.tx, zoomState.ty).scale(zoomState.sc));
+        this.updateZoomState(zoomState);
+        this.log.debug('Pan to', zoomState.tx, zoomState.ty, 'and zoom to', zoomState.sc);
+    }
+
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.css b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.css
new file mode 100644
index 0000000..c8acf1b
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.css
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ ONOS GUI -- Name Input Component (layout) -- CSS file
+ */
+#name-input-dialog {
+    top: 140px;
+    padding: 12px;
+}
+
+#name-input-dialog h3 {
+    display: inline-block;
+    font-weight: bold;
+    font-size: 18pt;
+}
+
+#name-input-dialog p {
+    font-size: 12pt;
+}
+
+#name-input-dialog p.strong {
+    font-weight: bold;
+    padding: 8px;
+    text-align: center;
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.html b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.html
new file mode 100644
index 0000000..e505780
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.html
@@ -0,0 +1,22 @@
+<!--
+~ Copyright 2019-present Open Networking Foundation
+~
+~ Licensed under the Apache License, Version 2.0 (the "License");
+~ you may not use this file except in compliance with the License.
+~ You may obtain a copy of the License at
+~
+~     http://www.apache.org/licenses/LICENSE-2.0
+~
+~ Unless required by applicable law or agreed to in writing, software
+~ distributed under the License is distributed on an "AS IS" BASIS,
+~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+~ See the License for the specific language governing permissions and
+~ limitations under the License.
+-->
+<div id="name-input-dialog" class="floatpanel dialog" [@niDlgState]="title!==''">
+    <h3>{{ title }}</h3><br>
+    <input #newName type="text" pattern="[a-zA-Z0-9\-:_]{3}" [placeholder]="placeholder" name="nameinput" [minLength]="minLen" [maxLength]="maxLen" width="40" required>
+    <p *ngIf="warning" class="warning strong">{{ warning }}</p>
+    <div tabindex="10" class="dialog-button" (click)="choice(true, newName.value)">OK</div>
+    <div tabindex="11" class="dialog-button" (click)="choice(false, '')">Cancel</div>
+</div>
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.spec.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.spec.ts
new file mode 100644
index 0000000..63ec2cb
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.spec.ts
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import {async, ComponentFixture, TestBed} from '@angular/core/testing';
+
+import {NameInputComponent} from './name-input.component';
+import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
+
+describe('NameInputComponent', () => {
+    let component: NameInputComponent;
+    let fixture: ComponentFixture<NameInputComponent>;
+
+    beforeEach(async(() => {
+        TestBed.configureTestingModule({
+            imports: [BrowserAnimationsModule],
+            declarations: [NameInputComponent]
+        })
+            .compileComponents();
+    }));
+
+    beforeEach(() => {
+        fixture = TestBed.createComponent(NameInputComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create', () => {
+        expect(component).toBeTruthy();
+    });
+});
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.ts
new file mode 100644
index 0000000..9d7116e
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.component.ts
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
+import {animate, state, style, transition, trigger} from '@angular/animations';
+
+export interface NameInputResult {
+    chosen: boolean;
+    name: string;
+}
+
+@Component({
+    selector: 'onos-name-input',
+    templateUrl: './name-input.component.html',
+    styleUrls: [
+        './name-input.component.css',
+        './name-input.theme.css',
+        '../../layer/dialog.css',
+        '../../layer/dialog.theme.css',
+        '../../widget/panel.css',
+        '../../widget/panel-theme.css'
+    ],
+    animations: [
+        trigger('niDlgState', [
+            state('true', style({
+                transform: 'translateX(-100%)',
+                opacity: '100'
+            })),
+            state('false', style({
+                transform: 'translateX(0%)',
+                opacity: '0'
+            })),
+            transition('0 => 1', animate('100ms ease-in')),
+            transition('1 => 0', animate('100ms ease-out'))
+        ])
+    ]
+})
+export class NameInputComponent implements OnInit {
+    @Input() warning: string;
+    @Input() title: string = '';
+    @Input() pattern;
+    @Input() minLen = 4;
+    @Input() maxLen = 40;
+    @Input() placeholder = 'name';
+    @Output() chosen: EventEmitter<NameInputResult> = new EventEmitter();
+
+    constructor() {
+    }
+
+    ngOnInit() {
+    }
+
+    /**
+     * When OK or Cancel is pressed, send an event to parent with choice
+     */
+    choice(chosen: boolean, newName: string): void {
+        if (chosen && (newName === undefined || newName === '')) {
+            return;
+        }
+        this.chosen.emit(<NameInputResult>{
+            chosen: chosen,
+            name: newName
+        });
+    }
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.theme.css b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.theme.css
new file mode 100644
index 0000000..bb88fb2
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/util/name-input/name-input.theme.css
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ ONOS GUI -- name-input-dialog Component (theme) -- CSS file
+ */
+/* temporarily removed .light */
+#name-input-dialog p.strong {
+    color: white;
+    background-color: #ce5b58;
+}
+
+#name-input-dialog.floatpanel.dialog {
+    background-color: #ffffff;
+}
+
+#name-input-dialog p.strong {
+    color: white;
+    background-color: #ce5b58;
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/public_api.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/public_api.ts
index 9c70db9..35cb29b 100644
--- a/web/gui2-fw-lib/projects/gui2-fw-lib/src/public_api.ts
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/public_api.ts
@@ -51,9 +51,11 @@
 export * from './lib/layer/quickhelp/quickhelp.component';
 export * from './lib/layer/loading/loading.component';
 export * from './lib/svg/icon/icon.component';
+export * from './lib/util/name-input/name-input.component';
 
 export * from './lib/widget/tableresize.directive';
 export * from './lib/detectbrowser.directive';
+export * from './lib/svg/zoomable.directive';
 
 export * from './lib/widget/tablefilter.pipe';
 
diff --git a/web/gui2-fw-lib/src/app/app.component.html b/web/gui2-fw-lib/src/app/app.component.html
index 80c9050..e3c6531 100644
--- a/web/gui2-fw-lib/src/app/app.component.html
+++ b/web/gui2-fw-lib/src/app/app.component.html
@@ -46,3 +46,4 @@
 </p>
 <onos-quickhelp #qh></onos-quickhelp>
 <button (click)="qh.ks.quickHelpShown = true">Toggle quick help shown</button>
+<onos-name-input [title]="'test-input'" [minLen]="10" [maxLen]="20"></onos-name-input>