-
Notifications
You must be signed in to change notification settings - Fork 440
/
callinfoview.js
370 lines (323 loc) · 11.9 KB
/
callinfoview.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* global Marionette, Handlebars */
/**
*
* @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
(function(OC, OCA, Marionette, Handlebars, $, _) {
'use strict';
OCA.SpreedMe = OCA.SpreedMe || {};
OCA.SpreedMe.Views = OCA.SpreedMe.Views || {};
var TEMPLATE =
'<div class="room-name"></div>' +
'<div class="call-controls-container">' +
' <div class="call-button">' +
' {{#if isInCall}}' +
' <button class="leave-call primary">' + t('spreed', 'Leave call') + '</button>' +
' {{else}}' +
' {{#if hasCall}}' +
' <button class="join-call call-ongoing primary">' + t('spreed', 'Join call') + '</button>' +
' {{else}}' +
' <button class="join-call primary">' + t('spreed', 'Start call') + '</button>' +
' {{/if}}' +
' {{/if}}' +
' </div>' +
'{{#if canModerate}}' +
' <div class="share-link-options">' +
' {{#if canFullModerate}}' +
' <input name="link-checkbox" id="link-checkbox" class="checkbox link-checkbox" value="1" {{#if isPublic}} checked="checked"{{/if}} type="checkbox">' +
' <label for="link-checkbox" class="link-checkbox-label">' + t('spreed', 'Share link') + '</label>' +
' {{/if}}' +
' {{#if isPublic}}' +
' <div class="clipboard-button"><span class="button icon-clippy"></span></div>' +
' <div class="password-button">' +
' <span class="button {{#if hasPassword}}icon-password"{{else}}icon-no-password{{/if}}"></span>' +
' <div class="popovermenu password-menu menu-right">' +
' <ul>' +
' <li>' +
' <span class="menuitem {{#if hasPassword}}icon-password"{{else}}icon-no-password{{/if}} password-option">' +
' <form class="password-form">' +
' <input class="password-input" required maxlength="200" type="password"' +
' placeholder="{{#if hasPassword}}' + t('spreed', 'Change password') + '{{else}}' + t('spreed', 'Set password') + '{{/if}}">'+
' <input type="submit" value="" autocomplete="new-password" class="icon icon-confirm password-confirm"></input>'+
' </form>' +
' </span>' +
' </li>' +
' </ul>' +
' </div>' +
' </div>' +
' {{/if}}' +
' </div>' +
'{{/if}}' +
'{{#if showShareLink}}' +
' <div class="share-link-options">' +
' <div class="clipboard-button"><span class="button icon-clippy"></span></div>' +
' </div>' +
'{{/if}}' +
'</div>';
var CallInfoView = Marionette.View.extend({
tagName: 'div',
template: Handlebars.compile(TEMPLATE),
renderTimeout: undefined,
templateContext: function() {
var canModerate = this._canModerate();
return $.extend(this.model.toJSON(), {
isGuest: this.model.get('participantType') === 4,
isInCall: (this.model.get('participantFlags') & OCA.SpreedMe.app.FLAG_IN_CALL) !== 0,
canModerate: canModerate,
canFullModerate: this._canFullModerate(),
isPublic: this.model.get('type') === 3,
showShareLink: !canModerate && this.model.get('type') === 3,
isDeletable: canModerate && (Object.keys(this.model.get('participants')).length > 2 || this.model.get('numGuests') > 0)
});
},
ui: {
'roomName': 'div.room-name',
'shareLinkOptions': '.share-link-options',
'clipboardButton': '.clipboard-button',
'linkCheckbox': '.link-checkbox',
'joinCallButton': 'button.join-call',
'leaveCallButton': 'button.leave-call',
'passwordButton': '.password-button .button',
'passwordForm': '.password-form',
'passwordMenu': '.password-menu',
'passwordOption': '.password-option',
'passwordInput': '.password-input',
'passwordConfirm': '.password-confirm',
'menu': '.password-menu',
},
regions: {
'roomName': '@ui.roomName',
},
events: {
'change @ui.linkCheckbox': 'toggleLinkCheckbox',
'keyup @ui.passwordInput': 'keyUpPassword',
'click @ui.passwordButton': 'showPasswordInput',
'click @ui.passwordConfirm': 'confirmPassword',
'submit @ui.passwordForm': 'confirmPassword',
'click @ui.joinCallButton': 'joinCall',
'click @ui.leaveCallButton': 'leaveCall',
},
modelEvents: {
'change:hasPassword': function() {
this.renderWhenInactive();
},
'change:hasCall': function() {
this.renderWhenInactive();
},
'change:participantFlags': function() {
this.renderWhenInactive();
},
'change:participantType': function() {
this._updateNameEditability();
// User permission change, refresh even when typing, because the
// action will fail in the future anyway.
this.render();
},
'change:type': function() {
this._updateNameEditability();
this.renderWhenInactive();
}
},
initialize: function() {
this._nameEditableTextLabel = new OCA.SpreedMe.Views.EditableTextLabel({
model: this.model,
modelAttribute: 'name',
modelSaveOptions: {
patch: true,
success: function() {
// Renaming a room by setting "displayName" causes "name" to
// change too in the server, so the model has to be fetched
// again to get the changes.
this.model.fetch();
}.bind(this)
},
extraClassNames: 'room-name',
labelTagName: 'h2',
inputMaxLength: '200',
inputPlaceholder: t('spreed', 'Name'),
labelPlaceholder: t('spreed', 'Conversation name'),
buttonTitle: t('spreed', 'Rename')
});
this._updateNameEditability();
},
renderWhenInactive: function() {
if (!OC._currentMenu || !OC._currentMenu.hasClass('password-menu') || this.ui.passwordInput.length === 0 || this.ui.passwordInput.val() === '') {
this.render();
return;
}
this.renderTimeout = setTimeout(_.bind(this.renderWhenInactive, this), 500);
},
onBeforeRender: function() {
// During the rendering the regions of this view are reset, which
// destroys its child views. If a child view has to be detached
// instead so it can be attached back after the rendering of the
// template finishes it is necessary to call "reset" with the
// "preventDestroy" option (in later Marionette versions a public
// "detachView" function was introduced instead).
// "allowMissingEl" is needed for the first time this view is
// rendered, as the element of the region does not exist yet at that
// time and without that option the call would fail otherwise.
this.getRegion('roomName').reset({ preventDestroy: true, allowMissingEl: true });
},
onRender: function() {
if (!_.isUndefined(this.renderTimeout)) {
clearTimeout(this.renderTimeout);
this.renderTimeout = undefined;
}
// Attach the child view again (or for the first time) after the
// template has been rendered.
this.showChildView('roomName', this._nameEditableTextLabel, { replaceElement: true } );
var roomURL = OC.generateUrl('/call/' + this.model.get('token')),
completeURL = window.location.protocol + '//' + window.location.host + roomURL;
this.ui.clipboardButton.attr('value', completeURL);
this.ui.clipboardButton.attr('data-clipboard-text', completeURL);
this.ui.clipboardButton.tooltip({
placement: 'bottom',
trigger: 'hover',
title: t('spreed', 'Copy link')
});
this.initClipboard();
this.ui.passwordButton.tooltip({
placement: 'bottom',
trigger: 'hover',
title: (this.model.get('hasPassword')) ? t('spreed', 'Change password') : t('spreed', 'Set password')
});
var self = this;
OC.registerMenu($(this.ui.passwordButton), $(this.ui.passwordMenu), function() {
$(self.ui.passwordInput).focus();
});
},
_canModerate: function() {
return this._canFullModerate() || this.model.get('participantType') === 6;
},
_canFullModerate: function() {
return this.model.get('participantType') === 1 || this.model.get('participantType') === 2;
},
_updateNameEditability: function() {
if (this._canModerate() && this.model.get('type') !== 1) {
this._nameEditableTextLabel.enableEdition();
} else {
this._nameEditableTextLabel.disableEdition();
}
// This if-case should be removed when we fix room model for
// oneToOne calls. OneToOne calls should not have userId set as room
// name by default. We use it now for avatars, but a new attribute
// should be added to the room model for displaying room images.
// This has to be added below the "enable/disableEdition" calls as
// those calls render the view if needed, while the setters expect
// the view to be already rendered.
if (this.model.get('type') === 1) {
this._nameEditableTextLabel.setModelAttribute(undefined);
this._nameEditableTextLabel.setLabelPlaceholder(t('spreed', 'Conversation with {name}', {name: this.model.get('displayName')}));
} else {
this._nameEditableTextLabel.setModelAttribute('name');
this._nameEditableTextLabel.setLabelPlaceholder(t('spreed', 'Conversation name'));
}
},
/**
* Share link
*/
toggleLinkCheckbox: function() {
var shareLink = this.ui.linkCheckbox.attr('checked') === 'checked';
$.ajax({
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + this.model.get('token') + '/public',
type: shareLink ? 'POST' : 'DELETE',
success: function() {
OCA.SpreedMe.app.signaling.syncRooms();
}
});
},
joinCall: function() {
OCA.SpreedMe.app.connection.joinCall(this.model.get('token'));
},
leaveCall: function() {
OCA.SpreedMe.app.connection.leaveCurrentCall();
},
/**
* Password
*/
confirmPassword: function(e) {
e.preventDefault();
var newPassword = this.ui.passwordInput.val().trim();
$.ajax({
url: OC.linkToOCS('apps/spreed/api/v1/room', 2) + this.model.get('token') + '/password',
type: 'PUT',
data: {
password: newPassword
},
success: function() {
this.ui.passwordInput.val('');
OC.hideMenus();
OCA.SpreedMe.app.signaling.syncRooms();
}.bind(this),
error: function() {
OC.Notification.show(t('spreed', 'Error occurred while setting password'), {type: 'error'});
}
});
},
keyUpPassword: function(e) {
e.preventDefault();
if (e.keyCode === 27) {
// ESC
OC.hideMenus();
}
},
/**
* Clipboard
*/
initClipboard: function() {
var clipboard = new Clipboard('.clipboard-button');
clipboard.on('success', function(e) {
var $input = $(e.trigger);
$input.tooltip('hide')
.attr('data-original-title', t('core', 'Link copied!'))
.tooltip('fixTitle')
.tooltip({placement: 'bottom', trigger: 'manual'})
.tooltip('show');
_.delay(function() {
$input.tooltip('hide')
.attr('data-original-title', t('core', 'Copy link'))
.tooltip('fixTitle');
}, 3000);
});
clipboard.on('error', function (e) {
var $input = $(e.trigger);
var actionMsg = '';
if (/iPhone|iPad/i.test(navigator.userAgent)) {
actionMsg = t('core', 'Not supported!');
} else if (/Mac/i.test(navigator.userAgent)) {
actionMsg = t('core', 'Press ⌘-C to copy.');
} else {
actionMsg = t('core', 'Press Ctrl-C to copy.');
}
$input.tooltip('hide')
.attr('data-original-title', actionMsg)
.tooltip('fixTitle')
.tooltip({placement: 'bottom', trigger: 'manual'})
.tooltip('show');
_.delay(function () {
$input.tooltip('hide')
.attr('data-original-title', t('spreed', 'Copy link'))
.tooltip('fixTitle');
}, 3000);
});
}
});
OCA.SpreedMe.Views.CallInfoView = CallInfoView;
})(OC, OCA, Marionette, Handlebars, $, _);