cryptpad/www/team/inner.js

466 lines
16 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',
'/common/common-interface.js',
'/common/common-feedback.js',
'/bower_components/nthen/index.js',
'/common/sframe-common.js',
'/common/proxy-manager.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',
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',
'less!/team/app-team.less',
], function (
$,
Toolbar,
DriveUI,
Util,
UI,
Feedback,
nThen,
SFCommon,
ProxyManager,
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,
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) {
sframeChan.query('Q_DRIVE_GETOBJECT', {
sharedFolder: fId
}, waitFor(function (err, newObj) {
folders[fId] = folders[fId] || {};
copyObjectValue(folders[fId], newObj);
if (manager && oldIds.indexOf(fId) === -1) {
manager.addProxy(fId, folders[fId]);
}
}));
});
}).nThen(function () {
cb();
});
};
var updateObject = function (sframeChan, obj, cb) {
sframeChan.query('Q_DRIVE_GETOBJECT', null, function (err, newObj) {
copyObjectValue(obj, newObj);
2019-09-06 16:47:18 +00:00
if (!driveAPP.loggedIn && driveAPP.newSharedFolder) {
2019-09-04 14:08:53 +00:00
obj.drive.sharedFolders = obj.drive.sharedFolders || {};
2019-09-06 16:47:18 +00:00
obj.drive.sharedFolders[driveAPP.newSharedFolder] = {};
2019-09-04 14:08:53 +00:00
}
cb();
});
};
2019-09-06 16:47:18 +00:00
var setEditable = DriveUI.setEditable;
var mainCategories = {
'general': [
'cp-team-info',
],
'list': [
'cp-team-list',
],
'create': [
'cp-team-create',
],
};
var teamCategories = {
2019-09-11 14:23:58 +00:00
'back': {
onClick: function (common) {
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(); }
2019-09-12 15:54:50 +00:00
APP.team = null;
APP.drive = null;
APP.buildUI(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'
],
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();
});
};
2019-09-11 14:23:58 +00:00
var createLeftSide = APP.createLeftSide = function (common, team) {
2019-09-06 16:47:18 +00:00
APP.$leftside.empty();
var $categories = $('<div>', {'class': 'cp-sidebarlayout-categories'})
.appendTo(APP.$leftside);
var categories = team ? teamCategories : mainCategories;
2019-09-11 14:23:58 +00:00
var active = team ? 'drive' : 'list';
2019-09-06 16:47:18 +00:00
Object.keys(categories).forEach(function (key) {
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 === 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');
} else {
APP.$rightside.removeClass('cp-rightside-drive');
}
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
2019-09-06 16:47:18 +00:00
$category.append(Messages['team_cat_'+key] || key); // XXX
});
2019-09-11 14:23:58 +00:00
if (active === 'drive') {
APP.$rightside.addClass('cp-rightside-drive');
}
2019-09-06 16:47:18 +00:00
showCategories(categories[active]);
};
2019-09-11 14:23:58 +00:00
var buildUI = APP.buildUI = function (common, team) {
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
2019-09-11 14:23:58 +00:00
createLeftSide(common, team);
2019-09-06 16:47:18 +00:00
};
// Team APP
2019-09-12 15:54:50 +00:00
var loadTeam = function (common, id, firstLoad) {
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-11 14:23:58 +00:00
if (firstLoad) {
buildUI(common, true);
}
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-12 15:54:50 +00:00
driveAPP.team = id;
2019-09-06 16:47:18 +00:00
var drive = DriveUI.create(common, {
proxy: proxy,
folders: folders,
updateObject: updateObject,
updateSharedFolders: updateSharedFolders,
APP: driveAPP
});
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) {
create[key] = function (common) {
var $div = $('<div>', {'class': 'cp-team-' + key + ' cp-sidebarlayout-element'});
getter(common, function (content) {
$div.append(content);
}, $div);
return $div;
};
};
makeBlock('info', function (common, cb) {
cb([
h('h3', 'Team application'), // XXX
h('p', 'From here you can ...') // XXX
]);
});
var refreshList = function (common, cb) {
var sframeChan = common.getSframeChannel();
var content = [];
content.push(h('h3', 'Your teams'));
APP.module.execCommand('LIST_TEAMS', null, function (obj) {
if (!obj) { return; }
if (obj.error) { return void console.error(obj.error); }
var lis = [];
Object.keys(obj).forEach(function (id) {
var team = obj[id];
var a = h('a', 'Open');
lis.push(h('li', h('ul', [
h('li', 'Name: ' + team.name), // XXX
h('li', 'ID: ' + id), // XXX
h('li', a) // XXX
])));
$(a).click(function () {
APP.module.execCommand('SUBSCRIBE', id, function () {
sframeChan.query('Q_SET_TEAM', id, function (err) {
if (err) { return void console.error(err); }
2019-09-12 15:54:50 +00:00
APP.team = id;
buildUI(common, true);
});
2019-09-11 14:23:58 +00:00
});
});
});
content.push(h('ul', lis));
cb(content);
});
return content;
};
makeBlock('list', function (common, cb) {
refreshList(common, cb);
});
makeBlock('create', function (common, cb) {
var content = [];
content.push(h('h3', 'Create a team')); // XXX
content.push(h('label', 'Team name')); // XXX
var input = h('input', {type:'text'});
content.push(input);
var button = h('button.btn.btn-success', 'Create'); // XXX
content.push(h('br'));
content.push(h('br'));
content.push(button);
var state = false;
$(button).click(function () {
if (state) { return; }
var name = $(input).val();
if (!name.trim()) { return; }
state = true;
UI.confirm('Are you sure?', function (yes) {
if (!yes) {
state = false;
return;
}
APP.module.execCommand('CREATE_TEAM', {
name: name
}, function () {
var $div = $('div.cp-team-list').empty();
refreshList(common, function (content) {
state = false;
$div.append(content);
$('div.cp-team-cat-list').click();
});
});
});
});
cb(content);
});
makeBlock('back', function (common, cb) {
refreshList(common, cb);
});
makeBlock('drive', function (common, cb) {
$('div.cp-team-drive').empty();
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'),
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-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) {
console.warn(obj);
common.setTeamChat(obj.channel);
MessengerUI.create($(container), common, true);
cb(content);
});
});
2019-09-11 14:23:58 +00:00
2019-09-09 14:30:28 +00:00
var onEvent = function (obj) {
var ev = obj.ev;
var data = obj.data;
if (ev === 'PEWPEW') {
2019-09-10 16:37:11 +00:00
data = data;
2019-09-09 14:30:28 +00:00
// Do something
return;
}
};
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-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();
if (!driveAPP.loggedIn) { throw new Error('NOT_LOGGED_IN'); }
common.setTabTitle('TEAMS'); // XXX
// Drive data
if (privateData.newSharedFolder) {
driveAPP.newSharedFolder = privateData.newSharedFolder;
2019-09-04 14:08:53 +00:00
}
2019-09-06 16:47:18 +00:00
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-09-06 16:47:18 +00:00
pageTitle: 'TEAMS', // XXX
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
/* add the usage */
2019-09-06 16:47:18 +00:00
// XXX Teams
if (false) {
// Synchronous callback...
2019-09-04 14:08:53 +00:00
common.createUsageBar(function (err, $limitContainer) {
if (err) { return void DriveUI.logError(err); }
2019-09-06 16:47:18 +00:00
driveAPP.$limit = $limitContainer;
2019-09-04 14:08:53 +00:00
}, true);
}
2019-09-06 16:47:18 +00:00
// Load the Team module
APP.module = common.makeUniversal('team', {
onEvent: onEvent
2019-09-04 14:08:53 +00:00
});
$('body').css('display', '');
2019-09-06 16:47:18 +00:00
if (privateData.teamId) {
2019-09-11 14:23:58 +00:00
loadTeam(common, privateData.teamId, true);
2019-09-06 16:47:18 +00:00
} else {
loadMain(common);
2019-09-04 14:08:53 +00:00
}
2019-09-06 16:47:18 +00:00
2019-09-04 14:08:53 +00:00
var onDisconnect = function (noAlert) {
setEditable(false);
2019-09-06 16:47:18 +00:00
if (driveAPP.refresh) { driveAPP.refresh(); }
toolbar.failed();
2019-09-04 14:08:53 +00:00
if (!noAlert) { UI.alert(Messages.common_connectionLost, undefined, true); }
};
var onReconnect = function (info) {
setEditable(true);
2019-09-06 16:47:18 +00:00
if (driveAPP.refresh) { driveAPP.refresh(); }
toolbar.reconnecting(info.myId);
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();
});
sframeChan.on('EV_NETWORK_RECONNECT', function (data) {
// data.myId;
onReconnect(data);
});
common.onLogout(function () { setEditable(false); });
});
};
main();
});