app.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 activitiModeler = angular.module('activitiModeler', [
  20. 'ngCookies',
  21. 'ngResource',
  22. 'ngSanitize',
  23. 'ngRoute',
  24. 'ngDragDrop',
  25. 'mgcrea.ngStrap',
  26. 'ngGrid',
  27. 'ngAnimate',
  28. 'pascalprecht.translate',
  29. 'duScroll',
  30. 'tm.pagination'
  31. ]);
  32. var activitiModule = activitiModeler;
  33. activitiModeler
  34. // Initialize routes
  35. .config(['$selectProvider', '$translateProvider', function ($selectProvider, $translateProvider) {
  36. // Override caret for bs-select directive
  37. angular.extend($selectProvider.defaults, {
  38. caretHtml: '&nbsp;<i class="icon icon-caret-down"></i>'
  39. });
  40. // Initialize angular-translate
  41. $translateProvider.useStaticFilesLoader({
  42. prefix: './editor-app/i18n/',
  43. suffix: '.json'
  44. });
  45. $translateProvider.preferredLanguage('en');
  46. // remember language
  47. $translateProvider.useCookieStorage();
  48. }])
  49. .run(['$rootScope', '$timeout', '$modal', '$translate', '$location', '$window', '$http', '$q',
  50. function ($rootScope, $timeout, $modal, $translate, $location, $window, $http, $q) {
  51. $rootScope.config = ACTIVITI.CONFIG;
  52. $rootScope.editorInitialized = false;
  53. $rootScope.editorFactory = $q.defer();
  54. $rootScope.forceSelectionRefresh = false;
  55. $rootScope.ignoreChanges = false; // by default never ignore changes
  56. $rootScope.validationErrors = [];
  57. $rootScope.staticIncludeVersion = Date.now();
  58. /**
  59. * A 'safer' apply that avoids concurrent updates (which $apply allows).
  60. */
  61. $rootScope.safeApply = function (fn) {
  62. var phase = this.$root.$$phase;
  63. if (phase == '$apply' || phase == '$digest') {
  64. if (fn && (typeof(fn) === 'function')) {
  65. fn();
  66. }
  67. } else {
  68. this.$apply(fn);
  69. }
  70. };
  71. /**
  72. * Initialize the event bus: couple all Oryx events with a dispatch of the
  73. * event of the event bus. This way, it gets much easier to attach custom logic
  74. * to any event.
  75. */
  76. /* Helper method to fetch model from server (always needed) */
  77. function fetchModel(modelId) {
  78. var modelUrl = KISBPM.URL.getModel(modelId);
  79. $http({method: 'GET', url: modelUrl}).success(function (data, status, headers, config) {
  80. $rootScope.editor = new ORYX.Editor(data);
  81. $rootScope.modelData = angular.fromJson(data);
  82. $rootScope.editorFactory.resolve();
  83. }).error(function (data, status, headers, config) {
  84. console.log('Error loading model with id ' + modelId + ' ' + data);
  85. });
  86. }
  87. function initScrollHandling() {
  88. var canvasSection = jQuery('#canvasSection');
  89. canvasSection.scroll(function () {
  90. // Hides the resizer and quick menu items during scrolling
  91. var selectedElements = $rootScope.editor.selection;
  92. var subSelectionElements = $rootScope.editor._subSelection;
  93. $rootScope.selectedElements = selectedElements;
  94. $rootScope.subSelectionElements = subSelectionElements;
  95. if (selectedElements && selectedElements.length > 0) {
  96. $rootScope.selectedElementBeforeScrolling = selectedElements[0];
  97. }
  98. jQuery('.Oryx_button').each(function (i, obj) {
  99. $rootScope.orginalOryxButtonStyle = obj.style.display;
  100. obj.style.display = 'none';
  101. });
  102. jQuery('.resizer_southeast').each(function (i, obj) {
  103. $rootScope.orginalResizerSEStyle = obj.style.display;
  104. obj.style.display = 'none';
  105. });
  106. jQuery('.resizer_northwest').each(function (i, obj) {
  107. $rootScope.orginalResizerNWStyle = obj.style.display;
  108. obj.style.display = 'none';
  109. });
  110. $rootScope.editor.handleEvents({type: ORYX.CONFIG.EVENT_CANVAS_SCROLL});
  111. });
  112. canvasSection.scrollStopped(function () {
  113. // Puts the quick menu items and resizer back when scroll is stopped.
  114. $rootScope.editor.setSelection([]); // needed cause it checks for element changes and does nothing if the elements are the same
  115. $rootScope.editor.setSelection($rootScope.selectedElements, $rootScope.subSelectionElements);
  116. $rootScope.selectedElements = undefined;
  117. $rootScope.subSelectionElements = undefined;
  118. function handleDisplayProperty(obj) {
  119. if (jQuery(obj).position().top > 0) {
  120. obj.style.display = 'block';
  121. } else {
  122. obj.style.display = 'none';
  123. }
  124. }
  125. jQuery('.Oryx_button').each(function (i, obj) {
  126. handleDisplayProperty(obj);
  127. });
  128. jQuery('.resizer_southeast').each(function (i, obj) {
  129. handleDisplayProperty(obj);
  130. });
  131. jQuery('.resizer_northwest').each(function (i, obj) {
  132. handleDisplayProperty(obj);
  133. });
  134. });
  135. }
  136. /**
  137. * Initialize the Oryx Editor when the content has been loaded
  138. */
  139. $rootScope.$on('$includeContentLoaded', function (event) {
  140. if (!$rootScope.editorInitialized) {
  141. ORYX._loadPlugins();
  142. var modelId = EDITOR.UTIL.getParameterByName('modelId');
  143. fetchModel(modelId);
  144. $rootScope.window = {};
  145. var updateWindowSize = function () {
  146. $rootScope.window.width = $window.innerWidth;
  147. $rootScope.window.height = $window.innerHeight;
  148. };
  149. // Window resize hook
  150. angular.element($window).bind('resize', function () {
  151. $rootScope.safeApply(updateWindowSize());
  152. });
  153. $rootScope.$watch('window.forceRefresh', function (newValue) {
  154. if (newValue) {
  155. $timeout(function () {
  156. updateWindowSize();
  157. $rootScope.window.forceRefresh = false;
  158. });
  159. }
  160. });
  161. updateWindowSize();
  162. // Hook in resizing of main panels when window resizes
  163. // TODO: perhaps move to a separate JS-file?
  164. jQuery(window).resize(function () {
  165. // Calculate the offset based on the bottom of the module header
  166. var offset = jQuery("#editor-header").offset();
  167. var propSectionHeight = jQuery('#propertySection').height();
  168. var canvas = jQuery('#canvasSection');
  169. var mainHeader = jQuery('#main-header');
  170. if (offset == undefined || offset === null
  171. || propSectionHeight === undefined || propSectionHeight === null
  172. || canvas === undefined || canvas === null || mainHeader === null) {
  173. return;
  174. }
  175. if ($rootScope.editor) {
  176. var selectedElements = $rootScope.editor.selection;
  177. var subSelectionElements = $rootScope.editor._subSelection;
  178. $rootScope.selectedElements = selectedElements;
  179. $rootScope.subSelectionElements = subSelectionElements;
  180. if (selectedElements && selectedElements.length > 0) {
  181. $rootScope.selectedElementBeforeScrolling = selectedElements[0];
  182. $rootScope.editor.setSelection([]); // needed cause it checks for element changes and does nothing if the elements are the same
  183. $rootScope.editor.setSelection($rootScope.selectedElements, $rootScope.subSelectionElements);
  184. $rootScope.selectedElements = undefined;
  185. $rootScope.subSelectionElements = undefined;
  186. }
  187. }
  188. var totalAvailable = jQuery(window).height() - offset.top - mainHeader.height() - 50;
  189. canvas.height(totalAvailable - propSectionHeight);
  190. jQuery('#paletteSection').height(totalAvailable);
  191. // Update positions of the resize-markers, according to the canvas
  192. var actualCanvas = null;
  193. if (canvas && canvas[0].children[1]) {
  194. actualCanvas = canvas[0].children[1];
  195. }
  196. var canvasTop = canvas.position().top;
  197. var canvasLeft = canvas.position().left;
  198. var canvasHeight = canvas[0].clientHeight;
  199. var canvasWidth = canvas[0].clientWidth;
  200. var iconCenterOffset = 8;
  201. var widthDiff = 0;
  202. var actualWidth = 0;
  203. if (actualCanvas) {
  204. // In some browsers, the SVG-element clientwidth isn't available, so we revert to the parent
  205. actualWidth = actualCanvas.clientWidth || actualCanvas.parentNode.clientWidth;
  206. }
  207. if (actualWidth < canvas[0].clientWidth) {
  208. widthDiff = actualWidth - canvas[0].clientWidth;
  209. // In case the canvas is smaller than the actual viewport, the resizers should be moved
  210. canvasLeft -= widthDiff / 2;
  211. canvasWidth += widthDiff;
  212. }
  213. var iconWidth = 17;
  214. var iconOffset = 20;
  215. var north = jQuery('#canvas-grow-N');
  216. north.css('top', canvasTop + iconOffset + 'px');
  217. north.css('left', canvasLeft - 10 + (canvasWidth - iconWidth) / 2 + 'px');
  218. var south = jQuery('#canvas-grow-S');
  219. south.css('top', (canvasTop + canvasHeight - iconOffset - iconCenterOffset) + 'px');
  220. south.css('left', canvasLeft - 10 + (canvasWidth - iconWidth) / 2 + 'px');
  221. var east = jQuery('#canvas-grow-E');
  222. east.css('top', canvasTop - 10 + (canvasHeight - iconWidth) / 2 + 'px');
  223. east.css('left', (canvasLeft + canvasWidth - iconOffset - iconCenterOffset) + 'px');
  224. var west = jQuery('#canvas-grow-W');
  225. west.css('top', canvasTop - 10 + (canvasHeight - iconWidth) / 2 + 'px');
  226. west.css('left', canvasLeft + iconOffset + 'px');
  227. north = jQuery('#canvas-shrink-N');
  228. north.css('top', canvasTop + iconOffset + 'px');
  229. north.css('left', canvasLeft + 10 + (canvasWidth - iconWidth) / 2 + 'px');
  230. south = jQuery('#canvas-shrink-S');
  231. south.css('top', (canvasTop + canvasHeight - iconOffset - iconCenterOffset) + 'px');
  232. south.css('left', canvasLeft + 10 + (canvasWidth - iconWidth) / 2 + 'px');
  233. east = jQuery('#canvas-shrink-E');
  234. east.css('top', canvasTop + 10 + (canvasHeight - iconWidth) / 2 + 'px');
  235. east.css('left', (canvasLeft + canvasWidth - iconOffset - iconCenterOffset) + 'px');
  236. west = jQuery('#canvas-shrink-W');
  237. west.css('top', canvasTop + 10 + (canvasHeight - iconWidth) / 2 + 'px');
  238. west.css('left', canvasLeft + iconOffset + 'px');
  239. });
  240. jQuery(window).trigger('resize');
  241. jQuery.fn.scrollStopped = function (callback) {
  242. jQuery(this).scroll(function () {
  243. var self = this, $this = jQuery(self);
  244. if ($this.data('scrollTimeout')) {
  245. clearTimeout($this.data('scrollTimeout'));
  246. }
  247. $this.data('scrollTimeout', setTimeout(callback, 50, self));
  248. });
  249. };
  250. // Always needed, cause the DOM element on which the scroll event listeners are attached are changed for every new model
  251. initScrollHandling();
  252. $rootScope.editorInitialized = true;
  253. }
  254. });
  255. /**
  256. * Initialize the event bus: couple all Oryx events with a dispatch of the
  257. * event of the event bus. This way, it gets much easier to attach custom logic
  258. * to any event.
  259. */
  260. $rootScope.editorFactory.promise.then(function () {
  261. KISBPM.eventBus.editor = $rootScope.editor;
  262. var eventMappings = [
  263. {
  264. oryxType: ORYX.CONFIG.EVENT_SELECTION_CHANGED,
  265. kisBpmType: KISBPM.eventBus.EVENT_TYPE_SELECTION_CHANGE
  266. },
  267. {oryxType: ORYX.CONFIG.EVENT_DBLCLICK, kisBpmType: KISBPM.eventBus.EVENT_TYPE_DOUBLE_CLICK},
  268. {oryxType: ORYX.CONFIG.EVENT_MOUSEOUT, kisBpmType: KISBPM.eventBus.EVENT_TYPE_MOUSE_OUT},
  269. {oryxType: ORYX.CONFIG.EVENT_MOUSEOVER, kisBpmType: KISBPM.eventBus.EVENT_TYPE_MOUSE_OVER}
  270. ];
  271. eventMappings.forEach(function (eventMapping) {
  272. $rootScope.editor.registerOnEvent(eventMapping.oryxType, function (event) {
  273. KISBPM.eventBus.dispatch(eventMapping.kisBpmType, event);
  274. });
  275. });
  276. $rootScope.editor.registerOnEvent(ORYX.CONFIG.EVENT_SHAPEREMOVED, function (event) {
  277. var validateButton = document.getElementById(event.shape.resourceId + "-validate-button");
  278. if (validateButton) {
  279. validateButton.style.display = 'none';
  280. }
  281. });
  282. // The Oryx canvas is ready (we know since we're in this promise callback) and the
  283. // event bus is ready. The editor is now ready for use
  284. KISBPM.eventBus.dispatch(KISBPM.eventBus.EVENT_TYPE_EDITOR_READY, {type: KISBPM.eventBus.EVENT_TYPE_EDITOR_READY});
  285. });
  286. // Alerts
  287. $rootScope.alerts = {
  288. queue: []
  289. };
  290. $rootScope.showAlert = function (alert) {
  291. if (alert.queue.length > 0) {
  292. alert.current = alert.queue.shift();
  293. // Start timout for message-pruning
  294. alert.timeout = $timeout(function () {
  295. if (alert.queue.length == 0) {
  296. alert.current = undefined;
  297. alert.timeout = undefined;
  298. } else {
  299. $rootScope.showAlert(alert);
  300. }
  301. }, (alert.current.type == 'error' ? 5000 : 1000));
  302. } else {
  303. $rootScope.alerts.current = undefined;
  304. }
  305. };
  306. $rootScope.addAlert = function (message, type) {
  307. var newAlert = {message: message, type: type};
  308. if (!$rootScope.alerts.timeout) {
  309. // Timeout for message queue is not running, start one
  310. $rootScope.alerts.queue.push(newAlert);
  311. $rootScope.showAlert($rootScope.alerts);
  312. } else {
  313. $rootScope.alerts.queue.push(newAlert);
  314. }
  315. };
  316. $rootScope.dismissAlert = function () {
  317. if (!$rootScope.alerts.timeout) {
  318. $rootScope.alerts.current = undefined;
  319. } else {
  320. $timeout.cancel($rootScope.alerts.timeout);
  321. $rootScope.alerts.timeout = undefined;
  322. $rootScope.showAlert($rootScope.alerts);
  323. }
  324. };
  325. $rootScope.addAlertPromise = function (promise, type) {
  326. if (promise) {
  327. promise.then(function (data) {
  328. $rootScope.addAlert(data, type);
  329. });
  330. }
  331. };
  332. }
  333. ])
  334. // Moment-JS date-formatting filter
  335. .filter('dateformat', function () {
  336. return function (date, format) {
  337. if (date) {
  338. if (format) {
  339. return moment(date).format(format);
  340. } else {
  341. return moment(date).calendar();
  342. }
  343. }
  344. return '';
  345. };
  346. });