Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 1 | import * as $ from 'jquery'; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 2 | import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers'; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 3 | |
| 4 | export interface IXosSidePanelService { |
| 5 | open(): void; |
| 6 | close(): void; |
| 7 | injectComponent(componentName: string, attributes?: any, transclude?: string): void; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 8 | removeInjectedComponents(): void; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | export class XosSidePanel implements IXosSidePanelService { |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 12 | static $inject = ['$rootScope', '$compile', '$timeout', 'XosComponentInjector']; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 13 | public sidePanelElName = 'xos-side-panel'; |
| 14 | public sidePanelElClass = '.xos-side-panel'; |
| 15 | public sidePanelEl: JQuery; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 16 | private hasComponentLoaded: boolean; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 17 | |
| 18 | constructor ( |
| 19 | private $rootScope: ng.IRootScopeService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 20 | private $compile: ng.ICompileService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 21 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 22 | private XosComponentInjector: IXosComponentInjectorService |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 23 | ) { |
| 24 | this.sidePanelEl = $(`${this.sidePanelElName} > ${this.sidePanelElClass}`); |
| 25 | } |
| 26 | |
| 27 | public open() { |
| 28 | $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).addClass('open'); |
| 29 | }; |
| 30 | |
| 31 | public close() { |
| 32 | $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).removeClass('open'); |
| 33 | }; |
| 34 | |
| 35 | public injectComponent(componentName: string, attributes?: any, transclude?: string) { |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 36 | let timeout = 0; |
| 37 | if (this.hasComponentLoaded) { |
| 38 | this.removeInjectedComponents(); |
| 39 | timeout = 501; // wait for panel to close and remove component |
| 40 | } |
| 41 | this.$timeout(() => { |
| 42 | this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true); |
| 43 | this.hasComponentLoaded = true; |
| 44 | this.open(); |
| 45 | }, timeout); |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 46 | } |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 47 | |
| 48 | public removeInjectedComponents() { |
| 49 | this.close(); |
| 50 | this.$timeout(() => { |
| 51 | this.XosComponentInjector.removeInjectedComponents('#side-panel-container'); |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 52 | this.hasComponentLoaded = false; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 53 | }, 500); |
| 54 | } |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 55 | } |