This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
activity_window_manager.js
98 lines (89 loc) · 2.88 KB
/
activity_window_manager.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
'use strict';
(function(exports) {
/**
* ActivityWindowManager manages the activity window instances.
*
* Currently it's only responsible to kill an existing
* activity window if the next creating instance has the same
* manifestURL + pageURL.
*
* @class ActivityWindowManager
*/
function ActivityWindowManager() {
}
ActivityWindowManager.prototype = {
/**
* The list of all current running activity window instances.
* @access private
* @type {Array}
* @memberof ActivityWindowManager.prototype
*/
_activities: [],
/**
* Register all event handlers.
* @memberof ActivityWindowManager.prototype
*/
start: function acwf_start() {
if (this._started) {
return;
}
this._started = true;
window.addEventListener('activitycreating', this);
window.addEventListener('activitycreated', this);
window.addEventListener('activityterminated', this);
},
/**
* Unregister all event handlers.
* @memberof ActivityWindowManager.prototype
*/
stop: function acwf_stop() {
if (!this._started) {
return;
}
this._started = false;
window.removeEventListener('activitycreating', this);
window.removeEventListener('activitycreated', this);
window.removeEventListener('activityterminated', this);
},
/**
* Activity Config
* @typedef {Object} ActivityConfig
* @property {String} manifestURL The manifestURL of the activity
* @property {String} url The URL of the activity handling page
* @property {Boolean} isActivity
* @property {Boolean} inline The disposition of the activty is inline
* or not
*/
handleEvent: function acwf_handleEvent(evt) {
switch (evt.type) {
case 'activitycreating':
// XXX: See Bug 931339
// Only the first matched manifestURL + pageURL is sent with
// system message, so we need to kill the previous opened one
// if the second one "equals" the previous.
var configuration = evt.detail;
this._activities.some(function iterator(activity) {
if (activity.manifestURL == configuration.manifestURL &&
activity.url == configuration.url &&
!activity.isActive()) {
activity.kill();
return true;
}
});
break;
case 'activityterminated':
this._activities.forEach(function iterator(activity, index) {
if (activity.instanceID === evt.detail.instanceID) {
this._activities.splice(index, 1);
return false;
}
}, this);
break;
case 'activitycreated':
this._activities.push(evt.detail);
break;
}
}
};
exports.ActivityWindowManager = ActivityWindowManager;
}(window));