| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- /**
- * Copyright (c) 2006-2017, JGraph Ltd
- * Copyright (c) 2006-2017, Gaudenz Alder
- */
- /**
- * Contains current settings.
- */
- var mxSettings =
- {
- /**
- * Defines current version of settings.
- */
- currentVersion: 18,
-
- defaultFormatWidth: (screen.width < 600) ? '0' : '240',
-
- // NOTE: Hardcoded in index.html due to timing of JS loading
- key: Editor.settingsKey,
- getLanguage: function()
- {
- return mxSettings.settings.language;
- },
- setLanguage: function(lang)
- {
- mxSettings.settings.language = lang;
- },
- isMainSettings: function()
- {
- return mxSettings.key == '.drawio-config';
- },
- getMainSettings: function()
- {
- var value = localStorage.getItem('.drawio-config');
- if (value == null)
- {
- value = mxSettings.getDefaults();
- delete value.isNew;
- }
- else
- {
- value = JSON.parse(value);
- value.version = mxSettings.currentVersion;
- }
- return value;
- },
- getUi: function()
- {
- return (mxSettings.isMainSettings()) ? mxSettings.settings.ui :
- mxSettings.getMainSettings().ui;
- },
- setUi: function(ui)
- {
- if (mxSettings.isMainSettings())
- {
- mxSettings.settings.ui = ui;
- if (!Editor.enableCssDarkMode && (ui == 'kennedy' || ui == ''))
- {
- mxSettings.settings.darkMode = false;
- }
- mxSettings.save();
- }
- else
- {
- var value = mxSettings.getMainSettings();
- value.ui = ui;
- if (!Editor.enableCssDarkMode && ui == 'kennedy')
- {
- value.darkMode = false;
- }
- localStorage.setItem('.drawio-config', JSON.stringify(value));
- }
- },
- getShowStartScreen: function()
- {
- return mxSettings.settings.showStartScreen;
- },
- setShowStartScreen: function(showStartScreen)
- {
- mxSettings.settings.showStartScreen = showStartScreen;
- },
- getGridColor: function(darkMode)
- {
- return (darkMode) ? mxSettings.settings.darkGridColor : mxSettings.settings.gridColor;
- },
- setGridColor: function(gridColor, darkMode)
- {
- if (darkMode)
- {
- mxSettings.settings.darkGridColor = gridColor;
- }
- else
- {
- mxSettings.settings.gridColor = gridColor;
- }
- },
- getAutosave: function()
- {
- return mxSettings.settings.autosave;
- },
- setAutosave: function(autosave)
- {
- mxSettings.settings.autosave = autosave;
- },
- getResizeImages: function()
- {
- return mxSettings.settings.resizeImages;
- },
- setResizeImages: function(resizeImages)
- {
- mxSettings.settings.resizeImages = resizeImages;
- },
- getOpenCounter: function()
- {
- return mxSettings.settings.openCounter;
- },
- setOpenCounter: function(openCounter)
- {
- mxSettings.settings.openCounter = openCounter;
- },
- setCustomFonts: function(fonts)
- {
- mxSettings.settings.customFonts = fonts;
- },
- getCustomFonts: function()
- {
- //Convert from old format to the new one
- var custFonts = mxSettings.settings.customFonts || [];
-
- for (var i = 0 ; i < custFonts.length; i++)
- {
- if (typeof custFonts[i] === 'string')
- {
- custFonts[i] = {name: custFonts[i], url: null};
- }
- }
-
- return custFonts;
- },
- getLibraries: function()
- {
- return mxSettings.settings.libraries;
- },
- setLibraries: function(libs)
- {
- mxSettings.settings.libraries = libs;
- },
- addCustomLibrary: function(id)
- {
- // Makes sure to update the latest data from the localStorage
- mxSettings.load();
-
- //If the setting is incorrect, reset it to an empty array
- if (!Array.isArray(mxSettings.settings.customLibraries))
- {
- mxSettings.settings.customLibraries = [];
- }
-
- if (mxUtils.indexOf(mxSettings.settings.customLibraries, id) < 0)
- {
- // Makes sure scratchpad is below search in sidebar
- if (id === 'L.scratchpad')
- {
- mxSettings.settings.customLibraries.splice(0, 0, id);
- }
- else
- {
- mxSettings.settings.customLibraries.push(id);
- }
- }
-
- mxSettings.save();
- },
- removeCustomLibrary: function(id)
- {
- // Makes sure to update the latest data from the localStorage
- mxSettings.load();
- mxUtils.remove(id, mxSettings.settings.customLibraries);
- mxSettings.save();
- },
- getCustomLibraries: function()
- {
- return mxSettings.settings.customLibraries;
- },
- getPlugins: function()
- {
- return mxSettings.settings.plugins;
- },
- setPlugins: function(plugins)
- {
- mxSettings.settings.plugins = plugins;
- },
- getRecentColors: function()
- {
- return mxSettings.settings.recentColors;
- },
- setRecentColors: function(recentColors)
- {
- mxSettings.settings.recentColors = recentColors;
- },
- getFormatWidth: function()
- {
- return parseInt(mxSettings.settings.formatWidth);
- },
- setFormatWidth: function(formatWidth)
- {
- mxSettings.settings.formatWidth = formatWidth;
- },
- isCreateTarget: function()
- {
- return mxSettings.settings.createTarget;
- },
- setCreateTarget: function(value)
- {
- mxSettings.settings.createTarget = value;
- },
- getPageFormat: function()
- {
- return mxSettings.settings.pageFormat;
- },
- setPageFormat: function(value)
- {
- mxSettings.settings.pageFormat = value;
- },
- getUnit: function()
- {
- return mxSettings.settings.unit || mxConstants.POINTS;
- },
- setUnit: function(value)
- {
- mxSettings.settings.unit = value;
- },
- isRulerOn: function()
- {
- return mxSettings.settings.isRulerOn;
- },
- setRulerOn: function(value)
- {
- mxSettings.settings.isRulerOn = value;
- },
- getDraftSaveDelay: function()
- {
- return mxSettings.settings.draftSaveDelay;
- },
- setDraftSaveDelay: function(value)
- {
- mxSettings.settings.draftSaveDelay = value;
- },
- getDefaults: function()
- {
- return {
- language: '',
- configVersion: Editor.configVersion,
- customFonts: [],
- libraries: Sidebar.prototype.defaultEntries,
- customLibraries: Editor.defaultCustomLibraries,
- plugins: [],
- recentColors: [],
- formatWidth: mxSettings.defaultFormatWidth,
- createTarget: urlParams['sketch'] == '1',
- pageFormat: mxGraph.prototype.pageFormat,
- search: true,
- showStartScreen: true,
- gridColor: mxGraphView.prototype.defaultGridColor,
- darkGridColor: mxGraphView.prototype.defaultDarkGridColor,
- autosave: !EditorUi.isElectronApp,
- resizeImages: null,
- openCounter: 0,
- version: mxSettings.currentVersion,
- // Only defined and true for new settings which haven't been saved
- isNew: true,
- unit: mxConstants.POINTS,
- isRulerOn: false
- };
- },
- init: function()
- {
- mxSettings.settings = mxSettings.getDefaults();
- },
- save: function()
- {
- if (isLocalStorage && typeof(JSON) !== 'undefined')
- {
- try
- {
- delete mxSettings.settings.isNew;
- mxSettings.settings.version = mxSettings.currentVersion;
- localStorage.setItem(mxSettings.key, JSON.stringify(mxSettings.settings));
- }
- catch (e)
- {
- // ignores quota exceeded
- }
- }
- },
- load: function()
- {
- try
- {
- if (isLocalStorage && typeof(JSON) !== 'undefined')
- {
- mxSettings.parse(localStorage.getItem(mxSettings.key));
- }
- }
- catch (e)
- {
- if (window.console != null)
- {
- console.log('Error loading settings:', mxSettings.key, e);
- }
- }
- if (mxSettings.settings == null)
- {
- mxSettings.init();
- }
- },
- parse: function(value)
- {
- var config = (value != null) ? JSON.parse(value) : null;
- if (config == null || (config.configVersion != Editor.configVersion) ||
- (Editor.config != null && Editor.config.override))
- {
- mxSettings.settings = null;
- mxSettings.init();
- }
- else
- {
- mxSettings.settings = config;
-
- if (mxSettings.settings.plugins == null)
- {
- mxSettings.settings.plugins = [];
- }
-
- if (mxSettings.settings.recentColors == null)
- {
- mxSettings.settings.recentColors = [];
- }
- if (mxSettings.settings.customFonts == null)
- {
- mxSettings.settings.customFonts = [];
- }
-
- if (mxSettings.settings.libraries == null)
- {
- mxSettings.settings.libraries = Sidebar.prototype.defaultEntries;
- }
-
- if (mxSettings.settings.customLibraries == null)
- {
- mxSettings.settings.customLibraries = Editor.defaultCustomLibraries;
- }
-
- if (mxSettings.settings.ui == null)
- {
- mxSettings.settings.ui = '';
- }
-
- if (mxSettings.settings.formatWidth == null)
- {
- mxSettings.settings.formatWidth = mxSettings.defaultFormatWidth;
- }
-
- if (mxSettings.settings.lastAlert != null)
- {
- delete mxSettings.settings.lastAlert;
- }
-
- if (mxSettings.settings.createTarget == null)
- {
- mxSettings.settings.createTarget = false;
- }
-
- if (mxSettings.settings.pageFormat == null)
- {
- mxSettings.settings.pageFormat = mxGraph.prototype.pageFormat;
- }
-
- if (mxSettings.settings.search == null)
- {
- mxSettings.settings.search = true;
- }
-
- if (mxSettings.settings.showStartScreen == null)
- {
- mxSettings.settings.showStartScreen = true;
- }
-
- if (mxSettings.settings.gridColor == null)
- {
- mxSettings.settings.gridColor = mxGraphView.prototype.defaultGridColor;
- }
- if (mxSettings.settings.darkGridColor == null)
- {
- mxSettings.settings.darkGridColor = mxGraphView.prototype.defaultDarkGridColor;
- }
-
- if (mxSettings.settings.autosave == null)
- {
- mxSettings.settings.autosave = !EditorUi.isElectronApp;
- }
- else if (EditorUi.isElectronApp && localStorage.getItem('._autoSaveTrans_') == null) //Transition to no autosave
- {
- localStorage.setItem('._autoSaveTrans_', '1');
- mxSettings.settings.autosave = false;
- mxSettings.save();
- }
-
- if (mxSettings.settings.scratchpadSeen != null)
- {
- delete mxSettings.settings.scratchpadSeen;
- }
- }
- },
- clear: function()
- {
- if (isLocalStorage)
- {
- localStorage.removeItem(mxSettings.key);
- }
- }
- }
- /**
- * Variable: mxLoadSettings
- *
- * Optional global config variable to toggle loading the settings. Default is true.
- *
- * (code)
- * <script type="text/javascript">
- * var mxLoadSettings = false;
- * </script>
- * (end)
- */
- if (typeof(mxLoadSettings) == 'undefined' || mxLoadSettings)
- {
- // Loads initial content
- mxSettings.load();
- }
|