87 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-07-16 11:41:58 -03:00
/*
2021-12-25 14:39:34 -08:00
* Copyright [2021] [wisemapping]
2021-07-16 11:41:58 -03:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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.
*/
import PersistenceManager from './PersistenceManager';
2021-07-16 11:41:58 -03:00
2021-12-04 15:39:20 -08:00
class LocalStorageManager extends PersistenceManager {
2022-01-31 00:05:22 +00:00
private documentUrl: string;
private forceLoad: boolean;
2022-03-08 10:19:02 -08:00
private readOnly: boolean;
constructor(documentUrl: string, forceLoad: boolean, readOnly = true) {
2021-12-04 15:39:20 -08:00
super();
2021-10-04 17:05:34 -07:00
this.documentUrl = documentUrl;
this.forceLoad = forceLoad;
2022-03-08 10:19:02 -08:00
this.readOnly = readOnly;
2021-12-04 15:39:20 -08:00
}
2021-07-16 11:41:58 -03:00
2022-03-13 16:36:39 -03:00
saveMapXml(mapId: string, mapDoc: Document, _pref: string, _saveHistory: boolean, events): void {
2022-02-13 18:06:50 -08:00
const mapXml = new XMLSerializer().serializeToString(mapDoc);
2022-03-08 10:19:02 -08:00
if (!this.readOnly) {
localStorage.setItem(`${mapId}-xml`, mapXml);
2022-03-13 16:36:39 -03:00
events.onSuccess();
2022-03-08 10:19:02 -08:00
}
2022-03-13 16:36:39 -03:00
console.log(`Map XML to save => ${mapXml}`);
2021-12-04 15:39:20 -08:00
}
2021-07-16 11:41:58 -03:00
2022-01-31 00:05:22 +00:00
discardChanges(mapId: string) {
2022-03-08 10:19:02 -08:00
if (!this.readOnly) {
localStorage.removeItem(`${mapId}-xml`);
}
2021-12-04 15:39:20 -08:00
}
2021-07-16 11:41:58 -03:00
2022-11-15 22:45:59 -08:00
loadMapDom(mapId: string): Promise<Document> {
let result: Promise<Document>;
let localStorate;
2022-03-08 10:19:02 -08:00
if (!this.readOnly) {
2022-11-15 22:45:59 -08:00
localStorate = localStorage.getItem(`${mapId}-xml`);
2022-03-08 10:19:02 -08:00
}
2022-11-15 22:45:59 -08:00
if (localStorate == null || this.forceLoad) {
const url = this.documentUrl.replace('{id}', mapId);
result = fetch(url, {
method: 'get',
2022-07-13 01:45:36 +00:00
headers: {
'Content-Type': 'text/plain',
Accept: 'application/xml',
'X-CSRF-Token': this.getCSRFToken(),
},
2022-11-15 22:45:59 -08:00
})
.then((response: Response) => {
if (!response.ok) {
console.error(`load error: ${response.status}`);
throw new Error(`load error: ${response.status}, ${response.statusText}`);
}
return response.text();
})
.then((xmlStr) => new DOMParser().parseFromString(xmlStr, 'text/xml'));
} else {
const doc = new DOMParser().parseFromString(localStorate, 'text/xml');
result = Promise.resolve(doc);
2021-07-16 11:41:58 -03:00
}
2022-11-15 22:45:59 -08:00
return result;
2021-12-04 15:39:20 -08:00
}
2021-10-04 17:05:34 -07:00
2022-02-16 12:43:53 -08:00
unlockMap(): void {
2021-10-04 17:05:34 -07:00
// Ignore, no implementation required ...
2021-12-04 15:39:20 -08:00
}
}
2021-07-16 11:41:58 -03:00
export default LocalStorageManager;