Settings.js 5.9 KB

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