|
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {animate, AnimationTriggerMetadata, style, transition, trigger} from '@angular/animations'; |
| 3 | + |
| 4 | +declare const require; |
| 5 | +const bowser = require('bowser'); |
| 6 | + |
| 7 | +@Injectable({ |
| 8 | + providedIn: 'root' |
| 9 | +}) |
| 10 | +export class UtilsHelperService { |
| 11 | + static fadeInOut(): AnimationTriggerMetadata { |
| 12 | + return trigger('fadeInOut', [ |
| 13 | + transition(':enter', [ |
| 14 | + style({opacity: 0}), |
| 15 | + animate(500, style({opacity: 1})) |
| 16 | + ]), |
| 17 | + transition(':leave', [ |
| 18 | + animate(500, style({opacity: 0})) |
| 19 | + ]) |
| 20 | + ]); |
| 21 | + } |
| 22 | + |
| 23 | + static scrollToElement(element) { |
| 24 | + if (element) { |
| 25 | + const distance = window.pageYOffset - Math.abs(element.getBoundingClientRect().y); |
| 26 | + |
| 27 | + window.scroll({ |
| 28 | + behavior: 'smooth', |
| 29 | + left: 0, |
| 30 | + top: element.getBoundingClientRect().top + window.scrollY - 150 |
| 31 | + }); |
| 32 | + |
| 33 | + setTimeout(() => { |
| 34 | + element.focus(); |
| 35 | + element.blur(); // Trigger error messages |
| 36 | + element.focus(); |
| 37 | + }, distance); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static markFormGroupTouched(formGroup) { |
| 42 | + (<any>Object).values(formGroup.controls).forEach(control => { |
| 43 | + control.markAsTouched(); |
| 44 | + |
| 45 | + if (control.controls) { |
| 46 | + UtilsHelperService.markFormGroupTouched(control); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + static isPalindrome(str) { |
| 52 | + const len = Math.floor(str.length / 2); |
| 53 | + for (let i = 0; i < len; i++) { |
| 54 | + if (str[i] !== str[str.length - i - 1]) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + static isBrowserValid() { |
| 62 | + const browser = bowser.getParser(window.navigator.userAgent); |
| 63 | + return browser.satisfies({ |
| 64 | + windows: { |
| 65 | + 'internet explorer': '>10', |
| 66 | + }, |
| 67 | + macos: { |
| 68 | + safari: '>10.1' |
| 69 | + }, |
| 70 | + chrome: '>20.1.1432', |
| 71 | + firefox: '>31', |
| 72 | + opera: '>22' |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments