1const UserPreferences = new class { 2 constructor() { 3 this.cookieName = 'user_prefs'; 4 this.expiryDays = 365; 5 6 this.defaultSettings = { 7 "theme": "light", 8 "cookieConsentGiven": false, 9 10 "modifyPlasmidAfterOperation": true, 11 "onlyModifyPlasmid": false, 12 "symmetricPrimers": false, 13 "HRMinLength": 18, 14 "HRTm": 50, 15 "HRSubcloningTm": 55, 16 "TBRTm": 60, 17 "maxTmSi": 49.5, 18 "primerConc": 100, 19 "TmAlgorithm": "oligoCalc", 20 "saltConc": 0, 21 "saltCorr": "SchildkrautLifson", 22 "dmsoConc": 0, 23 24 "useGCClamp": false, 25 26 "baseWidth": 16, 27 28 "preferredOrganism": "Escherichia coli", 29 30 "overwriteSnapGeneColors": true, 31 }; 32 33 const savedPreferences = this.loadPreferences(); 34 //console.log(`UserPreferences -> savedPreferences=\n${JSON.stringify(savedPreferences, null, 2)}`); 35 this.preferences = { ...this.defaultSettings, ...savedPreferences }; 36 //console.log(`UserPreferences -> merged=\n${JSON.stringify(this.preferences, null, 2)}`); 37 this.savePreferences(); 38 }; 39 40 /** 41 * Load preferences from cookie. 42 * 43 * @returns {Object} - Dictionary of settings 44 */ 45 loadPreferences() { 46 const cookies = document.cookie.split('; '); 47 let userPrefsCookie = null; 48 for (let cookie of cookies) { 49 let [key, value] = cookie.split('='); 50 if (key === this.cookieName) { 51 userPrefsCookie = decodeURIComponent(value); 52 }; 53 }; 54 return userPrefsCookie ? JSON.parse(userPrefsCookie) : {}; 55 }; 56 57 58 /** 59 * Save current preferences to the cookie. 60 */ 61 savePreferences() { 62 let expires = ''; 63 if (this.expiryDays) { 64 const date = new Date(); 65 date.setTime(date.getTime() + this.expiryDays * 24 * 60 * 60 * 1000); 66 expires = `; expires=${date.toUTCString()}`; 67 }; 68 69 document.cookie = `${this.cookieName}=${JSON.stringify(this.preferences)}${expires}; path=/`; 70 }; 71 72 73 /** 74 * Delete user preferences cookie 75 */ 76 deleteCookie() { 77 document.cookie = `${this.cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; 78 location.reload(); 79 }; 80 81 82 /** 83 * Set user preference. 84 * 85 * @param {String} key - Name of entry 86 * @param {any} value - Value of entry 87 */ 88 set(key, value) { 89 this.preferences[key] = value; 90 this.savePreferences(); 91 }; 92 93 94 setBulk(prefs) { 95 Object.assign(this.preferences, prefs); 96 this.savePreferences(); 97 }; 98 99 100 /** 101 * Get specific user preference value 102 * 103 * @param {String} key - Name of entry 104 * @returns {any} - Value of entry 105 */ 106 get(key) { 107 console.assert(key in this.preferences, `User preference "${key}" does not exist.`); 108 return this.preferences[key]; 109 }; 110};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.