cConf-1-4-8.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * Plugin for embed mode in Confluence Connect post version 1.4.8
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. // Extracts macro data from JSON protocol
  7. var macroData = {};
  8. mxEvent.addListener(window, 'message', mxUtils.bind(this, function(evt)
  9. {
  10. var data = evt.data;
  11. try
  12. {
  13. data = JSON.parse(data);
  14. if (data.action == 'load')
  15. {
  16. if (data.macroData != null)
  17. {
  18. macroData = data.macroData;
  19. if (ui.format != null)
  20. {
  21. ui.format.refresh();
  22. }
  23. }
  24. macroData.diagramDisplayName = data.title;
  25. }
  26. }
  27. catch (e)
  28. {
  29. data = null;
  30. }
  31. }));
  32. var renameAction = ui.actions.get("rename");
  33. renameAction.visible = true;
  34. renameAction.isEnabled = function()
  35. {
  36. return macroData.diagramDisplayName != null;
  37. }
  38. renameAction.funct = function()
  39. {
  40. var dlg = new FilenameDialog(ui, macroData.diagramDisplayName || "",
  41. mxResources.get('rename'), function(newName)
  42. {
  43. if (newName != null && newName.length > 0)
  44. {
  45. macroData.diagramDisplayName = newName;
  46. var parent = window.opener || window.parent;
  47. parent.postMessage(JSON.stringify({event: 'rename', name: newName}), '*');
  48. //Update file name in the UI
  49. var tmp = document.createElement('span');
  50. mxUtils.write(tmp, mxUtils.htmlEntities(newName));
  51. if (ui.embedFilenameSpan != null)
  52. {
  53. ui.embedFilenameSpan.parentNode.removeChild(ui.embedFilenameSpan);
  54. }
  55. ui.buttonContainer.appendChild(tmp);
  56. ui.embedFilenameSpan = tmp;
  57. }
  58. }, mxResources.get('rename'), function(name)
  59. {
  60. var err = "";
  61. if (name == null || name.length == 0)
  62. {
  63. err = 'Filename too short';
  64. }
  65. else if (/[&\*+=\\;/{}|\":<>\?~]/g.test(name))
  66. {
  67. err = 'Invalid characters \\ / | : { } < > & + ? = ; * " ~';
  68. }
  69. else
  70. {
  71. return true;
  72. }
  73. ui.showError(mxResources.get('error'), err, mxResources.get('ok'));
  74. return false;
  75. });
  76. ui.showDialog(dlg.container, 300, 80, true, true);
  77. dlg.init();
  78. }
  79. // Returns modified macro data to client
  80. var uiCreateLoadMessage = ui.createLoadMessage;
  81. ui.createLoadMessage = function(eventName)
  82. {
  83. var msg = uiCreateLoadMessage.apply(this, arguments);
  84. if (eventName == 'export')
  85. {
  86. msg.macroData = macroData;
  87. msg.comments = confComments;
  88. }
  89. return msg;
  90. };
  91. // Adds new section for confluence cloud
  92. var diagramFormatPanelInit = DiagramFormatPanel.prototype.init;
  93. DiagramFormatPanel.prototype.init = function()
  94. {
  95. this.container.appendChild(this.addViewerOptions(this.createPanel()));
  96. diagramFormatPanelInit.apply(this, arguments);
  97. };
  98. // Adds viewer config to style options and refreshes
  99. DiagramFormatPanel.prototype.addViewerOptions = function(div)
  100. {
  101. var ui = this.editorUi;
  102. var editor = ui.editor;
  103. var graph = editor.graph;
  104. div.appendChild(this.createTitle(mxResources.get('viewerSettings')));
  105. // Viewer simple
  106. div.appendChild(this.createOption(mxResources.get('simpleViewer'), function()
  107. {
  108. return macroData.simple == '1';
  109. }, function(checked)
  110. {
  111. macroData.simple = (checked) ? '1' : '0';
  112. }));
  113. // Viewer lightbox
  114. div.appendChild(this.createOption(mxResources.get('lightbox'), function()
  115. {
  116. return macroData.lbox != '0';
  117. }, function(checked)
  118. {
  119. macroData.lbox = (checked) ? '1' : '0';
  120. }));
  121. // Toolbar
  122. var stylePanel = this.createPanel();
  123. stylePanel.style.position = 'relative';
  124. stylePanel.style.borderWidth = '0px';
  125. stylePanel.style.marginLeft = '0px';
  126. stylePanel.style.paddingTop = '8px';
  127. stylePanel.style.paddingBottom = '4px';
  128. stylePanel.style.fontWeight = 'normal';
  129. stylePanel.className = 'geToolbarContainer';
  130. mxUtils.write(stylePanel, mxResources.get('toolbar'));
  131. // Adds toolbar options
  132. var tbSelect = document.createElement('select');
  133. tbSelect.style.position = 'absolute';
  134. tbSelect.style.right = '20px';
  135. tbSelect.style.width = '97px';
  136. tbSelect.style.marginTop = '-2px';
  137. var opts = [{value: 'top', title: mxResources.get('top')},
  138. {value: 'inline', title: mxResources.get('embed')},
  139. {value: 'hidden', title: mxResources.get('hidden')}]
  140. var validTb = false;
  141. for (var i = 0; i < opts.length; i++)
  142. {
  143. validTb = validTb || macroData.tbstyle == opts[i].value;
  144. var tbOption = document.createElement('option');
  145. tbOption.setAttribute('value', opts[i].value);
  146. mxUtils.write(tbOption, opts[i].title);
  147. tbSelect.appendChild(tbOption);
  148. }
  149. tbSelect.value = (validTb) ? macroData.tbstyle : 'top';
  150. stylePanel.appendChild(tbSelect);
  151. div.appendChild(stylePanel);
  152. mxEvent.addListener(tbSelect, 'change', function(evt)
  153. {
  154. macroData.tbstyle = tbSelect.value;
  155. mxEvent.consume(evt);
  156. });
  157. // Links
  158. stylePanel = stylePanel.cloneNode(false);
  159. stylePanel.style.paddingTop = '4px';
  160. mxUtils.write(stylePanel, mxResources.get('links'));
  161. // Adds links options
  162. var linksSelect = document.createElement('select');
  163. linksSelect.style.position = 'absolute';
  164. linksSelect.style.right = '20px';
  165. linksSelect.style.width = '97px';
  166. linksSelect.style.marginTop = '-2px';
  167. var opts = [{value: 'auto', title: mxResources.get('automatic')},
  168. {value: 'blank', title: mxResources.get('openInNewWindow')},
  169. {value: 'self', title: mxResources.get('openInThisWindow')}]
  170. var validLinks = false;
  171. for (var i = 0; i < opts.length; i++)
  172. {
  173. validLinks = validLinks || macroData.links == opts[i].value;
  174. var linkOption = document.createElement('option');
  175. linkOption.setAttribute('value', opts[i].value);
  176. mxUtils.write(linkOption, opts[i].title);
  177. linksSelect.appendChild(linkOption);
  178. }
  179. linksSelect.value = (validLinks) ? macroData.links : 'auto';
  180. stylePanel.appendChild(linksSelect);
  181. div.appendChild(stylePanel);
  182. mxEvent.addListener(linksSelect, 'change', function(evt)
  183. {
  184. macroData.links = linksSelect.value;
  185. mxEvent.consume(evt);
  186. });
  187. // Zoom
  188. var zoomOpt = this.createRelativeOption(mxResources.get('zoom'), null, null, function(input)
  189. {
  190. var value = (input.value == '') ? 100 : parseInt(input.value);
  191. value = Math.max(0, (isNaN(value)) ? 100 : value);
  192. input.value = value + ' %';
  193. macroData.zoom = value / 100;
  194. }, function(input)
  195. {
  196. input.value = (parseFloat(macroData.zoom || 1) * 100) + '%';
  197. });
  198. zoomOpt.style.fontWeight = 'normal';
  199. zoomOpt.style.paddingBottom = '6px';
  200. zoomOpt.style.paddingTop = '6px';
  201. zoomOpt.style.border = 'none';
  202. div.appendChild(zoomOpt);
  203. return div;
  204. };
  205. if (ui.format != null)
  206. {
  207. ui.format.refresh();
  208. }
  209. //Comments
  210. function setModified()
  211. {
  212. ui.editor.setStatus(mxUtils.htmlEntities(mxResources.get('unsavedChanges')));
  213. ui.editor.setModified(true);
  214. };
  215. var confUser = null;
  216. var confComments = null;
  217. ui.getCurrentUser = function()
  218. {
  219. if (confUser == null)
  220. {
  221. ui.remoteInvoke('getCurrentUser', null, null, function(user)
  222. {
  223. confUser = new DrawioUser(user.id, user.email, user.displayName, user.pictureUrl);
  224. }, function()
  225. {
  226. //ignore such that next call we retry
  227. });
  228. //Return a dummy user until we have the actual user in order for UI to be populated
  229. return new DrawioUser(Date.now(), null, 'Anonymous');
  230. }
  231. return confUser;
  232. };
  233. ui.commentsSupported = function()
  234. {
  235. return true;
  236. };
  237. function confCommentToDrawio(cComment, pCommentId)
  238. {
  239. var comment = new DrawioComment(null, cComment.id, cComment.content,
  240. cComment.modifiedDate, cComment.createdDate, cComment.isResolved,
  241. new DrawioUser(cComment.user.id, cComment.user.email,
  242. cComment.user.displayName, cComment.user.pictureUrl), pCommentId);
  243. for (var i = 0; cComment.replies != null && i < cComment.replies.length; i++)
  244. {
  245. comment.addReplyDirect(confCommentToDrawio(cComment.replies[i], cComment.id));
  246. }
  247. return comment;
  248. };
  249. ui.getComments = function(success, error)
  250. {
  251. if (confComments == null)
  252. {
  253. ui.remoteInvoke('getComments', [macroData.contentId], null, function(comments)
  254. {
  255. confComments = [];
  256. for (var i = 0; i < comments.length; i++)
  257. {
  258. confComments.push(confCommentToDrawio(comments[i]));
  259. }
  260. success(confComments);
  261. }, error);
  262. }
  263. else
  264. {
  265. success(confComments);
  266. }
  267. };
  268. ui.addComment = function(comment, success, error)
  269. {
  270. setModified();
  271. success(confUser.id + ':' + Date.now());
  272. };
  273. ui.newComment = function(content, user)
  274. {
  275. return new DrawioComment(null, null, content, Date.now(), Date.now(), false, user); //remove file information
  276. };
  277. //In Confluence, comments are part of the file (specifically custom contents), so needs to mark as changed with every change
  278. DrawioComment.prototype.addReply = function(reply, success, error, doResolve, doReopen)
  279. {
  280. setModified();
  281. success();
  282. };
  283. DrawioComment.prototype.editComment = function(newContent, success, error)
  284. {
  285. setModified();
  286. success();
  287. };
  288. DrawioComment.prototype.deleteComment = function(success, error)
  289. {
  290. setModified();
  291. success();
  292. };
  293. //Prefetch current user
  294. ui.getCurrentUser();
  295. });