1/**
2 * Copyright (c) 2006-2017, JGraph Ltd
3 * Copyright (c) 2006-2017, Gaudenz Alder
4 */
5/**
6 * Contains current settings.
7 */
8var mxSettings =
9{
10	/**
11	 * Defines current version of settings.
12	 */
13	currentVersion: 18,
14
15	defaultFormatWidth: (screen.width < 600) ? '0' : '240',
16
17	// NOTE: Hardcoded in index.html due to timing of JS loading
18	key: Editor.settingsKey,
19
20	getLanguage: function()
21	{
22		return mxSettings.settings.language;
23	},
24	setLanguage: function(lang)
25	{
26		mxSettings.settings.language = lang;
27	},
28	getUi: function()
29	{
30		return mxSettings.settings.ui;
31	},
32	setUi: function(ui)
33	{
34		// Writes to main configuration
35		var value = localStorage.getItem('.drawio-config');
36
37		if (value == null)
38		{
39			value = mxSettings.getDefaults();
40		}
41		else
42		{
43			value = JSON.parse(value);
44		}
45
46		value.ui = ui;
47
48		delete value.isNew;
49		value.version = mxSettings.currentVersion;
50		localStorage.setItem('.drawio-config', JSON.stringify(value));
51	},
52	getShowStartScreen: function()
53	{
54		return mxSettings.settings.showStartScreen;
55	},
56	setShowStartScreen: function(showStartScreen)
57	{
58		mxSettings.settings.showStartScreen = showStartScreen;
59	},
60	getGridColor: function(darkMode)
61	{
62		return (darkMode) ? mxSettings.settings.darkGridColor : mxSettings.settings.gridColor;
63	},
64	setGridColor: function(gridColor, darkMode)
65	{
66		if (darkMode)
67		{
68			mxSettings.settings.darkGridColor = gridColor;
69		}
70		else
71		{
72			mxSettings.settings.gridColor = gridColor;
73		}
74	},
75	getAutosave: function()
76	{
77		return mxSettings.settings.autosave;
78	},
79	setAutosave: function(autosave)
80	{
81		mxSettings.settings.autosave = autosave;
82	},
83	getResizeImages: function()
84	{
85		return mxSettings.settings.resizeImages;
86	},
87	setResizeImages: function(resizeImages)
88	{
89		mxSettings.settings.resizeImages = resizeImages;
90	},
91	getOpenCounter: function()
92	{
93		return mxSettings.settings.openCounter;
94	},
95	setOpenCounter: function(openCounter)
96	{
97		mxSettings.settings.openCounter = openCounter;
98	},
99	setCustomFonts: function(fonts)
100	{
101		mxSettings.settings.customFonts = fonts;
102	},
103	getCustomFonts: function()
104	{
105		//Convert from old format to the new one
106		var custFonts = mxSettings.settings.customFonts || [];
107
108		for (var i = 0 ; i < custFonts.length; i++)
109		{
110			if (typeof custFonts[i] === 'string')
111			{
112				custFonts[i] = {name: custFonts[i], url: null};
113			}
114		}
115
116		return custFonts;
117	},
118	getLibraries: function()
119	{
120		return mxSettings.settings.libraries;
121	},
122	setLibraries: function(libs)
123	{
124		mxSettings.settings.libraries = libs;
125	},
126	addCustomLibrary: function(id)
127	{
128		// Makes sure to update the latest data from the localStorage
129		mxSettings.load();
130
131		//If the setting is incorrect, reset it to an empty array
132		if (!Array.isArray(mxSettings.settings.customLibraries))
133		{
134			mxSettings.settings.customLibraries = [];
135		}
136
137		if (mxUtils.indexOf(mxSettings.settings.customLibraries, id) < 0)
138		{
139			// Makes sure scratchpad is below search in sidebar
140			if (id === 'L.scratchpad')
141			{
142				mxSettings.settings.customLibraries.splice(0, 0, id);
143			}
144			else
145			{
146				mxSettings.settings.customLibraries.push(id);
147			}
148		}
149
150		mxSettings.save();
151	},
152	removeCustomLibrary: function(id)
153	{
154		// Makes sure to update the latest data from the localStorage
155		mxSettings.load();
156		mxUtils.remove(id, mxSettings.settings.customLibraries);
157		mxSettings.save();
158	},
159	getCustomLibraries: function()
160	{
161		return mxSettings.settings.customLibraries;
162	},
163	getPlugins: function()
164	{
165		return mxSettings.settings.plugins;
166	},
167	setPlugins: function(plugins)
168	{
169		mxSettings.settings.plugins = plugins;
170	},
171	getRecentColors: function()
172	{
173		return mxSettings.settings.recentColors;
174	},
175	setRecentColors: function(recentColors)
176	{
177		mxSettings.settings.recentColors = recentColors;
178	},
179	getFormatWidth: function()
180	{
181		return parseInt(mxSettings.settings.formatWidth);
182	},
183	setFormatWidth: function(formatWidth)
184	{
185		mxSettings.settings.formatWidth = formatWidth;
186	},
187	isCreateTarget: function()
188	{
189		return mxSettings.settings.createTarget;
190	},
191	setCreateTarget: function(value)
192	{
193		mxSettings.settings.createTarget = value;
194	},
195	getPageFormat: function()
196	{
197		return mxSettings.settings.pageFormat;
198	},
199	setPageFormat: function(value)
200	{
201		mxSettings.settings.pageFormat = value;
202	},
203	getUnit: function()
204	{
205		return mxSettings.settings.unit || mxConstants.POINTS;
206	},
207	setUnit: function(value)
208	{
209		mxSettings.settings.unit = value;
210	},
211	isRulerOn: function()
212	{
213		return mxSettings.settings.isRulerOn;
214	},
215	setRulerOn: function(value)
216	{
217		mxSettings.settings.isRulerOn = value;
218	},
219	getDefaults: function()
220	{
221		return {
222			language: '',
223			configVersion: Editor.configVersion,
224			customFonts: [],
225			libraries: Sidebar.prototype.defaultEntries,
226			customLibraries: Editor.defaultCustomLibraries,
227			plugins: [],
228			recentColors: [],
229			formatWidth: mxSettings.defaultFormatWidth,
230			createTarget: urlParams['sketch'] == '1',
231			pageFormat: mxGraph.prototype.pageFormat,
232			search: true,
233			showStartScreen: true,
234			gridColor: mxGraphView.prototype.defaultGridColor,
235			darkGridColor: mxGraphView.prototype.defaultDarkGridColor,
236			autosave: true,
237			resizeImages: null,
238			openCounter: 0,
239			version: mxSettings.currentVersion,
240			// Only defined and true for new settings which haven't been saved
241			isNew: true,
242			unit: mxConstants.POINTS,
243			isRulerOn: false
244		};
245	},
246	init: function()
247	{
248		mxSettings.settings = mxSettings.getDefaults();
249	},
250	save: function()
251	{
252		if (isLocalStorage && typeof(JSON) !== 'undefined')
253		{
254			try
255			{
256				delete mxSettings.settings.isNew;
257				mxSettings.settings.version = mxSettings.currentVersion;
258				localStorage.setItem(mxSettings.key, JSON.stringify(mxSettings.settings));
259			}
260			catch (e)
261			{
262				// ignores quota exceeded
263			}
264		}
265	},
266	load: function()
267	{
268		if (isLocalStorage && typeof(JSON) !== 'undefined')
269		{
270			mxSettings.parse(localStorage.getItem(mxSettings.key));
271		}
272
273		if (mxSettings.settings == null)
274		{
275			mxSettings.init();
276		}
277	},
278	parse: function(value)
279	{
280		var config = (value != null) ? JSON.parse(value) : null;
281
282		if (config == null || (config.configVersion != Editor.configVersion) ||
283			(Editor.config != null && Editor.config.override))
284		{
285			mxSettings.settings = null;
286			mxSettings.init();
287		}
288		else
289		{
290			mxSettings.settings = config;
291
292			if (mxSettings.settings.plugins == null)
293			{
294				mxSettings.settings.plugins = [];
295			}
296
297			if (mxSettings.settings.recentColors == null)
298			{
299				mxSettings.settings.recentColors = [];
300			}
301
302			if (mxSettings.settings.customFonts == null)
303			{
304				mxSettings.settings.customFonts = [];
305			}
306
307			if (mxSettings.settings.libraries == null)
308			{
309				mxSettings.settings.libraries = Sidebar.prototype.defaultEntries;
310			}
311
312			if (mxSettings.settings.customLibraries == null)
313			{
314				mxSettings.settings.customLibraries = Editor.defaultCustomLibraries;
315			}
316
317			if (mxSettings.settings.ui == null)
318			{
319				mxSettings.settings.ui = '';
320			}
321
322			if (mxSettings.settings.formatWidth == null)
323			{
324				mxSettings.settings.formatWidth = mxSettings.defaultFormatWidth;
325			}
326
327			if (mxSettings.settings.lastAlert != null)
328			{
329				delete mxSettings.settings.lastAlert;
330			}
331
332			if (mxSettings.settings.createTarget == null)
333			{
334				mxSettings.settings.createTarget = false;
335			}
336
337			if (mxSettings.settings.pageFormat == null)
338			{
339				mxSettings.settings.pageFormat = mxGraph.prototype.pageFormat;
340			}
341
342			if (mxSettings.settings.search == null)
343			{
344				mxSettings.settings.search = true;
345			}
346
347			if (mxSettings.settings.showStartScreen == null)
348			{
349				mxSettings.settings.showStartScreen = true;
350			}
351
352			if (mxSettings.settings.gridColor == null)
353			{
354				mxSettings.settings.gridColor = mxGraphView.prototype.defaultGridColor;
355			}
356
357			if (mxSettings.settings.darkGridColor == null)
358			{
359				mxSettings.settings.darkGridColor = mxGraphView.prototype.defaultDarkGridColor;
360			}
361
362			if (mxSettings.settings.autosave == null)
363			{
364				mxSettings.settings.autosave = true;
365			}
366
367			if (mxSettings.settings.scratchpadSeen != null)
368			{
369				delete mxSettings.settings.scratchpadSeen;
370			}
371		}
372	},
373	clear: function()
374	{
375		if (isLocalStorage)
376		{
377			localStorage.removeItem(mxSettings.key);
378		}
379	}
380}
381
382/**
383 * Variable: mxLoadSettings
384 *
385 * Optional global config variable to toggle loading the settings. Default is true.
386 *
387 * (code)
388 * <script type="text/javascript">
389 * 		var mxLoadSettings = false;
390 * </script>
391 * (end)
392 */
393if (typeof(mxLoadSettings) == 'undefined' || mxLoadSettings)
394{
395	// Loads initial content
396	mxSettings.load();
397}
398