import { Injectable } from "@angular/core"; import { Api } from "../services/api"; import { Base, IRslt } from "../../public-api"; @Injectable({ providedIn: 'root' }) export class Utility { isLoaded: boolean = false; siteID:string = ''; location:string = ''; base:string = ''; constructor(private api: Api) { if (window.location.hostname === 'localhost') { //local this.location = 'local' } else if (window.location.hostname.indexOf('stage') >= 0) { //stage this.location = 'stage' } else { //production this.location = 'prod' } } public deepEqual(x:any, y:any) { const ok = Object.keys, tx = typeof x, ty = typeof y; const t:any = x && y && tx === 'object' && tx === ty ? ( ok(x).length === ok(y).length && x.toString() === y.toString() && ok(x).every(key => this.deepEqual(x[key], y[key])) ) : (x === y); return t; } setToken(token: string) { this.api.token = token } updateRslt(to: Array, from: IRslt>) { from.results.forEach(rslt => { const found = to.findIndex(f => f._id === rslt._id); if (found >= 0) { to[found] = rslt as T } else { to.push(rslt as T) } }) } update(to: Array, from: Array) { from.forEach(rslt => { const found = to.findIndex(f => f._id === rslt._id); if (found >= 0) { to[found] = rslt as T } else { to.push(rslt as T) } }) } // startup(token: string) { // // const token = this.getToken(); // // if (token) { // // // this.user.get(); // // // this.product.get(); // // // this.account.get(); // // this.router.navigateByUrl('/dashboard') // // this.isLoaded = true; // // } // if(!this.api.token){ // this.router.navigateByUrl('/') // } // // return this.isLoaded; // } // refresh() { // // const token = this.getToken(); // if (this.api.token) { // // this.user.get(); // // this.product.get(); // // this.account.get(); // this.router.navigateByUrl('/products') // this.isLoaded = true; // } // } // repeatLocation(){ // this.timer = setInterval(this.checkPermission, 5000); // } // checkPermission() { // navigator.permissions.query({ name: 'geolocation' }) // .then(t => { // this.isGeolocation = t.state === 'granted' // this.canGeoPrompt = t.state === 'prompt' // if (this.isGeolocation) { // this.getCurrLocation(f => {}); // } // }) // } // getCurrLocation(callback) { // const self = this; // navigator.geolocation.getCurrentPosition(function (location) { // self.currCoord = location; // callback(location.coords.speed) // console.log(location.coords.latitude); // console.log(location.coords.longitude); // console.log(location.coords.accuracy); // self.isGeolocation = true; // self.canGeoPrompt = false; // }); // } /** * Check localhost for token. * Uses token to login if found. * Success results in a user. * UI will reflect a successful login with user data */ storeToken(token: string) { if (typeof localStorage !== 'undefined'){ localStorage.setItem('auth', token); this.api.token = token; } } getToken() { if (typeof localStorage !== 'undefined') return localStorage.getItem('auth') return ''; } }