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