999949f384a3659f456ed94e8cf15d570720b2dd
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Private / TypeScript / Storage.ts
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 import Client = require('./Storage/Client');
15 import Persistent = require('./Storage/Persistent');
16
17 /**
18 * Module: TYPO3/CMS/Backend/Storage
19 * Adds a public API for the browsers' localStorage called
20 * TYPO3.Storage.Client and the Backend Users "uc",
21 * available via TYPO3.Storage.Persistent
22 * @exports TYPO3/CMS/Backend/Storage
23 * @deprecated
24 */
25 class Storage {
26 public Client: any;
27 public Persistent: any;
28
29 constructor() {
30 this.Client = {
31 clear: (): any => {
32 this.logDeprecated('Client', 'clear');
33 Client.clear();
34 },
35 get: (key: string): any => {
36 this.logDeprecated('Client', 'get');
37 return Client.get(key);
38 },
39 isset: (key: string): any => {
40 this.logDeprecated('Client', 'isset');
41 return Client.isset(key);
42 },
43 set: (key: string, value: string): any => {
44 this.logDeprecated('Client', 'set');
45 return Client.set(key, value);
46 },
47 unset: (key: string): any => {
48 this.logDeprecated('Client', 'unset');
49 return Client.unset(key);
50 },
51 };
52 this.Persistent = {
53 addToList: (key: string, value: string): any => {
54 this.logDeprecated('Persistent', 'addToList');
55 return Persistent.addToList(key, value);
56 },
57 clear: (): any => {
58 this.logDeprecated('Persistent', 'clear');
59 Persistent.clear();
60 },
61 get: (key: string): any => {
62 this.logDeprecated('Persistent', 'get');
63 return Persistent.get(key);
64 },
65 isset: (key: string): any => {
66 this.logDeprecated('Persistent', 'isset');
67 return Persistent.isset(key);
68 },
69 load: (data: any): any => {
70 this.logDeprecated('Persistent', 'load');
71 return Persistent.load(data);
72 },
73 removeFromList: (key: string, value: string): any => {
74 this.logDeprecated('Persistent', 'removeFromList');
75 return Persistent.removeFromList(key, value);
76 },
77 set: (key: string, value: string): any => {
78 this.logDeprecated('Persistent', 'set');
79 return Persistent.set(key, value);
80 },
81 unset: (key: string): any => {
82 this.logDeprecated('Persistent', 'unset');
83 return Persistent.unset(key);
84 },
85 };
86 }
87
88 private logDeprecated = (className: string, methodName: string): any => {
89 if (console) {
90 console.warn(
91 'top.TYPO3.Storage.' + className + '.' + methodName + '() is marked as deprecated since TYPO3 v9 and will be '
92 + 'removed in TYPO3 v10.',
93 );
94 }
95 }
96 }
97
98 let storageObject;
99 try {
100 // fetch from opening window
101 if (window.opener && window.opener.TYPO3 && window.opener.TYPO3.Storage) {
102 storageObject = window.opener.TYPO3.Storage;
103 }
104
105 // fetch from parent
106 if (parent && parent.window.TYPO3 && parent.window.TYPO3.Storage) {
107 storageObject = parent.window.TYPO3.Storage;
108 }
109
110 // fetch object from outer frame
111 if (top && top.TYPO3.Storage) {
112 storageObject = top.TYPO3.Storage;
113 }
114 } catch (e) {
115 // this only happens if the opener, parent or top is some other url (eg a local file)
116 // which loaded the current window. Then the browser's cross domain policy jumps in
117 // and raises an exception.
118 // for this case we are safe and we can create our global object below.
119 }
120
121 if (!storageObject) {
122 storageObject = new Storage();
123 }
124
125 TYPO3.Storage = storageObject;
126 export = storageObject;