Settings.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. /**
  6. * Contains current settings.
  7. */
  8. var mxSettings =
  9. {
  10. /**
  11. * Defines current version of settings.
  12. */
  13. currentVersion: 14,
  14. defaultFormatWidth: (screen.width < 600) ? '0' : '240',
  15. // NOTE: Hardcoded in index.html due to timing of JS loading
  16. key: '.drawio-config',
  17. getLanguage: function()
  18. {
  19. return mxSettings.settings.language;
  20. },
  21. setLanguage: function(lang)
  22. {
  23. mxSettings.settings.language = lang;
  24. },
  25. getUi: function()
  26. {
  27. return mxSettings.settings.ui;
  28. },
  29. setUi: function(ui)
  30. {
  31. mxSettings.settings.ui = ui;
  32. },
  33. getShowStartScreen: function()
  34. {
  35. return mxSettings.settings.showStartScreen;
  36. },
  37. setShowStartScreen: function(showStartScreen)
  38. {
  39. mxSettings.settings.showStartScreen = showStartScreen;
  40. },
  41. getGridColor: function()
  42. {
  43. return mxSettings.settings.gridColor;
  44. },
  45. setGridColor: function(gridColor)
  46. {
  47. mxSettings.settings.gridColor = gridColor;
  48. },
  49. getAutosave: function()
  50. {
  51. return mxSettings.settings.autosave;
  52. },
  53. setAutosave: function(autosave)
  54. {
  55. mxSettings.settings.autosave = autosave;
  56. },
  57. getLibraries: function()
  58. {
  59. return mxSettings.settings.libraries;
  60. },
  61. setLibraries: function(libs)
  62. {
  63. mxSettings.settings.libraries = libs;
  64. },
  65. addCustomLibrary: function(id)
  66. {
  67. // Makes sure to update the latest data from the localStorage
  68. mxSettings.load();
  69. if (mxUtils.indexOf(mxSettings.settings.customLibraries, id) < 0)
  70. {
  71. // Makes sure scratchpad is below search in sidebar
  72. if (id === 'L.scratchpad')
  73. {
  74. mxSettings.settings.customLibraries.splice(0, 0, id);
  75. }
  76. else
  77. {
  78. mxSettings.settings.customLibraries.push(id);
  79. }
  80. }
  81. mxSettings.save();
  82. },
  83. removeCustomLibrary: function(id)
  84. {
  85. // Makes sure to update the latest data from the localStorage
  86. mxSettings.load();
  87. mxUtils.remove(id, mxSettings.settings.customLibraries);
  88. mxSettings.save();
  89. },
  90. getCustomLibraries: function()
  91. {
  92. return mxSettings.settings.customLibraries;
  93. },
  94. getPlugins: function()
  95. {
  96. return mxSettings.settings.plugins;
  97. },
  98. setPlugins: function(plugins)
  99. {
  100. mxSettings.settings.plugins = plugins;
  101. },
  102. getRecentColors: function()
  103. {
  104. return mxSettings.settings.recentColors;
  105. },
  106. setRecentColors: function(recentColors)
  107. {
  108. mxSettings.settings.recentColors = recentColors;
  109. },
  110. getFormatWidth: function()
  111. {
  112. return parseInt(mxSettings.settings.formatWidth);
  113. },
  114. setFormatWidth: function(formatWidth)
  115. {
  116. mxSettings.settings.formatWidth = formatWidth;
  117. },
  118. getCurrentEdgeStyle: function()
  119. {
  120. return mxSettings.settings.currentEdgeStyle;
  121. },
  122. setCurrentEdgeStyle: function(value)
  123. {
  124. mxSettings.settings.currentEdgeStyle = value;
  125. },
  126. getCurrentVertexStyle: function()
  127. {
  128. return mxSettings.settings.currentVertexStyle;
  129. },
  130. setCurrentVertexStyle: function(value)
  131. {
  132. mxSettings.settings.currentVertexStyle = value;
  133. },
  134. isCreateTarget: function()
  135. {
  136. return mxSettings.settings.createTarget;
  137. },
  138. setCreateTarget: function(value)
  139. {
  140. mxSettings.settings.createTarget = value;
  141. },
  142. getPageFormat: function()
  143. {
  144. return mxSettings.settings.pageFormat;
  145. },
  146. setPageFormat: function(value)
  147. {
  148. mxSettings.settings.pageFormat = value;
  149. },
  150. init: function()
  151. {
  152. mxSettings.settings =
  153. {
  154. language: '',
  155. libraries: Sidebar.prototype.defaultEntries,
  156. customLibraries: Editor.defaultCustomLibraries,
  157. plugins: [],
  158. recentColors: [],
  159. formatWidth: mxSettings.defaultFormatWidth,
  160. currentEdgeStyle: Graph.prototype.defaultEdgeStyle,
  161. currentVertexStyle: Graph.prototype.defaultVertexStyle,
  162. createTarget: false,
  163. pageFormat: mxGraph.prototype.pageFormat,
  164. search: true,
  165. showStartScreen: true,
  166. gridColor: mxGraphView.prototype.gridColor,
  167. autosave: true,
  168. version: mxSettings.currentVersion,
  169. // Only defined and true for new settings which haven't been saved
  170. isNew: true
  171. };
  172. },
  173. save: function()
  174. {
  175. if (isLocalStorage && typeof(JSON) !== 'undefined')
  176. {
  177. try
  178. {
  179. delete mxSettings.settings.isNew;
  180. mxSettings.settings.version = mxSettings.currentVersion;
  181. localStorage.setItem(mxSettings.key, JSON.stringify(mxSettings.settings));
  182. }
  183. catch (e)
  184. {
  185. // ignores quota exceeded
  186. }
  187. }
  188. },
  189. load: function()
  190. {
  191. if (isLocalStorage && typeof(JSON) !== 'undefined')
  192. {
  193. mxSettings.parse(localStorage.getItem(mxSettings.key));
  194. }
  195. if (mxSettings.settings == null)
  196. {
  197. mxSettings.init();
  198. }
  199. },
  200. parse: function(value)
  201. {
  202. if (value != null)
  203. {
  204. mxSettings.settings = JSON.parse(value);
  205. if (mxSettings.settings.plugins == null)
  206. {
  207. mxSettings.settings.plugins = [];
  208. }
  209. if (mxSettings.settings.recentColors == null)
  210. {
  211. mxSettings.settings.recentColors = [];
  212. }
  213. if (mxSettings.settings.libraries == null)
  214. {
  215. mxSettings.settings.libraries = Sidebar.prototype.defaultEntries;
  216. }
  217. if (mxSettings.settings.customLibraries == null)
  218. {
  219. mxSettings.settings.customLibraries = Editor.defaultCustomLibraries;
  220. }
  221. if (mxSettings.settings.ui == null)
  222. {
  223. mxSettings.settings.ui = '';
  224. }
  225. if (mxSettings.settings.formatWidth == null)
  226. {
  227. mxSettings.settings.formatWidth = mxSettings.defaultFormatWidth;
  228. }
  229. if (mxSettings.settings.lastAlert != null)
  230. {
  231. delete mxSettings.settings.lastAlert;
  232. }
  233. if (mxSettings.settings.currentEdgeStyle == null)
  234. {
  235. mxSettings.settings.currentEdgeStyle = Graph.prototype.defaultEdgeStyle;
  236. }
  237. else if (mxSettings.settings.version <= 10)
  238. {
  239. // Adds new defaults for jetty size and loop routing
  240. mxSettings.settings.currentEdgeStyle['orthogonalLoop'] = 1;
  241. mxSettings.settings.currentEdgeStyle['jettySize'] = 'auto';
  242. }
  243. if (mxSettings.settings.currentVertexStyle == null)
  244. {
  245. mxSettings.settings.currentVertexStyle = Graph.prototype.defaultVertexStyle;
  246. }
  247. if (mxSettings.settings.createTarget == null)
  248. {
  249. mxSettings.settings.createTarget = false;
  250. }
  251. if (mxSettings.settings.pageFormat == null)
  252. {
  253. mxSettings.settings.pageFormat = mxGraph.prototype.pageFormat;
  254. }
  255. if (mxSettings.settings.search == null)
  256. {
  257. mxSettings.settings.search = true;
  258. }
  259. if (mxSettings.settings.showStartScreen == null)
  260. {
  261. mxSettings.settings.showStartScreen = true;
  262. }
  263. if (mxSettings.settings.gridColor == null)
  264. {
  265. mxSettings.settings.gridColor = mxGraphView.prototype.gridColor;
  266. }
  267. if (mxSettings.settings.autosave == null)
  268. {
  269. mxSettings.settings.autosave = true;
  270. }
  271. if (mxSettings.settings.scratchpadSeen != null)
  272. {
  273. delete mxSettings.settings.scratchpadSeen;
  274. }
  275. }
  276. },
  277. clear: function()
  278. {
  279. if (isLocalStorage)
  280. {
  281. localStorage.removeItem(mxSettings.key);
  282. }
  283. }
  284. }
  285. /**
  286. * Variable: mxLoadSettings
  287. *
  288. * Optional global config variable to toggle loading the settings. Default is true.
  289. *
  290. * (code)
  291. * <script type="text/javascript">
  292. * var mxLoadSettings = false;
  293. * </script>
  294. * (end)
  295. */
  296. if (typeof(mxLoadSettings) == 'undefined' || mxLoadSettings)
  297. {
  298. // Loads initial content
  299. mxSettings.load();
  300. }