Settings.js 6.0 KB

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