eventbus.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. var KISBPM = KISBPM || {};
  19. /** Inspired by https://github.com/krasimir/EventBus/blob/master/src/EventBus.js */
  20. KISBPM.eventBus = {
  21. /** Event fired when the editor is loaded and ready */
  22. EVENT_TYPE_EDITOR_READY: 'event-type-editor-ready',
  23. /** Event fired when a selection is made on the canvas. */
  24. EVENT_TYPE_SELECTION_CHANGE: 'event-type-selection-change',
  25. /** Event fired when a toolbar button has been clicked. */
  26. EVENT_TYPE_TOOLBAR_BUTTON_CLICKED: 'event-type-toolbar-button-clicked',
  27. /** Event fired when a stencil item is dropped on the canvas. */
  28. EVENT_TYPE_ITEM_DROPPED: 'event-type-item-dropped',
  29. /** Event fired when a property value is changed. */
  30. EVENT_TYPE_PROPERTY_VALUE_CHANGED: 'event-type-property-value-changed',
  31. /** Event fired on double click in canvas. */
  32. EVENT_TYPE_DOUBLE_CLICK: 'event-type-double-click',
  33. /** Event fired on a mouse out */
  34. EVENT_TYPE_MOUSE_OUT: 'event-type-mouse-out',
  35. /** Event fired on a mouse over */
  36. EVENT_TYPE_MOUSE_OVER: 'event-type-mouse-over',
  37. /** Event fired when a model is saved. */
  38. EVENT_TYPE_MODEL_SAVED: 'event-type-model-saved',
  39. /** Event fired when the quick menu buttons should be hidden. */
  40. EVENT_TYPE_HIDE_SHAPE_BUTTONS: 'event-type-hide-shape-buttons',
  41. /** A mapping for storing the listeners*/
  42. listeners: {},
  43. /** The Oryx editor, which is stored locally to send events to */
  44. editor: null,
  45. /**
  46. * Add an event listener to the event bus, listening to the event with the provided type.
  47. * Type and callback are mandatory parameters.
  48. *
  49. * Provide scope parameter if it is important that the callback is executed
  50. * within a specific scope.
  51. */
  52. addListener: function (type, callback, scope) {
  53. // Add to the listeners map
  54. if (typeof this.listeners[type] !== "undefined") {
  55. this.listeners[type].push({scope: scope, callback: callback});
  56. } else {
  57. this.listeners[type] = [
  58. {scope: scope, callback: callback}
  59. ];
  60. }
  61. },
  62. /**
  63. * Removes the provided event listener.
  64. */
  65. removeListener: function (type, callback, scope) {
  66. if (typeof this.listeners[type] != "undefined") {
  67. var numOfCallbacks = this.listeners[type].length;
  68. var newArray = [];
  69. for (var i = 0; i < numOfCallbacks; i++) {
  70. var listener = this.listeners[type][i];
  71. if (listener.scope === scope && listener.callback === callback) {
  72. // Do nothing, this is the listener and doesn't need to survive
  73. } else {
  74. newArray.push(listener);
  75. }
  76. }
  77. this.listeners[type] = newArray;
  78. }
  79. },
  80. hasListener:function(type, callback, scope) {
  81. if(typeof this.listeners[type] != "undefined") {
  82. var numOfCallbacks = this.listeners[type].length;
  83. if(callback === undefined && scope === undefined){
  84. return numOfCallbacks > 0;
  85. }
  86. for(var i=0; i<numOfCallbacks; i++) {
  87. var listener = this.listeners[type][i];
  88. if(listener.scope == scope && listener.callback == callback) {
  89. return true;
  90. }
  91. }
  92. }
  93. return false;
  94. },
  95. /**
  96. * Dispatch an event to all event listeners registered to that specific type.
  97. */
  98. dispatch:function(type, event) {
  99. if(typeof this.listeners[type] != "undefined") {
  100. var numOfCallbacks = this.listeners[type].length;
  101. for(var i=0; i<numOfCallbacks; i++) {
  102. var listener = this.listeners[type][i];
  103. if(listener && listener.callback) {
  104. listener.callback.apply(listener.scope, [event]);
  105. }
  106. }
  107. }
  108. },
  109. dispatchOryxEvent: function(event, uiObject) {
  110. KISBPM.eventBus.editor.handleEvents(event, uiObject);
  111. }
  112. };