Settings.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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: 15,
  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. getResizeImages: function()
  58. {
  59. return mxSettings.settings.resizeImages;
  60. },
  61. setResizeImages: function(resizeImages)
  62. {
  63. mxSettings.settings.resizeImages = resizeImages;
  64. },
  65. getLibraries: function()
  66. {
  67. return mxSettings.settings.libraries;
  68. },
  69. setLibraries: function(libs)
  70. {
  71. mxSettings.settings.libraries = libs;
  72. },
  73. addCustomLibrary: function(id)
  74. {
  75. // Makes sure to update the latest data from the localStorage
  76. mxSettings.load();
  77. if (mxUtils.indexOf(mxSettings.settings.customLibraries, id) < 0)
  78. {
  79. // Makes sure scratchpad is below search in sidebar
  80. if (id === 'L.scratchpad')
  81. {
  82. mxSettings.settings.customLibraries.splice(0, 0, id);
  83. }
  84. else
  85. {
  86. mxSettings.settings.customLibraries.push(id);
  87. }
  88. }
  89. mxSettings.save();
  90. },
  91. removeCustomLibrary: function(id)
  92. {
  93. // Makes sure to update the latest data from the localStorage
  94. mxSettings.load();
  95. mxUtils.remove(id, mxSettings.settings.customLibraries);
  96. mxSettings.save();
  97. },
  98. getCustomLibraries: function()
  99. {
  100. return mxSettings.settings.customLibraries;
  101. },
  102. getPlugins: function()
  103. {
  104. return mxSettings.settings.plugins;
  105. },
  106. setPlugins: function(plugins)
  107. {
  108. mxSettings.settings.plugins = plugins;
  109. },
  110. getRecentColors: function()
  111. {
  112. return mxSettings.settings.recentColors;
  113. },
  114. setRecentColors: function(recentColors)
  115. {
  116. mxSettings.settings.recentColors = recentColors;
  117. },
  118. getFormatWidth: function()
  119. {
  120. return parseInt(mxSettings.settings.formatWidth);
  121. },
  122. setFormatWidth: function(formatWidth)
  123. {
  124. mxSettings.settings.formatWidth = formatWidth;
  125. },
  126. getCurrentEdgeStyle: function()
  127. {
  128. return mxSettings.settings.currentEdgeStyle;
  129. },
  130. setCurrentEdgeStyle: function(value)
  131. {
  132. mxSettings.settings.currentEdgeStyle = value;
  133. },
  134. getCurrentVertexStyle: function()
  135. {
  136. return mxSettings.settings.currentVertexStyle;
  137. },
  138. setCurrentVertexStyle: function(value)
  139. {
  140. mxSettings.settings.currentVertexStyle = value;
  141. },
  142. isCreateTarget: function()
  143. {
  144. return mxSettings.settings.createTarget;
  145. },
  146. setCreateTarget: function(value)
  147. {
  148. mxSettings.settings.createTarget = value;
  149. },
  150. getPageFormat: function()
  151. {
  152. return mxSettings.settings.pageFormat;
  153. },
  154. setPageFormat: function(value)
  155. {
  156. mxSettings.settings.pageFormat = value;
  157. },
  158. init: function()
  159. {
  160. mxSettings.settings =
  161. {
  162. language: '',
  163. libraries: Sidebar.prototype.defaultEntries,
  164. customLibraries: Editor.defaultCustomLibraries,
  165. plugins: [],
  166. recentColors: [],
  167. formatWidth: mxSettings.defaultFormatWidth,
  168. currentEdgeStyle: Graph.prototype.defaultEdgeStyle,
  169. currentVertexStyle: Graph.prototype.defaultVertexStyle,
  170. createTarget: false,
  171. pageFormat: mxGraph.prototype.pageFormat,
  172. search: true,
  173. showStartScreen: true,
  174. gridColor: mxGraphView.prototype.gridColor,
  175. autosave: true,
  176. resizeImages: null,
  177. version: mxSettings.currentVersion,
  178. // Only defined and true for new settings which haven't been saved
  179. isNew: true
  180. };
  181. },
  182. save: function()
  183. {
  184. if (isLocalStorage && typeof(JSON) !== 'undefined')
  185. {
  186. try
  187. {
  188. delete mxSettings.settings.isNew;
  189. mxSettings.settings.version = mxSettings.currentVersion;
  190. localStorage.setItem(mxSettings.key, JSON.stringify(mxSettings.settings));
  191. }
  192. catch (e)
  193. {
  194. // ignores quota exceeded
  195. }
  196. }
  197. },
  198. load: function()
  199. {
  200. if (isLocalStorage && typeof(JSON) !== 'undefined')
  201. {
  202. mxSettings.parse(localStorage.getItem(mxSettings.key));
  203. }
  204. if (mxSettings.settings == null)
  205. {
  206. mxSettings.init();
  207. }
  208. },
  209. parse: function(value)
  210. {
  211. if (value != null)
  212. {
  213. mxSettings.settings = JSON.parse(value);
  214. if (mxSettings.settings.plugins == null)
  215. {
  216. mxSettings.settings.plugins = [];
  217. }
  218. if (mxSettings.settings.recentColors == null)
  219. {
  220. mxSettings.settings.recentColors = [];
  221. }
  222. if (mxSettings.settings.libraries == null)
  223. {
  224. mxSettings.settings.libraries = Sidebar.prototype.defaultEntries;
  225. }
  226. if (mxSettings.settings.customLibraries == null)
  227. {
  228. mxSettings.settings.customLibraries = Editor.defaultCustomLibraries;
  229. }
  230. if (mxSettings.settings.ui == null)
  231. {
  232. mxSettings.settings.ui = '';
  233. }
  234. if (mxSettings.settings.formatWidth == null)
  235. {
  236. mxSettings.settings.formatWidth = mxSettings.defaultFormatWidth;
  237. }
  238. if (mxSettings.settings.lastAlert != null)
  239. {
  240. delete mxSettings.settings.lastAlert;
  241. }
  242. if (mxSettings.settings.currentEdgeStyle == null)
  243. {
  244. mxSettings.settings.currentEdgeStyle = Graph.prototype.defaultEdgeStyle;
  245. }
  246. else if (mxSettings.settings.version <= 10)
  247. {
  248. // Adds new defaults for jetty size and loop routing
  249. mxSettings.settings.currentEdgeStyle['orthogonalLoop'] = 1;
  250. mxSettings.settings.currentEdgeStyle['jettySize'] = 'auto';
  251. }
  252. if (mxSettings.settings.currentVertexStyle == null)
  253. {
  254. mxSettings.settings.currentVertexStyle = Graph.prototype.defaultVertexStyle;
  255. }
  256. if (mxSettings.settings.createTarget == null)
  257. {
  258. mxSettings.settings.createTarget = false;
  259. }
  260. if (mxSettings.settings.pageFormat == null)
  261. {
  262. mxSettings.settings.pageFormat = mxGraph.prototype.pageFormat;
  263. }
  264. if (mxSettings.settings.search == null)
  265. {
  266. mxSettings.settings.search = true;
  267. }
  268. if (mxSettings.settings.showStartScreen == null)
  269. {
  270. mxSettings.settings.showStartScreen = true;
  271. }
  272. if (mxSettings.settings.gridColor == null)
  273. {
  274. mxSettings.settings.gridColor = mxGraphView.prototype.gridColor;
  275. }
  276. if (mxSettings.settings.autosave == null)
  277. {
  278. mxSettings.settings.autosave = true;
  279. }
  280. if (mxSettings.settings.scratchpadSeen != null)
  281. {
  282. delete mxSettings.settings.scratchpadSeen;
  283. }
  284. }
  285. },
  286. clear: function()
  287. {
  288. if (isLocalStorage)
  289. {
  290. localStorage.removeItem(mxSettings.key);
  291. }
  292. }
  293. }
  294. /**
  295. * Variable: mxLoadSettings
  296. *
  297. * Optional global config variable to toggle loading the settings. Default is true.
  298. *
  299. * (code)
  300. * <script type="text/javascript">
  301. * var mxLoadSettings = false;
  302. * </script>
  303. * (end)
  304. */
  305. if (typeof(mxLoadSettings) == 'undefined' || mxLoadSettings)
  306. {
  307. // Loads initial content
  308. mxSettings.load();
  309. }