Advertisement
Guest User

Untitled

a guest
May 18th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, Input, OnInit, ViewChild, EventEmitter, ElementRef, HostListener } from '@angular/core';
  2. import { FormGroup } from '@angular/forms';
  3.  
  4. import { Subject } from 'rxjs/Subject';
  5. import { LanguageService } from '../../../services/language/language.service';
  6. import { COUNTRY_PHONE_VALUES } from '../../../settings/constants/country-list.constants';
  7.  
  8. @Component({
  9.     selector: 'adj-dropdown',
  10.     templateUrl: 'dropdown.html',
  11. })
  12. export class DropdownComponent implements OnInit {
  13.     @ViewChild('optionsCont') optionsCont: any;
  14.     @ViewChild('filterInputElement') filterInputElement: any;
  15.     @ViewChild('filterableDataContainer') filterableDataContainer: any;
  16.     @ViewChild('unfilterableDataContainer') unfilterableDataContainer: any;
  17.     @Input() element: any;
  18.     @Input() formGroup: FormGroup;
  19.     wasInside: any;
  20.  
  21.     onChange = new EventEmitter();
  22.     onBlur = new EventEmitter();
  23.     onFocus = new EventEmitter();
  24.  
  25.     public selectedOption: any = null;
  26.     public isOpen = false;
  27.     public isFocused = false;
  28.     public filterString = '';
  29.     public filteredOptions: Array<any> = [];
  30.     public optionsOrientationUp = false;
  31.     private writtenStringTofocuseOption = '';
  32.     public blur$ = new Subject();
  33.     static countryIsSorted = false;
  34.  
  35.     constructor(
  36.         public languageService: LanguageService,
  37.         public el: ElementRef
  38.     ) { }
  39.  
  40.     ngOnInit() {
  41.         if (this.element && this.element.options) {
  42.             if (this.element.control) this.element.control.markAsPristine();
  43.  
  44.            
  45.             if (DropdownComponent.countryIsSorted && (this.element.key === 'country' || this.element.key === 'countryCode')) {
  46.                 this.sortOptionsValueStings(this.element.options);
  47.             }
  48.  
  49.             this.languageService.readyStateSubject
  50.                 .filter(value => value).subscribe(() => {
  51.                     this.element.options.filter((option: any) => option.label && option.label.langId)
  52.                         .map((option: any) => option.label)
  53.                         .forEach(this.languageService.AddObservable.bind(this.languageService));
  54.                     if (this.element.key === 'country' || this.element.key === 'countryCode') {
  55.                         this.sortOptionsValueStings(this.element.options);
  56.                         DropdownComponent.countryIsSorted = true;
  57.                     }
  58.                 });
  59.  
  60.             this.filteredOptions = this.element.options;
  61.             if (this.element.formItem || this.element.control) {
  62.                 (this.element.formItem || this.element.control)
  63.                     .valueChanges
  64.                     .subscribe((value: any) => this.setValue(value, false));
  65.             }
  66.  
  67.             if (this.element.control && this.element.control.value !== this.element.value) {
  68.                 const foundValue = this.element.options.filter((value: any) => value.value === this.element.control.value)[0];
  69.                 this.setOption(foundValue);
  70.             }
  71.         }
  72.         this.blur$.do(() => this.isFocused = false)
  73.             .debounceTime(200)
  74.             .filter(() => !this.isFocused)
  75.             .subscribe(() => this.blur());
  76.         this.setDefault();
  77.     }
  78.  
  79.     @HostListener('click')
  80.     clickInside() {
  81.         this.wasInside = true;
  82.     }
  83.  
  84.     @HostListener('document:click')
  85.     clickout() {
  86.         if (!this.wasInside) {
  87.             this.blur$.next();
  88.         }
  89.         this.wasInside = false;
  90.     }
  91.  
  92.  
  93.     sortOptionsValueStings(options: any[]) {
  94.         options.sort((a: any, b: any) => {
  95.             if (a.label.valueString.toLowerCase().trim() < b.label.valueString.toLowerCase().trim()) {
  96.                 return -1;
  97.             } else if (a.label.valueString.toLowerCase().trim() > b.label.valueString.toLowerCase().trim()) {
  98.                 return 1;
  99.             } else {
  100.                 return 0;
  101.             }
  102.         });
  103.     }
  104.  
  105.     setDefault() {
  106.         if (this.selectedOption) return;
  107.  
  108.         const defaultOption = this.element.value
  109.             ? this.element.options.filter((option: any) => option.value === this.element.value)
  110.             : this.element.options;
  111.  
  112.         if (!defaultOption || !defaultOption.length) return;
  113.         this.setOption(defaultOption[0]);
  114.     }
  115.  
  116.  
  117.     setValue(value: any, emit: boolean = true) {
  118.         const option = this.element.options.filter((r: any) => r && r.value).filter((opt: any) => {
  119.             if (value) return opt.value.toString() === value.toString();
  120.         });
  121.         if (option.length) {
  122.             this.setOption(option[0], emit);
  123.         }
  124.     }
  125.  
  126.     setOption(option: any, emit: boolean = true) {
  127.         if (!option) return;
  128.         this.selectedOption = option ? option : this.selectedOption;
  129.         this.element.value = option.value;
  130.         if (emit) {
  131.             this.element.control = this.element.control ? this.element.control : this.formGroup.controls[this.element.key];
  132.             if (this.element && this.element.control) {
  133.                 if (this.element.value === this.formGroup.controls[this.element.key].value) {
  134.                     return;
  135.                 }
  136.                 this.element.control.setValue(option.value);
  137.             }
  138.             this.onChange.emit(this.selectedOption.value);
  139.             if (option.value && this.element && this.element.control) this.element.control.markAsDirty();
  140.         }
  141.  
  142.     }
  143.  
  144.     itemSelect(option: any, emit: boolean = true) {
  145.         this.setOption(option, emit);
  146.         this.blur$.next();
  147.     }
  148.  
  149.     blur(): void {
  150.         this.isFocused = this.isOpen = false;
  151.         this.onBlur.emit();
  152.     }
  153.  
  154.     focus(event: any) {
  155.         const target = event.srcElement || event.target;
  156.         if (this.element && this.element.params && this.element.params.disabled) return;
  157.         if (target.classList.value === 'open' && this.isOpen) {
  158.             this.blur();
  159.             return;
  160.         }
  161.         this.onFocus.emit();
  162.         if (this.element.control) {
  163.             this.element.control.markAsTouched();
  164.             this.element.control.markAsDirty();
  165.         }
  166.         this.isOpen = true;
  167.         this.isFocused = true;
  168.         this.setOptionsOrientation();
  169.         this.scrollToActive();
  170.         if (this.element && this.element.params && this.element.params.filter) {
  171.             setTimeout(() => {
  172.                 if (!this.filterInputElement) return;
  173.                 this.filterInputElement.nativeElement.focus();
  174.                 this.isFocused = true;
  175.             }, 50);
  176.         }
  177.     }
  178.  
  179.     scrollToActive() {
  180.         setTimeout(() => {
  181.             if (this.el.nativeElement.classList.contains('active') || this.el.nativeElement.querySelector('div span.active')) {
  182.                 const scrollElement = this.el.nativeElement.querySelector('div span.active').offsetTop;
  183.                 this.el.nativeElement.querySelector('div').scrollTop = scrollElement;
  184.             }
  185.         }, 10);
  186.     }
  187.  
  188.     // indent moving on dropdown list
  189.     moveList(arg: any) {
  190.         setTimeout(() => {
  191.             if (this.el.nativeElement.classList.contains('active') || this.el.nativeElement.querySelector('div span.active')) {
  192.                 const scrollElement = this.el.nativeElement.querySelector('div span.active').offsetTop;
  193.                 const scrollElementHeight = this.el.nativeElement.querySelector('div span.active').offsetHeight;
  194.                 const scrollBlock = this.el.nativeElement.querySelector('div').scrollTop;
  195.  
  196.                 if (scrollElement + scrollElementHeight > scrollBlock + this.el.nativeElement.querySelector('div').offsetHeight && arg === 'dn') {
  197.                     this.el.nativeElement.querySelector('div').scrollTop = this.el.nativeElement.querySelector('div').scrollTop + scrollElementHeight;
  198.                 } else if (scrollElement < scrollBlock && arg === 'up') {
  199.                     this.el.nativeElement.querySelector('div').scrollTop = this.el.nativeElement.querySelector('div').scrollTop - scrollElementHeight;
  200.                 }
  201.             }
  202.         }, 10);
  203.     }
  204.  
  205.     setOptionsOrientation() {
  206.         if (!this.optionsCont) return;
  207.         const position = this.optionsCont.nativeElement.getBoundingClientRect();
  208.         this.optionsOrientationUp = position.bottom > 700;
  209.     }
  210.  
  211.     filter(event: any) {
  212.         if (event.keyCode === 38 || event.keyCode === 40 || event.keyCode === 13) {
  213.             this.arrows(event);
  214.         } else {
  215.             const newFilteredOptions = this.filteredOptions.filter((option: any) => {
  216.                 const value = option.label.valueString
  217.                     ? option.label.valueString
  218.                     : this.languageService.GetLangvalue(option.label.langId);
  219.                 return value.toString().toLowerCase().indexOf(this.filterString.toLowerCase()) === 0;
  220.             });
  221.  
  222.             this.setOption(newFilteredOptions[0]);
  223.             this.element.options = newFilteredOptions;
  224.             this.el.nativeElement.querySelector('div').scrollTop = 0;
  225.         }
  226.     }
  227.  
  228.     kd(event: any) {
  229.         event.preventDefault();
  230.         event.stopPropagation();
  231.     }
  232.  
  233.  
  234.     arrows(event: any) {
  235.         if (!this.isFocused) { return; }
  236.         this.kd(event);
  237.         let gotOne: any;
  238.  
  239.         switch (event.keyCode || event.which) {
  240.             case 38: // up
  241.                 let prevOption: any;
  242.                 this.element.options.forEach((option: any, i: number) => {
  243.                     if (gotOne) return;
  244.                     if (option.value === this.selectedOption.value) {
  245.                         gotOne = true;
  246.                         this.setOption(prevOption);
  247.                         this.moveList('up');
  248.                         return;
  249.                     }
  250.                     prevOption = this.element.options[i];
  251.                 });
  252.                 break;
  253.             case 40: // down
  254.                 let getNext: any;
  255.                 this.element.options.forEach((option: any, i: number) => {
  256.                     if (gotOne) return;
  257.                     if (option.value === this.selectedOption.value) {
  258.                         getNext = true;
  259.                         return;
  260.                     }
  261.                     if (getNext) {
  262.                         this.setOption(this.element.options[i]);
  263.                         this.moveList('dn');
  264.                         gotOne = true;
  265.                     }
  266.                 });
  267.                 break;
  268.             case 13: // enter
  269.                 this.blur();
  270.                 break;
  271.             case 9:  // tab
  272.                 const nextElement = Object.keys(this.element.control.parent.controls)
  273.                     .filter(key => this.element.control.parent.controls[key].element.order === this.element.order + 1)
  274.                     .map(key => this.element.control.parent.controls[key])[0];
  275.                 this.blur();
  276.                 break;
  277.             default: // typing when dropdown open and not has filter
  278.                 if (!this.element.params && this.element.params['keyFilter'] === undefined) { return; }
  279.                 this.writtenStringTofocuseOption += event.key === 'Shift' ? '' : event.key;
  280.                 const matchingOption = this.filteredOptions.filter((option: any) => {
  281.                     if (option['label'] !== undefined) {
  282.                         const value = option.label.valueString
  283.                             ? option.label.valueString
  284.                             : this.languageService.GetLangvalue(option.label.langId);
  285.                         return value.toString().toLowerCase().indexOf(this.writtenStringTofocuseOption.toLowerCase()) === 0;
  286.                     } else {
  287.                         return option.value.toString().toLowerCase().indexOf(this.writtenStringTofocuseOption.toLowerCase()) === 0;
  288.                     }
  289.                 });
  290.                 if (matchingOption.length && matchingOption['value'] !== '') {
  291.                     if (matchingOption['value'] === this.writtenStringTofocuseOption) {
  292.                         this.writtenStringTofocuseOption = '';
  293.                     } else {
  294.                         this.setOption(matchingOption[0]);
  295.                         this.scrollToActive();
  296.                     }
  297.  
  298.                 } else {
  299.                     this.setOption(this.filteredOptions[0]);
  300.                     this.scrollToActive();
  301.                     this.writtenStringTofocuseOption = event.key;
  302.                 }
  303.                 break;
  304.         }
  305.     }
  306.  
  307.     changeToCountryCode(value: any) {
  308.         const filtered = COUNTRY_PHONE_VALUES.filter(s => s.c.toString() === value.toString())[0];
  309.         return filtered ? filtered.m : '';
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement