forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.ts
More file actions
152 lines (131 loc) · 3.81 KB
/
events.ts
File metadata and controls
152 lines (131 loc) · 3.81 KB
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
import { nativeTimeout } from '../util/dom';
import { Platform } from '../platform/platform';
import { ScrollView } from '../util/scroll-view';
/**
* @name Events
* @description
* Events is a publish-subscribe style event system for sending and responding to application-level
* events across your app.
*
* @usage
* ```ts
* import { Events } from 'ionic-angular';
*
* constructor(public events: Events) {}
*
* // first page (publish an event when a user is created)
* function createUser(user) {
* console.log('User created!')
* events.publish('user:created', user);
* }
*
* // second page (listen for the user created event)
* events.subscribe('user:created', (userEventData) => {
* // userEventData is an array of parameters, so grab our first and only arg
* console.log('Welcome', userEventData[0]);
* });
*
* ```
* @demo /docs/v2/demos/events/
*/
export class Events {
private _channels: Array<any> = [];
/**
* Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
*
* @param {string} topic the topic to subscribe to
* @param {function} handler the event handler
*/
subscribe(topic: string, ...handlers: Function[]) {
if (!this._channels[topic]) {
this._channels[topic] = [];
}
handlers.forEach((handler) => {
this._channels[topic].push(handler);
});
}
/**
* Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
*
* @param {string} topic the topic to unsubscribe from
* @param {function} handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler: Function) {
let t = this._channels[topic];
if (!t) {
// Wasn't found, wasn't removed
return false;
}
if (!handler) {
// Remove all handlers for this topic
delete this._channels[topic];
return true;
}
// We need to find and remove a specific handler
let i = t.indexOf(handler);
if (i < 0) {
// Wasn't found, wasn't removed
return false;
}
t.splice(i, 1);
// If the channel is empty now, remove it from the channel map
if (!t.length) {
delete this._channels[topic];
}
return true;
}
/**
* Publish an event to the given topic.
*
* @param {string} topic the topic to publish to
* @param {any} eventData the data to send as the event
*/
publish(topic: string, ...args: any[]) {
var t = this._channels[topic];
if (!t) {
return null;
}
let responses: any[] = [];
t.forEach((handler: any) => {
responses.push(handler(args));
});
return responses;
}
}
export function setupEvents(platform: Platform): Events {
const events = new Events();
// start listening for resizes XXms after the app starts
nativeTimeout(() => {
window.addEventListener('online', (ev) => {
events.publish('app:online', ev);
}, false);
window.addEventListener('offline', (ev) => {
events.publish('app:offline', ev);
}, false);
window.addEventListener('orientationchange', (ev) => {
events.publish('app:rotated', ev);
});
// When that status taps, we respond
window.addEventListener('statusTap', (ev) => {
// TODO: Make this more better
let el = <HTMLElement>document.elementFromPoint(platform.width() / 2, platform.height() / 2);
if (!el) { return; }
let content = <HTMLElement>el.closest('.scroll-content');
if (content) {
var scroll = new ScrollView(content);
scroll.scrollTo(0, 0, 300);
}
});
window.addEventListener('resize', () => {
platform.windowResize();
});
}, 2000);
return events;
}
export function setupProvideEvents(platform: Platform) {
return function() {
return setupEvents(platform);
};
}