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. ]);
  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('zh-CN');
  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. //IE浏览器下会出现编辑完成不能保存的情况,设置json不缓存
  79. $http({method: 'GET', url: modelUrl,headers: {'Pragma': 'no-cache','Cache-Control':'no-cache'}}).
  80. success(function (data, status, headers, config) {
  81. $rootScope.editor = new ORYX.Editor(data);
  82. $rootScope.modelData = angular.fromJson(data);
  83. $rootScope.editorFactory.resolve();
  84. }).
  85. error(function (data, status, headers, config) {
  86. console.log('Error loading model with id ' + modelId + ' ' + data);
  87. });
  88. }
  89. function initScrollHandling() {
  90. var canvasSection = jQuery('#canvasSection');
  91. canvasSection.scroll(function() {
  92. // Hides the resizer and quick menu items during scrolling
  93. var selectedElements = $rootScope.editor.selection;
  94. var subSelectionElements = $rootScope.editor._subSelection;
  95. $rootScope.selectedElements = selectedElements;
  96. $rootScope.subSelectionElements = subSelectionElements;
  97. if (selectedElements && selectedElements.length > 0) {
  98. $rootScope.selectedElementBeforeScrolling = selectedElements[0];
  99. }
  100. jQuery('.Oryx_button').each(function(i, obj) {
  101. $rootScope.orginalOryxButtonStyle = obj.style.display;
  102. obj.style.display = 'none';
  103. });
  104. jQuery('.resizer_southeast').each(function(i, obj) {
  105. $rootScope.orginalResizerSEStyle = obj.style.display;
  106. obj.style.display = 'none';
  107. });
  108. jQuery('.resizer_northwest').each(function(i, obj) {
  109. $rootScope.orginalResizerNWStyle = obj.style.display;
  110. obj.style.display = 'none';
  111. });
  112. $rootScope.editor.handleEvents({type:ORYX.CONFIG.EVENT_CANVAS_SCROLL});
  113. });
  114. canvasSection.scrollStopped(function(){
  115. // Puts the quick menu items and resizer back when scroll is stopped.
  116. $rootScope.editor.setSelection([]); // needed cause it checks for element changes and does nothing if the elements are the same
  117. $rootScope.editor.setSelection($rootScope.selectedElements, $rootScope.subSelectionElements);
  118. $rootScope.selectedElements = undefined;
  119. $rootScope.subSelectionElements = undefined;
  120. function handleDisplayProperty(obj) {
  121. if (jQuery(obj).position().top > 0) {
  122. obj.style.display = 'block';
  123. } else {
  124. obj.style.display = 'none';
  125. }
  126. }
  127. jQuery('.Oryx_button').each(function(i, obj) {
  128. handleDisplayProperty(obj);
  129. });
  130. jQuery('.resizer_southeast').each(function(i, obj) {
  131. handleDisplayProperty(obj);
  132. });
  133. jQuery('.resizer_northwest').each(function(i, obj) {
  134. handleDisplayProperty(obj);
  135. });
  136. });
  137. }
  138. /**
  139. * Initialize the Oryx Editor when the content has been loaded
  140. */
  141. $rootScope.$on('$includeContentLoaded', function (event) {
  142. if (!$rootScope.editorInitialized) {
  143. ORYX._loadPlugins();
  144. var modelId = EDITOR.UTIL.getParameterByName('modelId');
  145. fetchModel(modelId);
  146. $rootScope.window = {};
  147. var updateWindowSize = function() {
  148. $rootScope.window.width = $window.innerWidth;
  149. $rootScope.window.height = $window.innerHeight;
  150. };
  151. // Window resize hook
  152. angular.element($window).bind('resize', function() {
  153. $rootScope.safeApply(updateWindowSize());
  154. });
  155. $rootScope.$watch('window.forceRefresh', function(newValue) {
  156. if(newValue) {
  157. $timeout(function() {
  158. updateWindowSize();
  159. $rootScope.window.forceRefresh = false;
  160. });
  161. }
  162. });
  163. updateWindowSize();
  164. // Hook in resizing of main panels when window resizes
  165. // TODO: perhaps move to a separate JS-file?
  166. jQuery(window).resize(function () {
  167. // Calculate the offset based on the bottom of the module header
  168. var offset = jQuery("#editor-header").offset();
  169. var propSectionHeight = jQuery('#propertySection').height();
  170. var canvas = jQuery('#canvasSection');
  171. var mainHeader = jQuery('#main-header');
  172. if (offset == undefined || offset === null
  173. || propSectionHeight === undefined || propSectionHeight === null
  174. || canvas === undefined || canvas === null || mainHeader === null) {
  175. return;
  176. }
  177. if ($rootScope.editor)
  178. {
  179. var selectedElements = $rootScope.editor.selection;
  180. var subSelectionElements = $rootScope.editor._subSelection;
  181. $rootScope.selectedElements = selectedElements;
  182. $rootScope.subSelectionElements = subSelectionElements;
  183. if (selectedElements && selectedElements.length > 0)
  184. {
  185. $rootScope.selectedElementBeforeScrolling = selectedElements[0];
  186. $rootScope.editor.setSelection([]); // needed cause it checks for element changes and does nothing if the elements are the same
  187. $rootScope.editor.setSelection($rootScope.selectedElements, $rootScope.subSelectionElements);
  188. $rootScope.selectedElements = undefined;
  189. $rootScope.subSelectionElements = undefined;
  190. }
  191. }
  192. var totalAvailable = jQuery(window).height() - offset.top - mainHeader.height() - 21;
  193. canvas.height(totalAvailable - propSectionHeight);
  194. jQuery('#paletteSection').height(totalAvailable);
  195. // Update positions of the resize-markers, according to the canvas
  196. var actualCanvas = null;
  197. if (canvas && canvas[0].children[1]) {
  198. actualCanvas = canvas[0].children[1];
  199. }
  200. var canvasTop = canvas.position().top;
  201. var canvasLeft = canvas.position().left;
  202. var canvasHeight = canvas[0].clientHeight;
  203. var canvasWidth = canvas[0].clientWidth;
  204. var iconCenterOffset = 8;
  205. var widthDiff = 0;
  206. var actualWidth = 0;
  207. if(actualCanvas) {
  208. // In some browsers, the SVG-element clientwidth isn't available, so we revert to the parent
  209. actualWidth = actualCanvas.clientWidth || actualCanvas.parentNode.clientWidth;
  210. }
  211. if(actualWidth < canvas[0].clientWidth) {
  212. widthDiff = actualWidth - canvas[0].clientWidth;
  213. // In case the canvas is smaller than the actual viewport, the resizers should be moved
  214. canvasLeft -= widthDiff / 2;
  215. canvasWidth += widthDiff;
  216. }
  217. var iconWidth = 17;
  218. var iconOffset = 20;
  219. var north = jQuery('#canvas-grow-N');
  220. north.css('top', canvasTop + iconOffset + 'px');
  221. north.css('left', canvasLeft - 10 + (canvasWidth - iconWidth) / 2 + 'px');
  222. var south = jQuery('#canvas-grow-S');
  223. south.css('top', (canvasTop + canvasHeight - iconOffset - iconCenterOffset) + 'px');
  224. south.css('left', canvasLeft - 10 + (canvasWidth - iconWidth) / 2 + 'px');
  225. var east = jQuery('#canvas-grow-E');
  226. east.css('top', canvasTop - 10 + (canvasHeight - iconWidth) / 2 + 'px');
  227. east.css('left', (canvasLeft + canvasWidth - iconOffset - iconCenterOffset) + 'px');
  228. var west = jQuery('#canvas-grow-W');
  229. west.css('top', canvasTop -10 + (canvasHeight - iconWidth) / 2 + 'px');
  230. west.css('left', canvasLeft + iconOffset + 'px');
  231. north = jQuery('#canvas-shrink-N');
  232. north.css('top', canvasTop + iconOffset + 'px');
  233. north.css('left', canvasLeft + 10 + (canvasWidth - iconWidth) / 2 + 'px');
  234. south = jQuery('#canvas-shrink-S');
  235. south.css('top', (canvasTop + canvasHeight - iconOffset - iconCenterOffset) + 'px');
  236. south.css('left', canvasLeft +10 + (canvasWidth - iconWidth) / 2 + 'px');
  237. east = jQuery('#canvas-shrink-E');
  238. east.css('top', canvasTop + 10 + (canvasHeight - iconWidth) / 2 + 'px');
  239. east.css('left', (canvasLeft + canvasWidth - iconOffset - iconCenterOffset) + 'px');
  240. west = jQuery('#canvas-shrink-W');
  241. west.css('top', canvasTop + 10 + (canvasHeight - iconWidth) / 2 + 'px');
  242. west.css('left', canvasLeft + iconOffset + 'px');
  243. });
  244. jQuery(window).trigger('resize');
  245. jQuery.fn.scrollStopped = function(callback) {
  246. jQuery(this).scroll(function(){
  247. var self = this, $this = jQuery(self);
  248. if ($this.data('scrollTimeout')) {
  249. clearTimeout($this.data('scrollTimeout'));
  250. }
  251. $this.data('scrollTimeout', setTimeout(callback,50,self));
  252. });
  253. };
  254. // Always needed, cause the DOM element on which the scroll event listeners are attached are changed for every new model
  255. initScrollHandling();
  256. $rootScope.editorInitialized = true;
  257. }
  258. });
  259. /**
  260. * Initialize the event bus: couple all Oryx events with a dispatch of the
  261. * event of the event bus. This way, it gets much easier to attach custom logic
  262. * to any event.
  263. */
  264. $rootScope.editorFactory.promise.then(function() {
  265. KISBPM.eventBus.editor = $rootScope.editor;
  266. var eventMappings = [
  267. { oryxType : ORYX.CONFIG.EVENT_SELECTION_CHANGED, kisBpmType : KISBPM.eventBus.EVENT_TYPE_SELECTION_CHANGE },
  268. { oryxType : ORYX.CONFIG.EVENT_DBLCLICK, kisBpmType : KISBPM.eventBus.EVENT_TYPE_DOUBLE_CLICK },
  269. { oryxType : ORYX.CONFIG.EVENT_MOUSEOUT, kisBpmType : KISBPM.eventBus.EVENT_TYPE_MOUSE_OUT },
  270. { oryxType : ORYX.CONFIG.EVENT_MOUSEOVER, kisBpmType : KISBPM.eventBus.EVENT_TYPE_MOUSE_OVER }
  271. ];
  272. eventMappings.forEach(function(eventMapping) {
  273. $rootScope.editor.registerOnEvent(eventMapping.oryxType, function(event) {
  274. KISBPM.eventBus.dispatch(eventMapping.kisBpmType, event);
  275. });
  276. });
  277. $rootScope.editor.registerOnEvent(ORYX.CONFIG.EVENT_SHAPEREMOVED, function (event) {
  278. var validateButton = document.getElementById(event.shape.resourceId + "-validate-button");
  279. if (validateButton)
  280. {
  281. validateButton.style.display = 'none';
  282. }
  283. });
  284. // The Oryx canvas is ready (we know since we're in this promise callback) and the
  285. // event bus is ready. The editor is now ready for use
  286. KISBPM.eventBus.dispatch(KISBPM.eventBus.EVENT_TYPE_EDITOR_READY, {type : KISBPM.eventBus.EVENT_TYPE_EDITOR_READY});
  287. });
  288. // Alerts
  289. $rootScope.alerts = {
  290. queue: []
  291. };
  292. $rootScope.showAlert = function(alert) {
  293. if(alert.queue.length > 0) {
  294. alert.current = alert.queue.shift();
  295. // Start timout for message-pruning
  296. alert.timeout = $timeout(function() {
  297. if (alert.queue.length == 0) {
  298. alert.current = undefined;
  299. alert.timeout = undefined;
  300. } else {
  301. $rootScope.showAlert(alert);
  302. }
  303. }, (alert.current.type == 'error' ? 5000 : 1000));
  304. } else {
  305. $rootScope.alerts.current = undefined;
  306. }
  307. };
  308. $rootScope.addAlert = function(message, type) {
  309. var newAlert = {message: message, type: type};
  310. if (!$rootScope.alerts.timeout) {
  311. // Timeout for message queue is not running, start one
  312. $rootScope.alerts.queue.push(newAlert);
  313. $rootScope.showAlert($rootScope.alerts);
  314. } else {
  315. $rootScope.alerts.queue.push(newAlert);
  316. }
  317. };
  318. $rootScope.dismissAlert = function() {
  319. if (!$rootScope.alerts.timeout) {
  320. $rootScope.alerts.current = undefined;
  321. } else {
  322. $timeout.cancel($rootScope.alerts.timeout);
  323. $rootScope.alerts.timeout = undefined;
  324. $rootScope.showAlert($rootScope.alerts);
  325. }
  326. };
  327. $rootScope.addAlertPromise = function(promise, type) {
  328. if (promise) {
  329. promise.then(function(data) {
  330. $rootScope.addAlert(data, type);
  331. });
  332. }
  333. };
  334. }
  335. ])
  336. // Moment-JS date-formatting filter
  337. .filter('dateformat', function() {
  338. return function(date, format) {
  339. if (date) {
  340. if (format) {
  341. return moment(date).format(format);
  342. } else {
  343. return moment(date).calendar();
  344. }
  345. }
  346. return '';
  347. };
  348. });