Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   Directive,
  3.   ElementRef,
  4.   Renderer2,
  5.   HostListener,
  6.   AfterViewInit
  7. } from '@angular/core';
  8.  
  9. @Directive({
  10.   selector: '[appSlider]'
  11. })
  12.  
  13. export class SliderDirective implements AfterViewInit {
  14.  
  15.   filterItems: HTMLCollectionOf<HTMLElement>;
  16.   performanceBlocks: HTMLCollectionOf<HTMLElement>;
  17.   performanceTitle: HTMLCollectionOf<HTMLElement>;
  18.   lastPerformanceIndex: number;
  19.   performanceIndex = 0;
  20.   performanceBlocksWidth = 0;
  21.   performanceTitlesWidth = 0;
  22.  
  23.   constructor(
  24.     private element: ElementRef,
  25.     private renderer: Renderer2) { }
  26.  
  27.   ngAfterViewInit() {
  28.  
  29.     this.filterItems = (document.getElementsByClassName('arrow') as HTMLCollectionOf<HTMLElement>);
  30.     this.performanceBlocks = (document.getElementsByClassName('performance') as HTMLCollectionOf<HTMLElement>);
  31.     this.performanceTitle = (document.getElementsByClassName('table-title') as HTMLCollectionOf<HTMLElement>);
  32.  
  33.     this.lastPerformanceIndex = this.performanceBlocks.length - 1;
  34.  
  35.     for (let i = 0; i < this.performanceBlocks.length; i++) {
  36.       this.performanceBlocksWidth += this.performanceBlocks[i].offsetWidth;
  37.     }
  38.  
  39.     for (let i = 0; i < this.performanceTitle.length; i++) {
  40.       this.performanceTitlesWidth += this.performanceTitle[i].offsetWidth;
  41.     }
  42.  
  43.   }
  44.  
  45.   @HostListener('click') onclick() {
  46.     const e = this.performanceBlocks[0];
  47.     const t = this.performanceTitle[0];
  48.  
  49.     const target = this.element.nativeElement.attributes['data-target'].value;
  50.  
  51.     if (target === 'next') {
  52.  
  53.       if (this.performanceIndex === this.lastPerformanceIndex) {
  54.         this.renderer.setStyle(e, 'margin-left', 0);
  55.         this.renderer.setStyle(t, 'margin-left', 0);
  56.         this.performanceIndex = 0;
  57.       } else {
  58.         this.performanceIndex++;
  59.         this.renderer.setStyle(e, 'margin-left', '-' + e.offsetWidth * this.performanceIndex + 'px');
  60.         this.renderer.setStyle(t, 'margin-left', '-' + t.offsetWidth * this.performanceIndex + 'px');
  61.       }
  62.  
  63.     } else if (target === 'prev') {
  64.  
  65.       if (this.performanceIndex === 0) {
  66.         this.performanceIndex = this.lastPerformanceIndex;
  67.         this.renderer.setStyle(e, 'margin-left', '-' + (this.performanceBlocksWidth - e.offsetWidth) + 'px');
  68.         this.renderer.setStyle(t, 'margin-left', '-' + (this.performanceBlocksWidth - t.offsetWidth) + 'px');
  69.       } else {
  70.         console.log(e['style'].marginLeft)
  71.         console.log(t['style'].marginLeft)
  72.         console.log(typeof (parseInt(t['style'].marginLeft)))
  73.         this.renderer.setStyle(e, 'margin-left', parseInt(e['style'].marginLeft) + e.offsetWidth + 'px');
  74.         this.renderer.setStyle(t, 'margin-left', parseInt(t['style'].marginLeft) + e.offsetWidth + 'px');
  75.         this.performanceIndex--;
  76.       }
  77.  
  78.     }
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement