Settings.js 7.3 KB

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