@@ -27,6 +27,28 @@ export class CoreBrowser {
2727 return new RegExp ( `(\\s|;|^)${ name } =` ) . test ( document . cookie ?? '' ) ;
2828 }
2929
30+ /**
31+ * Check whether a development setting is set.
32+ *
33+ * @param name Setting name.
34+ * @returns Whether the development setting is set.
35+ */
36+ static hasDevelopmentSetting ( name : string ) : boolean {
37+ const setting = this . getDevelopmentSettingKey ( name ) ;
38+
39+ return this . hasCookie ( setting ) || this . hasLocalStorage ( setting ) ;
40+ }
41+
42+ /**
43+ * Check whether the given localStorage key is set.
44+ *
45+ * @param key localStorage key.
46+ * @returns Whether the key is set.
47+ */
48+ static hasLocalStorage ( key : string ) : boolean {
49+ return localStorage . getItem ( key ) !== null ;
50+ }
51+
3052 /**
3153 * Read a cookie.
3254 *
@@ -45,4 +67,60 @@ export class CoreBrowser {
4567 return cookies [ name ] ?? null ;
4668 }
4769
70+ /**
71+ * Read a localStorage key.
72+ *
73+ * @param key localStorage key.
74+ * @return localStorage value.
75+ */
76+ static getLocalStorage ( key : string ) : string | null {
77+ return localStorage . getItem ( key ) ;
78+ }
79+
80+ /**
81+ * Get development setting value.
82+ *
83+ * @param name Setting name.
84+ * @returns Development setting value.
85+ */
86+ static getDevelopmentSetting ( name : string ) : string | null {
87+ const setting = this . getDevelopmentSettingKey ( name ) ;
88+
89+ return this . getCookie ( setting ) ?? this . getLocalStorage ( setting ) ;
90+ }
91+
92+ /**
93+ * Set development setting.
94+ *
95+ * @param name Setting name.
96+ * @param value Setting value.
97+ */
98+ static setDevelopmentSetting ( name : string , value : string ) : void {
99+ const setting = this . getDevelopmentSettingKey ( name ) ;
100+
101+ document . cookie = `${ setting } =${ value } ;path=/` ;
102+ localStorage . setItem ( setting , value ) ;
103+ }
104+
105+ /**
106+ * Unset development setting.
107+ *
108+ * @param name Setting name.
109+ */
110+ static clearDevelopmentSetting ( name : string ) : void {
111+ const setting = this . getDevelopmentSettingKey ( name ) ;
112+
113+ document . cookie = `${ setting } =;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT` ;
114+ localStorage . removeItem ( setting ) ;
115+ }
116+
117+ /**
118+ * Get development setting key.
119+ *
120+ * @param name Development setting name.
121+ */
122+ protected static getDevelopmentSettingKey ( name : string ) : string {
123+ return `MoodleApp${ name } ` ;
124+ }
125+
48126}
0 commit comments