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