toolbar-default-actions.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Activiti Modeler component part of the Activiti project
  3. * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. 'use strict';
  19. var KISBPM = KISBPM || {};
  20. KISBPM.TOOLBAR = {
  21. ACTIONS: {
  22. saveModel: function (services) {
  23. var modal = services.$modal({
  24. backdrop: true,
  25. keyboard: true,
  26. template: 'editor-app/popups/save-model.html?version=' + Date.now(),
  27. scope: services.$scope
  28. });
  29. },
  30. undo: function (services) {
  31. // Get the last commands
  32. var lastCommands = services.$scope.undoStack.pop();
  33. if (lastCommands) {
  34. // Add the commands to the redo stack
  35. services.$scope.redoStack.push(lastCommands);
  36. // Force refresh of selection, might be that the undo command
  37. // impacts properties in the selected item
  38. if (services.$rootScope && services.$rootScope.forceSelectionRefresh)
  39. {
  40. services.$rootScope.forceSelectionRefresh = true;
  41. }
  42. // Rollback every command
  43. for (var i = lastCommands.length - 1; i >= 0; --i) {
  44. lastCommands[i].rollback();
  45. }
  46. // Update and refresh the canvas
  47. services.$scope.editor.handleEvents({
  48. type: ORYX.CONFIG.EVENT_UNDO_ROLLBACK,
  49. commands: lastCommands
  50. });
  51. // Update
  52. services.$scope.editor.getCanvas().update();
  53. services.$scope.editor.updateSelection();
  54. }
  55. var toggleUndo = false;
  56. if (services.$scope.undoStack.length == 0)
  57. {
  58. toggleUndo = true;
  59. }
  60. var toggleRedo = false;
  61. if (services.$scope.redoStack.length > 0)
  62. {
  63. toggleRedo = true;
  64. }
  65. if (toggleUndo || toggleRedo) {
  66. for (var i = 0; i < services.$scope.items.length; i++) {
  67. var item = services.$scope.items[i];
  68. if (toggleUndo && item.action === 'KISBPM.TOOLBAR.ACTIONS.undo') {
  69. services.$scope.safeApply(function () {
  70. item.enabled = false;
  71. });
  72. }
  73. else if (toggleRedo && item.action === 'KISBPM.TOOLBAR.ACTIONS.redo') {
  74. services.$scope.safeApply(function () {
  75. item.enabled = true;
  76. });
  77. }
  78. }
  79. }
  80. },
  81. redo: function (services) {
  82. // Get the last commands from the redo stack
  83. var lastCommands = services.$scope.redoStack.pop();
  84. if (lastCommands) {
  85. // Add this commands to the undo stack
  86. services.$scope.undoStack.push(lastCommands);
  87. // Force refresh of selection, might be that the redo command
  88. // impacts properties in the selected item
  89. if (services.$rootScope && services.$rootScope.forceSelectionRefresh)
  90. {
  91. services.$rootScope.forceSelectionRefresh = true;
  92. }
  93. // Execute those commands
  94. lastCommands.each(function (command) {
  95. command.execute();
  96. });
  97. // Update and refresh the canvas
  98. services.$scope.editor.handleEvents({
  99. type: ORYX.CONFIG.EVENT_UNDO_EXECUTE,
  100. commands: lastCommands
  101. });
  102. // Update
  103. services.$scope.editor.getCanvas().update();
  104. services.$scope.editor.updateSelection();
  105. }
  106. var toggleUndo = false;
  107. if (services.$scope.undoStack.length > 0) {
  108. toggleUndo = true;
  109. }
  110. var toggleRedo = false;
  111. if (services.$scope.redoStack.length == 0) {
  112. toggleRedo = true;
  113. }
  114. if (toggleUndo || toggleRedo) {
  115. for (var i = 0; i < services.$scope.items.length; i++) {
  116. var item = services.$scope.items[i];
  117. if (toggleUndo && item.action === 'KISBPM.TOOLBAR.ACTIONS.undo') {
  118. services.$scope.safeApply(function () {
  119. item.enabled = true;
  120. });
  121. }
  122. else if (toggleRedo && item.action === 'KISBPM.TOOLBAR.ACTIONS.redo') {
  123. services.$scope.safeApply(function () {
  124. item.enabled = false;
  125. });
  126. }
  127. }
  128. }
  129. },
  130. cut: function (services) {
  131. KISBPM.TOOLBAR.ACTIONS._getOryxEditPlugin(services.$scope).editCut();
  132. for (var i = 0; i < services.$scope.items.length; i++) {
  133. var item = services.$scope.items[i];
  134. if (item.action === 'KISBPM.TOOLBAR.ACTIONS.paste') {
  135. services.$scope.safeApply(function () {
  136. item.enabled = true;
  137. });
  138. }
  139. }
  140. },
  141. copy: function (services) {
  142. KISBPM.TOOLBAR.ACTIONS._getOryxEditPlugin(services.$scope).editCopy();
  143. for (var i = 0; i < services.$scope.items.length; i++) {
  144. var item = services.$scope.items[i];
  145. if (item.action === 'KISBPM.TOOLBAR.ACTIONS.paste') {
  146. services.$scope.safeApply(function () {
  147. item.enabled = true;
  148. });
  149. }
  150. }
  151. },
  152. paste: function (services) {
  153. KISBPM.TOOLBAR.ACTIONS._getOryxEditPlugin(services.$scope).editPaste();
  154. },
  155. deleteItem: function (services) {
  156. KISBPM.TOOLBAR.ACTIONS._getOryxEditPlugin(services.$scope).editDelete();
  157. },
  158. addBendPoint: function (services) {
  159. var dockerPlugin = KISBPM.TOOLBAR.ACTIONS._getOryxDockerPlugin(services.$scope);
  160. var enableAdd = !dockerPlugin.enabledAdd();
  161. dockerPlugin.setEnableAdd(enableAdd);
  162. if (enableAdd)
  163. {
  164. dockerPlugin.setEnableRemove(false);
  165. document.body.style.cursor = 'pointer';
  166. }
  167. else
  168. {
  169. document.body.style.cursor = 'default';
  170. }
  171. },
  172. removeBendPoint: function (services) {
  173. var dockerPlugin = KISBPM.TOOLBAR.ACTIONS._getOryxDockerPlugin(services.$scope);
  174. var enableRemove = !dockerPlugin.enabledRemove();
  175. dockerPlugin.setEnableRemove(enableRemove);
  176. if (enableRemove)
  177. {
  178. dockerPlugin.setEnableAdd(false);
  179. document.body.style.cursor = 'pointer';
  180. }
  181. else
  182. {
  183. document.body.style.cursor = 'default';
  184. }
  185. },
  186. /**
  187. * Helper method: fetches the Oryx Edit plugin from the provided scope,
  188. * if not on the scope, it is created and put on the scope for further use.
  189. *
  190. * It's important to reuse the same EditPlugin while the same scope is active,
  191. * as the clipboard is stored for the whole lifetime of the scope.
  192. */
  193. _getOryxEditPlugin: function ($scope) {
  194. if ($scope.oryxEditPlugin === undefined || $scope.oryxEditPlugin === null) {
  195. $scope.oryxEditPlugin = new ORYX.Plugins.Edit($scope.editor);
  196. }
  197. return $scope.oryxEditPlugin;
  198. },
  199. zoomIn: function (services) {
  200. KISBPM.TOOLBAR.ACTIONS._getOryxViewPlugin(services.$scope).zoom([1.0 + ORYX.CONFIG.ZOOM_OFFSET]);
  201. },
  202. zoomOut: function (services) {
  203. KISBPM.TOOLBAR.ACTIONS._getOryxViewPlugin(services.$scope).zoom([1.0 - ORYX.CONFIG.ZOOM_OFFSET]);
  204. },
  205. zoomActual: function (services) {
  206. KISBPM.TOOLBAR.ACTIONS._getOryxViewPlugin(services.$scope).setAFixZoomLevel(1);
  207. },
  208. zoomFit: function (services) {
  209. KISBPM.TOOLBAR.ACTIONS._getOryxViewPlugin(services.$scope).zoomFitToModel();
  210. },
  211. alignVertical: function (services) {
  212. KISBPM.TOOLBAR.ACTIONS._getOryxArrangmentPlugin(services.$scope).alignShapes([ORYX.CONFIG.EDITOR_ALIGN_MIDDLE]);
  213. },
  214. alignHorizontal: function (services) {
  215. KISBPM.TOOLBAR.ACTIONS._getOryxArrangmentPlugin(services.$scope).alignShapes([ORYX.CONFIG.EDITOR_ALIGN_CENTER]);
  216. },
  217. sameSize: function (services) {
  218. KISBPM.TOOLBAR.ACTIONS._getOryxArrangmentPlugin(services.$scope).alignShapes([ORYX.CONFIG.EDITOR_ALIGN_SIZE]);
  219. },
  220. closeEditor: function(services) {
  221. //window.location.href = "/model-list.html";
  222. top.modelClose();
  223. },
  224. /**
  225. * Helper method: fetches the Oryx View plugin from the provided scope,
  226. * if not on the scope, it is created and put on the scope for further use.
  227. */
  228. _getOryxViewPlugin: function ($scope) {
  229. if ($scope.oryxViewPlugin === undefined || $scope.oryxViewPlugin === null) {
  230. $scope.oryxViewPlugin = new ORYX.Plugins.View($scope.editor);
  231. }
  232. return $scope.oryxViewPlugin;
  233. },
  234. _getOryxArrangmentPlugin: function ($scope) {
  235. if ($scope.oryxArrangmentPlugin === undefined || $scope.oryxArrangmentPlugin === null) {
  236. $scope.oryxArrangmentPlugin = new ORYX.Plugins.Arrangement($scope.editor);
  237. }
  238. return $scope.oryxArrangmentPlugin;
  239. },
  240. _getOryxDockerPlugin: function ($scope) {
  241. if ($scope.oryxDockerPlugin === undefined || $scope.oryxDockerPlugin === null) {
  242. $scope.oryxDockerPlugin = new ORYX.Plugins.AddDocker($scope.editor);
  243. }
  244. return $scope.oryxDockerPlugin;
  245. }
  246. }
  247. };
  248. /** Custom controller for the save dialog */
  249. var SaveModelCtrl = [ '$rootScope', '$scope', '$http', '$route', '$location',
  250. function ($rootScope, $scope, $http, $route, $location) {
  251. var modelMetaData = $scope.editor.getModelMetaData();
  252. var description = '';
  253. if (modelMetaData.description) {
  254. description = modelMetaData.description;
  255. }
  256. var saveDialog = { 'name' : modelMetaData.name,
  257. 'description' : description};
  258. $scope.saveDialog = saveDialog;
  259. var json = $scope.editor.getJSON();
  260. json = JSON.stringify(json);
  261. var params = {
  262. modeltype: modelMetaData.model.modelType,
  263. json_xml: json,
  264. name: 'model'
  265. };
  266. $scope.status = {
  267. loading: false
  268. };
  269. $scope.close = function () {
  270. $scope.$hide();
  271. };
  272. $scope.saveAndClose = function () {
  273. $scope.save(function() {
  274. //window.location.href = "/model-list.html";
  275. top.modelClose();
  276. });
  277. };
  278. $scope.save = function (successCallback) {
  279. if (!$scope.saveDialog.name || $scope.saveDialog.name.length == 0) {
  280. return;
  281. }
  282. // Indicator spinner image
  283. $scope.status = {
  284. loading: true
  285. };
  286. modelMetaData.name = $scope.saveDialog.name;
  287. modelMetaData.description = $scope.saveDialog.description;
  288. var json = $scope.editor.getJSON();
  289. json = JSON.stringify(json);
  290. var selection = $scope.editor.getSelection();
  291. $scope.editor.setSelection([]);
  292. // Get the serialized svg image source
  293. var svgClone = $scope.editor.getCanvas().getSVGRepresentation(true);
  294. $scope.editor.setSelection(selection);
  295. if ($scope.editor.getCanvas().properties["oryx-showstripableelements"] === false) {
  296. var stripOutArray = jQuery(svgClone).find(".stripable-element");
  297. for (var i = stripOutArray.length - 1; i >= 0; i--) {
  298. stripOutArray[i].remove();
  299. }
  300. }
  301. // Remove all forced stripable elements
  302. var stripOutArray = jQuery(svgClone).find(".stripable-element-force");
  303. for (var i = stripOutArray.length - 1; i >= 0; i--) {
  304. stripOutArray[i].remove();
  305. }
  306. // Parse dom to string
  307. var svgDOM = DataManager.serialize(svgClone);
  308. var params = {
  309. json_xml: json,
  310. svg_xml: svgDOM,
  311. name: $scope.saveDialog.name,
  312. description: $scope.saveDialog.description
  313. };
  314. // Update
  315. $http({ method: 'PUT',
  316. data: params,
  317. ignoreErrors: true,
  318. headers: {'Accept': 'application/json',
  319. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
  320. transformRequest: function (obj) {
  321. var str = [];
  322. for (var p in obj) {
  323. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
  324. }
  325. return str.join("&");
  326. },
  327. url: KISBPM.URL.putModel(modelMetaData.modelId)})
  328. .success(function (data, status, headers, config) {
  329. $scope.editor.handleEvents({
  330. type: ORYX.CONFIG.EVENT_SAVED
  331. });
  332. $scope.modelData.name = $scope.saveDialog.name;
  333. $scope.modelData.lastUpdated = data.lastUpdated;
  334. $scope.status.loading = false;
  335. $scope.$hide();
  336. // Fire event to all who is listening
  337. var saveEvent = {
  338. type: KISBPM.eventBus.EVENT_TYPE_MODEL_SAVED,
  339. model: params,
  340. modelId: modelMetaData.modelId,
  341. eventType: 'update-model'
  342. };
  343. KISBPM.eventBus.dispatch(KISBPM.eventBus.EVENT_TYPE_MODEL_SAVED, saveEvent);
  344. // Reset state
  345. $scope.error = undefined;
  346. $scope.status.loading = false;
  347. // Execute any callback
  348. if (successCallback) {
  349. successCallback();
  350. }
  351. })
  352. .error(function (data, status, headers, config) {
  353. $scope.error = {};
  354. console.log('Something went wrong when updating the process model:' + JSON.stringify(data));
  355. $scope.status.loading = false;
  356. });
  357. };
  358. }];