2023-06-25 00:05:01 +02:00
|
|
|
import * as padUtils from "./pad_utils.js";
|
2023-06-25 13:22:01 +02:00
|
|
|
import {JQueryGritter} from "../module/CustomWindow";
|
|
|
|
import {i18nextvar} from "./vendors/i18next";
|
2020-12-20 07:15:58 +00:00
|
|
|
'use strict';
|
2011-03-26 13:10:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2023-06-25 00:05:01 +02:00
|
|
|
const Cookies = { Cookies: padUtils }.Cookies;
|
|
|
|
export const padcookie = new class {
|
2023-06-25 13:22:01 +02:00
|
|
|
cookieName_: string;
|
2023-06-25 00:05:01 +02:00
|
|
|
constructor() {
|
|
|
|
this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp';
|
2020-10-02 23:46:41 -04:00
|
|
|
}
|
2023-06-25 00:05:01 +02:00
|
|
|
init() {
|
|
|
|
const prefs = this.readPrefs_() || {};
|
|
|
|
delete prefs.userId;
|
|
|
|
delete prefs.name;
|
|
|
|
delete prefs.colorId;
|
|
|
|
this.writePrefs_(prefs);
|
|
|
|
// Re-read the saved cookie to test if cookies are enabled.
|
|
|
|
if (this.readPrefs_() == null) {
|
2023-06-25 13:22:01 +02:00
|
|
|
($ as unknown as JQueryGritter).gritter.add({
|
2023-06-25 00:05:01 +02:00
|
|
|
title: 'Error',
|
2023-06-25 13:22:01 +02:00
|
|
|
text: i18nextvar('pad.noCookie'),
|
2023-06-25 00:05:01 +02:00
|
|
|
sticky: true,
|
|
|
|
class_name: 'error',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readPrefs_() {
|
|
|
|
try {
|
2023-06-25 13:22:01 +02:00
|
|
|
// @ts-ignore
|
2023-06-25 00:05:01 +02:00
|
|
|
const json = Cookies.get(this.cookieName_);
|
|
|
|
if (json == null)
|
|
|
|
return null;
|
|
|
|
return JSON.parse(json);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writePrefs_(prefs) {
|
2023-06-25 13:22:01 +02:00
|
|
|
// @ts-ignore
|
2023-06-25 00:05:01 +02:00
|
|
|
Cookies.set(this.cookieName_, JSON.stringify(prefs), { expires: 365 * 100 });
|
|
|
|
}
|
|
|
|
getPref(prefName) {
|
|
|
|
return this.readPrefs_()[prefName];
|
|
|
|
}
|
|
|
|
setPref(prefName, value) {
|
|
|
|
const prefs = this.readPrefs_();
|
|
|
|
prefs[prefName] = value;
|
|
|
|
this.writePrefs_(prefs);
|
|
|
|
}
|
|
|
|
clear() {
|
|
|
|
this.writePrefs_({});
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2020-10-02 23:46:41 -04:00
|
|
|
}();
|