-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
155 lines (135 loc) · 5.81 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
(function() {
'use strict';
// This file is adapted from Angular UI ngGrid project
// MIT License
// https://github.com/angular-ui/ng-grid/blob/v3.0.0-rc.20/src/js/core/factories/GridApi.js
angular.module('angular-api', []).factory('Api', ['$q', '$rootScope', function($q, $rootScope) {
/**
* Api provides the ability to register public methods events inside an app and allow
* for other components to use the api via featureName.raise.methodName and featureName.on.eventName(function(args){}).
*
* @appInstance: App which the API is for
* @apiId: Unique id in case multiple API instances do exist inside the same Angular environment
*/
var Api = function Api(appInstance, apiId) {
this.gantt = appInstance;
this.apiId = apiId;
this.eventListeners = [];
};
/**
* Used to execute a function while disabling the specified event listeners.
* Disables the listenerFunctions, executes the callbackFn, and then enables the listenerFunctions again
*
* @listenerFuncs: Listener function or array of listener functions to suppress. These must be the same
* @functions that were used in the .on.eventName method
* @callBackFn: Function to execute with surpressed events
*
* Example:
* var clicked = function (){
* // Button clicked event handler
* }
*
* api.suppressEvents(clicked, function() {
* // No clicked events will be fired
* api.ui.form.main.submit.click(scope);
* });
*/
Api.prototype.suppressEvents = function(listenerFuncs, callBackFn) {
var self = this;
var listeners = angular.isArray(listenerFuncs) ? listenerFuncs : [listenerFuncs];
var foundListeners = [];
listeners.forEach(function(l) {
foundListeners = self.eventListeners.filter(function(lstnr) {
return l === lstnr.handler;
});
});
foundListeners.forEach(function(l) {
l.dereg();
});
callBackFn();
foundListeners.forEach(function(l) {
l.dereg = registerEventWithAngular(l.eventId, l.handler, self.gantt, l._this);
});
};
/**
* Registers a new event for the given feature.
*
* @featureName: Name of the feature that raises the event
* @eventName: Name of the event
*
* To trigger the event call:
* .raise.eventName()
*
* To register a event listener call:
* .on.eventName(scope, callBackFn, _this)
* scope: A scope reference to add a deregister call to the scopes .$on('destroy')
* callBackFn: The function to call
* _this: Optional this context variable for callbackFn. If omitted, gantt.api will be used for the context
*
* .on.eventName returns a de-register funtion that will remove the listener. It's not necessary to use it as the listener
* will be removed when the scope is destroyed.
*/
Api.prototype.registerEvent = function(featureName, eventName) {
var self = this;
if (!self[featureName]) {
self[featureName] = {};
}
var feature = self[featureName];
if (!feature.on) {
feature.on = {};
feature.raise = {};
}
var eventId = 'event:api:' + this.apiId + ':' + featureName + ':' + eventName;
// Creating raise event method: featureName.raise.eventName
feature.raise[eventName] = function() {
$rootScope.$emit.apply($rootScope, [eventId].concat(Array.prototype.slice.call(arguments)));
};
// Creating on event method: featureName.oneventName
feature.on[eventName] = function(scope, handler, _this) {
var deregAngularOn = registerEventWithAngular(eventId, handler, self.gantt, _this);
var listener = {
handler: handler,
dereg: deregAngularOn,
eventId: eventId,
scope: scope,
_this: _this
};
self.eventListeners.push(listener);
var removeListener = function() {
listener.dereg();
var index = self.eventListeners.indexOf(listener);
self.eventListeners.splice(index, 1);
};
scope.$on('$destroy', function() {
removeListener();
});
return removeListener;
};
};
function registerEventWithAngular(eventId, handler, app, _this) {
return $rootScope.$on(eventId, function() {
var args = Array.prototype.slice.call(arguments);
args.splice(0, 1); // Remove evt argument
handler.apply(_this ? _this : app, args);
});
}
/**
* Registers a new event for the given feature
*
* @featureName: Name of the feature
* @methodName: Name of the method
* @callBackFn: Function to execute
* @_this: Binds callBackFn 'this' to _this. Defaults to Api.app
*/
Api.prototype.registerMethod = function(featureName, methodName, callBackFn, _this) {
if (!this[featureName]) {
this[featureName] = {};
}
var feature = this[featureName];
feature[methodName] = function() {
callBackFn.apply(_this || this.app, arguments);
};
};
return Api;
}]);
})();