blob: fc07332d7408bbcf20bd557da004e4897ec5a783 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080019import * as $ from 'jquery';
Matteo Scandolo4222a432017-01-23 12:18:40 -080020import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers';
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080021
22export interface IXosSidePanelService {
23 open(): void;
24 close(): void;
25 injectComponent(componentName: string, attributes?: any, transclude?: string): void;
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080026 removeInjectedComponents(): void;
Max Chu40529442017-08-02 17:07:57 -070027 toggleComponent(componentName: string, attributes?: any, transclude?: string): void;
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080028}
29
30export class XosSidePanel implements IXosSidePanelService {
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080031 static $inject = ['$rootScope', '$compile', '$timeout', 'XosComponentInjector'];
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080032 public sidePanelElName = 'xos-side-panel';
33 public sidePanelElClass = '.xos-side-panel';
34 public sidePanelEl: JQuery;
Matteo Scandolo520a8a12017-03-10 17:31:37 -080035 private hasComponentLoaded: boolean;
Max Chu40529442017-08-02 17:07:57 -070036 private componentToggle = false;
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080037
38 constructor (
39 private $rootScope: ng.IRootScopeService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080040 private $compile: ng.ICompileService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080041 private $timeout: ng.ITimeoutService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080042 private XosComponentInjector: IXosComponentInjectorService
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080043 ) {
44 this.sidePanelEl = $(`${this.sidePanelElName} > ${this.sidePanelElClass}`);
45 }
46
47 public open() {
48 $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).addClass('open');
49 };
50
51 public close() {
52 $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).removeClass('open');
53 };
54
55 public injectComponent(componentName: string, attributes?: any, transclude?: string) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -080056 let timeout = 0;
57 if (this.hasComponentLoaded) {
58 this.removeInjectedComponents();
59 timeout = 501; // wait for panel to close and remove component
60 }
61 this.$timeout(() => {
62 this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true);
63 this.hasComponentLoaded = true;
64 this.open();
65 }, timeout);
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080066 }
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080067
68 public removeInjectedComponents() {
69 this.close();
70 this.$timeout(() => {
71 this.XosComponentInjector.removeInjectedComponents('#side-panel-container');
Matteo Scandolo520a8a12017-03-10 17:31:37 -080072 this.hasComponentLoaded = false;
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080073 }, 500);
74 }
Max Chu7ae40f72017-08-02 17:07:57 -070075
76 public toggleComponent(componentName: string, attributes?: any, transclude?: string) {
77 this.componentToggle = !this.componentToggle;
78 if (this.componentToggle) {
79 this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true);
80 this.open();
81 }
82 else {
83 this.removeInjectedComponents();
84 }
85 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080086}