]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/core/store.service.ts
LP#1819179: Angular value formatter gets link smarts
[Evergreen.git] / Open-ILS / src / eg2 / src / app / core / store.service.ts
1 /**
2  * Store and retrieve data from various sources.
3  *
4  * Data Types:
5  * 1. LocalItem: Stored in window.localStorage and persist indefinitely.
6  * 2. SessionItem: Stored in window.sessionStorage and persist until
7  *    the end of the current browser tab/window.  Data is only available
8  *    to the tab/window where the data was set.
9  * 3. LoginItem: Stored as session cookies and persist until the browser
10  *    is closed.  These values are avalable to all browser windows/tabs.
11  */
12 import {Injectable} from '@angular/core';
13 import {CookieService} from 'ngx-cookie';
14
15 @Injectable({providedIn: 'root'})
16 export class StoreService {
17
18     // Base path for cookie-based storage.
19     // Useful for limiting cookies to subsections of the application.
20     // Store cookies globally by default.
21     // Note cookies shared with /eg/staff must be stored at "/"
22     loginSessionBasePath = '/';
23
24     // Set of keys whose values should disappear at logout.
25     loginSessionKeys: string[] = [
26         'eg.auth.token',
27         'eg.auth.time',
28         'eg.auth.token.oc',
29         'eg.auth.time.oc'
30     ];
31
32     constructor(
33         private cookieService: CookieService) {
34     }
35
36     private parseJson(valJson: string): any {
37         if (valJson === undefined || valJson === null || valJson === '') {
38             return null;
39         }
40         try {
41             return JSON.parse(valJson);
42         } catch (E) {
43             console.error(`Failure to parse JSON: ${E} => ${valJson}`);
44             return null;
45         }
46     }
47
48     /**
49      * Add a an app-local login session key
50      */
51     addLoginSessionKey(key: string): void {
52         this.loginSessionKeys.push(key);
53     }
54
55     setLocalItem(key: string, val: any, isJson?: boolean): void {
56         if (!isJson) {
57             val = JSON.stringify(val);
58         }
59         window.localStorage.setItem(key, val);
60     }
61
62     setSessionItem(key: string, val: any, isJson?: boolean): void {
63         if (!isJson) {
64             val = JSON.stringify(val);
65         }
66         window.sessionStorage.setItem(key, val);
67     }
68
69     setLoginSessionItem(key: string, val: any, isJson?: boolean): void {
70         if (!isJson) {
71             val = JSON.stringify(val);
72         }
73         this.cookieService.put(key, val,
74             {path : this.loginSessionBasePath, secure: true});
75     }
76
77     getLocalItem(key: string): any {
78         return this.parseJson(window.localStorage.getItem(key));
79     }
80
81     getSessionItem(key: string): any {
82         return this.parseJson(window.sessionStorage.getItem(key));
83     }
84
85     getLoginSessionItem(key: string): any {
86         return this.parseJson(this.cookieService.get(key));
87     }
88
89     removeLocalItem(key: string): void {
90         window.localStorage.removeItem(key);
91     }
92
93     removeSessionItem(key: string): void {
94         window.sessionStorage.removeItem(key);
95     }
96
97     removeLoginSessionItem(key: string): void {
98         this.cookieService.remove(key, {path : this.loginSessionBasePath});
99     }
100
101     clearLoginSessionItems(): void {
102         this.loginSessionKeys.forEach(
103             key => this.removeLoginSessionItem(key)
104         );
105     }
106 }
107