blob: 6857f7d40b07fe03a8a13c09c8f724b33fb5bf16 [file] [log] [blame]
Matteo Scandolo9d7940c2017-01-19 18:28:43 -08001import * as $ from 'jquery';
Matteo Scandolo4222a432017-01-23 12:18:40 -08002import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers';
Matteo Scandolo9d7940c2017-01-19 18:28:43 -08003
4export interface IXosSidePanelService {
5 open(): void;
6 close(): void;
7 injectComponent(componentName: string, attributes?: any, transclude?: string): void;
Matteo Scandolo5053cbe2017-01-31 17:37:56 -08008 removeInjectedComponents(): void;
Matteo Scandolo9d7940c2017-01-19 18:28:43 -08009}
10
11export class XosSidePanel implements IXosSidePanelService {
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080012 static $inject = ['$rootScope', '$compile', '$timeout', 'XosComponentInjector'];
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080013 public sidePanelElName = 'xos-side-panel';
14 public sidePanelElClass = '.xos-side-panel';
15 public sidePanelEl: JQuery;
Matteo Scandolo520a8a12017-03-10 17:31:37 -080016 private hasComponentLoaded: boolean;
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080017
18 constructor (
19 private $rootScope: ng.IRootScopeService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080020 private $compile: ng.ICompileService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080021 private $timeout: ng.ITimeoutService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080022 private XosComponentInjector: IXosComponentInjectorService
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080023 ) {
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 Scandolo520a8a12017-03-10 17:31:37 -080036 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 Scandolo9d7940c2017-01-19 18:28:43 -080046 }
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080047
48 public removeInjectedComponents() {
49 this.close();
50 this.$timeout(() => {
51 this.XosComponentInjector.removeInjectedComponents('#side-panel-container');
Matteo Scandolo520a8a12017-03-10 17:31:37 -080052 this.hasComponentLoaded = false;
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080053 }, 500);
54 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080055}