GUI2 Changed loading screen display to be a component

Change-Id: Id1592b498e6827ec4cd3544dec92e46f00e8d378
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.css b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.css
new file mode 100644
index 0000000..9095bd2
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.css
@@ -0,0 +1,28 @@
+/*
+ *  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 -- Loading Service -- CSS file
+ */
+
+#loading-anim {
+    position: fixed;
+    top: 50%;
+    left: 50%;
+    -webkit-transform: translate(-50%, -50%);
+    transform: translate(-50%, -50%);
+    z-index: -5000;
+}
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.html b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.html
new file mode 100644
index 0000000..8220197
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.html
@@ -0,0 +1,16 @@
+<!--
+~ 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.
+-->
+<img id="loading-anim" [src]="img" alt="loading - please wait" [@loadingState]="running"/>
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.spec.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.spec.ts
new file mode 100644
index 0000000..185c20e
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.spec.ts
@@ -0,0 +1,49 @@
+/*
+ * 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 { LoadingComponent } from './loading.component';
+import {LogService} from '../../log.service';
+import {ConsoleLoggerService} from '../../consolelogger.service';
+import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
+
+describe('LoadingComponent', () => {
+    let log: LogService;
+    let component: LoadingComponent;
+    let fixture: ComponentFixture<LoadingComponent>;
+
+    beforeEach(async(() => {
+        log = new ConsoleLoggerService();
+        TestBed.configureTestingModule({
+            imports: [ BrowserAnimationsModule ],
+            declarations: [ LoadingComponent ],
+            providers: [
+                { provide: LogService, useValue: log }
+            ]
+        })
+        .compileComponents();
+    }));
+
+    beforeEach(() => {
+        fixture = TestBed.createComponent(LoadingComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create', () => {
+        expect(component).toBeTruthy();
+    });
+});
diff --git a/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.ts b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.ts
new file mode 100644
index 0000000..a8e5433
--- /dev/null
+++ b/web/gui2-fw-lib/projects/gui2-fw-lib/src/lib/layer/loading/loading.component.ts
@@ -0,0 +1,110 @@
+/*
+ * 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, Input, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
+import {LogService} from '../../log.service';
+import {animate, state, style, transition, trigger} from '@angular/animations';
+
+const LOADING_IMG_DIR = 'assets/loading/';
+const LOADING_PFX = '/load-';
+const NUM_IMGS = 16;
+
+/**
+ * ONOS GUI - A component that shows the loading icon
+ *
+ * Should be shown if someone has to wait for more than
+ * a certain time for data to be retrieved
+ * Note the animation - there is a pause of 500ms before the images appear
+ * and then it eases in over 200ms
+ */
+@Component({
+    selector: 'onos-loading',
+    templateUrl: './loading.component.html',
+    styleUrls: ['./loading.component.css'],
+    animations: [
+        trigger('loadingState', [
+            state('false', style({
+                opacity: '0.0',
+                'z-index': -5000
+            })),
+            state('true', style({
+                opacity: '1.0',
+                'z-index': 5000
+            })),
+            transition('0 => 1', animate('200ms 500ms ease-in')),
+            transition('1 => 0', animate('200ms ease-out'))
+        ])
+    ]
+})
+export class LoadingComponent implements OnChanges {
+    @Input() theme: string = 'light';
+    @Input() running: boolean;
+
+    speed: number = 8; // Frames per second
+    idx = 1;
+    images: HTMLImageElement[] = [];
+    img: string;
+    task: any;
+
+    constructor(
+        private log: LogService,
+    ) {
+        let idx: number;
+
+        for (idx = 1; idx <= NUM_IMGS ; idx++) {
+            this.addImg('light', idx);
+            this.addImg('dark', idx);
+        }
+
+        this.log.debug('LoadingComponent constructed - images preloaded from', this.fname(1, this.theme));
+    }
+
+    /**
+     * Detects changes in in Input variable
+     * Here we want to detect if running has been enabled or disabled
+     * @param changes
+     */
+    ngOnChanges(changes: SimpleChanges): void {
+        if (changes['running']) {
+            const newRunning: boolean = changes['running'].currentValue;
+
+            if (newRunning) {
+                this.task = setInterval(() => this.nextFrame(), 1000 / this.speed);
+            } else {
+                if (this.task) {
+                    clearInterval(this.task);
+                    this.task = null;
+                }
+            }
+        }
+    }
+
+    private addImg(theme: string, idx: number): void {
+        const img = new Image();
+        img.src = this.fname(idx, theme);
+        this.images.push(img);
+    }
+
+    private fname(i: number, theme: string): string {
+        const z = i > 9 ? '' : '0';
+        return LOADING_IMG_DIR + theme + LOADING_PFX + z + i + '.png';
+    }
+
+    private nextFrame(): void {
+        this.idx = this.idx === 16 ? 1 : this.idx + 1;
+        this.img  = this.fname(this.idx, this.theme);
+    }
+
+}