Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / launchpad / src / settings.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 function storageAvailable(type) {
20 try {
21 var storage = window[type];
22 var x = '__storage_test_DEADBEEF__';
23 storage.setItem(x, x);
24 storage.removeItem(x);
25 return true;
26 } catch (e) {
27 return false;
28 }
29 }
30
31 export class LaunchpadSettings {
32
33 constructor(args) {
34 this.openedNsrsStorageKey = "openedNsrList";
35 this.debug = (args && args.debug);
36
37 if (!storageAvailable('localStorage')) {
38 this.canUseStorage = false;
39 console.log("Unable to use window.localStorage");
40 //throw "Unable to use window.localStorage";
41
42 } else {
43 this.canUseStorage = true;
44 }
45 }
46 openedNSRs() {
47 if(this.debug) {
48 console.log("LaunchpadSettings.openedNSRs called");
49 }
50 var nsrs = null;
51 if (this.canUseStorage) {
52 let nsrStr = window.localStorage.getItem(this.openedNsrsStorageKey);
53 if (nsrStr) {
54 nsrs = nsrStr.split(',');
55 }
56 if (this.debug) {
57 console.log("got nsr ids=", nsrs);
58 }
59 }
60 return (nsrs) ? nsrs : [];
61 }
62
63 addOpenNSR(nsrId) {
64 let nsrs = this.openedNSRs();
65 if (this.debug) {
66 console.log("LaunchpadSettings.addOpenNSR. nsrs=", nsrs);
67 }
68
69 if (!nsrs.includes(nsrId)) {
70 if (this.debug) {
71 console.log("adding nsr id=", nsrId);
72 }
73 nsrs.unshift(nsrId);
74 window.localStorage.setItem(this.openedNsrsStorageKey, nsrs);
75 }
76 return this;
77
78 }
79
80 removeOpenNSR(nsrId) {
81 if (this.debug) {
82 console.log("LaunchpadSettings.removeOpenNSR called with id:", nsrId);
83 }
84 if (this.canUseStorage) {
85 let nsrs = this.openedNSRs().filter(nsr_id => nsr_id != nsrId);
86 window.localStorage.setItem(this.openedNsrsStorageKey, nsrs);
87 }
88 return this;
89 }
90
91 removeAllOpenNSRs() {
92 if (this.canUseStorage) {
93 window.localStorage.removeItem(this.openedNsrsStorageKey);
94 }
95 }
96 }
97
98 /*
99 module.exports = {
100 LaunchpadSettings: LaunchpadSettings
101 }
102 */