forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity.js
More file actions
executable file
·221 lines (191 loc) · 6.89 KB
/
activity.js
File metadata and controls
executable file
·221 lines (191 loc) · 6.89 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Defines the activity module.
* @module activity
*/
define(["webL10n.sugarizer",
"sugar-web/activity/shortcut",
"sugar-web/bus",
"sugar-web/env",
"sugar-web/datastore",
"sugar-web/presence",
"sugar-web/graphics/icon",
"sugar-web/graphics/activitypalette"], function (
l10n, shortcut, bus, env, datastore, presence, icon, activitypalette) {
'use strict';
var datastoreObject = null;
var presenceCallback = null;
var presenceResponse = null;
/**
* Represents the activity module.
* @namespace activity
*/
var activity = {};
/**
* Sets up the activity, initializing various components and event listeners.
*/
activity.setup = function () {
// Listen for bus events
bus.listen();
// Start localization
l10n.start();
/**
* Sends a pause event when notified by the bus and triggers a reflective prompt.
*/
function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
pauseEvent.initCustomEvent('activityPause', false, false, {
'cancelable': true
});
window.dispatchEvent(pauseEvent);
// Trigger reflective prompt
const reflectionPrompt = `What did you do? Why did you do it? What did you learn? What will you do next?`;
alert(reflectionPrompt);
}
// Listen for 'activity.pause' notification to send pause event
bus.onNotification("activity.pause", sendPauseEvent);
// An activity that handles 'activityStop' can also call
// event.preventDefault() to prevent the close, and explicitly
// call activity.close() after storing.
/**
* Sends a stop event and closes the activity when notified by the bus.
*/
function sendStopEvent() {
var stopEvent = document.createEvent("CustomEvent");
stopEvent.initCustomEvent('activityStop', false, false, {
'cancelable': true
});
var result = window.dispatchEvent(stopEvent);
if (result) {
activity.close();
}
}
// Listen for 'activity.stop' notification to send stop event
bus.onNotification("activity.stop", sendStopEvent);
// Initialize datastore object
datastoreObject = new datastore.DatastoreObject();
// Initialize activity palette
var activityButton = document.getElementById("activity-button");
var activityPalette = new activitypalette.ActivityPalette(
activityButton, datastoreObject);
// Colorize the activity icon.
activity.getXOColor(function (error, colors) {
icon.colorize(activityButton, colors);
var invokerElem =
document.querySelector("#activity-palette .palette-invoker");
icon.colorize(invokerElem, colors);
});
// Make the activity stop with the stop button.
var stopButton = document.getElementById("stop-button");
stopButton.addEventListener('click', function (e) {
sendStopEvent();
});
// Add shortcut for closing activity
shortcut.add("Ctrl", "Q", this.close);
// Get environment information
env.getEnvironment(function (error, environment) {
// Set metadata for datastore object
if (!environment.objectId) {
datastoreObject.setMetadata({
"title": environment.activityName + " Activity",
"title_set_by_user": "0",
"activity": environment.bundleId,
"activity_id": environment.activityId
});
}
// Join network if in Sugarizer environment
if (env.isSugarizer()) {
presence.joinNetwork(function(error, presence) {
if (environment.sharedId) {
presence.joinSharedActivity(environment.sharedId, function() {
var group_color = presence.getSharedInfo().colorvalue;
icon.colorize(activityButton, group_color);
datastoreObject.setMetadata({"buddy_color":group_color});
datastoreObject.save(function() {});
});
}
if (presenceCallback) {
presenceCallback(error, presence);
} else {
presenceResponse = {error: error, presence: presence};
}
});
}
// Save datastore metadata and set title description for activity palette
datastoreObject.save(function () {
datastoreObject.getMetadata(function (error, metadata) {
activityPalette.setTitleDescription(metadata);
});
});
});
};
/**
* Gets the datastore object.
* @returns {object} The datastore object.
*/
activity.getDatastoreObject = function () {
return datastoreObject;
};
/**
* Gets the presence object.
* @param {function} connectionCallback - The callback function to handle the presence object.
* @returns {object} The presence object.
*/
activity.getPresenceObject = function(connectionCallback) {
if (presenceResponse == null) {
presenceCallback = connectionCallback;
} else {
connectionCallback(presenceResponse.error, presenceResponse.presence);
presenceResponse = null;
}
return presence;
};
/**
* Retrieves the XO color.
* @param {function} callback - The callback function to handle the XO color.
*/
activity.getXOColor = function (callback) {
function onResponseReceived(error, result) {
if (error === null) {
callback(null, {
stroke: result[0][0],
fill: result[0][1]
});
} else {
callback(null, {
stroke: "#00A0FF",
fill: "#8BFF7A"
});
}
}
bus.sendMessage("activity.get_xo_color", [], onResponseReceived);
};
/**
* Closes the activity.
* @param {function} callback - The callback function to handle the closing of the activity.
*/
activity.close = function (callback) {
function onResponseReceived(error, result) {
if (error === null) {
callback(null);
} else {
callback(error, null);
}
}
bus.sendMessage("activity.close", [], onResponseReceived);
};
/**
* Shows the object chooser.
* @param {function} callback - The callback function to handle the object chooser.
*/
activity.showObjectChooser = function (callback) {
function onResponseReceived(error, result) {
if (error === null) {
callback(null, result[0]);
} else {
callback(error, null);
}
}
bus.sendMessage("activity.show_object_chooser", [], onResponseReceived);
};
return activity;
});