blob: 385f5346c46ec26c76f18850807c5306c7109bb3 [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;
Max Chu9cdd9532017-08-02 17:03:54 -070017 private componentToggle = false;
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080018
19 constructor (
20 private $rootScope: ng.IRootScopeService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080021 private $compile: ng.ICompileService,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080022 private $timeout: ng.ITimeoutService,
Matteo Scandolo4222a432017-01-23 12:18:40 -080023 private XosComponentInjector: IXosComponentInjectorService
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080024 ) {
25 this.sidePanelEl = $(`${this.sidePanelElName} > ${this.sidePanelElClass}`);
26 }
27
28 public open() {
29 $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).addClass('open');
30 };
31
32 public close() {
33 $(`${this.sidePanelElName} > ${this.sidePanelElClass}`).removeClass('open');
34 };
35
36 public injectComponent(componentName: string, attributes?: any, transclude?: string) {
Matteo Scandolo520a8a12017-03-10 17:31:37 -080037 let timeout = 0;
38 if (this.hasComponentLoaded) {
39 this.removeInjectedComponents();
40 timeout = 501; // wait for panel to close and remove component
41 }
42 this.$timeout(() => {
43 this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true);
44 this.hasComponentLoaded = true;
45 this.open();
46 }, timeout);
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080047 }
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080048
49 public removeInjectedComponents() {
50 this.close();
51 this.$timeout(() => {
52 this.XosComponentInjector.removeInjectedComponents('#side-panel-container');
Matteo Scandolo520a8a12017-03-10 17:31:37 -080053 this.hasComponentLoaded = false;
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080054 }, 500);
55 }
Max Chu9cdd9532017-08-02 17:03:54 -070056
57 public toggleComponent(componentName: string, attributes?: any, transclude?: string) {
58 this.componentToggle = !this.componentToggle;
59 if (this.componentToggle) {
60 this.XosComponentInjector.injectComponent('#side-panel-container', componentName, attributes, transclude, true);
61 this.open();
62 }
63 else {
64 this.removeInjectedComponents();
65 }
66 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080067}