forked from NetsBlox/Snap--Build-Your-Own-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactions-ext.js
More file actions
190 lines (164 loc) · 5.25 KB
/
actions-ext.js
File metadata and controls
190 lines (164 loc) · 5.25 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
/* globals UndoManager, SERVER_ADDRESS, ActionManager, SnapActions, NetsBloxSerializer,
HintInputSlotMorph, SnapCloud, Action, copy*/
// NetsBlox Specific Actions
SnapActions.addActions(
'addMessageType',
'deleteMessageType'
);
ActionManager.URL = 'ws://' + SERVER_ADDRESS + '/collaboration';
ActionManager.prototype._deleteMessageType = function(name) {
var fields = this.ide().stage.messageTypes.getMsgType(name).fields;
return [name, fields];
};
ActionManager.prototype.onAddMessageType = function(name, fields) {
var ide = this.ide();
ide.stage.addMessageType({
name: name,
fields: fields
});
ide.flushBlocksCache('network'); // b/c of inheritance
ide.refreshPalette();
this.completeAction();
};
ActionManager.prototype.onDeleteMessageType = function(name) {
var ide = this.ide();
ide.stage.deleteMessageType(name);
ide.flushBlocksCache('network'); // b/c of inheritance
ide.refreshPalette();
this.completeAction();
};
// HintInputSlotMorph support
ActionManager.prototype._setField = function(field, value) {
var fieldId = this.getId(field),
oldValue = field.contents().text;
if (field instanceof HintInputSlotMorph && field.empty) {
oldValue = '';
}
return [
fieldId,
value,
oldValue
];
};
UndoManager.Invert.addMessageType = function() {
return 'deleteMessageType';
};
UndoManager.Invert.deleteMessageType = function() {
return 'addMessageType';
};
SnapActions.serializer = new NetsBloxSerializer();
SnapActions.__sessionId = Date.now();
SnapActions.enableCollaboration =
SnapActions.disableCollaboration = function() {};
SnapActions.isCollaborating = function() {
return this.ide().room.getCurrentOccupants().length > 1;
};
// Recording user actions
SnapActions.send = function(event) {
// Netsblox addition: start
var socket = this.ide().sockets;
this._ws = socket.websocket;
// Netsblox addition: end
event.id = event.id || this.lastSeen + 1;
if (!this.isUserAction(event)) {
this.lastSent = event.id;
}
if (event.type !== 'openProject') {
// Netsblox addition: start
socket.send(JSON.stringify({
type: 'user-action',
action: event
}));
// Netsblox addition: end
}
};
SnapActions.submitAction = function(action) {
this.recordActionNB(copy(action));
return ActionManager.prototype.submitAction.call(this, action);
};
SnapActions.onMessage = function(msg) {
ActionManager.prototype.onMessage.apply(this, arguments);
if (location.hash.indexOf('collaborate') !== -1) {
location.hash = '';
}
if (msg.type === 'session-user-count') {
this.sessionUsersCount = msg.value;
}
};
SnapActions.requestMissingActions = function() {
var socket = this.ide().sockets;
if (!socket.inActionRequest) {
socket.inActionRequest = true;
return socket.sendJSON({
type: 'request-actions',
actionId: this.lastSeen
});
}
};
SnapActions.onReceiveAction = function(msg) {
// If the message is not building on the current commit, then
// request the commits up until our current commit
var lastId = this.lastSeen;
if (this.queuedActions.length) {
lastId = this.queuedActions[this.queuedActions.length-1].id;
}
var missingActions = lastId < (msg.id - 1);
if (missingActions) {
return this.requestMissingActions();
}
ActionManager.prototype.onReceiveAction.apply(this, arguments);
};
SnapActions.recordActionNB = function(action) {
var socket = this.ide().sockets,
msg = {};
// Record the action
msg.type = 'record-action';
msg.sessionId = this.__sessionId;
msg.action = action;
socket.sendMessage(msg);
};
SnapActions.completeAction = function(error) {
if (error) {
this.ide().submitBugReport(null, error);
}
return ActionManager.prototype.completeAction.apply(this, arguments);
};
SnapActions.submitIfAllowed = function(event) {
var myself = this,
ide = this.ide(),
room = ide.room;
if (event.type === 'openProject') {
this.submitAction(event);
} else if (room.isCapturingTrace()) {
ide.promptExitTraceCapture(function() {
myself.submitIfAllowed(event);
});
} else if (room.isReplayingTrace()) {
ide.promptExitTraceReplay(function() {
myself.submitIfAllowed(event);
});
} else if (!room.isEditable()) {
ide.confirm(
'Edits cannot be made on projects by guests.\n\nWould ' +
'you like to request to be made a collaborator?',
'Request Collaborator Priviledges?',
function () {
ide.sockets.sendMessage({
type: 'permission-elevation-request',
projectId: SnapCloud.projectId,
guest: SnapCloud.username
});
}
);
} else {
ActionManager.prototype.submitIfAllowed.call(this, event);
}
};
SnapActions.isReadOnly = function() {
var room = this.ide().room;
var isEditable = room.isEditable() &&
!room.isCapturingTrace() &&
!room.isReplayingTrace() &&
!this.ide().isReplayMode;
return !isEditable;
};