etherpad-lite/src/static/js/pad_cookie.ts

70 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-06-25 00:05:01 +02:00
import * as padUtils from "./pad_utils.js";
import {JQueryGritter} from "../module/CustomWindow";
import {i18nextvar} from "./vendors/i18next";
'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 {
cookieName_: string;
2023-06-25 00:05:01 +02:00
constructor() {
this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp';
}
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) {
($ as unknown as JQueryGritter).gritter.add({
2023-06-25 00:05:01 +02:00
title: 'Error',
text: i18nextvar('pad.noCookie'),
2023-06-25 00:05:01 +02:00
sticky: true,
class_name: 'error',
});
}
}
readPrefs_() {
try {
// @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) {
// @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
}
}();