Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
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 =
  30.       (document.getElementsByClassName('arrow') as HTMLCollectionOf<HTMLElement>);
  31.  
  32.     this.performanceBlocks =
  33.       (document.getElementsByClassName('performance') as HTMLCollectionOf<HTMLElement>);
  34.  
  35.     this.performanceTitle =
  36.       (document.getElementsByClassName('table-title') as HTMLCollectionOf<HTMLElement>);
  37.  
  38.     this.lastPerformanceIndex = this.performanceBlocks.length - 1;
  39.  
  40.     for (let i = 0; i < this.performanceBlocks.length; i++) {
  41.       this.performanceBlocksWidth += this.performanceBlocks[i].offsetWidth;
  42.     }
  43.  
  44.     for (let i = 0; i < this.performanceTitle.length; i++) {
  45.       this.performanceTitlesWidth += this.performanceTitle[i].offsetWidth;
  46.     }
  47.  
  48.   }
  49.  
  50.   @HostListener('click') onclick() {
  51.     const e = this.performanceBlocks[0];
  52.     const t = this.performanceTitle[0];
  53.  
  54.     const target = this.element.nativeElement.attributes['data-target'].value;
  55.  
  56.     if (target === 'next') {
  57.  
  58.       if (this.performanceIndex === this.lastPerformanceIndex) {
  59.         this.renderer.setStyle(
  60.           e,
  61.           'margin-left',
  62.           0);
  63.  
  64.         this.renderer.setStyle(
  65.           t,
  66.           'margin-left',
  67.           0);
  68.  
  69.         this.performanceIndex = 0;
  70.       } else {
  71.         this.performanceIndex++;
  72.         this.renderer.setStyle(
  73.           e,
  74.           'margin-left',
  75.           '-' + e.offsetWidth * this.performanceIndex + 'px');
  76.         this.renderer.setStyle(
  77.           t,
  78.           'margin-left',
  79.           '-' + t.offsetWidth * this.performanceIndex + 'px');
  80.       }
  81.  
  82.     } else if (target === 'prev') {
  83.  
  84.       if (this.performanceIndex === 0) {
  85.         this.performanceIndex = this.lastPerformanceIndex;
  86.         this.renderer.setStyle(
  87.           e,
  88.           'margin-left',
  89.           '-' + (this.performanceBlocksWidth - e.offsetWidth) + 'px');
  90.         this.renderer.setStyle(
  91.           t,
  92.           'margin-left',
  93.           '-' + (this.performanceBlocksWidth - t.offsetWidth) + 'px');
  94.       } else {
  95.         console.log(typeof (parseInt(t['style'].marginLeft)))
  96.         this.renderer.setStyle(
  97.           e,
  98.           'margin-left',
  99.           parseInt(e['style'].marginLeft) + e.offsetWidth + 'px');
  100.         this.renderer.setStyle(
  101.           t,
  102.           'margin-left',
  103.           parseInt(t['style'].marginLeft) + e.offsetWidth + 'px');
  104.         this.performanceIndex--;
  105.       }
  106.  
  107.     }
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement