Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 19 | import * as $ from 'jquery'; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 20 | import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers'; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 21 | |
| 22 | export interface IXosSidePanelService { |
| 23 | open(): void; |
| 24 | close(): void; |
| 25 | injectComponent(componentName: string, attributes?: any, transclude?: string): void; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 26 | removeInjectedComponents(): void; |
Max Chu | 4052944 | 2017-08-02 17:07:57 -0700 | [diff] [blame] | 27 | toggleComponent(componentName: string, attributes?: any, transclude?: string): void; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | export class XosSidePanel implements IXosSidePanelService { |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 31 | static $inject = ['$rootScope', '$compile', '$timeout', 'XosComponentInjector']; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 32 | public sidePanelElName = 'xos-side-panel'; |
| 33 | public sidePanelElClass = '.xos-side-panel'; |
| 34 | public sidePanelEl: JQuery; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 35 | private hasComponentLoaded: boolean; |
Max Chu | 4052944 | 2017-08-02 17:07:57 -0700 | [diff] [blame] | 36 | private componentToggle = false; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 37 | |
| 38 | constructor ( |
| 39 | private $rootScope: ng.IRootScopeService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 40 | private $compile: ng.ICompileService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 41 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 42 | private XosComponentInjector: IXosComponentInjectorService |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 43 | ) { |
| 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 Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 56 | 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 Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 66 | } |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 67 | |
| 68 | public removeInjectedComponents() { |
| 69 | this.close(); |
| 70 | this.$timeout(() => { |
| 71 | this.XosComponentInjector.removeInjectedComponents('#side-panel-container'); |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 72 | this.hasComponentLoaded = false; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 73 | }, 500); |
| 74 | } |
Max Chu | 7ae40f7 | 2017-08-02 17:07:57 -0700 | [diff] [blame] | 75 | |
| 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 Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 86 | } |