cryptpad/www/teams/inner.js

1406 lines
54 KiB
JavaScript
Raw Normal View History

2019-09-04 14:08:53 +00:00
define([
'jquery',
'/common/toolbar3.js',
'/common/drive-ui.js',
'/common/common-util.js',
2019-10-01 12:18:30 +00:00
'/common/common-hash.js',
2019-09-04 14:08:53 +00:00
'/common/common-interface.js',
'/common/common-ui-elements.js',
2019-09-04 14:08:53 +00:00
'/common/common-feedback.js',
2019-09-24 16:08:59 +00:00
'/common/common-constants.js',
2019-09-04 14:08:53 +00:00
'/bower_components/nthen/index.js',
'/common/sframe-common.js',
'/common/proxy-manager.js',
2019-10-14 10:01:44 +00:00
'/common/userObject.js',
2019-09-09 14:30:28 +00:00
'/common/hyperscript.js',
2019-09-04 14:08:53 +00:00
'/customize/application_config.js',
2019-09-17 09:05:32 +00:00
'/common/messenger-ui.js',
'/common/invitation.js',
2019-09-04 14:08:53 +00:00
'/customize/messages.js',
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
2019-09-24 09:58:57 +00:00
'less!/teams/app-team.less',
2019-09-04 14:08:53 +00:00
], function (
$,
Toolbar,
DriveUI,
Util,
2019-10-01 12:18:30 +00:00
Hash,
2019-09-04 14:08:53 +00:00
UI,
UIElements,
2019-09-04 14:08:53 +00:00
Feedback,
2019-09-24 16:08:59 +00:00
Constants,
2019-09-04 14:08:53 +00:00
nThen,
SFCommon,
ProxyManager,
2019-10-14 10:01:44 +00:00
UserObject,
2019-09-09 14:30:28 +00:00
h,
2019-09-04 14:08:53 +00:00
AppConfig,
2019-09-17 09:05:32 +00:00
MessengerUI,
InviteInner,
2019-09-04 14:08:53 +00:00
Messages)
{
var APP = {};
2019-09-12 15:54:50 +00:00
var driveAPP = {};
2019-09-10 16:37:11 +00:00
//var SHARED_FOLDER_NAME = Messages.fm_sharedFolderName;
2019-09-04 14:08:53 +00:00
var copyObjectValue = function (objRef, objToCopy) {
for (var k in objRef) { delete objRef[k]; }
$.extend(true, objRef, objToCopy);
};
var updateSharedFolders = function (sframeChan, manager, drive, folders, cb) {
if (!drive || !drive.sharedFolders) {
return void cb();
}
var oldIds = Object.keys(folders);
nThen(function (waitFor) {
Object.keys(drive.sharedFolders).forEach(function (fId) {
2019-10-07 16:30:46 +00:00
var sfData = drive.sharedFolders[fId] || {};
2019-10-14 10:01:44 +00:00
var href = UserObject.getHref(sfData, APP.cryptor);
var parsed = Hash.parsePadUrl(href);
2019-10-07 12:35:11 +00:00
var secret = Hash.getSecrets('drive', parsed.hash, sfData.password);
2019-09-04 14:08:53 +00:00
sframeChan.query('Q_DRIVE_GETOBJECT', {
sharedFolder: fId
}, waitFor(function (err, newObj) {
if (newObj && newObj.deprecated) {
delete folders[fId];
delete drive.sharedFolders[fId];
if (manager && manager.folders) {
delete manager.folders[fId];
}
return;
}
2019-09-04 14:08:53 +00:00
folders[fId] = folders[fId] || {};
copyObjectValue(folders[fId], newObj);
2019-10-14 14:02:21 +00:00
folders[fId].readOnly = !secret.keys.secondaryKey;
2019-09-04 14:08:53 +00:00
if (manager && oldIds.indexOf(fId) === -1) {
2019-10-10 10:35:01 +00:00
manager.addProxy(fId, { proxy: folders[fId] }, null, secret.keys.secondaryKey);
2019-09-04 14:08:53 +00:00
}
var readOnly = !secret.keys.editKeyStr;
if (!manager || !manager.folders[fId]) { return; }
manager.folders[fId].userObject.setReadOnly(readOnly, secret.keys.secondaryKey);
2019-09-04 14:08:53 +00:00
}));
});
// Remove from memory folders that have been deleted from the drive remotely
oldIds.forEach(function (fId) {
if (!drive.sharedFolders[fId]) {
delete folders[fId];
delete drive.sharedFolders[fId];
if (manager && manager.folders) {
delete manager.folders[fId];
}
}
});
2019-09-04 14:08:53 +00:00
}).nThen(function () {
cb();
});
};
var updateObject = function (sframeChan, obj, cb) {
sframeChan.query('Q_DRIVE_GETOBJECT', null, function (err, newObj) {
copyObjectValue(obj, newObj);
cb();
});
};
2019-09-06 16:47:18 +00:00
var setEditable = DriveUI.setEditable;
2019-10-14 10:01:44 +00:00
var closeTeam = function (common, cb) {
var sframeChan = common.getSframeChannel();
APP.module.execCommand('SUBSCRIBE', null, function () {
sframeChan.query('Q_SET_TEAM', null, function (err) {
if (err) { return void console.error(err); }
if (APP.drive && APP.drive.close) { APP.drive.close(); }
$('.cp-toolbar-title-value').text(Messages.type.teams);
sframeChan.event('EV_SET_TAB_TITLE', Messages.type.teams);
APP.team = null;
APP.teamEdPublic = null;
APP.drive = null;
APP.cryptor = null;
APP.buildUI(common);
if (APP.usageBar) {
APP.usageBar.stop();
APP.usageBar = null;
}
if (cb) {
cb(common);
}
});
});
};
2019-09-06 16:47:18 +00:00
var mainCategories = {
'list': [
'cp-team-list',
],
'create': [
'cp-team-create',
],
2019-09-24 09:49:11 +00:00
'general': [
'cp-team-info',
],
2019-12-13 16:03:24 +00:00
'link': [
'cp-team-link',
],
2019-09-06 16:47:18 +00:00
};
var teamCategories = {
2019-09-11 14:23:58 +00:00
'back': {
onClick: function (common) {
2019-10-14 10:01:44 +00:00
closeTeam(common);
2019-09-11 14:23:58 +00:00
}
},
2019-09-06 16:47:18 +00:00
'drive': [
2019-09-11 14:23:58 +00:00
'cp-team-drive'
2019-09-06 16:47:18 +00:00
],
'members': [
2019-09-11 14:23:58 +00:00
'cp-team-roster'
2019-09-06 16:47:18 +00:00
],
2019-09-17 09:05:32 +00:00
'chat': [
'cp-team-chat'
],
'admin': [
2019-10-01 12:18:30 +00:00
'cp-team-edpublic',
'cp-team-name',
2019-09-27 16:04:48 +00:00
'cp-team-avatar',
'cp-team-delete',
],
2019-09-04 14:08:53 +00:00
};
2019-09-06 16:47:18 +00:00
var create = {};
// Sidebar layout
var hideCategories = function () {
APP.$rightside.find('> div').hide();
};
var showCategories = function (cat) {
hideCategories();
cat.forEach(function (c) {
APP.$rightside.find('.'+c).show();
});
};
var createLeftSide = APP.createLeftSide = function (common, team, teamAdmin) {
2019-09-06 16:47:18 +00:00
APP.$leftside.empty();
var $categories = $('<div>', {'class': 'cp-sidebarlayout-categories'})
.appendTo(APP.$leftside);
var hash = common.getMetadataMgr().getPrivateData().teamInviteHash && mainCategories.link;
2019-12-13 16:03:24 +00:00
2019-09-06 16:47:18 +00:00
var categories = team ? teamCategories : mainCategories;
2019-12-13 16:03:24 +00:00
var active = team ? 'drive' : (hash ? 'link' : 'list');
2019-09-06 16:47:18 +00:00
if (team && APP.team) {
var $category = $('<div>', {'class': 'cp-sidebarlayout-category cp-team-cat-header'}).appendTo($categories);
var avatar = h('div.cp-avatar');
var $avatar = $(avatar);
APP.module.execCommand('GET_TEAM_METADATA', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) {
return void UI.warn(Messages.error);
}
common.displayAvatar($avatar, obj.avatar, obj.name);
$category.append($avatar);
$avatar.append(h('span.cp-sidebarlayout-category-name', obj.name));
});
}
2019-09-06 16:47:18 +00:00
Object.keys(categories).forEach(function (key) {
if (key === 'admin' && !teamAdmin) { return; }
2019-09-10 16:32:12 +00:00
var $category = $('<div>', {'class': 'cp-sidebarlayout-category cp-team-cat-'+key}).appendTo($categories);
2019-09-06 16:47:18 +00:00
if (key === 'general') { $category.append($('<span>', {'class': 'fa fa-info-circle'})); }
2019-09-10 16:32:12 +00:00
if (key === 'list') { $category.append($('<span>', {'class': 'fa fa-list cp-team-cat-list'})); }
2019-09-06 16:47:18 +00:00
if (key === 'create') { $category.append($('<span>', {'class': 'fa fa-plus-circle'})); }
if (key === 'back') { $category.append($('<span>', {'class': 'fa fa-arrow-left'})); }
if (key === 'members') { $category.append($('<span>', {'class': 'fa fa-users'})); }
2019-09-17 09:05:32 +00:00
if (key === 'chat') { $category.append($('<span>', {'class': 'fa fa-comments'})); }
2019-09-06 16:47:18 +00:00
if (key === 'drive') { $category.append($('<span>', {'class': 'fa fa-hdd-o'})); }
if (key === 'admin') { $category.append($('<span>', {'class': 'fa fa-cogs'})); }
2019-12-18 17:02:33 +00:00
if (key === 'link') { $category.append($('<span>', {'class': 'fa fa-envelope'})); }
2019-09-06 16:47:18 +00:00
if (key === active) {
$category.addClass('cp-leftside-active');
}
$category.click(function () {
if (!Array.isArray(categories[key]) && categories[key].onClick) {
2019-09-11 14:23:58 +00:00
categories[key].onClick(common);
2019-09-06 16:47:18 +00:00
return;
}
2019-09-11 14:23:58 +00:00
if (active === key) { return; }
active = key;
2019-09-17 09:05:32 +00:00
if (key === 'drive' || key === 'chat') {
2019-09-11 14:23:58 +00:00
APP.$rightside.addClass('cp-rightside-drive');
APP.$leftside.addClass('cp-leftside-narrow');
2019-09-11 14:23:58 +00:00
} else {
APP.$rightside.removeClass('cp-rightside-drive');
APP.$leftside.removeClass('cp-leftside-narrow');
2019-09-11 14:23:58 +00:00
}
if (key === 'chat') {
$category.find('.cp-team-chat-notification').removeClass('cp-team-chat-notification');
}
2019-09-11 14:23:58 +00:00
2019-09-06 16:47:18 +00:00
$categories.find('.cp-leftside-active').removeClass('cp-leftside-active');
$category.addClass('cp-leftside-active');
showCategories(categories[key]);
});
2019-09-04 14:08:53 +00:00
$category.append(h('span.cp-sidebarlayout-category-name', Messages['team_cat_'+key] || key));
2019-09-06 16:47:18 +00:00
});
2019-09-11 14:23:58 +00:00
if (active === 'drive') {
APP.$rightside.addClass('cp-rightside-drive');
APP.$leftside.on('mouseover', function() {
APP.$leftside.addClass('cp-leftside-narrow');
APP.$leftside.off('mouseover');
});
2019-09-24 14:55:05 +00:00
} else {
APP.$rightside.removeClass('cp-rightside-drive');
APP.$leftside.removeClass('cp-leftside-narrow');
2019-09-11 14:23:58 +00:00
}
2019-09-06 16:47:18 +00:00
showCategories(categories[active]);
};
var buildUI = APP.buildUI = function (common, team, teamAdmin) {
2019-09-06 16:47:18 +00:00
var $rightside = APP.$rightside;
$rightside.empty();
var addItem = function (cssClass) {
var item = cssClass.slice(8);
if (typeof (create[item]) === "function") {
$rightside.append(create[item](common));
}
};
var categories = team ? teamCategories : mainCategories;
for (var cat in categories) {
if (!Array.isArray(categories[cat])) { continue; }
categories[cat].forEach(addItem);
2019-09-04 14:08:53 +00:00
}
2019-09-06 16:47:18 +00:00
createLeftSide(common, team, teamAdmin);
2019-09-06 16:47:18 +00:00
};
// Team APP
var loadTeam = function (common, id) {
2019-10-10 11:56:12 +00:00
var metadataMgr = common.getMetadataMgr();
var privateData = metadataMgr.getPrivateData();
2019-09-06 16:47:18 +00:00
var sframeChan = common.getSframeChannel();
2019-09-10 16:37:11 +00:00
var proxy = {};
var folders = {};
2019-09-06 16:47:18 +00:00
nThen(function (waitFor) {
updateObject(sframeChan, proxy, waitFor(function () {
updateSharedFolders(sframeChan, null, proxy.drive, folders, waitFor());
}));
2019-09-10 16:37:11 +00:00
}).nThen(function () {
2019-09-06 16:47:18 +00:00
if (!proxy.drive || typeof(proxy.drive) !== 'object') {
throw new Error("Corrupted drive");
}
2019-09-23 13:45:24 +00:00
if (APP.usageBar) { APP.usageBar.stop(); }
APP.usageBar = common.createUsageBar(APP.team, function (err, $limitContainer) {
if (err) { return void DriveUI.logError(err); }
driveAPP.$limit = $limitContainer;
2019-10-07 09:19:02 +00:00
$limitContainer.attr('title', Messages.team_quota);
2019-09-23 13:45:24 +00:00
}, true);
2019-09-12 15:54:50 +00:00
driveAPP.team = id;
2019-10-07 12:35:11 +00:00
// Provide secondaryKey
2019-10-10 11:56:12 +00:00
var teamData = (privateData.teams || {})[id] || {};
driveAPP.readOnly = !teamData.hasSecondaryKey;
2019-09-06 16:47:18 +00:00
var drive = DriveUI.create(common, {
proxy: proxy,
folders: folders,
updateObject: updateObject,
updateSharedFolders: updateSharedFolders,
APP: driveAPP,
2019-10-07 12:35:11 +00:00
edPublic: APP.teamEdPublic,
2019-10-10 11:56:12 +00:00
editKey: teamData.secondaryKey
2019-09-06 16:47:18 +00:00
});
APP.drive = drive;
2019-09-06 16:47:18 +00:00
driveAPP.refresh = drive.refresh;
});
};
var loadMain = function (common) {
buildUI(common);
UI.removeLoadingScreen();
2019-09-04 14:08:53 +00:00
};
2019-09-11 14:23:58 +00:00
// Rightside elements
var makeBlock = function (key, getter, full) {
var safeKey = key.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
2019-09-11 14:23:58 +00:00
create[key] = function (common) {
var $div = $('<div>', {'class': 'cp-team-' + key + ' cp-sidebarlayout-element'});
if (full) {
$('<label>').text(Messages['team_'+safeKey+'Title'] || key).appendTo($div);
$('<span>', {'class': 'cp-sidebarlayout-description'})
.text(Messages['team_'+safeKey+'Hint'] || 'Coming soon...').appendTo($div);
}
2019-09-11 14:23:58 +00:00
getter(common, function (content) {
$div.append(content);
}, $div);
return $div;
};
};
makeBlock('info', function (common, cb) {
cb([
h('h3', Messages.team_infoLabel),
h('p', Messages.team_infoContent)
2019-09-11 14:23:58 +00:00
]);
});
2019-09-24 14:55:05 +00:00
var MAX_TEAMS_SLOTS = Constants.MAX_TEAMS_SLOTS;
2019-10-14 10:01:44 +00:00
var openTeam = function (common, id, team) {
2019-09-11 14:23:58 +00:00
var sframeChan = common.getSframeChannel();
2019-10-14 10:01:44 +00:00
APP.module.execCommand('SUBSCRIBE', id, function () {
var t = Messages._getKey('team_title', [Util.fixHTML(team.metadata.name)]);
sframeChan.query('Q_SET_TEAM', id, function (err) {
if (err) { return void console.error(err); }
// Change title
$('.cp-toolbar-title-value').text(t);
sframeChan.event('EV_SET_TAB_TITLE', t);
// Get secondary key
var secret = Hash.getSecrets('team', team.hash || team.roHash, team.password);
APP.cryptor = UserObject.createCryptor(secret.keys.secondaryKey);
// Load data
APP.team = id;
APP.teamEdPublic = Util.find(team, ['keys', 'drive', 'edPublic']);
buildUI(common, true, team.owner);
});
});
};
var refreshList = function (common, cb) {
2019-09-11 14:23:58 +00:00
var content = [];
APP.module.execCommand('LIST_TEAMS', null, function (obj) {
if (!obj) { return; }
2019-11-14 10:44:23 +00:00
if (obj.error === "OFFLINE") { return UI.alert(Messages.driveOfflineError); }
2019-09-11 14:23:58 +00:00
if (obj.error) { return void console.error(obj.error); }
2019-09-24 14:55:05 +00:00
var list = [];
var keys = Object.keys(obj).slice(0,3);
var slots = '('+Math.min(keys.length, MAX_TEAMS_SLOTS)+'/'+MAX_TEAMS_SLOTS+')';
for (var i = keys.length; i < MAX_TEAMS_SLOTS; i++) {
obj[i] = {
empty: true
2019-09-25 16:21:45 +00:00
};
2019-09-24 14:55:05 +00:00
keys.push(i);
}
content.push(h('h3', Messages.team_listTitle + ' ' + slots));
keys.forEach(function (id) {
2019-09-11 14:23:58 +00:00
var team = obj[id];
2019-09-24 14:55:05 +00:00
if (team.empty) {
2019-09-25 16:21:45 +00:00
list.push(h('div.cp-team-list-team.empty', [
h('span.cp-team-list-name.empty', Messages.team_listSlot)
2019-09-24 14:55:05 +00:00
]));
return;
}
var btn;
var avatar = h('span.cp-avatar');
list.push(h('div.cp-team-list-team', [
h('span.cp-team-list-avatar', avatar),
h('span.cp-team-list-name', {
title: team.metadata.name
}, team.metadata.name),
btn = h('button.cp-team-list-open.btn.btn-primary', Messages.team_listLoad)
]));
common.displayAvatar($(avatar), team.metadata.avatar, team.metadata.name);
2019-09-24 14:55:05 +00:00
$(btn).click(function () {
2019-10-14 10:01:44 +00:00
openTeam(common, id, team);
2019-09-11 14:23:58 +00:00
});
});
2019-09-24 14:55:05 +00:00
content.push(h('div.cp-team-list-container', list));
2019-09-11 14:23:58 +00:00
cb(content);
});
return content;
};
makeBlock('list', function (common, cb) {
refreshList(common, cb);
});
2019-12-18 11:32:13 +00:00
var refreshLink = function () {}; // placeholder
2019-10-08 13:25:19 +00:00
var refreshCreate = function (common, cb) {
2019-09-24 13:25:54 +00:00
var metadataMgr = common.getMetadataMgr();
var privateData = metadataMgr.getPrivateData();
2019-09-11 14:23:58 +00:00
var content = [];
2019-09-24 13:25:54 +00:00
var isOwner = Object.keys(privateData.teams || {}).filter(function (id) {
2019-09-24 13:25:54 +00:00
return privateData.teams[id].owner;
}).length >= Constants.MAX_TEAMS_OWNED && !privateData.devMode;
var getWarningBox = function () {
return h('div.alert.alert-warning', {
2019-09-24 13:25:54 +00:00
role:'alert'
}, isOwner ? Messages.team_maxOwner : Messages._getKey('team_maxTeams', [MAX_TEAMS_SLOTS]));
};
if (Object.keys(privateData.teams || {}).length >= Constants.MAX_TEAMS_SLOTS || isOwner) {
content.push(getWarningBox());
2019-09-24 13:25:54 +00:00
return void cb(content);
}
content.push(h('h3', Messages.team_createLabel));
content.push(h('label', Messages.team_createName));
2019-09-11 14:23:58 +00:00
var input = h('input', {type:'text'});
content.push(input);
var button = h('button.btn.btn-success', Messages.creation_create);
2019-09-11 14:23:58 +00:00
content.push(h('br'));
content.push(h('br'));
content.push(button);
2019-10-08 13:25:19 +00:00
var $spinner = $('<span>', {'class': 'fa fa-spinner fa-pulse'}).hide();
content.push($spinner[0]);
2019-09-11 14:23:58 +00:00
var state = false;
$(button).click(function () {
if (state) { return; }
var name = $(input).val();
if (!name.trim()) { return; }
state = true;
2019-10-08 13:25:19 +00:00
$spinner.show();
APP.module.execCommand('CREATE_TEAM', {
name: name
}, function (obj) {
if (obj && obj.error) {
2019-11-14 10:44:23 +00:00
if (obj.error === "OFFLINE") { return UI.alert(Messages.driveOfflineError); }
console.error(obj.error);
2019-10-08 13:25:19 +00:00
$spinner.hide();
return void UI.warn(Messages.error);
}
// Redraw the create block
var $createDiv = $('div.cp-team-create').empty();
isOwner = true;
$createDiv.append(getWarningBox());
// Redraw the teams list
var $div = $('div.cp-team-list').empty();
refreshList(common, function (content) {
2019-09-11 14:23:58 +00:00
state = false;
$div.append(content);
2019-10-08 13:25:19 +00:00
$spinner.hide();
$('div.cp-team-cat-list').click();
2019-09-11 14:23:58 +00:00
});
var $divLink = $('div.cp-team-link').empty();
if ($divLink.length) {
refreshLink(common, function (content) {
$divLink.append(content);
});
}
2019-09-11 14:23:58 +00:00
});
});
cb(content);
2019-10-08 13:25:19 +00:00
};
makeBlock('create', function (common, cb) {
refreshCreate(common, cb);
2019-09-11 14:23:58 +00:00
});
makeBlock('drive', function (common, cb, $div) {
$('div.cp-team-drive').empty();
$div.removeClass('cp-sidebarlayout-element'); // Don't apply buttons and input styles from sidebarlayout
2019-09-11 14:23:58 +00:00
var content = [
h('div.cp-app-drive-container', {tabindex:0}, [
h('div#cp-app-drive-tree'),
h('div#cp-app-drive-content-container', [
h('div#cp-app-drive-toolbar'),
2019-10-14 14:02:21 +00:00
h('div#cp-app-drive-connection-state', {style: "display: none;"}, Messages.disconnected),
2019-09-11 14:23:58 +00:00
h('div#cp-app-drive-content', {tabindex:2})
])
])
];
UI.addLoadingScreen();
cb(content);
2019-09-12 15:54:50 +00:00
loadTeam(common, APP.team, false);
2019-09-11 14:23:58 +00:00
});
2019-09-19 11:30:31 +00:00
var redrawRoster = function (common, _$roster) {
var $roster = _$roster || $('#cp-team-roster-container');
if (!$roster.length) { return; }
APP.module.execCommand('GET_TEAM_ROSTER', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) {
return void UI.warn(Messages.error);
}
var roster = APP.refreshRoster(common, obj);
$roster.empty().append(roster);
});
};
2019-11-12 11:05:59 +00:00
var makePermissions = function () {
var $blockContainer = UIElements.createModal({
id: 'cp-teams-roster-dialog',
}).show();
var makeRow = function (arr, first) {
return arr.map(function (val) {
return h(first ? 'th' : 'td', val);
});
};
// Global rights
var rows = [];
var firstRow = [Messages.teams_table_role, Messages.share_linkView, Messages.share_linkEdit,
Messages.teams_table_admins, Messages.teams_table_owners];
rows.push(h('tr', makeRow(firstRow, true)));
rows.push(h('tr', makeRow([
Messages.team_viewers, h('span.fa.fa-check'), h('span.fa.fa-times'), h('span.fa.fa-times'), h('span.fa.fa-times')
])));
rows.push(h('tr', makeRow([
Messages.team_members, h('span.fa.fa-check'), h('span.fa.fa-check'), h('span.fa.fa-times'), h('span.fa.fa-times')
])));
rows.push(h('tr', makeRow([
Messages.team_admins, h('span.fa.fa-check'), h('span.fa.fa-check'), h('span.fa.fa-check'), h('span.fa.fa-times')
])));
rows.push(h('tr', makeRow([
Messages.team_owner, h('span.fa.fa-check'), h('span.fa.fa-check'), h('span.fa.fa-check'), h('span.fa.fa-check')
])));
var t = h('table.cp-teams-generic', rows);
var content = [
h('h4', Messages.teams_table_generic),
h('p', [
Messages.teams_table_generic_view,
h('br'),
Messages.teams_table_generic_edit,
h('br'),
Messages.teams_table_generic_admin,
h('br'),
Messages.teams_table_generic_own,
h('br')
]),
t
];
APP.module.execCommand('GET_EDITABLE_FOLDERS', {
teamId: APP.team
}, function (arr) {
if (!Array.isArray(arr) || !arr.length) {
return void $blockContainer.find('.cp-modal').append(content);
}
content.push(h('h5', Messages.teams_table_specific));
content.push(h('p', Messages.teams_table_specificHint));
var paths = arr.map(function (obj) {
obj.path.push(obj.name);
return h('li', obj.path.join('/'));
});
content.push(h('ul', paths));
/*
var rows = [];
rows.push(h('tr', makeRow(firstRow, true)));
rows.push(h('tr', makeRow([Messages.team_viewers, , , '', ''])));
content.push(h('table', rows));
*/
$blockContainer.find('.cp-modal').append(content);
});
};
var ROLES = ['VIEWER', 'MEMBER', 'ADMIN', 'OWNER'];
var describeUser = function (common, curvePublic, data, icon) {
2019-09-19 11:30:31 +00:00
APP.module.execCommand('DESCRIBE_USER', {
teamId: APP.team,
curvePublic: curvePublic,
2019-09-19 11:30:31 +00:00
data: data
}, function (obj) {
if (obj && obj.error) {
$(icon).show();
return void UI.alert(Messages.error);
}
redrawRoster(common);
2019-09-19 11:30:31 +00:00
});
};
var makeMember = function (common, data, me, roster) {
2019-09-19 11:30:31 +00:00
if (!data.curvePublic) { return; }
var otherOwners = Object.keys(roster || {}).some(function (key) {
var user = roster[key];
return user.role === "OWNER" && user.curvePublic !== me.curvePublic && !user.pendingOwner;
});
2019-09-19 11:30:31 +00:00
// Avatar
var avatar = h('span.cp-avatar.cp-team-member-avatar');
common.displayAvatar($(avatar), data.avatar, data.displayName);
// Name
var name = h('span.cp-team-member-name', data.displayName);
2019-09-30 13:17:26 +00:00
if (data.pendingOwner) {
2019-10-01 09:57:21 +00:00
$(name).append(h('em', {
title: Messages.team_pendingOwnerTitle
}, ' ' + Messages.team_pendingOwner));
2019-09-30 13:17:26 +00:00
}
2019-09-25 16:21:45 +00:00
// Status
var status = h('span.cp-team-member-status'+(data.online ? '.online' : ''));
2019-09-19 11:30:31 +00:00
// Actions
var actions = h('span.cp-team-member-actions');
var $actions = $(actions);
var isMe = me && me.curvePublic === data.curvePublic;
var myRole = me ? (ROLES.indexOf(me.role) || 1) : -1;
2019-10-14 10:01:44 +00:00
var theirRole = ROLES.indexOf(data.role);
var ADMIN = ROLES.indexOf('ADMIN');
2019-09-30 13:17:26 +00:00
// If they're an admin and I am an owner, I can promote them to owner
if (!isMe && myRole > theirRole && theirRole === ADMIN && !data.pending) {
2019-09-30 13:20:44 +00:00
var promoteOwner = h('span.fa.fa-angle-double-up', {
2019-09-30 14:39:47 +00:00
title: Messages.team_rosterPromoteOwner
2019-09-30 13:17:26 +00:00
});
2019-09-30 13:20:44 +00:00
$(promoteOwner).click(function () {
2019-09-30 14:39:47 +00:00
UI.confirm(Messages.team_ownerConfirm, function (yes) {
2019-09-30 13:20:44 +00:00
if (!yes) { return; }
2019-10-01 09:33:42 +00:00
$(promoteOwner).hide();
2019-09-30 13:17:26 +00:00
APP.module.execCommand('OFFER_OWNERSHIP', {
teamId: APP.team,
curvePublic: data.curvePublic
}, function (obj) {
if (obj && obj.error) {
console.error(obj.error);
return void UI.warn(Messages.error);
}
UI.log(Messages.sent);
2019-09-30 13:17:26 +00:00
});
});
});
2019-09-30 13:20:44 +00:00
$actions.append(promoteOwner);
2019-09-30 13:17:26 +00:00
}
// If they're a viewer/member and I have a higher role than them, I can promote them to admin
if (!isMe && myRole >= ADMIN && theirRole < ADMIN && !data.pending) {
2019-09-20 13:27:20 +00:00
var promote = h('span.fa.fa-angle-double-up', {
title: Messages.team_rosterPromote
2019-09-19 11:30:31 +00:00
});
$(promote).click(function () {
$(promote).hide();
describeUser(common, data.curvePublic, {
role: ROLES[theirRole + 1]
}, promote);
2019-09-19 11:30:31 +00:00
});
$actions.append(promote);
}
// If I'm not a member and I have an equal or higher role than them, I can demote them
// (if they're not already a MEMBER)
if (myRole >= theirRole && myRole >= ADMIN && theirRole > 0 && !data.pending) {
2019-09-20 13:27:20 +00:00
var demote = h('span.fa.fa-angle-double-down', {
title: Messages.team_rosterDemote
2019-09-19 11:30:31 +00:00
});
$(demote).click(function () {
2019-10-01 09:57:21 +00:00
var todo = function () {
var role = ROLES[theirRole - 1] || 'VIEWER';
2019-10-01 09:57:21 +00:00
$(demote).hide();
describeUser(common, data.curvePublic, {
role: role
}, promote);
};
if (isMe) {
return void UI.confirm(Messages.team_demoteMeConfirm, function (yes) {
if (!yes) { return; }
todo();
});
}
todo();
2019-09-19 11:30:31 +00:00
});
if (!(isMe && myRole === 3 && !otherOwners)) {
$actions.append(demote);
}
2019-09-19 11:30:31 +00:00
}
// If I'm at least an admin and I have an equal or higher role than them, I can remove them
2019-09-30 13:17:26 +00:00
// Note: we can't remove owners, we have to demote them first
if (!isMe && myRole >= ADMIN && myRole >= theirRole && theirRole !== ROLES.indexOf('OWNER')) {
2019-09-20 13:27:20 +00:00
var remove = h('span.fa.fa-times', {
title: Messages.team_rosterKick
2019-09-19 11:30:31 +00:00
});
$(remove).click(function () {
$(remove).hide();
2019-09-30 14:39:47 +00:00
UI.confirm(Messages._getKey('team_kickConfirm', [Util.fixHTML(data.displayName)]), function (yes) {
if (!yes) { return; }
APP.module.execCommand('REMOVE_USER', {
pending: data.pending,
teamId: APP.team,
curvePublic: data.curvePublic,
}, function (obj) {
if (obj && obj.error) {
$(remove).show();
return void UI.alert(Messages.error);
}
redrawRoster(common);
});
2019-09-19 11:30:31 +00:00
});
});
$actions.append(remove);
}
// User
var content = [
avatar,
name,
2019-09-25 16:21:45 +00:00
actions,
status,
2019-09-19 11:30:31 +00:00
];
2019-09-23 09:16:03 +00:00
var div = h('div.cp-team-roster-member', {
title: data.displayName
}, content);
2019-09-19 11:30:31 +00:00
if (data.profile) {
$(div).dblclick(function (e) {
2019-09-19 11:30:31 +00:00
e.preventDefault();
e.stopPropagation();
common.openURL('/profile/#' + data.profile);
});
}
return div;
};
2019-09-19 12:18:09 +00:00
APP.refreshRoster = function (common, roster) {
2019-09-19 11:30:31 +00:00
if (!roster || typeof(roster) !== "object" || Object.keys(roster) === 0) { return; }
var metadataMgr = common.getMetadataMgr();
2019-09-20 13:27:20 +00:00
var userData = metadataMgr.getUserData();
var me = roster[userData.curvePublic] || {};
2019-09-19 11:30:31 +00:00
var owner = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
2019-09-30 13:17:26 +00:00
return roster[k].role === "OWNER" || roster[k].pendingOwner;
2019-09-19 11:30:31 +00:00
}).map(function (k) {
return makeMember(common, roster[k], me, roster);
2019-09-19 11:30:31 +00:00
});
var admins = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
2019-09-19 11:30:31 +00:00
return roster[k].role === "ADMIN";
}).map(function (k) {
return makeMember(common, roster[k], me);
});
var members = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
2019-09-19 11:30:31 +00:00
return roster[k].role === "MEMBER" || !roster[k].role;
}).map(function (k) {
return makeMember(common, roster[k], me);
});
var viewers = Object.keys(roster).filter(function (k) {
if (roster[k].pending) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
return roster[k].role === "VIEWER";
}).map(function (k) {
return makeMember(common, roster[k], me);
});
2019-09-25 16:21:45 +00:00
var pending = Object.keys(roster).filter(function (k) {
if (!roster[k].pending) { return; }
if (roster[k].inviteChannel) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
return roster[k].role === "MEMBER" || roster[k].role === "VIEWER" || !roster[k].role;
2019-09-25 16:21:45 +00:00
}).map(function (k) {
return makeMember(common, roster[k], me);
});
var links = Object.keys(roster).filter(function (k) {
if (!roster[k].pending) { return; }
if (!roster[k].inviteChannel) { return; }
2019-12-18 18:10:30 +00:00
roster[k].curvePublic = k;
return roster[k].role === "VIEWER" || !roster[k].role;
}).map(function (k) {
return makeMember(common, roster[k], me);
});
2019-09-20 13:27:20 +00:00
var header = h('div.cp-app-team-roster-header');
var $header = $(header);
// If you're an admin or an owner, you can invite your friends to the team
// TODO and acquaintances later?
if (me && (me.role === 'ADMIN' || me.role === 'OWNER')) {
var invite = h('button.btn.btn-primary', Messages.team_inviteButton);
2019-09-20 13:27:20 +00:00
var inviteFriends = common.getFriends();
Object.keys(inviteFriends).forEach(function (curve) {
// Keep only friends that are not already in the team and that you can contact
// via their mailbox
if (roster[curve] && !roster[curve].pending) {
delete inviteFriends[curve];
}
});
var inviteCfg = {
teamId: APP.team,
common: common,
friends: inviteFriends,
module: APP.module
};
$(invite).click(function () {
UIElements.createInviteTeamModal(inviteCfg);
});
$header.append(invite);
}
if (me && (me.role !== 'OWNER')) {
var leave = h('button.btn.btn-danger', Messages.team_leaveButton);
2019-09-23 09:34:57 +00:00
$(leave).click(function () {
UI.confirm(Messages.team_leaveConfirm, function (yes) {
2019-09-23 09:34:57 +00:00
if (!yes) { return; }
APP.module.execCommand('LEAVE_TEAM', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) {
return void UI.warn(Messages.error);
}
});
});
});
$header.append(leave);
}
2019-10-28 16:05:11 +00:00
var table = h('button.btn.btn-primary', Messages.teams_table);
$(table).click(function (e) {
e.stopPropagation();
2019-11-12 11:05:59 +00:00
makePermissions();
2019-10-28 16:05:11 +00:00
});
$header.append(table);
2019-09-30 14:39:47 +00:00
var noPending = pending.length ? '' : '.cp-hidden';
var noLinks = links.length ? '' : '.cp-hidden';
2019-09-30 14:39:47 +00:00
2019-09-19 11:30:31 +00:00
return [
2019-09-20 13:27:20 +00:00
header,
h('h3', Messages.team_owner),
2019-09-19 11:30:31 +00:00
h('div', owner),
h('h3', Messages.team_admins),
2019-09-19 11:30:31 +00:00
h('div', admins),
h('h3', Messages.team_members),
2019-09-25 16:21:45 +00:00
h('div', members),
2019-10-22 11:10:57 +00:00
h('h3', Messages.team_viewers || 'VIEWERS'),
h('div', viewers),
2019-09-30 14:39:47 +00:00
h('h3'+noPending, Messages.team_pending),
h('div'+noPending, pending),
h('h3'+noLinks, Messages.team_links),
h('div'+noLinks, links)
2019-09-19 11:30:31 +00:00
];
};
makeBlock('roster', function (common, cb) {
var container = h('div#cp-team-roster-container');
var content = [container];
redrawRoster(common, $(container));
cb(content);
});
2019-09-17 09:05:32 +00:00
makeBlock('chat', function (common, cb) {
var container = h('div#cp-app-contacts-container.cp-app-contacts-inapp');
var content = [container];
APP.module.execCommand('OPEN_TEAM_CHAT', {
teamId: APP.team
}, function (obj) {
2019-09-17 14:24:48 +00:00
if (obj && obj.error) {
return void UI.alert(Messages.error);
2019-09-17 14:24:48 +00:00
}
2019-09-17 09:05:32 +00:00
common.setTeamChat(obj.channel);
MessengerUI.create($(container), common, {
chat: $('.cp-team-cat-chat'),
team: true,
readOnly: obj.readOnly
});
2019-09-17 09:05:32 +00:00
cb(content);
});
});
2019-09-11 14:23:58 +00:00
2019-10-01 12:18:30 +00:00
makeBlock('edpublic', function (common, cb) {
var container = h('div');
var $div = $(container);
var metadataMgr = common.getMetadataMgr();
var privateData = metadataMgr.getPrivateData();
var team = privateData.teams[APP.team];
if (!team) { return void cb(); }
var publicKey = team.edPublic;
var name = team.name;
if (publicKey) {
var $key = $('<div>', {'class': 'cp-sidebarlayout-element'}).appendTo($div);
var userHref = Hash.getUserHrefFromKeys(privateData.origin, name, publicKey);
var $pubLabel = $('<span>', {'class': 'label'})
.text(Messages.settings_publicSigningKey);
$key.append($pubLabel).append(UI.dialog.selectable(userHref));
}
var content = [container];
cb(content);
});
makeBlock('name', function (common, cb) {
var $inputBlock = $('<div>', {'class': 'cp-sidebarlayout-input-block'});
var $input = $('<input>', {
'type': 'text',
'id': 'cp-settings-displayname',
'placeholder': Messages.anonymous}).appendTo($inputBlock);
var $save = $('<button>', {'class': 'btn btn-primary'}).text(Messages.settings_save).appendTo($inputBlock);
var $ok = $('<span>', {'class': 'fa fa-check', title: Messages.saved}).hide();
var $spinner = $('<span>', {'class': 'fa fa-spinner fa-pulse'}).hide();
var todo = function () {
var newName = $input.val();
2019-09-24 16:08:59 +00:00
if (!newName.trim()) { return; }
$spinner.show();
2019-09-23 08:51:35 +00:00
APP.module.execCommand('GET_TEAM_METADATA', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) { return void UI.warn(Messages.error); }
obj.name = newName;
APP.module.execCommand('SET_TEAM_METADATA', {
teamId: APP.team,
metadata: obj
}, function () {
$spinner.hide();
$ok.show();
});
});
};
APP.module.execCommand('GET_TEAM_METADATA', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) {
return void UI.warn(Messages.error);
}
$input.val(obj.name);
$input.on('keyup', function (e) {
if ($input.val() !== obj.name) { $ok.hide(); }
if (e.which === 13) { todo(); }
});
$save.click(todo);
var content = [
$inputBlock[0],
$ok[0],
$spinner[0]
];
cb(content);
});
}, true);
makeBlock('avatar', function (common, cb) {
// Upload
var avatar = h('div.cp-team-avatar.cp-avatar');
var $avatar = $(avatar);
var data = UIElements.addAvatar(common, function (ev, data) {
if (!data.url) { return void UI.warn(Messages.error); }
2019-09-23 08:51:35 +00:00
APP.module.execCommand('GET_TEAM_METADATA', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) { return void UI.warn(Messages.error); }
obj.avatar = data.url;
APP.module.execCommand('SET_TEAM_METADATA', {
teamId: APP.team,
metadata: obj
}, function () {
$avatar.empty();
common.displayAvatar($avatar, data.url);
});
});
});
var $upButton = common.createButton('upload', false, data);
$upButton.text(Messages.profile_upload);
$upButton.prepend($('<span>', {'class': 'fa fa-upload'}));
APP.module.execCommand('GET_TEAM_METADATA', {
teamId: APP.team
}, function (obj) {
if (obj && obj.error) {
return void UI.warn(Messages.error);
}
var val = obj.avatar;
if (!val) {
var $img = $('<img>', {
src: '/customize/images/avatar.png',
title: Messages.profile_avatar,
alt: 'Avatar'
});
var mt = h('media-tag', $img[0]);
$avatar.append(mt);
} else {
common.displayAvatar($avatar, val);
}
// Display existing + button
var content = [
avatar,
h('br'),
$upButton[0]
];
cb(content);
});
}, true);
2019-09-30 14:39:47 +00:00
makeBlock('delete', function (common, cb) {
var deleteTeam = h('button.btn.btn-danger', Messages.team_deleteButton);
2019-09-27 16:04:48 +00:00
var $ok = $('<span>', {'class': 'fa fa-check', title: Messages.saved}).hide();
var $spinner = $('<span>', {'class': 'fa fa-spinner fa-pulse'}).hide();
var deleting = false;
$(deleteTeam).click(function () {
if (deleting) { return; }
2019-09-30 14:39:47 +00:00
UI.confirm(Messages.team_deleteConfirm, function (yes) {
2019-09-30 13:20:44 +00:00
if (!yes) { return; }
2019-09-27 16:04:48 +00:00
if (deleting) { return; }
deleting = true;
$spinner.show();
APP.module.execCommand("DELETE_TEAM", {
teamId: APP.team
}, function (obj) {
$spinner.hide();
2019-09-30 13:20:44 +00:00
deleting = false;
2019-09-27 16:04:48 +00:00
if (obj && obj.error) {
return void UI.warn(obj.error);
}
$ok.show();
2019-09-30 14:39:47 +00:00
UI.log(Messages.deleted);
2019-09-27 16:04:48 +00:00
});
});
});
cb([
deleteTeam,
$ok[0],
$spinner[0]
]);
}, true);
2019-12-16 17:36:34 +00:00
var displayUser = function (common, data) {
var avatar = h('span.cp-teams-invite-from-avatar.cp-avatar');
UIElements.displayAvatar(common, $(avatar), data.avatar, data.displayName);
return h('div.cp-teams-invite-from-author', [
avatar,
h('span.cp-teams-invite-from-name', data.displayName)
]);
};
2019-12-18 17:25:44 +00:00
refreshLink = function (common, cb, wrongPassword) {
if (!mainCategories.link) { return; }
2019-12-17 09:57:13 +00:00
var privateData = common.getMetadataMgr().getPrivateData();
var hash = privateData.teamInviteHash;
2019-12-16 17:36:34 +00:00
var hashData = Hash.parseTypeHash('invite', hash);
var password = hashData.password;
var seeds = InviteInner.deriveSeeds(hashData.key);
if (Object.keys(privateData.teams || {}).length >= Constants.MAX_TEAMS_SLOTS) {
return void cb([
h('div.alert.alert-danger', {
role: 'alert'
}, Messages._getKey('team_maxTeams', [Constants.MAX_TEAMS_SLOTS]))
]);
}
2019-12-16 17:36:34 +00:00
var div = h('div', [
h('i.fa.fa-spin.fa-spinner')
]);
var $div = $(div);
2019-12-17 16:18:40 +00:00
var errorBlock;
var c = [
2019-12-18 12:57:43 +00:00
h('h2', Messages.team_inviteTitle || 'Team invitation'), // XXX
2019-12-18 17:25:44 +00:00
errorBlock = h('div.alert.alert-danger',
wrongPassword ? undefined : {style: 'display: none;'},
wrongPassword ? Messages.drive_sfPasswordError : undefined),
2019-12-16 17:36:34 +00:00
div
];
2019-12-18 16:46:04 +00:00
// "cb" will put the content into the UI.
// We're displaying a spinner while we're cryptgetting the preview content
cb(c);
2019-12-18 16:46:04 +00:00
var declineButton = h('button.btn.btn-danger', {
style: 'display: none;'
}, Messages.friendRequest_decline);
2019-12-18 12:57:43 +00:00
var acceptButton = h('button.btn.btn-primary', Messages.team_inviteJoin || 'JOIN TEAM'); // XXX
2019-12-17 16:27:41 +00:00
var inviteDiv = h('div', [
h('nav', [
declineButton,
acceptButton
])
]);
2019-12-16 17:36:34 +00:00
var $inviteDiv = $(inviteDiv);
2019-12-17 16:27:41 +00:00
$(declineButton).click(function() {
});
2019-12-16 17:36:34 +00:00
var process = function (pw) {
$inviteDiv.empty();
var bytes64;
2019-12-18 09:52:20 +00:00
var spinnerText;
var $spinner;
2019-12-17 16:27:41 +00:00
nThen(function (waitFor) {
$inviteDiv.append(h('div', [
h('i.fa.fa-spin.fa-spinner'),
2019-12-18 12:57:43 +00:00
spinnerText = h('span', Messages.team_invitePasswordLoading || 'Scrypt...') // XXX
2019-12-17 16:27:41 +00:00
]));
2019-12-18 09:52:20 +00:00
$spinner = $(spinnerText);
2019-12-17 16:27:41 +00:00
setTimeout(waitFor(), 150);
}).nThen(function (waitFor) {
var salt = InviteInner.deriveSalt(pw, AppConfig.loginSalt);
InviteInner.deriveBytes(seeds.scrypt, salt, waitFor(function (bytes) {
bytes64 = bytes;
}));
}).nThen(function (waitFor) {
2019-12-18 12:57:43 +00:00
$spinner.text(Messages.team_inviteGetData || 'get team data'); // XXX
2019-12-18 10:35:39 +00:00
APP.module.execCommand('ACCEPT_LINK_INVITATION', {
2019-12-17 16:27:41 +00:00
bytes64: bytes64,
hash: hash,
password: pw,
2019-12-18 09:52:20 +00:00
}, waitFor(function (obj) {
if (obj && obj.error) {
// Wrong password or other error...
// XXX if DELETED, password prompt again?
console.error(obj.error);
waitFor.abort();
2019-12-18 17:25:44 +00:00
if (obj.error === 'INVALID_INVITE_CONTENT') {
// Wrong password...
var $divLink = $('div.cp-team-link').empty();
if ($divLink.length) {
refreshLink(common, function (content) {
$divLink.append(content);
}, true);
}
}
2019-12-18 09:52:20 +00:00
return;
}
2019-12-18 10:35:39 +00:00
// No error: join successful!
var $div = $('div.cp-team-list').empty();
refreshList(common, function (content) {
$div.append(content);
$('div.cp-team-cat-list').click();
var $divLink = $('div.cp-team-link').empty();
if ($divLink.length) {
$divLink.remove();
$('div.cp-team-cat-link').remove();
delete mainCategories.link;
}
2019-12-18 10:35:39 +00:00
});
var $divCreate = $('div.cp-team-create');
if ($divCreate.length) {
refreshCreate(common, function (content) {
$divCreate.empty().append(content);
});
}
2019-12-18 10:35:39 +00:00
2019-12-17 16:27:41 +00:00
}));
});
2019-12-16 17:36:34 +00:00
};
nThen(function (waitFor) {
2019-12-18 12:05:01 +00:00
// Get preview content.
2019-12-18 16:46:04 +00:00
var sframeChan = common.getSframeChannel();
2019-12-18 17:10:33 +00:00
sframeChan.query('Q_ANON_GET_PREVIEW_CONTENT', { seeds: seeds }, waitFor(function (err, json) {
2019-12-18 17:02:33 +00:00
if (json && json.error) {
2019-12-18 00:27:39 +00:00
// XXX APP.module is not ready yet?
2019-12-17 16:18:40 +00:00
// err === DELETED: different message?
2019-12-18 12:57:43 +00:00
$(errorBlock).text(Messages.error + json.error).show(); // XXX
2019-12-17 16:18:40 +00:00
waitFor.abort();
$div.empty();
return;
2019-12-16 17:36:34 +00:00
// XXX handle errors
}
$div.empty();
$div.append(h('div.cp-teams-invite-from', [
2019-12-18 12:57:43 +00:00
Messages.team_inviteFrom || 'From:', // XXX
2019-12-16 17:36:34 +00:00
displayUser(common, json.author)
2019-12-18 17:02:33 +00:00
]));
2019-12-18 12:57:43 +00:00
$div.append(UI.setHTML(h('p.cp-teams-invite-to'),
Messages._getKey('team_inviteFromMsg',
[Util.fixHTML(json.author.displayName),
Util.fixHTML(json.teamName)]))); // XXX
2019-12-18 12:57:43 +00:00
if (json.message) {
$div.append(h('div.cp-teams-invite-message', json.message));
}
2019-12-16 17:36:34 +00:00
}));
}).nThen(function (waitFor) {
// If you're logged in, move on to the next nThen
if (driveAPP.loggedIn) { return; }
// If you're not logged in, display the login buttons
2019-12-13 16:03:24 +00:00
var anonLogin, anonRegister;
2019-12-18 12:57:43 +00:00
$div.append(h('p', Messages.team_invitePleaseLogin || 'Please log in or register to accept this invitation...')); // XXX
2019-12-16 17:36:34 +00:00
$div.append(h('div', [
anonLogin = h('button.btn.btn-primary', Messages.login_login),
anonRegister = h('button.btn.btn-secondary', Messages.login_register),
]));
2019-12-13 16:03:24 +00:00
$(anonLogin).click(function () {
common.setLoginRedirect(function () {
common.gotoURL('/login/');
});
});
$(anonRegister).click(function () {
common.setLoginRedirect(function () {
common.gotoURL('/register/');
});
});
2019-12-16 17:36:34 +00:00
waitFor.abort();
}).nThen(function () {
$div.append($inviteDiv);
}).nThen(function (waitFor) {
// If there is no password, move on to the next block
if (!password) { return; }
2019-12-13 16:03:24 +00:00
2019-12-16 17:36:34 +00:00
// If there is a password, display the password prompt
2019-12-13 16:03:24 +00:00
var pwInput = UI.passwordInput();
2019-12-17 16:27:41 +00:00
$(acceptButton).click(function () {
2019-12-13 16:03:24 +00:00
var val = $(pwInput).find('input').val();
if (!val) { return; }
process(val);
});
2019-12-18 10:35:21 +00:00
$inviteDiv.prepend(h('div.cp-teams-invite-password', [
2019-12-18 12:57:43 +00:00
h('p', Messages.team_inviteEnterPassword || 'Please enter the invitation password to continue...'), // XXX
2019-12-17 16:27:41 +00:00
pwInput
2019-12-18 12:57:43 +00:00
]));
2019-12-16 17:36:34 +00:00
waitFor.abort();
}).nThen(function () {
// No password, display the invitation proposal
2019-12-17 16:27:41 +00:00
$(acceptButton).click(function () {
process('');
});
2019-12-16 17:36:34 +00:00
});
return c;
};
makeBlock('link', function (common, cb) {
refreshLink(common, cb);
2019-12-13 16:03:24 +00:00
});
2019-10-14 10:01:44 +00:00
var redrawTeam = function (common) {
if (!APP.team) { return; }
var teamId = APP.team;
APP.module.execCommand('LIST_TEAMS', null, function (obj) {
if (!obj) { return; }
if (obj.error) { return void console.error(obj.error); }
var team = obj[teamId];
if (!team) { return; }
closeTeam(common, function () {
openTeam(common, teamId, team);
});
});
};
2019-09-04 14:08:53 +00:00
var main = function () {
var common;
var readOnly;
nThen(function (waitFor) {
$(waitFor(function () {
UI.addLoadingScreen();
}));
window.cryptpadStore.getAll(waitFor(function (val) {
2019-09-06 16:47:18 +00:00
driveAPP.store = JSON.parse(JSON.stringify(val));
2019-09-04 14:08:53 +00:00
}));
SFCommon.create(waitFor(function (c) { common = c; }));
}).nThen(function (waitFor) {
2019-09-06 16:47:18 +00:00
APP.$container = $('#cp-sidebarlayout-container');
APP.$leftside = $('<div>', {id: 'cp-sidebarlayout-leftside'}).appendTo(APP.$container);
APP.$rightside = $('<div>', {id: 'cp-sidebarlayout-rightside'}).appendTo(APP.$container);
2019-09-10 16:37:11 +00:00
var sFrameChan = common.getSframeChannel();
2019-09-06 16:47:18 +00:00
sFrameChan.onReady(waitFor());
2019-09-10 16:37:11 +00:00
}).nThen(function () {
2019-09-06 16:47:18 +00:00
var sframeChan = common.getSframeChannel();
2019-09-04 14:08:53 +00:00
var metadataMgr = common.getMetadataMgr();
var privateData = metadataMgr.getPrivateData();
2019-09-23 09:52:13 +00:00
2019-09-06 16:47:18 +00:00
readOnly = driveAPP.readOnly = metadataMgr.getPrivateData().readOnly;
2019-09-04 14:08:53 +00:00
2019-09-06 16:47:18 +00:00
driveAPP.loggedIn = common.isLoggedIn();
2019-12-13 16:03:24 +00:00
//if (!driveAPP.loggedIn) { throw new Error('NOT_LOGGED_IN'); }
2019-09-06 16:47:18 +00:00
2019-10-01 08:57:46 +00:00
common.setTabTitle(Messages.type.teams);
2019-09-06 16:47:18 +00:00
// Drive data
driveAPP.disableSF = !privateData.enableSF && AppConfig.disableSharedFolders;
2019-09-04 14:08:53 +00:00
2019-09-06 16:47:18 +00:00
// Toolbar
var $bar = $('#cp-toolbar');
2019-09-04 14:08:53 +00:00
var configTb = {
displayed: ['useradmin', 'pageTitle', 'newpad', 'limit', 'notifications'],
2019-10-01 08:57:46 +00:00
pageTitle: Messages.type.teams,
2019-09-04 14:08:53 +00:00
metadataMgr: metadataMgr,
readOnly: privateData.readOnly,
sfCommon: common,
2019-09-06 16:47:18 +00:00
$container: $bar
2019-09-04 14:08:53 +00:00
};
2019-09-06 16:47:18 +00:00
var toolbar = Toolbar.create(configTb);
toolbar.$rightside.hide(); // hide the bottom part of the toolbar
// Update the name in the user menu
driveAPP.$displayName = $bar.find('.' + Toolbar.constants.username);
metadataMgr.onChange(function () {
var name = metadataMgr.getUserData().name || Messages.anonymous;
driveAPP.$displayName.text(name);
});
2019-09-04 14:08:53 +00:00
2019-09-06 16:47:18 +00:00
// Load the Team module
var onEvent = function (obj) {
var ev = obj.ev;
var data = obj.data;
if (ev === 'LEAVE_TEAM') {
$('div.cp-team-cat-back').click();
return;
}
if (ev === 'ROSTER_CHANGE') {
if (Number(APP.team) === Number(data)) {
redrawRoster(common);
}
return;
}
2019-10-14 10:01:44 +00:00
if (ev === 'ROSTER_CHANGE_RIGHTS') {
redrawTeam(common);
return;
}
};
2019-09-06 16:47:18 +00:00
APP.module = common.makeUniversal('team', {
onEvent: onEvent
2019-09-04 14:08:53 +00:00
});
2019-12-13 16:03:24 +00:00
var hash = privateData.teamInviteHash;
if (!hash && !driveAPP.loggedIn) {
UI.alert(Messages.mustLogin, function () {
common.setLoginRedirect(function () {
common.gotoURL('/login/');
});
}, {forefront: true});
return;
}
if (!hash) {
delete mainCategories.link;
} else if (!driveAPP.loggedIn) {
delete mainCategories.list;
delete mainCategories.create;
}
2019-09-04 14:08:53 +00:00
$('body').css('display', '');
loadMain(common);
2019-09-06 16:47:18 +00:00
metadataMgr.onChange(function () {
var $div = $('div.cp-team-list');
2019-10-08 13:25:19 +00:00
if ($div.length) {
refreshList(common, function (content) {
$div.empty().append(content);
});
}
var $divLink = $('div.cp-team-link').empty();
if ($divLink.length) {
refreshLink(common, function (content) {
$divLink.append(content);
});
}
2019-10-08 13:25:19 +00:00
var $divCreate = $('div.cp-team-create');
if ($divCreate.length) {
refreshCreate(common, function (content) {
$divCreate.empty().append(content);
});
}
});
2019-09-04 14:08:53 +00:00
var onDisconnect = function (noAlert) {
setEditable(false);
if (APP.team && driveAPP.refresh) { driveAPP.refresh(); }
2019-09-06 16:47:18 +00:00
toolbar.failed();
2019-09-04 14:08:53 +00:00
if (!noAlert) { UI.alert(Messages.common_connectionLost, undefined, true); }
};
2019-11-14 10:44:23 +00:00
var onReconnect = function () {
2019-09-04 14:08:53 +00:00
setEditable(true);
if (APP.team && driveAPP.refresh) { driveAPP.refresh(); }
2019-11-14 10:44:23 +00:00
toolbar.reconnecting();
2019-09-04 14:08:53 +00:00
UI.findOKButton().click();
};
sframeChan.on('EV_DRIVE_LOG', function (msg) {
UI.log(msg);
});
sframeChan.on('EV_NETWORK_DISCONNECT', function () {
onDisconnect();
});
2019-11-14 10:44:23 +00:00
sframeChan.on('EV_NETWORK_RECONNECT', function () {
onReconnect();
2019-09-04 14:08:53 +00:00
});
common.onLogout(function () { setEditable(false); });
});
};
main();
});