Advertisement
Guest User

frfr

a guest
Mar 30th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. import {
  2. Component,
  3. ViewChild,
  4. ElementRef,
  5. OnDestroy,
  6. OnInit,
  7. ChangeDetectionStrategy,
  8. ChangeDetectorRef,
  9. NgZone
  10. } from '@angular/core';
  11. import { ApiService } from '../../../api/api.service';
  12.  
  13. @Component({
  14. selector: 'app-live-offers-bar',
  15. templateUrl: './live-offers-bar.component.html',
  16. styleUrls: ['./live-offers-bar.component.scss'],
  17. changeDetection: ChangeDetectionStrategy.OnPush
  18. })
  19. export class LiveOffersBarComponent implements OnInit, OnDestroy {
  20. PAN_ACTION = {
  21. LEFT: 'panleft',
  22. RIGHT: 'panright'
  23. };
  24.  
  25. list: Array<{
  26. platform: string,
  27. slug: string,
  28. name: string,
  29. price: number,
  30. discount: number,
  31. }> = [];
  32.  
  33. updated = false;
  34.  
  35. @ViewChild('ticker') ticker: ElementRef;
  36. @ViewChild('container') container: ElementRef;
  37. style: any;
  38.  
  39. // ticker: ElementRef;
  40.  
  41. offset = 0;
  42.  
  43. interval: any;
  44.  
  45. transform: any;
  46.  
  47. constructor(private apiService: ApiService,
  48. private changeDetectorRef: ChangeDetectorRef,
  49. private zone: NgZone) {
  50. this.changeDetectorRef = changeDetectorRef;
  51. this.changeDetectorRef.detach();
  52. console.log(this.ticker)
  53. }
  54.  
  55. ngOnInit() {
  56. this.loadList();
  57. }
  58.  
  59. ngOnDestroy() {
  60. this.zone.runOutsideAngular(() => {
  61. cancelAnimationFrame(this.interval);
  62. });
  63. }
  64.  
  65. loadList() {
  66. this.updated = true;
  67.  
  68. this.apiService.getLatestOffers(30).subscribe((data: any) => {
  69. data = data;
  70.  
  71.  
  72.  
  73.  
  74. const list = data.map(item => ({
  75. platform: item.platformSlug,
  76. slug: item.slug,
  77. name: item.name,
  78. price: item.lowest_price,
  79. discount: item.official_price > item.lowest_price ? Math.floor(100 - ((item.lowest_price / item.official_price) * 100)) : null,
  80. })).filter(item => !!item.discount);
  81.  
  82. this.list = list;
  83. this.updated = false;
  84. this.changeDetectorRef.detectChanges();
  85.  
  86. // this.zone.runOutsideAngular(() => {
  87. // setTimeout(() => this.play(), 50);
  88. // });
  89.  
  90. let sliderWidth = 0;
  91. let animationWidth = 0;
  92. const liveTimeOffers = document.getElementsByClassName('live-offers__item') as HTMLCollection;
  93. const sliderHeight = this.ticker.nativeElement.offsetHeight;
  94. for (let i = 0; i < liveTimeOffers.length; i++) {
  95. console.dir((liveTimeOffers[i] as HTMLElement))
  96. animationWidth += (liveTimeOffers[i] as HTMLElement).offsetWidth;
  97. }
  98. console.dir(sliderHeight)
  99. console.log(animationWidth)
  100. // if (activeEditions.length > 0) {
  101. // for (let i = 0; i < activeEditions.length; i++) {
  102. // (activeEditions[i] as HTMLElement).click()
  103. // }
  104. // }
  105. this.zone.runOutsideAngular(() => {
  106. const containerWidth = this.container.nativeElement.offsetWidth;
  107.  
  108. let slidesVisible = containerWidth / (animationWidth / liveTimeOffers.length);
  109. slidesVisible = Math.ceil(slidesVisible);
  110. console.log(slidesVisible)
  111.  
  112. const slidesNumber = liveTimeOffers.length;
  113. const speed = slidesNumber * 5;
  114.  
  115.  
  116.  
  117. // const elo = Array.from(document.getElementsByClassName('live-offers__item') as HTMLCollection)
  118. // .slice(0, slidesVisible)
  119. // console.log(elo)
  120. // $('>div',container).css({'width':sliderWidth,'height':sliderHeight});
  121.  
  122. this.style = {'width': sliderWidth, 'height': sliderHeight};
  123.  
  124. // console.log($('>div',container))
  125. const animation = 'elo'
  126. const styleElement = document.createElement('style');
  127. styleElement.type = 'text/css';
  128. styleElement.innerHTML = '@keyframes ' +
  129. animation +
  130. ' { 0% { margin-left: 0px; } 100% { margin-left: -'+
  131. animationWidth +
  132. 'px; } } '+
  133. '.first'+
  134. ' { -webkit-animation: '+
  135. animation+' '+
  136. speed+"s linear infinite; -moz-animation: "
  137. +animation+" "+speed+
  138. "s linear infinite; -ms-animation: "+animation+
  139. " "+speed+"s linear infinite; -o-animation: " +
  140. animation+" "+speed+"s linear infinite; animation: "+
  141. animation+" "+speed + 's linear infinite; }'
  142. document.head.appendChild(styleElement);
  143. // Insert styles to html
  144. console.dir(liveTimeOffers[0] as HTMLElement)
  145. // $("<style type='text/css'>@keyframes "+animation+" { 0% { margin-left: 0px; } 100% { margin-left: -"+animationWidth+"px; } } "+$('>div>div:first-of-type',container).selector+" { -webkit-animation: "+animation+" "+speed+"s linear infinite; -moz-animation: "+animation+" "+speed+"s linear infinite; -ms-animation: "+animation+" "+speed+"s linear infinite; -o-animation: "+animation+" "+speed+"s linear infinite; animation: "+animation+" "+speed+"s linear infinite; }</style>");
  146. })
  147. });
  148. }
  149.  
  150. // tick() {
  151. // const tickerOffsetWidth = this.ticker.nativeElement.offsetWidth;
  152.  
  153. // if (!!this.interval) {
  154. // this.zone.runOutsideAngular(() => {
  155. // cancelAnimationFrame(this.interval);
  156. // });
  157. // }
  158.  
  159. // if (tickerOffsetWidth <= 0) {
  160. // return;
  161. // }
  162.  
  163. // if (this.offset >= tickerOffsetWidth) {
  164. // this.offset = 0;
  165. // this.loadList();
  166. // return;
  167. // }
  168.  
  169. // this.offset++;
  170. // this.offset = this.offset - 0.4;
  171.  
  172. // this.transform = { transform: `translate3d(-${this.offset}px, 0, 0)` };
  173. // this.changeDetectorRef.detectChanges();
  174.  
  175. // // this.zone.runOutsideAngular(() => {
  176. // this.interval = requestAnimationFrame(() => this.tick());
  177. // // });
  178. // }
  179.  
  180. // play() {
  181. // // this.zone.runOutsideAngular(() => {
  182. // this.interval = requestAnimationFrame(() => this.tick());
  183. // // });
  184. // }
  185.  
  186. // pause() {
  187. // // this.zone.runOutsideAngular(() => {
  188. // cancelAnimationFrame(this.interval);
  189. // // });
  190. // }
  191.  
  192. // pan(event) {
  193. // if (event.type === this.PAN_ACTION.RIGHT) {
  194. // if (this.offset < 0) {
  195. // this.offset = 0;
  196. // }
  197. // this.offset -= 4;
  198. // }
  199. // if (event.type === this.PAN_ACTION.LEFT) {
  200. // this.offset += 4;
  201. // }
  202. // }
  203.  
  204. // moved(event) {
  205. // event.preventDefault();
  206.  
  207. // this.zone.runOutsideAngular(() => {
  208. // cancelAnimationFrame(this.interval);
  209. // });
  210.  
  211. // this.transform = { transform: `translate3d(-${this.offset}px, 0, 0)` };
  212. // this.changeDetectorRef.detectChanges();
  213. // }
  214.  
  215. // trackById(index: number, item: { id: number }) {
  216. // return item.id;
  217. // }
  218. // function initSmoothScrolling(container,animation){
  219. // /*
  220. // * @param {String} container Class or ID of the animation container
  221. // * @param {String} animation Name of the animation, e.g. smoothscroll
  222. // */
  223.  
  224. // const sliderWidth = 0;
  225. // const animationWidth = 0;
  226. // var sliderHeight = $('>div>div:first-of-type',container).outerHeight(false);
  227.  
  228. // $('>div>div', container).each(function(){
  229. // animationWidth += $(this).outerWidth(false);
  230. // });
  231.  
  232. // // detect number of visible slides
  233. // var slidesVisible = $(container).width() / $('>div>div:first-of-type',container).outerWidth(false);
  234. // slidesVisible = Math.ceil(slidesVisible);
  235.  
  236. // // count slides to determine animation speed
  237. // var slidesNumber = $('>div>div', container).length;
  238. // var speed = slidesNumber*2;
  239.  
  240. // // append the tail
  241. // $('>div>div',container).slice(0,slidesVisible).clone().appendTo($('>div',container));
  242.  
  243. // // Detect the slider width with appended tail
  244. // $('>div>div', container).each(function(){
  245. // sliderWidth += $(this).outerWidth(false);
  246. // });
  247.  
  248. // // set slider dimensions
  249. // $('>div',container).css({'width':sliderWidth,'height':sliderHeight});
  250.  
  251. // // Insert styles to html
  252. // $("<style type='text/css'>@keyframes "+animation+" { 0% { margin-left: 0px; } 100% { margin-left: -"+animationWidth+"px; } } "+$('>div>div:first-of-type',container).selector+" { -webkit-animation: "+animation+" "+speed+"s linear infinite; -moz-animation: "+animation+" "+speed+"s linear infinite; -ms-animation: "+animation+" "+speed+"s linear infinite; -o-animation: "+animation+" "+speed+"s linear infinite; animation: "+animation+" "+speed+"s linear infinite; }</style>").appendTo("head");
  253.  
  254. // // restart the animation (e.g. for safari & ie)
  255. // var cl = $(container).attr("class");
  256. // $(container).removeClass(cl).animate({'nothing':null}, 1, function () {
  257. // $(this).addClass(cl);
  258. // });
  259. // }
  260. }
  261.  
  262.  
  263. <div class="live-offers">
  264. <h5 class="live-offers__label">
  265. {{ 'live_offers_bar.label' | translate }}
  266. </h5>
  267. <div class="live-offers__container"
  268. [class.live-offers__container--updated]="updated"
  269. #container
  270. [ngStyle]="style">
  271. <ul class="live-offers__list" #ticker>
  272. <ng-container *ngTemplateOutlet="items"></ng-container>
  273. </ul>
  274. <!-- <ul class="live-offers__list" [ngStyle]="transform">
  275. <ng-container *ngTemplateOutlet="items"></ng-container>
  276. </ul> -->
  277. </div>
  278. <div>
  279.  
  280. <ng-template #items>
  281. <li class="live-offers__item" *ngFor="let item of list; trackBy: trackById; let first = first" [ngClass]="{'first': first}">
  282. <div routerLink="/product/{{ item.slug }}"
  283. class="offer"
  284. >
  285. <span class="offer__platform offer__platform--{{item.platform}}"></span>
  286. <span class="offer__name">{{item.name}}</span>
  287. <span class="offer__price"> {{ 'live_offers_bar.from' | translate }}&nbsp;<b>{{item.price | exchange }}</b></span>
  288. <span class="offer__discount">&minus;{{item.discount}}%</span>
  289. </div>
  290. </li>
  291. </ng-template>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement