ronaldkwandy

Untitled

May 23rd, 2019
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. import { switchMap, takeUntil } from 'rxjs/operators';
  4.  
  5. import { DashboardComponent as NgxDashboardComponent, WidgetComponent } from 'ngx-dashboard';
  6.  
  7. import { GaResponse } from 'src/app/gaji-id-resources/gaji-id-api-response.model';
  8. import { UserWidget } from 'src/app/gaji-id-resources/resources/system/user-widget/user-widget.model';
  9. import { Widget } from 'src/app/gaji-id-resources/resources/system/widget/widget.model';
  10.  
  11. import { AuthenticationState } from 'src/app/base/authentication/authentication.state';
  12. import { ContentAlertService } from 'src/app/components/messages/alerts/content-alert/content-alert.service';
  13. import { UiBlockService } from 'src/app/components/ui-block/ui-block.service';
  14. import { UserWidgetService } from 'src/app/gaji-id-resources/resources/system/user-widget/user-widget.service';
  15.  
  16. import { WidgetCatatanComponent } from './widgets/catatan/widget-catatan.component';
  17.  
  18. import * as _ from 'lodash';
  19.  
  20. @Component({
  21.   selector: 'app-dashboard',
  22.   templateUrl: './dashboard.component.html',
  23.   styleUrls: ['./dashboard.component.scss']
  24. })
  25. export class DashboardComponent implements OnInit, OnDestroy {
  26.  
  27.   @ViewChild(NgxDashboardComponent) public dashboard: NgxDashboardComponent;
  28.   public widgetsSize: number[] = [300, 100];
  29.   public dashboardMargin: number = 15;
  30.  
  31.   public widgets: Widget[] = [];
  32.  
  33.   private ngUnsubscribe: Subject<boolean> = new Subject();
  34.  
  35.   constructor(
  36.     private authenticationState: AuthenticationState,
  37.     private contentAlertService: ContentAlertService,
  38.     private uiBlockService: UiBlockService,
  39.     private userWidgetService: UserWidgetService
  40.   ) {
  41.     this.widgets.push({ id: null, version: 0, rootId: null, rootVersion: null, widgetId: '1', widgetName: '1', cols: 1, rows: 2, active: true, size: [1, 2], initialSize: [1, 2] });
  42.     this.widgets.push({ id: null, version: 0, rootId: null, rootVersion: null, widgetId: '2', widgetName: '2', cols: 1, rows: 3, active: true, size: [1, 3], initialSize: [1, 3] });
  43.     this.widgets.push({ id: null, version: 0, rootId: null, rootVersion: null, widgetId: '3', widgetName: '3', cols: 1, rows: 2, active: true, size: [1, 2], initialSize: [1, 2] });
  44.     this.widgets.push({ id: null, version: 0, rootId: null, rootVersion: null, widgetId: '4', widgetName: '4', cols: 2, rows: 4, active: true, size: [2, 4], initialSize: [2, 4] });
  45.   }
  46.  
  47.   ngOnInit() {
  48.     this.dashboard.disableDrag();
  49.  
  50.     /*
  51.       Note :
  52.       For some reason, must declare new instance instead use loop instance.
  53.       Don't know why @_@
  54.     */
  55.     for (let widget of this.widgets) {
  56.       const newWidget = new Widget(widget);
  57.       const ref = this.dashboard.addItem(WidgetCatatanComponent);
  58.       ref.widget = newWidget;
  59.       ref.widgetId = newWidget.widgetId;
  60.       ref.size = [newWidget.cols, newWidget.rows];
  61.       ref.dashboard = this.dashboard;
  62.       ref.widgetsSize = this.widgetsSize;
  63.       ref.onRemove.subscribe(
  64.         (result) => {
  65.           console.log('result : ', result);
  66.           console.log('newWidget : ', widget);
  67.         }
  68.       );
  69.     }
  70.     this.dashboard.refreshWidgets();
  71.     /*
  72.     this.uiBlockService.showUiBlock();
  73.     this.authenticationState.getUserLogin()
  74.       .pipe(
  75.         switchMap((userLogin) => this.userWidgetService.search({ userId: userLogin.id })),
  76.         takeUntil(this.ngUnsubscribe)
  77.       ).subscribe(
  78.         (result: GaResponse<UserWidget[]>) => {
  79.           this.widgets = result.data.map(userWidget => {
  80.             userWidget.widget.size = [userWidget.widget.cols, userWidget.widget.rows];
  81.             userWidget.widget.initialSize = [userWidget.widget.cols, userWidget.widget.rows];
  82.             return userWidget.widget;
  83.           });
  84.           console.log(this.widgets);
  85.         },
  86.         (error) => { this.contentAlertService.error(error.errors); },
  87.         () => {
  88.           // Note :
  89.           // For some reason, must declare new instance instead use loop instance.
  90.           // Don't know why @_@
  91.           for (let widget of this.widgets) {
  92.             const newWidget = new Widget(widget);
  93.             const ref = this.dashboard.addItem(WidgetCatatanComponent);
  94.             ref.widget = newWidget;
  95.             ref.widgetId = newWidget.widgetId;
  96.             ref.size = [newWidget.cols, newWidget.rows];
  97.             ref.dashboard = this.dashboard;
  98.             ref.widgetsSize = this.widgetsSize;
  99.             ref.onRemove.subscribe(
  100.               (result) => {
  101.                 console.log('result : ', result);
  102.                 console.log('newWidget : ', widget);
  103.               }
  104.             );
  105.           }
  106.           this.dashboard.refreshWidgets();
  107.         }
  108.       );
  109.       */
  110.   }
  111.  
  112.   ngOnDestroy() {
  113.     this.ngUnsubscribe.next(true);
  114.     this.ngUnsubscribe.complete();
  115.   }
  116.  
  117.   ngAfterViewInit(): void {
  118.     setTimeout(() => { this._onResize(null); })
  119.   }
  120.  
  121.   private _onResize(event: any) {
  122.     if (window.innerWidth < 750) {
  123.       this.widgetsSize = [this.dashboard.width / 2 - this.dashboardMargin, this.widgetsSize[1]];
  124.     } else {
  125.       const nbColumn = Math.floor(this.dashboard.width / (300 + this.dashboardMargin));
  126.       this.widgetsSize = [this.dashboard.width / nbColumn - this.dashboardMargin, this.widgetsSize[1]];
  127.     }
  128.   }
  129.  
  130.   public log(widget: WidgetComponent, type: string) {
  131.     // console.log(widget, type);
  132.     // console.log('widgets : ', this.widgets);
  133.   }
  134.  
  135.   public logOrder(order: Array<string>) {
  136.     // save order number here!
  137.     console.log(order, 'orderchange');
  138.   }
  139.  
  140.   close(e: any, id: string) {
  141.     this.dashboard.removeItemById(id);
  142.     e.preventDefault();
  143.     e.stopPropagation();
  144.   }
  145.  
  146.   editWidget() {
  147.     if (this.dashboard.dragEnable) {
  148.       this.dashboard.disableDrag();
  149.     } else {
  150.       this.dashboard.enableDrag();
  151.     }
  152.   }
  153.  
  154.   public addWidget() {
  155.     const random = Math.random() + '';
  156.     const newWidget = new Widget({ id: null, version: 0, rootId: null, rootVersion: null, widgetId: random, widgetName: random, cols: 2, rows: 1, active: true, size: [2, 3], initialSize: [2, 3] });
  157.     const ref = this.dashboard.addItem(WidgetCatatanComponent);
  158.     ref.widget = newWidget;
  159.     ref.widgetId = newWidget.widgetId;
  160.     ref.size = [newWidget.cols, newWidget.rows];
  161.     ref.dashboard = this.dashboard;
  162.     ref.widgetsSize = this.widgetsSize;
  163.     ref.onRemove.subscribe(
  164.       (result) => {
  165.         console.log('result : ', result);
  166.         console.log('newWidget : ', newWidget);
  167.       }
  168.     );
  169.  
  170.     this.dashboard.refreshWidgets();
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment