Settings.js 5.6 KB

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