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; |
Max Chu | fba1085 | 2017-08-02 17:07:57 -0700 | [diff] [blame] | 9 | toggleComponent(componentName: string, attributes?: any, transclude?: string): void; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | export class XosSidePanel implements IXosSidePanelService { |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 13 | static $inject = ['$rootScope', '$compile', '$timeout', 'XosComponentInjector']; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 14 | public sidePanelElName = 'xos-side-panel'; |
| 15 | public sidePanelElClass = '.xos-side-panel'; |
| 16 | public sidePanelEl: JQuery; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 17 | private hasComponentLoaded: boolean; |
Max Chu | 9cdd953 | 2017-08-02 17:03:54 -0700 | [diff] [blame] | 18 | private componentToggle = false; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 19 | |
| 20 | constructor ( |
| 21 | private $rootScope: ng.IRootScopeService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 22 | private $compile: ng.ICompileService, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 23 | private $timeout: ng.ITimeoutService, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 24 | private XosComponentInjector: IXosComponentInjectorService |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 25 | ) { |
| 26 | this.sidePanelEl = $(`${this.sidePanelElName} > ${this.sidePanelElClass}`); |
| 27 | } |
| 28 | |
| 29 | public open() { |
| 30 | $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).addClass('open'); |
| 31 | }; |
| 32 | |
| 33 | public close() { |
| 34 | $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).removeClass('open'); |
| 35 | }; |
| 36 | |
| 37 | public injectComponent(componentName: string, attributes?: any, transclude?: string) { |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 38 | let timeout = 0; |
| 39 | if (this.hasComponentLoaded) { |
| 40 | this.removeInjectedComponents(); |
| 41 | timeout = 501; // wait for panel to close and remove component |
| 42 | } |
| 43 | this.$timeout(() => { |
| 44 | this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true); |
| 45 | this.hasComponentLoaded = true; |
| 46 | this.open(); |
| 47 | }, timeout); |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 48 | } |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 49 | |
| 50 | public removeInjectedComponents() { |
| 51 | this.close(); |
| 52 | this.$timeout(() => { |
| 53 | this.XosComponentInjector.removeInjectedComponents('#side-panel-container'); |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 54 | this.hasComponentLoaded = false; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 55 | }, 500); |
| 56 | } |
Max Chu | 9cdd953 | 2017-08-02 17:03:54 -0700 | [diff] [blame] | 57 | |
| 58 | public toggleComponent(componentName: string, attributes?: any, transclude?: string) { |
| 59 | this.componentToggle = !this.componentToggle; |
| 60 | if (this.componentToggle) { |
| 61 | this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true); |
| 62 | this.open(); |
| 63 | } |
| 64 | else { |
| 65 | this.removeInjectedComponents(); |
| 66 | } |
| 67 | } |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 68 | } |