Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. import { Component, OnInit, Inject, OnDestroy, Input, NgZone, ElementRef, EventEmitter, Output } from '@angular/core';
  2. import { ROUTER_DIRECTIVES } from '@angular/router';
  3. import { fromEvent } from 'rxjs/observable/fromEvent';
  4. import { Subscriber, Subscription } from 'rxjs/Rx';
  5. import { RegionMapComponent } from '../../common/region-map/region-map.component';
  6. import { Config } from '../../app.config';
  7.  
  8. let tpl = require('./home-header.template.html');
  9. let style = require('./home-header.css');
  10.  
  11. @Component({
  12. selector: 'home-header',
  13. template: tpl,
  14. styles: [style],
  15. directives: [RegionMapComponent, ROUTER_DIRECTIVES]
  16. })
  17.  
  18. export class HomeHeaderComponent implements OnInit, OnDestroy {
  19. protected home: any = {};
  20. protected mapData: any;
  21. protected math: any;
  22. protected countryName: any;
  23. protected isOpenArticle: boolean = false;
  24. protected familyShortInfoPosition: number = -88;
  25. protected isShowAboutData: boolean = false;
  26. protected isShowAboutDataFullScreen: boolean = false;
  27. protected aboutDataPosition: {left?: number;top?: number;} = {};
  28. protected windowHeight: number = window.innerHeight;
  29. protected maxHeightPopUp: number = this.windowHeight * .95 - 91;
  30. protected Angulartics2GoogleAnalytics: any;
  31.  
  32. @Input('placeId')
  33. private placeId: string;
  34. private homeHeaderService: any;
  35. private homeHeaderServiceSubscribe: Subscriber<any>;
  36. private scrollSubscribe: Subscription;
  37. private resizeSubscribe: Subscription;
  38. private zone: NgZone;
  39. private element: HTMLElement;
  40. private headerElement: HTMLElement;
  41. private headerHeight: number;
  42. private headerContentHeight: number;
  43.  
  44. @Output('familyExpandBlock')
  45. private familyExpandBlock: EventEmitter<any> = new EventEmitter<any>();
  46.  
  47. public constructor(@Inject('HomeHeaderService') homeHeaderService: any,
  48. @Inject(ElementRef) element: ElementRef,
  49. @Inject('Math') math: any,
  50. @Inject(NgZone) zone: NgZone,
  51. @Inject('Angulartics2GoogleAnalytics') Angulartics2GoogleAnalytics: any) {
  52. this.Angulartics2GoogleAnalytics = Angulartics2GoogleAnalytics;
  53. this.homeHeaderService = homeHeaderService;
  54. this.zone = zone;
  55. this.math = math;
  56. this.element = element.nativeElement;
  57. }
  58.  
  59. public ngOnInit(): void {
  60. this.headerElement = document.querySelector('.header-container') as HTMLElement;
  61. this.headerHeight = this.headerElement.offsetHeight;
  62. this.headerContentHeight = this.element.offsetHeight;
  63.  
  64. this.homeHeaderServiceSubscribe = this.homeHeaderService
  65. .getHomeHeaderData(`placeId=${this.placeId}`)
  66. .subscribe((res: any): any => {
  67. if (res.err) {
  68. console.error(res.err);
  69. return;
  70. }
  71.  
  72. this.home = res.data;
  73. this.mapData = this.home.country;
  74. this.truncCountryName(this.home.country);
  75. });
  76.  
  77. this.scrollSubscribe = fromEvent(document, 'scroll')
  78. .subscribe(() => {
  79. let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  80.  
  81. this.zone.run(() => {
  82. if (scrollTop > this.element.offsetHeight - this.headerHeight) {
  83. this.familyShortInfoPosition = this.headerHeight;
  84. } else {
  85. this.familyShortInfoPosition = -this.headerHeight;
  86. }
  87. });
  88. });
  89.  
  90. this.resizeSubscribe = fromEvent(window, 'resize')
  91. .debounceTime(300)
  92. .subscribe(() => {
  93. this.zone.run(() => {
  94. this.windowHeight = window.innerHeight;
  95. this.maxHeightPopUp = this.windowHeight * .95 - 91;
  96. this.headerHeight = this.headerElement.offsetHeight;
  97. this.headerContentHeight = this.element.offsetHeight;
  98. });
  99. });
  100. }
  101.  
  102. public ngOnDestroy(): void {
  103. this.homeHeaderServiceSubscribe.unsubscribe();
  104.  
  105. if (this.resizeSubscribe) {
  106. this.resizeSubscribe.unsubscribe();
  107. }
  108.  
  109. if (this.scrollSubscribe) {
  110. this.scrollSubscribe.unsubscribe();
  111. }
  112. }
  113.  
  114. protected openInfo(isOpenArticle: boolean): void {
  115. this.isOpenArticle = !isOpenArticle;
  116. }
  117.  
  118. protected closeAboutDataPopUp(event: MouseEvent): void {
  119. let el = event && event.target as HTMLElement;
  120.  
  121. if (el.className.indexOf('closeMenu') !== -1) {
  122. this.isShowAboutData = false;
  123. this.isShowAboutDataFullScreen = false;
  124. }
  125. }
  126.  
  127. protected showAboutData(event: MouseEvent, fixed: boolean): void {
  128. if (fixed) {
  129. event.preventDefault();
  130. }
  131.  
  132. if (!arguments.length) {
  133. this.isShowAboutData = false;
  134.  
  135. return;
  136. }
  137.  
  138. if (fixed) {
  139. this.isShowAboutData = true;
  140. this.isShowAboutDataFullScreen = true;
  141.  
  142. return;
  143. }
  144.  
  145. let aboutDataContainer = this.element.querySelector('.about-data-container') as HTMLElement;
  146. let targetElement = event.target as HTMLElement;
  147.  
  148. Config.getCoordinates(`.${targetElement.className}`, (data: any) => {
  149. this.aboutDataPosition.left = data.left - aboutDataContainer.clientWidth + 28;
  150. this.aboutDataPosition.top = data.top + 28;
  151.  
  152. this.isShowAboutData = true;
  153. });
  154. }
  155.  
  156. protected scrollToStart(event: MouseEvent): void {
  157. let targetElement = event.target as HTMLElement;
  158. let elementClassName: string = targetElement.className;
  159.  
  160. if (elementClassName === 'short-about-info-image' || elementClassName === 'portrait') {
  161. return;
  162. }
  163.  
  164. event.preventDefault();
  165.  
  166. this.animateScroll('scrollBackToTop', 20, 1000);
  167. }
  168.  
  169. protected truncCountryName(countryData: any): any {
  170.  
  171. if (countryData.alias.length > 10) {
  172. this.countryName = countryData.alias.slice(0, 7) + '...';
  173. }
  174.  
  175. if (countryData.alias === 'South Africa') {
  176. this.countryName = 'SA';
  177. }
  178.  
  179. if (countryData.alias === 'United States') {
  180. this.countryName = 'USA';
  181. }
  182.  
  183. if (countryData.alias === 'United Kingdom') {
  184. this.countryName = 'UK';
  185. }
  186. }
  187.  
  188. protected openExpandBlock(): void {
  189. this.familyExpandBlock.emit({thingId: this.home.familyThingId});
  190. }
  191.  
  192. private animateScroll(id: string, inc: number, duration: number): any {
  193. const elem = document.getElementById(id);
  194. const startScroll = document.body.scrollTop || document.documentElement.scrollTop;
  195. const endScroll = elem.offsetTop;
  196. const step = (endScroll - startScroll) / duration * inc;
  197.  
  198. window.requestAnimationFrame(this.goToScroll(step, duration, inc));
  199. }
  200.  
  201. private goToScroll(step: number, duration: number, inc: number): any {
  202. return () => {
  203. const currentDuration = duration - inc;
  204.  
  205. this.incScrollTop(step);
  206.  
  207. if (currentDuration < inc) {
  208. return;
  209. }
  210.  
  211. window.requestAnimationFrame(this.goToScroll(step, currentDuration, inc));
  212. };
  213. }
  214.  
  215. private incScrollTop(step: number): void {
  216. if (document.body.scrollTop) {
  217. document.body.scrollTop += step;
  218. } else {
  219. document.documentElement.scrollTop += step;
  220. }
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement