-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathsetpermissions.js
616 lines (506 loc) · 24.7 KB
/
setpermissions.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*!
* Copyright 2014 Apereo Foundation (AF) Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
define(['jquery', 'oae.core'], function($, oae) {
/*!
* This widget will be initiated with a widgetData object that contains the following properties:
*
* @param {Number} count The number of items that have been prepared to add into the system
* @param {Object[]} ghosts Ghost items that should be added to the autosuggest share field upon initialization
* @param {Bookean} ghosts[i].selected Whether or not the ghost item should be selected by default
* @param {Object[]} preFill Items that should be pre-filled into the autosuggest share field upon initialization
* @param {Bookean} preFill[i].fixed Whether or not the pre-filled item should be undeleteable from the share list
* @param {String} type The type of item that will be added to the system (one of `file`, `link`, `collabdoc` or `folder`)
* @param {String} visibility The default visibility for the items that are being added to the system (one of `public`, `loggedin` or `private`)
*/
return function(uid, showSettings, widgetData) {
// The widget container
var $rootel = $('#' + uid);
// Caches the member updates that need to be applied. Each key is a
// member (user or group) id and its value is the role for that member.
var currentMembers = {};
// Caches the member the invite. Each key is a member (user or group)
// email and its value is the role for that member.
var currentInvitations = {};
// Variable that keeps track of the current page context, as this will be used as a default
// user/group that the item will be shared with
var pageContext = null;
// Caches the visibility of the context (e.g. group/content/discussion/...)
var visibility = null;
// Variable that will be used to keep track of the current infinite scroll instance
var infinityScroll = null;
// Declare all possible roles for each resourses
var roles = {'collabdoc': [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'editor', 'name': oae.api.i18n.translate('__MSG__CAN_EDIT__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'link': [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'upload': [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'file' : [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'discussion' : [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'folder' : [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
], 'meeting-jitsi' : [
{'id': 'viewer', 'name': oae.api.i18n.translate('__MSG__CAN_VIEW__')},
{'id': 'manager', 'name': oae.api.i18n.translate('__MSG__CAN_MANAGE__')}
]};
////////////////////////////
// ACTIONS //
////////////////////////////
/**
* Trigger an event when changes have been made to the members settings
*/
var saveMembersChange = function() {
// Get members from the autosuggest selection
var membersPartition = _.partition(getAutosuggestSelection(), function(member) {
return (member.profile.resourceType === 'email');
});
var newInvitations = membersPartition[0];
var newMembers = membersPartition[1];
var invitationsUpdates = {};
// Keep track of all members updates
_.each(newMembers, function(newMember) {
currentMembers[newMember.profile.id] = newMember.role;
});
// Keep track of all invitations updates
_.each(newInvitations, function(newInvitation) {
invitationsUpdates[newInvitation.profile.id] = newInvitation.role;
});
// Update the invitations list
if (!_.isEmpty(newInvitations)) {
updateInvitations(invitationsUpdates);
}
// Update the members infinite scroll
var newMembers = _.map(newMembers, function(member) {
member.profile.role = member.role;
return member.profile;
});
// Add members to the permissions modal list
_.each(newMembers, function(newMember) {
infinityScroll.prependItems(newMember);
});
// Inform the widget caller that the member/invitation list have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'members': currentMembers,
'invitations': currentInvitations
});
};
/**
* Fire an event to let the widget caller know that the members change has been aborted
*/
var cancelShareChange = function() {
$(document).trigger('oae.setpermissions.cancel.' + uid);
};
/**
* Trigger an event when changes have been made to the visibility settings
*/
var saveVisibilityChange = function() {
// Get the selected visibility setting
visibility = $('#setpermissions-visibility input[type="radio"]:checked', $rootel).val();
// Inform the widget caller that the permissions have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'visibility': visibility
});
// Change icone visibility on the permission modal
$('#setpermissions-overview-visibility-container i').attr('class', 'fa fa-oae-' + visibility + ' pull-left');
// Change text visibility on the permission modal
if (visibility === 'private') {
$('#setpermissions-overview-visibility-container span').html(oae.api.i18n.translate('__MSG__PRIVATE__'));
} else if (visibility === 'loggedin') {
$('#setpermissions-overview-visibility-container span').html(widgetData.oae.data.me.tenant.displayName);
} else {
$('#setpermissions-overview-visibility-container span').html(oae.api.i18n.translate('__MSG__PUBLIC__'));
}
// Inform the widget caller that visibility have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'visibility': visibility
});
};
/**
* Fire an event to let the widget caller know that the permission change has been aborted
*/
var cancelPermissionsChange = function() {
$(document).trigger('oae.setpermissions.cancel.' + uid);
};
/**
* Get the folder's members if the resource is inside of a folder
*
* @param {String} folderId The folder id
* @param {Function} callback Standard callback function
*/
var getFolderMembers = function(folderId, callback) {
if (oae.data.me.id === folderId) {
return callback(null, []);
}
// Get all members
oae.api.folder.getMembers(folderId, null, null, function(err, members) {
if (err) {
return callback(err);
};
// Get out the creator of the resource
var folderMembers = _.chain(members.results)
.map(function(member) { member.profile.role = member.role; return member.profile; })
.reject(function(member) { return member.id === widgetData.oae.data.me.id; })
.value();
if (_.isEmpty(folderMembers)) {
return callback(null, []);
} else {
return callback(null, folderMembers);
}
});
};
/**
* Remove a member from the member list.
*/
var deleteMember = function() {
var memberId = $(this).attr('data-id');
// Remove members from the list
if (oae.api.util.validation().isValidEmail(memberId)) {
delete currentInvitations[memberId];
currentInvitations = _.reject(currentInvitations, function(member) { return member.id == memberId; });
// If there is no more pending invitations, hide the list
if (_.isEmpty(currentInvitations)) {
currentInvitations = {};
$('#setpermissions-overview-invitations').hide();
}
updateInvitations();
} else {
// Remove members to the permissions modal list
infinityScroll.removeItems(memberId);
delete currentMembers[memberId];
// Inform the widget caller that the member list have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'members': currentMembers
});
}
};
/**
* Update the role of a member. This will only be persisted when the `Save` button is clicked.
*/
var updateRole = function() {
var memberId = $(this).attr('data-id');
var selectedRole = $(this).val();
if (oae.api.util.validation().isValidEmail(memberId)) {
currentInvitations[memberId] = selectedRole;
// Inform the widget caller that the invitation list have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'invitations': currentInvitations
});
} else {
currentMembers[memberId] = selectedRole;
// Inform the widget caller that the member list have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'members': currentMembers,
});
}
};
/**
* Update the invitations from the current invitation role state
*/
var updateInvitations = function(invitationsUpdates) {
_.each(invitationsUpdates, function(newInvitationRole, newInvitationEmail) {
currentInvitations[newInvitationEmail] = createMemberProfileForEmail(newInvitationEmail, newInvitationRole);
});
if (_.isEmpty(currentInvitations)) {
$('#setpermissions-overview-invitations').hide();
} else {
$('#setpermissions-overview-invitations').show();
oae.api.util.template().render($('#setpermissions-overview-selected-template'), {
'creator': widgetData.oae.data.me.id,
'results': currentInvitations,
'roles': roles[widgetData.type],
'displayOptions': {
'metadata': false
}
}, $('#setpermissions-overview-selected-invitations-container'));
}
// Inform the widget caller that the invitation list have changed
$(document).trigger('oae.setpermissions.changed.' + uid, {
'invitations': currentInvitations
});
};
/**
* Given an email and a role, create a standard resource member profile of resource type
* 'email', where the id is assigned to the email in place of a proper resource id
*
* @param {String} email The email from which to create a standard resource profile
* @param {String} role The role of the resource in the members list
* @return {Object} The standard resource member object required from the members list template
*/
var createMemberProfileForEmail = function(email, role) {
return {
'resourceType': 'email',
'id': email,
'displayName': email,
'role': role,
'beenInvited': false
};
};
////////////////////////////
// RENDER / SHOW PANEL //
////////////////////////////
/**
* Renders the visibility view.
*/
var renderVisibility = function() {
oae.api.util.template().render($('#setpermissions-visibility-template', $rootel), {
'contextProfile': widgetData.contextProfile,
'visibility': widgetData.visibility,
'resourceType': widgetData.type,
'tenant': widgetData.oae.data.me.tenant.displayName
}, $('#setpermissions-visibility', $rootel));
};
/**
* Renders the share view.
*/
var renderShare = function() {
oae.api.util.template().render($('#setpermissions-share-template', $rootel), {
'results': [widgetData.contextProfile],
'defaultRole': widgetData.defaultRole,
'roles': roles[widgetData.type]
}, $('#setpermissions-share', $rootel));
$('#setpermissions-savepermissions-share', $rootel).prop('disabled', true);
setUpAutoSuggest();
};
/**
* Shows a specified panel using the provided name.
*
* @param {String} panel The name of the panel to show (i.e. 'visibility')
*/
var showPanel = function(panel) {
$('.modal-body > div').hide();
$('.modal-footer').hide();
if (widgetData.type === 'file') {
$('#upload-permissions-container').show();
} else {
$('#create' + widgetData.type + '-permissions-container').show();
}
$('#' + uid + ' > div').hide();
$('#setpermissions-' + panel + '-footer').show();
$('#setpermissions-' + panel).show();
};
////////////////////////////
// AUTO SUGGEST //
////////////////////////////
/**
* Disable/enable the add button when an item has been added/removed from the autosuggest field.
*/
var autoSuggestChanged = function() {
$('#setpermissions-savepermissions-share', $rootel).prop('disabled', getAutosuggestSelection().length ? false : true);
};
/**
* Initializes the autosuggest used for sharing with other users or groups.
*/
var setUpAutoSuggest = function() {
oae.api.util.autoSuggest().setup($('#setpermissions-share-autosuggest', $rootel), {
'allowEmail': true,
'exclude': widgetData.oae.data.me.id,
'selectionChanged': autoSuggestChanged
}, null, function() {
// Focus on the autosuggest field once it has been set up
focusAutoSuggest();
});
};
/**
* Focus on the autosuggest field used for sharing with other users or groups after the
* autosuggest component has finished initializing and after the modal dialog has finished
* loading.
*/
var focusAutoSuggest = function() {
// Only focus the autosuggest field when the share panel is showing
if ($('#setpermissions-share', $rootel).is(':visible')) {
oae.api.util.autoSuggest().focus($rootel);
}
};
/**
* Get the principals that were selected in the autosuggest field.
*
* @return {Object[]} Trimmed member object as used by the members feed containing all properties necessary to render a list item in the infinite scroll
*/
var getAutosuggestSelection = function() {
// Convert these into an object that reflects the members feed, using a `profile` property
// for the principal profile and a `role` property for the new role
var selectedItems = [];
$.each(oae.api.util.autoSuggest().getSelection($rootel), function(index, selectedItem) {
// Autosuggest will provide the query that was used to find someone. If someone used
// an email address to locate a user, we use it as criteria that indicates the user
// performing the search can interact with that user, regardless of their profile
// visibility or tenancy
selectedItems.push({
'email': selectedItem.email,
'profile': _.omit(selectedItem, 'email'),
'role': $('#setpermissions-share-role', $rootel).val()
});
});
return selectedItems;
};
////////////////////////////
// MEMBER INFINITE SCROLL //
////////////////////////////
/**
* Initialize a new infinite scroll container that fetches the members.
*/
var renderMembers = function(folderMembers, callback) {
// Disable the previous infinite scroll
if (!_.isEmpty(infinityScroll)) {
infinityScroll.kill();
}
var url = '/api/user/' + widgetData.oae.data.me.id;
// Set up the infinite scroll instance
infinityScroll = $('#setpermissions-overview-shared .oae-list').infiniteScroll(url, {
'limit': 8
}, '#setpermissions-overview-selected-template', {
'scrollContainer': $('#setpermissions-overview-shared', $rootel),
'postProcessor': function(principal) {
var data = {};
// Add the principal
data.results = [principal];
// Add the possible roles for the resource
data.roles = roles[widgetData.type];
data.displayOptions = {
'metadata': false
};
data.creator = widgetData.oae.data.me.id;
_.each(data.results, function(member) {
currentMembers[member.id] = member.role;
});
return data;
},
'postRenderer': function(data, $templateOutput) {
return callback();
}
});
// Add the members of the folders if there is any
_.each(folderMembers, function(member) {
currentMembers[member.id] = member.role;
infinityScroll.prependItems(member);
});
};
//////////////////////
// DEINITIALIZATION //
//////////////////////
/**
* Resets the widget to its initial state
*/
var reset = function() {
// Reset the invitations and members caches
currentMembers = {};
currentInvitations = {};
if (!_.isEmpty(infinityScroll)) {
infinityScroll.kill();
infinityScroll = {};
}
$('#setpermissions-overview-invitations').hide();
};
////////////////////////////
// INITIALIZATION //
////////////////////////////
/**
* Inform the widget caller the initial permissions and members
*/
var initSetPermissions = function() {
var folder = '';
var str = pageContext.id.split(':');
if (str[0] === 'f') {
folder = pageContext.id;
}
$(document).trigger('oae.setpermissions.changed.' + uid, {
'members': currentMembers,
'selectedFolderItems': folder
});
};
/**
* Initialize the manage setpermissions widget
*/
var setUpManageModal = function() {
widgetData.tenantName = 'tenant';
// Render the setpermissions template
oae.api.util.template().render($('#setpermissions-manage-template', $rootel), widgetData, $('#setpermissions-manage', $rootel));
// Generate a user-friendly summary
var summary = oae.api.util.template().render($('#setpermissions-manage-template', $rootel), {
'resourceType': widgetData.type,
'visibility': widgetData.visibility,
'tenantName': widgetData.oae.data.me.tenant.displayName
});
// Send the summary
$(document).trigger('oae.setpermissions.changed.' + uid, {
'summary': summary
});
// Trigger and bind an event that will ask for the page context
$(document).on('oae.context.send.setpermissions.' + uid, function(ev, data) {
pageContext = data;
widgetData.tenantName = pageContext.tenant.displayName;
// Render the setpermissions template
oae.api.util.template().render($('#setpermissions-manage-template', $rootel), widgetData, $('#setpermissions-manage', $rootel));
// If the resource is inside of a folder, get the folder members to add them to the resource
getFolderMembers(pageContext.id, function(err, folderMembers) {
if (err) {
return err;
}
renderMembers(folderMembers, initSetPermissions);
});
});
// Delete a member from the list
$(document).on('click', '.oae-listitem-actions .oae-listitem-remove', deleteMember);
// Update a member's role in the list
$(document).on('change', '.oae-listitem-actions select', updateRole);
renderVisibility();
setUpSetVisibilityModal();
setUpShareModal();
$('#setpermissions-overview-invitations').hide();
// Reset the widget when it is dismissed.
$('#create' + widgetData.type + '-modal').on('hidden.bs.modal', reset);
$('#upload-modal').on('hidden.bs.modal', reset);
};
/**
* Initialize the visibility setpermissions widget
*/
var setUpSetVisibilityModal = function() {
// Open the visibility modal
$(document).on('click', '#setpermissions-change-visibility', function() {
showPanel('visibility');
});
$('#setpermissions-savepermissions', $rootel).on('click', saveVisibilityChange);
$('#setpermissions-cancelpermissions', $rootel).on('click', cancelPermissionsChange);
$(document).trigger('oae.context.get', 'setpermissions.' + uid);
// Catch changes in the visibility radio group
$rootel.on('change', '.oae-large-options-container input[type="radio"]', function() {
$('.oae-large-options-container label', $rootel).removeClass('checked');
$(this).parents('label').addClass('checked');
});
};
/**
* Initialize the share setpermissions widget
*/
var setUpShareModal = function() {
// Open the add members modal
$(document).on('click', '#setpermissions-share-add-more', function() {
renderShare();
showPanel('share');
});
$(document).on('click', '#setpermissions-savepermissions-share', saveMembersChange);
$(document).on('click', '#setpermissions-cancelpermissions-share', cancelShareChange);
};
setUpManageModal();
};
});