cryptpad/www/common/common-ui-elements.js

4090 lines
162 KiB
JavaScript
Raw Normal View History

2017-08-18 16:43:04 +00:00
define([
'jquery',
2017-09-21 15:56:24 +00:00
'/api/config',
'/common/common-util.js',
2017-11-13 15:32:40 +00:00
'/common/common-hash.js',
'/common/common-language.js',
'/common/common-interface.js',
2018-01-12 08:48:40 +00:00
'/common/common-constants.js',
2017-11-23 11:28:49 +00:00
'/common/common-feedback.js',
2017-12-11 11:19:44 +00:00
'/common/hyperscript.js',
2018-01-17 17:39:45 +00:00
'/common/clipboard.js',
2017-11-23 11:28:49 +00:00
'/customize/messages.js',
'/customize/application_config.js',
2018-09-13 16:47:46 +00:00
'/customize/pages.js',
'/bower_components/nthen/index.js',
2020-02-17 09:53:04 +00:00
'/common/inner/invitation.js',
'/common/visible.js',
'css!/customize/fonts/cptools/style.css',
], function ($, Config, Util, Hash, Language, UI, Constants, Feedback, h, Clipboard,
Messages, AppConfig, Pages, NThen, InviteInner, Visible) {
var UIElements = {};
2017-11-23 11:28:49 +00:00
2020-05-07 12:58:27 +00:00
UIElements.getSvgLogo = function () {
var svg = (function(){/*
<svg width="45" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<metadata>
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
<dc:title/>
</cc:Work>
</rdf:RDF>
</metadata>
<path d="m22.422 1.4356-14.641 2.7035c-0.51734-0.36799-1.1561-0.57696-1.8393-0.57696a3.1356 3.1339 0 0 0-3.1373 3.1355 3.1602 3.1585 0 0 0 1.6227 2.7575v21.103c0 1.9871 0.88906 4.1328 2.6508 6.3801 1.5602 1.9878 3.7668 4.027 6.5635 6.0547 1.9912 1.4389 3.9942 2.6545 5.6782 3.6005a3.1431 3.1413 0 0 0 5.9855 0.12499c1.7264-0.96163 3.8202-2.224 5.8986-3.7254 2.7979-2.0277 5.0033-4.0668 6.5635-6.0547 1.7624-2.2501 2.6508-4.393 2.6508-6.3801v-21.12c0.95814-0.53813 1.622-1.5642 1.622-2.74 0-1.733-1.4213-3.1355-3.1556-3.1355a3.1075 3.1059 0 0 0-1.8028 0.57696zm-0.03584 3.118 13.379 2.4687c0.01952 0.19629 0.03611 0.37448 0.08998 0.55867l-7.8432 5.1004c-1.4028-1.5215-3.4192-2.4877-5.6439-2.4877-2.2404 0-4.2687 0.98212-5.6797 2.5235l-7.7539-5.028c0.071056-0.21736 0.10472-0.44988 0.12659-0.68519zm14.426 4.4869c0.2616 0.22946 0.5738 0.4256 0.90137 0.55867v20.526c0 0.27043-0.02367 0.55955-0.07169 0.84677-0.31548 1.2546-1.0076 2.5682-2.0734 3.9289-1.3362 1.7018-3.231 3.4367-5.6264 5.172-1.9203 1.3873-3.8601 2.5452-5.3983 3.3932a3.1431 3.1413 0 0 0-4.3574-0.06325c-1.5186-0.84169-3.4128-1.979-5.2825-3.3299-2.3934-1.7353-4.2716-3.4702-5.6072-5.172-1.2729-1.6234-2.017-3.1789-2.2176-4.6492v-20.616c0.31859-0.12253 0.60591-0.30939 0.86477-0.52285l9.9357 6.3976a5.0789 5.0761 0 0 1 4.4893-2.685c1.9639 0 3.6499 1.0816 4.4901 2.7027zm-21.812 6.6849c-0.20732 0.69421-0.33324 1.4177-0.33324 2.1767 0 2.1872 0.94799 4.1262 2.4242 5.5287l-2.7125 5.4541c-0.01034-1.35e-4 -0.02017-0.0015-0.03051-0.0015-1.2807 0-2.3266 1.045-2.3266 2.3246 0 1.2799 1.046 2.3063 2.3266 2.3063a2.3009 2.2996 0 0 0 1.7349-0.78198h3.999v-2.6119h-3.3974l3.1938-6.4136c0.27643-0.55529 0.08702-1.2992-0.42094-1.6546-1.3154-0.91704-2.178-2.4154-2.178-4.1499 0-0.22399 0.03023-0.43738 0.05796-0.65318zm14.737 0.01524-2.3518 1.5372c0.02538 0.20682 0.04347 0.40998 0.04347 0.62422 0 1.7288-0.83863 3.2165-2.149 4.1356-0.50796 0.3555-0.69738 1.0994-0.42094 1.6546l3.1655 6.4281h-3.3829v2.6112h4.1363c0.42696 0.47986 1.0501 0.78274 1.744 0.78274a2.3009 2.2996 0 0 0 2.3076-2.3056c0-1.2799-1.0273-2.3253-2.3076-2.3253-0.05792 0-0.1147 0.0049-0.17158 0.0092l-2.7186-5.4769c1.4697-1.401 2.4106-3.3321 2.4106-5.5143 0-0.74898-0.10409-1.475-0.30503-2.1607zm-7.4398 0.2477a2.1129 2.1118 0 0 0-2.078 2.1111 2.1132 2.1119 0 1 0 4.2262 0 2.1129 2.1118 0 0 0-2.1482-2.1111z" style="stroke-width:1.2608"/>
</svg>
*/}).toString().slice(14,-3);
return svg;
};
UIElements.prettySize = function (bytes) {
var kB = Util.bytesToKilobytes(bytes);
if (kB < 1024) { return kB + Messages.KB; }
var mB = Util.bytesToMegabytes(bytes);
return mB + Messages.MB;
};
2020-06-05 13:14:26 +00:00
UIElements.updateTags = function (common, hrefs) {
var existing, tags;
2020-06-05 13:14:26 +00:00
var allTags = {};
if (!hrefs || typeof (hrefs) === "string") {
hrefs = [hrefs];
}
NThen(function(waitFor) {
common.getSframeChannel().query("Q_GET_ALL_TAGS", null, waitFor(function(err, res) {
if (err || res.error) { return void console.error(err || res.error); }
existing = Object.keys(res.tags).sort();
}));
}).nThen(function (waitFor) {
2020-06-05 13:14:26 +00:00
var _err;
hrefs.forEach(function (href) {
common.getPadAttribute('tags', waitFor(function (err, res) {
if (err) {
if (err === 'NO_ENTRY') {
UI.alert(Messages.tags_noentry);
}
waitFor.abort();
_err = err;
return void console.error(err);
}
2020-06-05 13:14:26 +00:00
allTags[href] = res || [];
if (tags) {
// Intersect with tags from previous pads
tags = (res || []).filter(function (tag) {
return tags.indexOf(tag) !== -1;
});
} else {
tags = res || [];
}
}), href);
});
}).nThen(function () {
UI.dialog.tagPrompt(tags, existing, function (newTags) {
if (!Array.isArray(newTags)) { return; }
2020-06-05 13:14:26 +00:00
var added = [];
var removed = [];
newTags.forEach(function (tag) {
if (tags.indexOf(tag) === -1) {
added.push(tag);
}
});
tags.forEach(function (tag) {
if (newTags.indexOf(tag) === -1) {
removed.push(tag);
}
});
var update = function (oldTags) {
Array.prototype.push.apply(oldTags, added);
removed.forEach(function (tag) {
var idx = oldTags.indexOf(tag);
oldTags.splice(idx, 1);
});
};
hrefs.forEach(function (href) {
var oldTags = allTags[href] || [];
update(oldTags);
common.setPadAttribute('tags', Util.deduplicateString(oldTags), null, href);
});
2017-10-05 14:58:34 +00:00
});
});
};
2020-02-05 15:18:09 +00:00
var dcAlert;
UIElements.disconnectAlert = function () {
if (dcAlert && $(dcAlert.element).length) { return; }
dcAlert = UI.alert(Messages.common_connectionLost, undefined, true);
};
UIElements.reconnectAlert = function () {
if (!dcAlert) { return; }
if (!dcAlert.delete) {
dcAlert = undefined;
return;
}
dcAlert.delete();
dcAlert = undefined;
};
var importContent = function (type, f, cfg) {
return function () {
var $files = $('<input>', {type:"file"});
if (cfg && cfg.accept) {
$files.attr('accept', cfg.accept);
}
$files.click();
$files.on('change', function (e) {
var file = e.target.files[0];
var reader = new FileReader();
var parsed = file && file.name && /.+\.([^.]+)$/.exec(file.name);
var ext = parsed && parsed[1];
reader.onload = function (e) { f(e.target.result, file, ext); };
if (cfg && cfg.binary && cfg.binary.indexOf(ext) !== -1) {
reader.readAsArrayBuffer(file, type);
} else {
reader.readAsText(file, type);
}
});
};
};
2019-09-26 10:53:02 +00:00
UIElements.getUserGrid = function (label, config, onSelect) {
var common = config.common;
2019-09-26 10:53:02 +00:00
var users = config.data;
if (!users) { return; }
2019-09-26 10:53:02 +00:00
var icons = Object.keys(users).map(function (key, i) {
var data = users[key];
var name = data.displayName || data.name || Messages.anonymous;
var avatar = h('span.cp-usergrid-avatar.cp-avatar');
common.displayAvatar($(avatar), data.avatar, name);
2020-02-18 10:12:50 +00:00
var removeBtn, el;
if (config.remove) {
removeBtn = h('span.fa.fa-times');
$(removeBtn).click(function () {
config.remove(el);
});
}
el = h('div.cp-usergrid-user'+(data.selected?'.cp-selected':'')+(config.large?'.large':''), {
'data-ed': data.edPublic,
2019-09-27 14:00:58 +00:00
'data-teamid': data.teamId,
2019-09-13 14:45:07 +00:00
'data-curve': data.curvePublic || '',
2019-09-12 16:51:03 +00:00
'data-name': name.toLowerCase(),
'data-order': i,
style: 'order:'+i+';'
},[
avatar,
2020-02-18 10:12:50 +00:00
h('span.cp-usergrid-user-name', name),
2020-02-18 15:10:39 +00:00
data.notRemovable ? undefined : removeBtn
]);
2020-02-18 10:12:50 +00:00
return el;
}).filter(function (x) { return x; });
2019-09-26 10:53:02 +00:00
var noOthers = icons.length === 0 ? '.cp-usergrid-empty' : '';
2020-02-18 10:12:50 +00:00
var classes = noOthers + (config.large?'.large':'') + (config.list?'.list':'');
var inputFilter = h('input', {
placeholder: Messages.share_filterFriend
});
2020-02-18 10:12:50 +00:00
var div = h('div.cp-usergrid-container' + classes, [
2019-11-25 16:33:56 +00:00
label ? h('label', label) : undefined,
h('div.cp-usergrid-filter', (config.noFilter || config.noSelect) ? undefined : [
2019-12-02 11:34:34 +00:00
inputFilter
]),
]);
var $div = $(div);
// Hide friends when they are filtered using the text input
var redraw = function () {
2019-09-12 16:51:03 +00:00
var name = $(inputFilter).val().trim().replace(/"/g, '').toLowerCase();
2019-09-26 10:53:02 +00:00
$div.find('.cp-usergrid-user').show();
if (name) {
2019-09-26 10:53:02 +00:00
$div.find('.cp-usergrid-user:not(.cp-selected):not([data-name*="'+name+'"])').hide();
}
};
$(inputFilter).on('keydown keyup change', redraw);
2019-09-26 10:53:02 +00:00
$(div).append(h('div.cp-usergrid-grid', icons));
if (!config.noSelect) {
$div.on('click', '.cp-usergrid-user', function () {
var sel = $(this).hasClass('cp-selected');
if (!sel) {
$(this).addClass('cp-selected');
} else {
var order = $(this).attr('data-order');
order = order ? 'order:'+order : '';
$(this).removeClass('cp-selected').attr('style', order);
}
onSelect();
});
}
2019-09-20 13:27:20 +00:00
return {
2019-09-26 10:53:02 +00:00
icons: icons,
div: div
};
};
2019-11-20 15:57:30 +00:00
var createShareWithFriends = function (config, onShare, linkGetter) {
var common = config.common;
2019-09-13 14:45:07 +00:00
var sframeChan = common.getSframeChannel();
var title = config.title;
var friends = config.friends || {};
var teams = config.teams || {};
2019-08-28 14:27:18 +00:00
var myName = common.getMetadataMgr().getUserData().name;
var order = [];
var smallCurves = Object.keys(friends).map(function (c) {
return friends[c].curvePublic.slice(0,8);
});
2019-11-20 09:47:57 +00:00
var div = h('div.contains-nav');
2019-09-13 14:45:07 +00:00
var $div = $(div);
// Replace "copy link" by "share with friends" if at least one friend is selected
// Also create the "share with friends" button if it doesn't exist
var refreshButtons = function () {
2019-09-20 13:27:20 +00:00
var $nav = $div.closest('.alertify').find('nav');
2019-09-26 10:53:02 +00:00
var friendMode = $div.find('.cp-usergrid-user.cp-selected').length;
if (friendMode) {
2019-09-12 16:51:03 +00:00
$nav.find('button.cp-share-with-friends').prop('disabled', '');
} else {
2019-09-12 16:51:03 +00:00
$nav.find('button.cp-share-with-friends').prop('disabled', 'disabled');
}
};
2019-09-20 13:27:20 +00:00
config.noInclude = true;
2019-09-26 10:53:02 +00:00
Object.keys(friends).forEach(function (curve) {
var data = friends[curve];
if (curve.length > 40 && data.notifications) { return; }
delete friends[curve];
});
var others = [];
if (Object.keys(friends).length) {
var friendsList = UIElements.getUserGrid(Messages.share_linkFriends, {
common: common,
data: friends,
noFilter: false,
large: true
}, refreshButtons);
var friendDiv = friendsList.div;
$div.append(friendDiv);
others = friendsList.icons;
}
if (Object.keys(teams).length) {
var teamsList = UIElements.getUserGrid(Messages.share_linkTeam, {
common: common,
noFilter: true,
large: true,
data: teams
}, refreshButtons);
$div.append(teamsList.div);
}
2019-09-13 14:45:07 +00:00
var shareButton = {
2019-09-12 16:51:03 +00:00
className: 'primary cp-share-with-friends',
name: Messages.share_withFriends,
onClick: function () {
var href;
NThen(function (waitFor) {
var w = waitFor();
// linkGetter can be async if this is a burn after reading URL
var res = linkGetter({}, function (url) {
if (!url) {
waitFor.abort();
return;
}
href = url;
setTimeout(w);
});
if (res && /^http/.test(res)) {
href = Hash.getRelativeHref(res);
setTimeout(w);
return;
}
}).nThen(function () {
var $friends = $div.find('.cp-usergrid-user.cp-selected');
$friends.each(function (i, el) {
var curve = $(el).attr('data-curve');
// Check if the selected element is a friend or a team
if (curve) { // Friend
if (!curve || !friends[curve]) { return; }
var friend = friends[curve];
if (!friend.notifications || !friend.curvePublic) { return; }
common.mailbox.sendTo("SHARE_PAD", {
href: href,
password: config.password,
isTemplate: config.isTemplate,
name: myName,
title: title
}, {
channel: friend.notifications,
curvePublic: friend.curvePublic
});
return;
}
// Team
var ed = $(el).attr('data-ed');
var team = teams[ed];
if (!team) { return; }
sframeChan.query('Q_STORE_IN_TEAM', {
2019-09-13 14:45:07 +00:00
href: href,
password: config.password,
path: config.isTemplate ? ['template'] : undefined,
title: title,
teamId: team.id
}, function (err) {
if (err) { return void console.error(err); }
2019-09-13 14:45:07 +00:00
});
2019-09-12 16:51:03 +00:00
});
UI.findCancelButton().click();
// Update the "recently shared with" array:
// Get the selected curves
var curves = $friends.toArray().map(function (el) {
return ($(el).attr('data-curve') || '').slice(0,8);
}).filter(function (x) { return x; });
// Prepend them to the "order" array
Array.prototype.unshift.apply(order, curves);
order = Util.deduplicateString(order);
// Make sure we don't have "old" friends and save
order = order.filter(function (curve) {
return smallCurves.indexOf(curve) !== -1;
});
common.setAttribute(['general', 'share-friends'], order);
if (onShare) {
onShare.fire();
}
2019-09-12 16:51:03 +00:00
});
},
keys: [13]
};
2019-09-12 16:51:03 +00:00
common.getAttribute(['general', 'share-friends'], function (err, val) {
order = val || [];
// Sort friends by "recently shared with"
others.sort(function (a, b) {
var ca = ($(a).attr('data-curve') || '').slice(0,8);
var cb = ($(b).attr('data-curve') || '').slice(0,8);
if (!ca && !cb) { return 0; }
if (!ca) { return 1; }
if (!cb) { return -1; }
var ia = order.indexOf(ca);
var ib = order.indexOf(cb);
if (ia === -1 && ib === -1) { return 0; }
if (ia === -1) { return 1; }
if (ib === -1) { return -1; }
return ia - ib;
});
// Reorder the friend icons
others.forEach(function (el, i) {
$(el).attr('data-order', i).css('order', i);
});
// Display them
2019-09-26 10:53:02 +00:00
$(friendDiv).find('.cp-usergrid-grid').detach();
$(friendDiv).append(h('div.cp-usergrid-grid', others));
2019-09-12 16:51:03 +00:00
refreshButtons();
});
return {
content: div,
buttons: [shareButton]
};
};
var noContactsMessage = function(common){
var metadataMgr = common.getMetadataMgr();
var data = metadataMgr.getUserData();
var origin = metadataMgr.getPrivateData().origin;
if (common.isLoggedIn()) {
return {
content: h('p', Messages.share_noContactsLoggedIn),
buttons: [{
className: 'secondary',
name: Messages.share_copyProfileLink,
onClick: function () {
var profile = data.profile ? (origin + '/profile/#' + data.profile) : '';
var success = Clipboard.copy(profile);
if (success) { UI.log(Messages.shareSuccess); }
},
keys: [13]
}]
};
} else {
return {
content: h('p', Messages.share_noContactsNotLoggedIn),
buttons: [{
className: 'secondary',
name: Messages.login_register,
onClick: function () {
common.setLoginRedirect(function () {
2019-12-09 16:21:48 +00:00
common.gotoURL('/register/');
});
}
}, {
className: 'secondary',
name: Messages.login_login,
onClick: function () {
common.setLoginRedirect(function () {
2019-12-09 16:21:48 +00:00
common.gotoURL('/login/');
});
}
}
]
};
}
};
var getEditableTeams = function (common, config) {
var privateData = common.getMetadataMgr().getPrivateData();
var teamsData = Util.tryParse(JSON.stringify(privateData.teams)) || {};
var teams = {};
Object.keys(teamsData).forEach(function (id) {
// config.teamId only exists when we're trying to share a pad from a team drive
// In this case, we don't want to share the pad with the current team
if (config.teamId && config.teamId === id) { return; }
if (!teamsData[id].hasSecondaryKey) { return; }
var t = teamsData[id];
teams[t.edPublic] = {
notifications: true,
displayName: t.name,
edPublic: t.edPublic,
avatar: t.avatar,
id: id
};
});
return teams;
};
var makeBurnAfterReadingUrl = function (common, href, channel, cb) {
var keyPair = Hash.generateSignPair();
var parsed = Hash.parsePadUrl(href);
var newHref = parsed.getUrl({
ownerKey: keyPair.safeSignKey
});
var sframeChan = common.getSframeChannel();
2020-01-21 10:01:07 +00:00
var rtChannel;
NThen(function (waitFor) {
2020-01-21 10:01:07 +00:00
if (parsed.type !== "sheet") { return; }
common.getPadAttribute('rtChannel', waitFor(function (err, chan) {
rtChannel = chan;
}));
}).nThen(function (waitFor) {
sframeChan.query('Q_SET_PAD_METADATA', {
channel: channel,
command: 'ADD_OWNERS',
value: [keyPair.validateKey]
}, waitFor(function (err) {
if (err) {
waitFor.abort();
UI.warn(Messages.error);
}
}));
2020-01-21 10:01:07 +00:00
if (rtChannel) {
sframeChan.query('Q_SET_PAD_METADATA', {
channel: rtChannel,
command: 'ADD_OWNERS',
value: [keyPair.validateKey]
}, waitFor(function (err) {
if (err) {
console.error(err);
}
}));
}
}).nThen(function () {
cb(newHref);
});
};
2018-01-17 17:39:45 +00:00
UIElements.createShareModal = function (config) {
var origin = config.origin;
var pathname = config.pathname;
var hashes = config.hashes;
var common = config.common;
2019-10-14 15:37:54 +00:00
if (!hashes || (!hashes.editHash && !hashes.viewHash)) { return; }
2019-05-29 17:00:20 +00:00
2020-01-17 13:24:31 +00:00
// check if the pad is password protected
2019-12-04 11:41:08 +00:00
var hash = hashes.editHash || hashes.viewHash;
var href = origin + pathname + '#' + hash;
var parsedHref = Hash.parsePadUrl(href);
var hasPassword = parsedHref.hashData.password;
var makeFaqLink = function () {
var link = h('span', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-question-circle'),
h('a', {href: '#'}, Messages.passwordFaqLink)
]);
$(link).click(function () {
common.openURL(config.origin + "/faq.html#security-pad_password");
});
return link;
};
2020-01-17 13:24:31 +00:00
2019-12-05 10:10:48 +00:00
var parsed = Hash.parsePadUrl(pathname);
var canPresent = ['code', 'slide'].indexOf(parsed.type) !== -1;
var canBAR = parsed.type !== 'drive';
var burnAfterReading = (hashes.viewHash && canBAR) ?
UI.createRadio('accessRights', 'cp-share-bar', Messages.burnAfterReading_linkBurnAfterReading, false, {
mark: {tabindex:1},
label: {style: "display: none;"}
2020-02-11 14:32:39 +00:00
}) : undefined;
2019-11-21 13:50:46 +00:00
var rights = h('div.msg.cp-inline-radio-group', [
2019-11-20 11:01:19 +00:00
h('label', Messages.share_linkAccess),
2019-11-21 12:29:23 +00:00
h('div.radio-group',[
2019-11-20 11:01:19 +00:00
UI.createRadio('accessRights', 'cp-share-editable-false',
Messages.share_linkView, true, { mark: {tabindex:1} }),
canPresent ? UI.createRadio('accessRights', 'cp-share-present',
Messages.share_linkPresent, false, { mark: {tabindex:1} }) : undefined,
2019-11-20 11:01:19 +00:00
UI.createRadio('accessRights', 'cp-share-editable-true',
Messages.share_linkEdit, false, { mark: {tabindex:1} })]),
burnAfterReading
2019-11-20 11:01:19 +00:00
]);
2019-11-19 15:30:23 +00:00
// Burn after reading
// Check if we are an owner of this pad. If we are, we can show the burn after reading option.
// When BAR is selected, display a red message indicating the consequence and add
// the options to generate the BAR url
var barAlert = h('div.alert.alert-danger.cp-alertify-bar-selected', {
style: 'display: none;'
2020-01-17 13:24:31 +00:00
}, Messages.burnAfterReading_warningLink);
var channel = Hash.getSecrets('pad', hash, config.password).channel;
common.getPadMetadata({
channel: channel
}, function (obj) {
if (!obj || obj.error) { return; }
var priv = common.getMetadataMgr().getPrivateData();
// Not an owner: don't display the burn after reading option
if (!Array.isArray(obj.owners) || obj.owners.indexOf(priv.edPublic) === -1) {
$(burnAfterReading).remove();
return;
}
// When the burn after reading option is selected, transform the modal buttons
$(burnAfterReading).css({
display: 'flex'
});
});
2019-11-22 16:03:17 +00:00
var $rights = $(rights);
2019-11-20 09:47:57 +00:00
2018-01-17 17:39:45 +00:00
var saveValue = function () {
2019-11-22 16:03:17 +00:00
var edit = Util.isChecked($rights.find('#cp-share-editable-true'));
var present = Util.isChecked($rights.find('#cp-share-present'));
2018-01-17 17:39:45 +00:00
common.setAttribute(['general', 'share'], {
edit: edit,
present: present
});
};
var burnAfterReadingUrl;
var getLinkValue = function (initValue, cb) {
var val = initValue || {};
2019-11-22 16:03:17 +00:00
var edit = val.edit !== undefined ? val.edit : Util.isChecked($rights.find('#cp-share-editable-true'));
var embed = val.embed;
2019-11-22 16:03:17 +00:00
var present = val.present !== undefined ? val.present : Util.isChecked($rights.find('#cp-share-present'));
var burnAfterReading = Util.isChecked($rights.find('#cp-share-bar'));
if (burnAfterReading && !burnAfterReadingUrl) {
if (cb) { // Called from the contacts tab, "share" button
var barHref = origin + pathname + '#' + (hashes.viewHash || hashes.editHash);
return makeBurnAfterReadingUrl(common, barHref, channel, function (url) {
cb(url);
});
}
2020-01-17 13:24:31 +00:00
return Messages.burnAfterReading_generateLink;
}
2018-05-23 14:45:05 +00:00
var hash = (!hashes.viewHash || (edit && hashes.editHash)) ? hashes.editHash : hashes.viewHash;
var href = burnAfterReading ? burnAfterReadingUrl : (origin + pathname + '#' + hash);
2018-01-17 17:39:45 +00:00
var parsed = Hash.parsePadUrl(href);
return origin + parsed.getUrl({embed: embed, present: present});
};
2019-11-19 16:28:05 +00:00
2019-11-20 09:47:57 +00:00
var makeCancelButton = function() {
2019-11-22 16:03:17 +00:00
return {
className: 'cancel',
name: Messages.cancel,
onClick: function () {},
keys: [27]
};
2019-11-19 16:28:05 +00:00
};
// Share link tab
var linkContent = config.sharedFolder ? [
h('label', Messages.sharedFolders_share),
h('br'),
] : [
UI.createCheckbox('cp-share-embed', Messages.share_linkEmbed, false, { mark: {tabindex:1} }),
];
2019-12-06 16:52:53 +00:00
linkContent.push(h('div.cp-spacer'));
2019-12-04 12:46:56 +00:00
linkContent.push(UI.dialog.selectableArea('', { id: 'cp-share-link-preview', tabindex: 1, rows:3}));
2019-12-04 11:41:08 +00:00
// Show alert if the pad is password protected
2019-12-03 12:01:31 +00:00
if (hasPassword) {
linkContent.push(h('div.alert.alert-primary', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-lock'),
2019-12-05 11:16:12 +00:00
Messages.share_linkPasswordAlert, h('br'),
makeFaqLink()
2019-12-05 11:19:46 +00:00
]));
}
2019-12-03 12:01:31 +00:00
2020-01-17 13:24:31 +00:00
// warning about sharing links
var localStore = window.cryptpadStore;
2020-01-17 13:24:31 +00:00
var dismissButton = h('span.fa.fa-times');
var shareLinkWarning = h('div.alert.alert-warning.dismissable',
{ style: 'display: none;' },
[
h('span.cp-inline-alert-text', Messages.share_linkWarning),
dismissButton
]);
linkContent.push(shareLinkWarning);
localStore.get('hide-alert-shareLinkWarning', function (val) {
if (val === '1') { return; }
$(shareLinkWarning).show();
$(dismissButton).on('click', function () {
localStore.put('hide-alert-shareLinkWarning', '1');
$(shareLinkWarning).remove();
});
});
linkContent.push($(barAlert).clone()[0]); // Burn after reading
var link = h('div.cp-share-modal', linkContent);
2019-11-22 16:03:17 +00:00
var $link = $(link);
2019-11-20 11:01:19 +00:00
var linkButtons = [
makeCancelButton(),
!config.sharedFolder && {
className: 'secondary cp-nobar',
2019-11-20 11:01:19 +00:00
name: Messages.share_linkOpen,
onClick: function () {
saveValue();
var v = getLinkValue({
2019-11-22 16:03:17 +00:00
embed: Util.isChecked($link.find('#cp-share-embed'))
});
2019-11-20 11:01:19 +00:00
window.open(v);
return true;
2019-11-20 11:01:19 +00:00
},
keys: [[13, 'ctrl']]
}, {
className: 'primary cp-nobar',
2019-11-20 11:01:19 +00:00
name: Messages.share_linkCopy,
onClick: function () {
saveValue();
var v = getLinkValue({
2019-11-22 16:03:17 +00:00
embed: Util.isChecked($link.find('#cp-share-embed'))
});
2019-11-20 11:01:19 +00:00
var success = Clipboard.copy(v);
if (success) { UI.log(Messages.shareSuccess); }
},
keys: [13]
}, {
className: 'primary cp-bar',
name: 'GENERATE LINK',
onClick: function () {
var barHref = origin + pathname + '#' + (hashes.viewHash || hashes.editHash);
makeBurnAfterReadingUrl(common, barHref, channel, function (url) {
burnAfterReadingUrl = url;
$rights.find('input[type="radio"]').trigger('change');
});
return true;
},
keys: []
2019-11-20 11:01:19 +00:00
}
];
2019-11-20 09:47:57 +00:00
2019-05-29 17:00:20 +00:00
var frameLink = UI.dialog.customModal(link, {
buttons: linkButtons,
onClose: config.onClose,
2019-05-29 17:00:20 +00:00
});
$(frameLink).find('.cp-bar').hide();
2018-01-17 17:39:45 +00:00
// Share with contacts tab
2019-11-19 15:30:23 +00:00
var teams = getEditableTeams(common, config);
config.teams = teams;
var hasFriends = Object.keys(config.friends || {}).length ||
Object.keys(teams).length;
2019-11-19 15:30:23 +00:00
var onFriendShare = Util.mkEvent();
var friendsObject = hasFriends ? createShareWithFriends(config, onFriendShare, getLinkValue) : noContactsMessage(common);
var friendsList = friendsObject.content;
2019-11-19 15:30:23 +00:00
onFriendShare.reg(saveValue);
var contactsContent = h('div.cp-share-modal');
2019-12-04 11:41:08 +00:00
var $contactsContent = $(contactsContent);
2020-01-17 13:24:31 +00:00
2019-12-04 11:41:08 +00:00
$contactsContent.append(friendsList);
2020-01-17 13:24:31 +00:00
2019-12-04 11:41:08 +00:00
// Show alert if the pad is password protected
if (hasPassword) {
$contactsContent.append(h('div.alert.alert-primary', [
h('i.fa.fa-unlock'),
2019-12-05 11:16:12 +00:00
Messages.share_contactPasswordAlert, h('br'),
makeFaqLink()
2019-12-05 11:19:46 +00:00
]));
}
2019-12-04 11:41:08 +00:00
$(contactsContent).append($(barAlert).clone()); // Burn after reading
2019-11-19 15:30:23 +00:00
var contactButtons = friendsObject.buttons;
contactButtons.unshift(makeCancelButton());
var onShowContacts = function () {
if (!hasFriends) {
$rights.hide();
}
};
var frameContacts = UI.dialog.customModal(contactsContent, {
2019-11-19 16:28:05 +00:00
buttons: contactButtons,
2019-11-19 15:30:23 +00:00
onClose: config.onClose,
});
2018-01-17 17:39:45 +00:00
// Embed tab
var getEmbedValue = function () {
var url = getLinkValue({
embed: true
});
2018-01-17 17:39:45 +00:00
return '<iframe src="' + url + '"></iframe>';
};
2019-11-20 15:39:33 +00:00
var embedContent = [
2018-01-17 17:39:45 +00:00
h('p', Messages.viewEmbedTag),
2019-12-05 10:58:54 +00:00
UI.dialog.selectableArea(getEmbedValue(), { id: 'cp-embed-link-preview', tabindex: 1, rows: 3})
2019-11-20 15:39:33 +00:00
];
2019-12-05 10:10:48 +00:00
// Show alert if the pad is password protected
if (hasPassword) {
embedContent.push(h('div.alert.alert-primary', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-lock'), ' ',
2019-12-05 11:16:12 +00:00
Messages.share_embedPasswordAlert, h('br'),
makeFaqLink()
2019-12-05 11:19:46 +00:00
]));
}
2019-12-05 10:10:48 +00:00
2019-11-19 16:28:05 +00:00
var embedButtons = [
2019-11-20 09:47:57 +00:00
makeCancelButton(), {
className: 'primary',
2018-01-17 17:39:45 +00:00
name: Messages.share_linkCopy,
onClick: function () {
var v = getEmbedValue();
2018-01-17 17:39:45 +00:00
var success = Clipboard.copy(v);
if (success) { UI.log(Messages.shareSuccess); }
2018-01-18 09:53:36 +00:00
},
keys: [13]
2018-01-17 17:39:45 +00:00
}];
2019-11-20 15:39:33 +00:00
var onShowEmbed = function () {
$rights.find('#cp-share-bar').closest('label').hide();
$rights.find('input[type="radio"]:enabled').first().prop('checked', 'checked');
$rights.find('input[type="radio"]').trigger('change');
};
2019-11-20 15:39:33 +00:00
var embed = h('div.cp-share-modal', embedContent);
var $embed = $(embed);
2019-11-20 15:39:33 +00:00
var frameEmbed = UI.dialog.customModal(embed, {
buttons: embedButtons,
onClose: config.onClose,
});
// update values for link and embed preview when radio btns change
$embed.find('#cp-embed-link-preview').val(getEmbedValue());
$link.find('#cp-share-link-preview').val(getLinkValue());
2019-11-22 16:03:17 +00:00
$rights.find('input[type="radio"]').on('change', function () {
$link.find('#cp-share-link-preview').val(getLinkValue({
embed: Util.isChecked($link.find('#cp-share-embed'))
}));
// Hide or show the burn after reading alert
if (Util.isChecked($rights.find('#cp-share-bar')) && !burnAfterReadingUrl) {
$('.cp-alertify-bar-selected').show();
// Show burn after reading button
$('.alertify').find('.cp-bar').show();
$('.alertify').find('.cp-nobar').hide();
return;
}
$embed.find('#cp-embed-link-preview').val(getEmbedValue());
// Hide burn after reading button
$('.alertify').find('.cp-nobar').show();
$('.alertify').find('.cp-bar').hide();
$('.cp-alertify-bar-selected').hide();
2019-11-20 15:39:33 +00:00
});
$link.find('input[type="checkbox"]').on('change', function () {
$link.find('#cp-share-link-preview').val(getLinkValue({
embed: Util.isChecked($link.find('#cp-share-embed'))
}));
});
2018-01-17 17:39:45 +00:00
2018-01-17 17:39:45 +00:00
// Create modal
var resetTab = function () {
$rights.show();
$rights.find('label.cp-radio').show();
};
2018-01-18 10:16:30 +00:00
var tabs = [{
2019-11-19 16:24:29 +00:00
title: Messages.share_contactCategory,
2019-11-21 13:03:40 +00:00
icon: "fa fa-address-book",
2019-11-29 15:47:18 +00:00
content: frameContacts,
active: hasFriends,
onShow: onShowContacts,
onHide: resetTab
2019-11-29 15:47:18 +00:00
}, {
title: Messages.share_linkCategory,
icon: "fa fa-link",
content: frameLink,
active: !hasFriends
2018-01-17 17:39:45 +00:00
}, {
title: Messages.share_embedCategory,
2019-11-21 13:03:40 +00:00
icon: "fa fa-code",
content: frameEmbed,
onShow: onShowEmbed,
onHide: resetTab
2018-01-18 10:16:30 +00:00
}];
2018-01-17 17:39:45 +00:00
if (typeof(AppConfig.customizeShareOptions) === 'function') {
2018-01-18 10:16:30 +00:00
AppConfig.customizeShareOptions(hashes, tabs, {
2018-01-17 17:39:45 +00:00
type: 'DEFAULT',
origin: origin,
pathname: pathname
});
}
2019-11-20 13:11:38 +00:00
var modal = UI.dialog.tabs(tabs);
$(modal).find('.alertify-tabs-titles').after(rights);
// disable edit share options if you don't have edit rights
if (!hashes.editHash) {
2019-11-22 16:03:17 +00:00
$rights.find('#cp-share-editable-false').attr('checked', true);
$rights.find('#cp-share-editable-true').removeAttr('checked').attr('disabled', true);
} else if (!hashes.viewHash) {
2019-11-22 16:03:17 +00:00
$rights.find('#cp-share-editable-false').removeAttr('checked').attr('disabled', true);
$rights.find('#cp-share-present').removeAttr('checked').attr('disabled', true);
$rights.find('#cp-share-editable-true').attr('checked', true);
}
2018-01-17 17:39:45 +00:00
common.getAttribute(['general', 'share'], function (err, val) {
val = val || {};
if (val.present && canPresent) {
2019-11-22 16:03:17 +00:00
$rights.find('#cp-share-editable-false').prop('checked', false);
$rights.find('#cp-share-editable-true').prop('checked', false);
$rights.find('#cp-share-present').prop('checked', true);
} else if ((val.edit === false && hashes.viewHash) || !hashes.editHash) {
2019-11-22 16:03:17 +00:00
$rights.find('#cp-share-editable-false').prop('checked', true);
$rights.find('#cp-share-editable-true').prop('checked', false);
$rights.find('#cp-share-present').prop('checked', false);
} else {
2019-11-22 16:03:17 +00:00
$rights.find('#cp-share-editable-true').prop('checked', true);
$rights.find('#cp-share-editable-false').prop('checked', false);
$rights.find('#cp-share-present').prop('checked', false);
2018-01-17 17:39:45 +00:00
}
delete val.embed;
if (!canPresent) {
2019-10-14 15:37:54 +00:00
delete val.present;
}
2019-11-22 16:03:17 +00:00
$link.find('#cp-share-link-preview').val(getLinkValue(val));
2018-01-17 17:39:45 +00:00
});
2018-02-22 11:43:06 +00:00
common.getMetadataMgr().onChange(function () {
2019-05-29 17:00:20 +00:00
// "hashes" is only available is the secure "share" app
2019-10-14 15:37:54 +00:00
var _hashes = common.getMetadataMgr().getPrivateData().hashes;
if (!_hashes) { return; }
hashes = _hashes;
2019-11-22 16:03:17 +00:00
$link.find('#cp-share-link-preview').val(getLinkValue());
2018-01-17 17:39:45 +00:00
});
2019-11-20 13:11:38 +00:00
return modal;
2018-01-17 17:39:45 +00:00
};
2018-01-17 17:39:45 +00:00
UIElements.createFileShareModal = function (config) {
var origin = config.origin;
var pathname = config.pathname;
var hashes = config.hashes;
var common = config.common;
var fileData = config.fileData;
2018-01-17 17:39:45 +00:00
if (!hashes.fileHash) { throw new Error("You must provide a file hash"); }
var url = origin + pathname + '#' + hashes.fileHash;
2020-01-17 13:24:31 +00:00
// check if the file is password protected
var parsedHref = Hash.parsePadUrl(url);
var hasPassword = parsedHref.hashData.password;
var makeFaqLink = function () {
var link = h('span', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-question-circle'),
h('a', {href: '#'}, Messages.passwordFaqLink)
]);
$(link).click(function () {
common.openURL(config.origin + "/faq.html#security-pad_password");
});
return link;
};
2018-01-17 17:39:45 +00:00
var getLinkValue = function () { return url; };
2019-11-20 16:47:06 +00:00
var makeCancelButton = function() {
return {className: 'cancel',
2018-01-18 09:53:36 +00:00
name: Messages.cancel,
onClick: function () {},
2019-11-20 16:47:06 +00:00
keys: [27]};
};
2019-11-20 16:47:06 +00:00
// Share link tab
var linkContent = [
UI.dialog.selectableArea(getLinkValue(), { id: 'cp-share-link-preview', tabindex: 1, rows:2 })
2019-11-20 16:47:06 +00:00
];
// Show alert if the pad is password protected
if (hasPassword) {
linkContent.push(h('div.alert.alert-primary', [
h('i.fa.fa-lock'),
Messages.share_linkPasswordAlert, h('br'),
makeFaqLink()
]));
}
2020-01-17 13:24:31 +00:00
// warning about sharing links
var localStore = window.cryptpadStore;
2020-01-17 13:24:31 +00:00
var dismissButton = h('span.fa.fa-times');
var shareLinkWarning = h('div.alert.alert-warning.dismissable',
{ style: 'display: none;' },
[
h('span.cp-inline-alert-text', Messages.share_linkWarning),
dismissButton
]);
linkContent.push(shareLinkWarning);
localStore.get('hide-alert-shareLinkWarning', function (val) {
if (val === '1') { return; }
$(shareLinkWarning).show();
$(dismissButton).on('click', function () {
localStore.put('hide-alert-shareLinkWarning', '1');
$(shareLinkWarning).remove();
});
});
2019-11-20 16:47:06 +00:00
var link = h('div.cp-share-modal', linkContent);
var linkButtons = [
makeCancelButton(),
{
className: 'primary',
name: Messages.share_linkCopy,
onClick: function () {
var v = getLinkValue();
var success = Clipboard.copy(v);
if (success) { UI.log(Messages.shareSuccess);
2019-11-22 16:03:17 +00:00
}
2019-11-20 16:47:06 +00:00
},
keys: [13]
}
];
2019-07-04 13:35:35 +00:00
var frameLink = UI.dialog.customModal(link, {
buttons: linkButtons,
onClose: config.onClose,
});
2018-01-17 17:39:45 +00:00
// share with contacts tab
var teams = getEditableTeams(common, config);
config.teams = teams;
var hasFriends = Object.keys(config.friends || {}).length ||
Object.keys(teams).length;
2019-11-20 16:47:06 +00:00
var friendsObject = hasFriends ? createShareWithFriends(config, null, getLinkValue) : noContactsMessage(common);
2019-11-20 16:47:06 +00:00
var friendsList = friendsObject.content;
var contactsContent = h('div.cp-share-modal');
var $contactsContent = $(contactsContent);
$contactsContent.append(friendsList);
// Show alert if the pad is password protected
if (hasPassword) {
$contactsContent.append(h('div.alert.alert-primary', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-unlock'),
Messages.share_contactPasswordAlert, h('br'),
makeFaqLink()
]));
}
2019-11-20 16:47:06 +00:00
var contactButtons = friendsObject.buttons;
contactButtons.unshift(makeCancelButton());
2019-11-20 16:47:06 +00:00
var frameContacts = UI.dialog.customModal(contactsContent, {
buttons: contactButtons,
onClose: config.onClose,
});
2018-01-17 17:39:45 +00:00
// Embed tab
var embed = h('div.cp-share-modal', [
h('p', Messages.fileEmbedScript),
UI.dialog.selectable(common.getMediatagScript()),
h('p', Messages.fileEmbedTag),
UI.dialog.selectable(common.getMediatagFromHref(fileData)),
2018-01-17 17:39:45 +00:00
]);
// Show alert if the pad is password protected
if (hasPassword) {
embed.append(h('div.alert.alert-primary', [
2020-01-17 13:24:31 +00:00
h('i.fa.fa-lock'), ' ',
Messages.share_embedPasswordAlert, h('br'),
makeFaqLink()
]));
}
2018-01-17 17:39:45 +00:00
var embedButtons = [{
className: 'cancel',
2018-01-18 09:53:36 +00:00
name: Messages.cancel,
onClick: function () {},
keys: [27]
}, {
className: 'primary',
2018-01-17 17:39:45 +00:00
name: Messages.share_mediatagCopy,
onClick: function () {
var v = common.getMediatagFromHref(fileData);
2018-01-17 17:39:45 +00:00
var success = Clipboard.copy(v);
if (success) { UI.log(Messages.shareSuccess); }
2018-01-18 09:53:36 +00:00
},
keys: [13]
2018-01-17 17:39:45 +00:00
}];
2019-07-04 13:35:35 +00:00
var frameEmbed = UI.dialog.customModal(embed, {
buttons: embedButtons,
onClose: config.onClose,
});
2018-01-17 17:39:45 +00:00
// Create modal
2018-01-18 10:16:30 +00:00
var tabs = [{
2019-11-20 16:47:06 +00:00
title: Messages.share_contactCategory,
2019-11-21 15:39:23 +00:00
icon: "fa fa-address-book",
content: frameContacts,
active: hasFriends,
}, {
title: Messages.share_linkCategory,
icon: "fa fa-link",
content: frameLink,
active: !hasFriends
2018-01-17 17:39:45 +00:00
}, {
title: Messages.share_embedCategory,
2019-11-21 15:39:23 +00:00
icon: "fa fa-code",
2018-01-17 17:39:45 +00:00
content: frameEmbed
2018-01-18 10:16:30 +00:00
}];
2018-01-17 17:39:45 +00:00
if (typeof(AppConfig.customizeShareOptions) === 'function') {
2018-01-18 10:16:30 +00:00
AppConfig.customizeShareOptions(hashes, tabs, {
2018-01-17 17:39:45 +00:00
type: 'FILE',
origin: origin,
pathname: pathname
});
}
2019-11-20 16:47:06 +00:00
var modal = UI.dialog.tabs(tabs);
return modal;
2018-01-17 17:39:45 +00:00
};
2019-09-20 13:27:20 +00:00
UIElements.createInviteTeamModal = function (config) {
var common = config.common;
var hasFriends = Object.keys(config.friends || {}).length !== 0;
var privateData = common.getMetadataMgr().getPrivateData();
var team = privateData.teams[config.teamId];
if (!team) { return void UI.warn(Messages.error); }
var origin = privateData.origin;
2019-09-30 13:17:26 +00:00
var module = config.module || common.makeUniversal('team');
2019-09-20 13:27:20 +00:00
2019-12-12 14:30:14 +00:00
// Invite contacts
2019-09-20 13:27:20 +00:00
var $div;
var refreshButton = function () {
if (!$div) { return; }
var $modal = $div.closest('.alertify');
var $nav = $modal.find('nav');
var $btn = $nav.find('button.primary');
2019-09-26 10:53:02 +00:00
var selected = $div.find('.cp-usergrid-user.cp-selected').length;
2019-09-20 13:27:20 +00:00
if (selected) {
$btn.prop('disabled', '');
} else {
$btn.prop('disabled', 'disabled');
}
};
2019-12-12 14:30:14 +00:00
var getContacts = function () {
var list = UIElements.getUserGrid(Messages.team_pickFriends, {
common: common,
data: config.friends,
large: true
}, refreshButton);
var div = h('div.contains-nav');
var $div = $(div);
$div.append(list.div);
var contactsButtons = [{
className: 'primary',
name: Messages.team_inviteModalButton,
onClick: function () {
var $sel = $div.find('.cp-usergrid-user.cp-selected');
var sel = $sel.toArray();
if (!sel.length) { return; }
2019-09-20 13:27:20 +00:00
2019-12-12 14:30:14 +00:00
sel.forEach(function (el) {
var curve = $(el).attr('data-curve');
module.execCommand('INVITE_TO_TEAM', {
teamId: config.teamId,
user: config.friends[curve]
}, function (obj) {
if (obj && obj.error) {
console.error(obj.error);
return UI.warn(Messages.error);
}
});
});
},
keys: [13]
}];
return {
content: div,
buttons: contactsButtons
};
};
var friendsObject = hasFriends ? getContacts() : noContactsMessage(common);
var friendsList = friendsObject.content;
var contactsButtons = friendsObject.buttons;
contactsButtons.unshift({
2019-09-20 13:27:20 +00:00
className: 'cancel',
name: Messages.cancel,
onClick: function () {},
keys: [27]
2019-12-12 14:30:14 +00:00
});
2019-09-20 13:27:20 +00:00
2019-12-12 14:30:14 +00:00
var contactsContent = h('div.cp-share-modal', [
friendsList
]);
2019-09-20 13:27:20 +00:00
2019-12-12 14:30:14 +00:00
var frameContacts = UI.dialog.customModal(contactsContent, {
buttons: contactsButtons,
});
var linkName, linkPassword, linkMessage, linkError, linkSpinText;
var linkForm, linkSpin, linkResult;
2019-12-16 15:06:22 +00:00
var linkWarning;
2019-12-12 14:30:14 +00:00
// Invite from link
2019-12-16 15:06:22 +00:00
var dismissButton = h('span.fa.fa-times');
2019-12-12 14:30:14 +00:00
var linkContent = h('div.cp-share-modal', [
h('p', Messages.team_inviteLinkTitle ),
linkError = h('div.alert.alert-danger.cp-teams-invite-alert', {style : 'display: none;'}),
linkForm = h('div.cp-teams-invite-form', [
// autofill: 'off' was insufficient
// adding these two fake inputs confuses firefox and prevents unwanted form autofill
h('input', { type: 'text', style: 'display: none'}),
h('input', { type: 'password', style: 'display: none'}),
linkName = h('input', {
placeholder: Messages.team_inviteLinkTempName
}),
h('br'),
2019-12-16 15:06:22 +00:00
h('div.cp-teams-invite-block', [
h('span', Messages.team_inviteLinkSetPassword),
2019-12-16 15:06:22 +00:00
h('a.cp-teams-help.fa.fa-question-circle', {
href: origin + '/faq.html#security-pad_password',
target: "_blank",
'data-tippy-placement': "right"
})
]),
linkPassword = UI.passwordInput({
id: 'cp-teams-invite-password',
placeholder: Messages.login_password
}),
2019-12-16 15:06:22 +00:00
h('div.cp-teams-invite-block',
h('span', Messages.team_inviteLinkNote)
2019-12-16 15:06:22 +00:00
),
linkMessage = h('textarea.cp-teams-invite-message', {
placeholder: Messages.team_inviteLinkNoteMsg,
rows: 3
})
]),
2019-12-16 16:05:41 +00:00
linkSpin = h('div.cp-teams-invite-spinner', {
style: 'display: none;'
}, [
h('i.fa.fa-spinner.fa-spin'),
linkSpinText = h('span', Messages.team_inviteLinkLoading)
]),
linkResult = h('div', {
style: 'display: none;'
}, h('textarea', {
readonly: 'readonly'
2019-12-16 15:06:22 +00:00
})),
linkWarning = h('div.cp-teams-invite-alert.alert.alert-warning.dismissable', {
style: "display: none;"
}, [
2019-12-16 16:03:56 +00:00
h('span.cp-inline-alert-text', Messages.team_inviteLinkWarning),
2019-12-16 15:06:22 +00:00
dismissButton
])
2019-09-20 13:27:20 +00:00
]);
2019-12-18 15:36:06 +00:00
$(linkMessage).keydown(function (e) {
if (e.which === 13) {
e.stopPropagation();
}
});
2019-12-16 15:06:22 +00:00
var localStore = window.cryptpadStore;
localStore.get('hide-alert-teamInvite', function (val) {
if (val === '1') { return; }
$(linkWarning).show();
$(dismissButton).on('click', function () {
localStore.put('hide-alert-teamInvite', '1');
$(linkWarning).remove();
});
});
var $linkContent = $(linkContent);
var href;
2019-12-12 17:14:17 +00:00
var process = function () {
var $nav = $linkContent.closest('.alertify').find('nav');
2019-12-12 17:14:17 +00:00
$(linkError).text('').hide();
var name = $(linkName).val();
var pw = $(linkPassword).find('input').val();
2019-12-12 17:14:17 +00:00
var msg = $(linkMessage).val();
var hash = Hash.createRandomHash('invite', pw);
var hashData = Hash.parseTypeHash('invite', hash);
href = origin + '/teams/#' + hash;
2019-12-12 17:14:17 +00:00
if (!name || !name.trim()) {
$(linkError).text(Messages.team_inviteLinkErrorName).show();
2019-12-12 17:14:17 +00:00
return true;
}
var seeds = InviteInner.deriveSeeds(hashData.key);
var salt = InviteInner.deriveSalt(pw, AppConfig.loginSalt);
var bytes64;
2019-12-12 17:16:42 +00:00
NThen(function (waitFor) {
$(linkForm).hide();
$(linkSpin).show();
$nav.find('button.cp-teams-invite-create').hide();
2019-12-17 11:18:38 +00:00
$nav.find('button.cp-teams-invite-copy').show();
setTimeout(waitFor(), 150);
}).nThen(function (waitFor) {
InviteInner.deriveBytes(seeds.scrypt, salt, waitFor(function (_bytes) {
bytes64 = _bytes;
}));
2019-12-12 17:14:17 +00:00
}).nThen(function (waitFor) {
module.execCommand('CREATE_INVITE_LINK', {
name: name,
password: pw,
message: msg,
bytes64: bytes64,
2019-12-13 16:03:24 +00:00
hash: hash,
2019-12-12 17:14:17 +00:00
teamId: config.teamId,
seeds: seeds,
2019-12-18 09:52:20 +00:00
}, waitFor(function (obj) {
if (obj && obj.error) {
2020-01-17 13:24:31 +00:00
waitFor.abort();
$(linkSpin).hide();
2019-12-19 10:09:39 +00:00
$(linkForm).show();
2019-12-17 11:18:38 +00:00
$nav.find('button.cp-teams-invite-create').show();
$nav.find('button.cp-teams-invite-copy').hide();
2019-12-19 10:09:39 +00:00
return void $(linkError).text(Messages.team_inviteLinkError).show();
2019-12-12 17:14:17 +00:00
}
// Display result here
$(linkSpin).hide();
$(linkResult).show().find('textarea').text(href);
$nav.find('button.cp-teams-invite-copy').prop('disabled', '');
2019-12-12 17:14:17 +00:00
}));
});
return true;
};
var linkButtons = [{
className: 'cancel',
name: Messages.cancel,
onClick: function () {},
keys: [27]
}, {
className: 'primary cp-teams-invite-create',
name: Messages.team_inviteLinkCreate,
2019-12-12 17:14:17 +00:00
onClick: function () {
return process();
},
2019-12-18 15:36:06 +00:00
keys: []
}, {
className: 'primary cp-teams-invite-copy',
name: Messages.team_inviteLinkCopy,
onClick: function () {
if (!href) { return; }
var success = Clipboard.copy(href);
if (success) { UI.log(Messages.shareSuccess); }
},
keys: []
2019-12-12 17:14:17 +00:00
}];
2019-12-12 14:30:14 +00:00
var frameLink = UI.dialog.customModal(linkContent, {
buttons: linkButtons,
});
$(frameLink).find('.cp-teams-invite-copy').prop('disabled', 'disabled').hide();
2019-09-20 13:27:20 +00:00
2019-12-12 14:30:14 +00:00
// Create modal
var tabs = [{
title: Messages.share_contactCategory,
icon: "fa fa-address-book",
content: frameContacts,
active: hasFriends
}, {
title: Messages.share_linkCategory,
icon: "fa fa-link",
content: frameLink,
active: !hasFriends
}];
var modal = UI.dialog.tabs(tabs);
2019-09-20 13:27:20 +00:00
UI.openCustomModal(modal);
};
UIElements.createButton = function (common, type, rightside, data, callback) {
2017-09-06 16:26:10 +00:00
var AppConfig = common.getAppConfig();
var button;
2017-09-07 16:56:58 +00:00
var sframeChan = common.getSframeChannel();
var appType = (common.getMetadataMgr().getMetadata().type || 'pad').toUpperCase();
2017-09-06 16:26:10 +00:00
switch (type) {
case 'export':
button = $('<button>', {
'class': 'fa fa-download cp-toolbar-icon-export',
2017-09-06 16:26:10 +00:00
title: Messages.exportButtonTitle,
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.exportButton));
button.click(common.prepareFeedback(type));
if (callback) {
button.click(callback);
}
break;
case 'import':
button = $('<button>', {
'class': 'fa fa-upload cp-toolbar-icon-import',
2017-09-06 16:26:10 +00:00
title: Messages.importButtonTitle,
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.importButton));
2018-03-22 16:01:01 +00:00
/*if (data.types) {
// New import button in the toolbar
var importFunction = {
template: function () {
UIElements.openTemplatePicker(common, true);
},
file: function (cb) {
importContent('text/plain', function (content, file) {
cb(content, file);
}, {accept: data ? data.accept : undefined})
}
};
var toImport = [];
Object.keys(data.types).forEach(function (importType) {
if (!importFunction[importType] || !data.types[importType]) { return; }
var option = h('button', importType);
$(option).click(function () {
importFunction[importType](data.types[importType]);
});
toImport.push(options);
});
button.click(common.prepareFeedback(type));
if (toImport.length === 1) {
button.click(function () { $(toImport[0]).click(); });
} else {
Cryptpad.alert(h('p.cp-import-container', toImport));
}
}
else if (callback) {*/
// Old import button, used in settings
2017-09-06 16:26:10 +00:00
button
.click(common.prepareFeedback(type))
.click(importContent((data && data.binary) ? 'application/octet-stream' : 'text/plain', callback, {
accept: data ? data.accept : undefined,
binary: data ? data.binary : undefined
}));
2018-03-22 16:01:01 +00:00
//}
2017-09-06 16:26:10 +00:00
break;
case 'upload':
button = $('<button>', {
'class': 'btn btn-primary new',
title: Messages.uploadButtonTitle,
}).append($('<span>', {'class':'fa fa-upload'})).append(' '+Messages.uploadButton);
if (!data.FM) { return; }
var $input = $('<input>', {
'type': 'file',
'style': 'display: none;',
'multiple': 'multiple'
2017-09-06 16:26:10 +00:00
}).on('change', function (e) {
var files = Util.slice(e.target.files);
files.forEach(function (file) {
var ev = {
target: data.target
};
if (data.filter && !data.filter(file)) {
return;
}
if (data.transformer) {
data.transformer(file, function (newFile) {
data.FM.handleFile(newFile, ev);
});
return;
}
data.FM.handleFile(file, ev);
});
2017-09-06 16:26:10 +00:00
if (callback) { callback(); }
});
if (data.accept) { $input.attr('accept', data.accept); }
button.click(function () { $input.click(); });
break;
2020-02-21 14:45:16 +00:00
case 'copy':
button = $('<button>', {
'class': 'fa fa-clone cp-toolbar-icon-import',
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.makeACopy));
button
.click(common.prepareFeedback(type))
.click(function () {
sframeChan.query('EV_MAKE_A_COPY');
});
break;
2018-03-22 16:01:01 +00:00
case 'importtemplate':
if (!AppConfig.enableTemplates) { return; }
if (!common.isLoggedIn()) { return; }
2018-03-22 16:01:01 +00:00
button = $('<button>', {
'class': 'fa fa-upload cp-toolbar-icon-import',
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.template_import));
button
.click(common.prepareFeedback(type))
.click(function () {
2018-03-23 09:53:31 +00:00
UIElements.openTemplatePicker(common, true);
2018-03-22 16:01:01 +00:00
});
break;
2017-09-06 16:26:10 +00:00
case 'template':
if (!AppConfig.enableTemplates) { return; }
if (!common.isLoggedIn()) { return; }
2017-09-06 16:26:10 +00:00
button = $('<button>', {
'class': 'fa fa-bookmark cp-toolbar-icon-template',
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.saveTemplateButton));
2017-09-06 16:26:10 +00:00
if (data.rt) {
button
.click(function () {
var title = data.getTitle() || document.title;
var todo = function (val) {
if (typeof(val) !== "string") { return; }
var toSave = data.rt.getUserDoc();
if (val.trim()) {
val = val.trim();
title = val;
try {
var parsed = JSON.parse(toSave);
var meta;
if (Array.isArray(parsed) && typeof(parsed[3]) === "object") {
meta = parsed[3].metadata; // pad
} else if (parsed.info) {
2017-09-06 16:26:10 +00:00
meta = parsed.info; // poll
} else {
meta = parsed.metadata;
}
if (typeof(meta) === "object") {
meta.title = val;
meta.defaultTitle = val;
delete meta.users;
}
toSave = JSON.stringify(parsed);
} catch(e) {
console.error("Parse error while setting the title", e);
}
}
2017-09-07 16:56:58 +00:00
sframeChan.query('Q_SAVE_AS_TEMPLATE', {
toSave: toSave,
title: title
2017-09-06 16:26:10 +00:00
}, function () {
UI.alert(Messages.templateSaved);
2017-11-23 11:28:49 +00:00
Feedback.send('TEMPLATE_CREATED');
2017-09-06 16:26:10 +00:00
});
};
UI.prompt(Messages.saveTemplatePrompt, title, todo);
2017-09-06 16:26:10 +00:00
});
}
break;
case 'forget':
button = $('<button>', {
'class': "fa fa-trash cp-toolbar-icon-forget"
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.fc_delete));
callback = typeof callback === "function" ? callback : function () {};
button
.click(common.prepareFeedback(type))
.click(function() {
common.isPadStored(function (err, data) {
if (!data) {
return void UI.alert(Messages.autostore_notAvailable);
}
sframeChan.query('Q_IS_ONLY_IN_SHARED_FOLDER', null, function (err, res) {
if (err || res.error) { return void console.log(err || res.error); }
var msg = Messages.forgetPrompt;
if (res) {
UI.alert(Messages.sharedFolders_forget);
return;
} else if (!common.isLoggedIn()) {
msg = Messages.fm_removePermanentlyDialog;
}
UI.confirm(msg, function (yes) {
if (!yes) { return; }
sframeChan.query('Q_MOVE_TO_TRASH', null, function (err, obj) {
err = err || (obj && obj.error);
if (err) {
callback(err);
return void UI.warn(Messages.fm_forbidden);
}
var cMsg = common.isLoggedIn() ? Messages.movedToTrash : Messages.deleted;
var msg = common.fixLinks($('<div>').html(cMsg));
UI.alert(msg);
callback();
return;
});
});
2017-09-06 16:26:10 +00:00
});
});
});
2017-09-06 16:26:10 +00:00
break;
2017-09-07 16:56:58 +00:00
case 'present':
2020-05-07 16:16:36 +00:00
button = $(h('button', {
2020-05-26 15:52:02 +00:00
//title: Messages.presentButtonTitle, // TODO display if the label text is collapsed
2020-05-07 16:16:36 +00:00
}, [
h('i.fa.fa-play-circle'),
h('span.cp-toolbar-name', Messages.share_linkPresent)
])).click(common.prepareFeedback(type));
2017-09-07 16:56:58 +00:00
break;
case 'preview':
2020-05-07 16:16:36 +00:00
button = $(h('button', {
2020-05-26 15:52:02 +00:00
//title: Messages.previewButtonTitle, // TODO display if the label text is collapsed
2020-05-07 16:16:36 +00:00
}, [
h('i.fa.fa-eye'),
h('span.cp-toolbar-name', Messages.share_linkOpen)
])).click(common.prepareFeedback(type));
2017-09-07 16:56:58 +00:00
break;
case 'print':
button = $('<button>', {
2018-12-11 10:09:34 +00:00
title: Messages.printButtonTitle2,
'class': "fa fa-print cp-toolbar-icon-print",
// XXX people don't realize this does PDF (https://github.com/xwiki-labs/cryptpad/issues/357#issuecomment-640711724)
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.printText));
break;
2017-09-06 16:26:10 +00:00
case 'history':
if (!AppConfig.enableHistory) {
button = $('<span>');
break;
}
var active = $(".cp-toolbar-history:visible").length !== 0;
2017-09-06 16:26:10 +00:00
button = $('<button>', {
title: active ? Messages.history_closeTitle : Messages.historyButton,
'class': "fa fa-history cp-toolbar-icon-history",
2017-09-06 16:26:10 +00:00
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.historyText));
button.toggleClass("active", active);
2017-09-06 16:26:10 +00:00
if (data.histConfig) {
if (active) {
button.click(function () { $(".cp-toolbar-history-close").trigger("click"); });
}
else {
button
.click(common.prepareFeedback(type))
.on('click', function () {
common.getHistory(data.histConfig);
});
}
2017-09-06 16:26:10 +00:00
}
break;
case 'mediatag':
2020-05-13 12:42:22 +00:00
button = $(h('button.cp-toolbar-mediatag', {
2020-05-26 15:52:02 +00:00
//title: Messages.filePickerButton, // TODO display if the label text is collapsed
2020-05-07 16:16:36 +00:00
}, [
h('i.fa.fa-picture-o'),
h('span.cp-toolbar-name', Messages.toolbar_insert)
])).click(common.prepareFeedback(type));
break;
case 'savetodrive':
2020-05-13 12:42:22 +00:00
button = $(h('button.cp-toolbar-savetodrive', {
title: Messages.canvas_saveToDrive,
2020-05-13 12:42:22 +00:00
}, [
h('i.fa.fa-file-image-o'),
h('span.cp-toolbar-name.cp-toolbar-drawer-element', Messages.toolbar_savetodrive)
])).click(common.prepareFeedback(type));
break;
2017-09-19 13:30:08 +00:00
case 'hashtag':
button = $('<button>', {
'class': 'fa fa-hashtag cp-toolbar-icon-hashtag',
2017-09-19 13:30:08 +00:00
title: Messages.tags_title,
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.fc_hashtag));
button.click(common.prepareFeedback(type))
.click(function () {
common.isPadStored(function (err, data) {
if (!data) {
return void UI.alert(Messages.autostore_notAvailable);
}
UIElements.updateTags(common, null);
});
});
2017-09-19 13:30:08 +00:00
break;
case 'toggle':
2020-05-13 12:42:22 +00:00
button = $(h('button.cp-toolbar-tools', {
2020-05-26 15:52:02 +00:00
//title: data.title || '', // TODO display if the label text is collapsed
2020-05-07 16:16:36 +00:00
}, [
h('i.fa.fa-wrench'),
h('span.cp-toolbar-name', Messages.toolbar_tools)
])).click(common.prepareFeedback(type));
/*
window.setTimeout(function () {
button.attr('title', data.title);
});
var updateIcon = function (isVisible) {
button.removeClass('fa-caret-down').removeClass('fa-caret-up');
if (!isVisible) { button.addClass('fa-caret-down'); }
else { button.addClass('fa-caret-up'); }
};
2020-05-07 16:16:36 +00:00
*/
button.click(function (e) {
data.element.toggle();
var isVisible = data.element.is(':visible');
if (callback) { callback(isVisible); }
if (isVisible) {
button.addClass('cp-toolbar-button-active');
if (e.originalEvent) { Feedback.send('TOGGLE_SHOW_' + appType); }
} else {
button.removeClass('cp-toolbar-button-active');
if (e.originalEvent) { Feedback.send('TOGGLE_HIDE_' + appType); }
}
2020-05-07 16:16:36 +00:00
//updateIcon(isVisible);
});
2020-05-07 16:16:36 +00:00
//updateIcon(data.element.is(':visible'));
break;
case 'properties':
button = $('<button>', {
'class': 'fa fa-info-circle cp-toolbar-icon-properties',
title: Messages.propertiesButtonTitle,
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'})
.text(Messages.propertiesButton))
.click(common.prepareFeedback(type))
.click(function () {
common.isPadStored(function (err, data) {
if (!data) {
return void UI.alert(Messages.autostore_notAvailable);
}
sframeChan.event('EV_PROPERTIES_OPEN');
});
});
break;
case 'save': // OnlyOffice save
button = $('<button>', {
'class': 'fa fa-save',
2018-05-14 12:20:37 +00:00
title: Messages.settings_save,
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'})
2018-05-14 12:20:37 +00:00
.text(Messages.settings_save))
.click(common.prepareFeedback(type));
if (callback) { button.click(callback); }
break;
case 'newpad':
button = $('<button>', {
title: Messages.newButtonTitle,
'class': 'fa fa-plus cp-toolbar-icon-newpad',
}).append($('<span>', {'class': 'cp-toolbar-drawer-element'}).text(Messages.newButton));
button
.click(common.prepareFeedback(type))
.click(function () {
common.createNewPadModal();
});
break;
2017-09-06 16:26:10 +00:00
default:
data = data || {};
2020-05-13 12:42:22 +00:00
var drawerCls = data.drawer === false ? '' : '.cp-toolbar-drawer-element';
var icon = data.icon || "fa-question";
2020-05-07 16:16:36 +00:00
button = $(h('button', {
2020-06-03 14:26:59 +00:00
title: data.tippy || ''
2020-05-26 15:52:02 +00:00
//title: data.title || '',
2020-05-07 16:16:36 +00:00
}, [
h('i.fa.' + icon),
2020-05-13 12:42:22 +00:00
h('span.cp-toolbar-name'+drawerCls, data.text)
2020-05-07 16:16:36 +00:00
])).click(common.prepareFeedback(data.name || 'DEFAULT'));
if (callback) {
button.click(callback);
}
if (data.style) { button.attr('style', data.style); }
if (data.id) { button.attr('id', data.id); }
if (data.hiddenReadOnly) { button.addClass('cp-hidden-if-readonly'); }
if (data.name) {
button.addClass('cp-toolbar-icon-'+data.name);
}
2017-09-06 16:26:10 +00:00
}
if (rightside) {
button.addClass('cp-toolbar-rightside-button');
}
return button;
};
var createMdToolbar = function (common, editor) {
var $toolbar = $('<div>', {
'class': 'cp-markdown-toolbar'
});
var clean = function (str) {
return str.replace(/^(\n)+/, '').replace(/(\n)+$/, '');
};
var actions = {
'bold': {
expr: '**{0}**',
icon: 'fa-bold'
},
'italic': {
expr: '_{0}_',
icon: 'fa-italic'
},
'strikethrough': {
expr: '~~{0}~~',
icon: 'fa-strikethrough'
},
'heading': {
apply: function (str) {
return '\n'+clean(str).split('\n').map(function (line) {
return '# '+line;
}).join('\n')+'\n';
},
icon: 'fa-header'
},
'link': {
expr: '[{0}](http://)',
icon: 'fa-link'
},
'quote': {
apply: function (str) {
return '\n\n'+str.split('\n').map(function (line) {
return '> '+line;
}).join('\n')+'\n\n';
},
icon: 'fa-quote-right'
},
'nlist': {
apply: function (str) {
return '\n'+clean(str).split('\n').map(function (line) {
return '1. '+line;
}).join('\n')+'\n';
},
icon: 'fa-list-ol'
},
'list': {
apply: function (str) {
return '\n'+clean(str).split('\n').map(function (line) {
return '* '+line;
}).join('\n')+'\n';
},
icon: 'fa-list-ul'
},
'check': {
apply: function (str) {
return '\n' + clean(str).split('\n').map(function (line) {
return '* [ ] ' + line;
}).join('\n') + '\n';
},
icon: 'fa-check-square-o'
},
'code': {
apply: function (str) {
if (str.indexOf('\n') !== -1) {
return '\n```\n' + clean(str) + '\n```\n';
}
return '`' + str + '`';
},
icon: 'fa-code'
2019-02-21 10:41:08 +00:00
},
'toc': {
expr: '[TOC]',
icon: 'fa-newspaper-o'
}
};
var onClick = function () {
var type = $(this).attr('data-type');
var texts = editor.getSelections();
var newTexts = texts.map(function (str) {
str = str || Messages.mdToolbar_defaultText;
if (actions[type].apply) {
return actions[type].apply(str);
}
return actions[type].expr.replace('{0}', str);
});
editor.replaceSelections(newTexts, 'around');
editor.focus();
};
for (var k in actions) {
$('<button>', {
'data-type': k,
2019-12-19 09:34:08 +00:00
'class': 'pure-button fa ' + actions[k].icon,
title: Messages['mdToolbar_' + k] || k
}).click(onClick).appendTo($toolbar);
}
$('<button>', {
2019-12-19 09:34:08 +00:00
'class': 'pure-button fa fa-question cp-markdown-help',
title: Messages.mdToolbar_help
}).click(function () {
var href = Messages.mdToolbar_tutorial;
common.openUnsafeURL(href);
}).appendTo($toolbar);
return $toolbar;
};
UIElements.createMarkdownToolbar = function (common, editor) {
var readOnly = common.getMetadataMgr().getPrivateData().readOnly;
if (readOnly) {
return {
toolbar: $(),
button: $(),
setState: function () {}
};
}
var $toolbar = createMdToolbar(common, editor);
var cfg = {
title: Messages.mdToolbar_button,
element: $toolbar
};
var onClick = function (visible) {
common.setAttribute(['general', 'markdown-help'], visible, function (e) {
if (e) { return void console.error(e); }
});
};
var $toolbarButton = common.createButton('toggle', true, cfg, onClick);
var tbState = true;
common.getAttribute(['general', 'markdown-help'], function (e, data) {
if (e) { return void console.error(e); }
2018-03-19 15:27:33 +00:00
if ($(window).height() < 800 && $(window).width() < 800) { return; }
if (data === true && $toolbarButton.length && tbState) {
$toolbarButton.click();
}
});
// setState provides the ability to disable the toolbar and the button in case we don't
// have the markdown editor available (in code we can switch mode, in poll we can publish)
var setState = function (state) {
tbState = state;
if (!state) {
$toolbar.hide();
$toolbarButton.hide();
return;
}
common.getAttribute(['general', 'markdown-help'], function (e, data) {
if (e) { return void console.error(e); }
2018-03-19 15:27:33 +00:00
if ($(window).height() < 800 && $(window).width() < 800) { return; }
if (data === true && $toolbarButton) {
// Show the toolbar using the button to make sure the icon in the button is
// correct (caret-down / caret-up)
$toolbar.hide();
$toolbarButton.click();
return;
}
$toolbar.show();
$toolbarButton.click();
});
$toolbarButton.show();
};
return {
toolbar: $toolbar,
button: $toolbarButton,
setState: setState
};
};
2018-05-31 11:26:06 +00:00
var setHTML = UIElements.setHTML = function (e, html) {
e.innerHTML = html;
return e;
};
UIElements.createHelpMenu = function (common, categories) {
2018-02-27 16:38:29 +00:00
var type = common.getMetadataMgr().getMetadata().type || 'pad';
var elements = [];
if (Messages.help && Messages.help.generic) {
Object.keys(Messages.help.generic).forEach(function (el) {
elements.push(setHTML(h('li'), Messages.help.generic[el]));
});
}
if (categories) {
categories.forEach(function (cat) {
var msgs = Messages.help[cat];
if (msgs) {
Object.keys(msgs).forEach(function (el) {
elements.push(setHTML(h('li'), msgs[el]));
});
}
});
}
var text = h('p.cp-help-text', [
h('h1', Messages.help.title),
h('ul', elements)
]);
2018-03-23 14:05:26 +00:00
common.fixLinks(text);
var closeButton = h('span.cp-help-close.fa.fa-times');
var $toolbarButton = common.createButton('', true, {
title: Messages.hide_help_button,
text: Messages.help_button,
name: 'help'
}).addClass('cp-toolbar-button-active');
2018-02-27 16:38:29 +00:00
var help = h('div.cp-help-container', [
closeButton,
text
]);
var toggleHelp = function (forceClose) {
if ($(help).hasClass('cp-help-hidden')) {
if (forceClose) { return; }
common.setAttribute(['hideHelp', type], false);
$toolbarButton.addClass('cp-toolbar-button-active');
$toolbarButton.attr('title', Messages.hide_help_button);
2018-02-27 16:38:29 +00:00
return void $(help).removeClass('cp-help-hidden');
}
$toolbarButton.removeClass('cp-toolbar-button-active');
$toolbarButton.attr('title', Messages.show_help_button);
2018-02-27 16:38:29 +00:00
$(help).addClass('cp-help-hidden');
common.setAttribute(['hideHelp', type], true);
};
var showMore = function () {
$(text).addClass("cp-help-small");
var $dot = $('<span>').text('...').appendTo($(text).find('h1'));
$(text).click(function () {
$(text).removeClass('cp-help-small');
$(text).off('click');
$dot.remove();
});
};
$(closeButton).click(function (e) {
e.stopPropagation();
toggleHelp(true);
});
2018-02-27 16:38:29 +00:00
$toolbarButton.click(function () {
toggleHelp();
});
common.getAttribute(['hideHelp', type], function (err, val) {
//if ($(window).height() < 800 || $(window).width() < 800) { return void toggleHelp(true); }
if (val === true) { return void toggleHelp(true); }
// Note: Help is always hidden by default now, to avoid displaying to many things in the UI
// This is why we have (true || ...)
if (!val && (true || $(window).height() < 800 || $(window).width() < 800)) {
return void showMore();
}
2018-02-27 16:38:29 +00:00
});
return {
menu: help,
button: $toolbarButton,
text: text
};
};
2017-09-21 15:56:24 +00:00
/* Create a usage bar which keeps track of how much storage space is used
by your CryptDrive. The getPinnedUsage RPC is one of the heavier calls,
so we throttle its usage. Clients will not update more than once per
LIMIT_REFRESH_RATE. It will be update at least once every three such intervals
If changes are made to your drive in the interim, they will trigger an
update.
*/
2019-09-06 16:47:18 +00:00
// NOTE: The callback must stay SYNCHRONOUS
2017-09-21 15:56:24 +00:00
var LIMIT_REFRESH_RATE = 30000; // milliseconds
2019-09-23 13:45:24 +00:00
UIElements.createUsageBar = function (common, teamId, cb) {
2018-01-12 13:50:56 +00:00
if (AppConfig.hideUsageBar) { return cb('USAGE_BAR_HIDDEN'); }
2017-09-21 15:56:24 +00:00
if (!common.isLoggedIn()) { return cb("NOT_LOGGED_IN"); }
// getPinnedUsage updates common.account.usage, and other values
// so we can just use those and only check for errors
2017-09-25 09:48:42 +00:00
var $container = $('<span>', {'class':'cp-limit-container'});
2017-11-30 16:21:58 +00:00
var todo = function (err, data) {
2018-03-21 17:27:20 +00:00
if (err || !data) { return void console.error(err || 'No data'); }
2017-09-21 15:56:24 +00:00
var usage = data.usage;
var limit = data.limit;
var plan = data.plan;
$container.html('');
var unit = Util.magnitudeOfBytes(limit);
2017-09-27 08:36:16 +00:00
usage = unit === 'GB'? Util.bytesToGigabytes(usage):
2017-09-21 15:56:24 +00:00
Util.bytesToMegabytes(usage);
2017-09-27 08:36:16 +00:00
limit = unit === 'GB'? Util.bytesToGigabytes(limit):
2017-09-21 15:56:24 +00:00
Util.bytesToMegabytes(limit);
2017-09-25 09:48:42 +00:00
var $limit = $('<span>', {'class': 'cp-limit-bar'}).appendTo($container);
2017-09-21 15:56:24 +00:00
var quota = usage/limit;
2017-09-25 09:48:42 +00:00
var $usage = $('<span>', {'class': 'cp-limit-usage'}).css('width', quota*100+'%');
var $buttons = $(h('span.cp-limit-buttons')).appendTo($container);
2017-09-21 15:56:24 +00:00
2017-10-20 08:16:01 +00:00
var urls = common.getMetadataMgr().getPrivateData().accounts;
2017-09-21 15:56:24 +00:00
var makeDonateButton = function () {
2019-01-08 14:03:02 +00:00
var $a = $('<a>', {
'class': 'cp-limit-upgrade btn btn-primary',
2017-10-20 08:16:01 +00:00
href: urls.donateURL,
2017-09-21 15:56:24 +00:00
rel: "noreferrer noopener",
target: "_blank",
}).text(Messages.crowdfunding_button2).appendTo($buttons);
2019-01-08 14:03:02 +00:00
$a.click(function () {
Feedback.send('SUPPORT_CRYPTPAD');
});
2017-09-21 15:56:24 +00:00
};
var makeUpgradeButton = function () {
2019-01-08 14:03:02 +00:00
var $a = $('<a>', {
2017-09-25 09:48:42 +00:00
'class': 'cp-limit-upgrade btn btn-success',
2017-10-20 08:16:01 +00:00
href: urls.upgradeURL,
2017-09-21 15:56:24 +00:00
rel: "noreferrer noopener",
target: "_blank",
}).text(Messages.upgradeAccount).appendTo($buttons);
2019-01-08 14:03:02 +00:00
$a.click(function () {
Feedback.send('UPGRADE_ACCOUNT');
});
2017-09-21 15:56:24 +00:00
};
if (!Config.removeDonateButton) {
if (!common.isLoggedIn() || !Config.allowSubscriptions) {
// user is not logged in, or subscriptions are disallowed
makeDonateButton();
} else if (!plan) {
// user is logged in and subscriptions are allowed
// and they don't have one. show upgrades
makeDonateButton();
2017-09-21 15:56:24 +00:00
makeUpgradeButton();
} else {
// they have a plan. show nothing
}
}
var prettyUsage;
var prettyLimit;
if (unit === 'GB') {
prettyUsage = Messages._getKey('formattedGB', [usage]);
prettyLimit = Messages._getKey('formattedGB', [limit]);
} else {
prettyUsage = Messages._getKey('formattedMB', [usage]);
prettyLimit = Messages._getKey('formattedMB', [limit]);
}
2017-09-25 09:48:42 +00:00
if (quota < 0.8) { $usage.addClass('cp-limit-usage-normal'); }
else if (quota < 1) { $usage.addClass('cp-limit-usage-warning'); }
else { $usage.addClass('cp-limit-usage-above'); }
var $text = $('<span>', {'class': 'cp-limit-usage-text'});
2019-11-08 10:24:07 +00:00
$text.html(Messages._getKey('storageStatus', [prettyUsage, prettyLimit]));
$container.prepend($text);
$limit.append($usage);
2017-09-21 15:56:24 +00:00
};
2017-11-30 16:21:58 +00:00
var updateUsage = Util.notAgainForAnother(function () {
2019-09-23 13:45:24 +00:00
common.getPinUsage(teamId, todo);
2017-11-30 16:21:58 +00:00
}, LIMIT_REFRESH_RATE);
2019-09-23 13:45:24 +00:00
var interval = setInterval(function () {
2017-09-21 15:56:24 +00:00
updateUsage();
}, LIMIT_REFRESH_RATE * 3);
Visible.onChange(function (state) {
if (!state) {
clearInterval(interval);
return;
}
interval = setInterval(function () {
updateUsage();
}, LIMIT_REFRESH_RATE * 3);
updateUsage();
});
2017-09-21 15:56:24 +00:00
updateUsage();
cb(null, $container);
2019-09-23 13:45:24 +00:00
return {
$container: $container,
2019-09-23 13:45:24 +00:00
stop: function () {
clearInterval(interval);
}
};
2017-09-21 15:56:24 +00:00
};
// Create a button with a dropdown menu
// input is a config object with parameters:
// - container (optional): the dropdown container (span)
// - text (optional): the button text value
// - options: array of {tag: "", attributes: {}, content: "string"}
//
// allowed options tags: ['a', 'hr', 'p']
UIElements.createDropdown = function (config) {
if (typeof config !== "object" || !Array.isArray(config.options)) { return; }
2017-11-23 11:28:49 +00:00
if (config.feedback && !config.common) { return void console.error("feedback in a dropdown requires sframe-common"); }
var isElement = function (o) {
return /HTML/.test(Object.prototype.toString.call(o)) &&
typeof(o.tagName) === 'string';
};
2019-05-17 14:19:41 +00:00
var allowedTags = ['a', 'p', 'hr', 'div'];
var isValidOption = function (o) {
if (typeof o !== "object") { return false; }
if (isElement(o)) { return true; }
if (!o.tag || allowedTags.indexOf(o.tag) === -1) { return false; }
return true;
};
// Container
var $container = $(config.container);
var containerConfig = {
'class': 'cp-dropdown-container'
};
if (config.buttonTitle) {
containerConfig.title = config.buttonTitle;
}
if (!config.container) {
$container = $('<span>', containerConfig);
}
// Button
var $button = $('<button>', {
'class': ''
}).append($('<span>', {'class': 'cp-dropdown-button-title'}).html(config.text || ""));
if (config.caretDown) {
$('<span>', {
'class': 'fa fa-caret-down',
}).prependTo($button);
}
// Menu
var $innerblock = $('<div>', {'class': 'cp-dropdown-content'});
if (config.left) { $innerblock.addClass('cp-dropdown-left'); }
config.options.forEach(function (o) {
if (!isValidOption(o)) { return; }
if (isElement(o)) { return $innerblock.append($(o)); }
var $el = $('<' + o.tag + '>', o.attributes || {}).html(o.content || '');
$el.appendTo($innerblock);
if (typeof(o.action) === 'function') {
$el.click(o.action);
}
});
$container.append($button).append($innerblock);
var value = config.initialValue || '';
var setActive = function ($el) {
if ($el.length !== 1) { return; }
$innerblock.find('.cp-dropdown-element-active').removeClass('cp-dropdown-element-active');
$el.addClass('cp-dropdown-element-active');
var scroll = $el.position().top + $innerblock.scrollTop();
if (scroll < $innerblock.scrollTop()) {
$innerblock.scrollTop(scroll);
} else if (scroll > ($innerblock.scrollTop() + 280)) {
$innerblock.scrollTop(scroll-270);
}
};
var hide = function () {
window.setTimeout(function () { $innerblock.hide(); }, 0);
};
var show = function () {
var wh = $(window).height();
2020-05-28 10:17:32 +00:00
var button = $button[0].getBoundingClientRect();
var topPos = button.bottom;
$innerblock.css('bottom', '');
if (config.noscroll) {
var h = $innerblock.outerHeight();
if ((topPos + h) > wh) {
$innerblock.css('bottom', button.height+'px');
}
} else {
$innerblock.css('max-height', Math.floor(wh - topPos - 1)+'px');
}
$innerblock.show();
$innerblock.find('.cp-dropdown-element-active').removeClass('cp-dropdown-element-active');
if (config.isSelect && value) {
var $val = $innerblock.find('[data-value="'+value+'"]');
setActive($val);
$innerblock.scrollTop($val.position().top + $innerblock.scrollTop());
}
2017-11-23 11:28:49 +00:00
if (config.feedback) { Feedback.send(config.feedback); }
};
$container.click(function (e) {
e.stopPropagation();
var state = $innerblock.is(':visible');
$('.cp-dropdown-content').hide();
2020-05-27 14:32:28 +00:00
var $c = $container.closest('.cp-toolbar-drawer-content');
$c.removeClass('cp-dropdown-visible');
if (!state) { $c.addClass('cp-dropdown-visible'); }
try {
$('iframe').each(function (idx, ifrw) {
$(ifrw).contents().find('.cp-dropdown-content').hide();
});
} catch (er) {
// empty try catch in case this iframe is problematic (cross-origin)
}
if (state) {
hide();
return;
}
show();
});
if (config.isSelect) {
var pressed = '';
var to;
$container.on('click', 'a', function () {
value = $(this).data('value');
2020-01-16 16:51:02 +00:00
var $val = $(this);
var textValue = $val.html() || value;
$button.find('.cp-dropdown-button-title').html(textValue);
});
$container.keydown(function (e) {
var $value = $innerblock.find('[data-value].cp-dropdown-element-active:visible');
if (e.which === 38) { // Up
if ($value.length) {
$value.mouseleave();
var $prev = $value.prev();
$prev.mouseenter();
setActive($prev);
}
}
if (e.which === 40) { // Down
if ($value.length) {
$value.mouseleave();
var $next = $value.next();
$next.mouseenter();
setActive($next);
}
}
if (e.which === 13) { //Enter
if ($value.length) {
$value.click();
hide();
}
}
if (e.which === 27) { // Esc
$value.mouseleave();
hide();
}
});
$container.keypress(function (e) {
window.clearTimeout(to);
var c = String.fromCharCode(e.which);
pressed += c;
var $value = $innerblock.find('[data-value^="'+pressed+'"]:first');
if ($value.length) {
setActive($value);
$innerblock.scrollTop($value.position().top + $innerblock.scrollTop());
}
to = window.setTimeout(function () {
pressed = '';
}, 1000);
});
$container.setValue = function (val, name) {
value = val;
var $val = $innerblock.find('[data-value="'+val+'"]');
var textValue = name || $val.html() || val;
$button.find('.cp-dropdown-button-title').html(textValue);
};
$container.getValue = function () {
return value || '';
};
}
return $container;
};
2020-06-03 17:29:38 +00:00
UIElements.displayInfoMenu = function (Common, metadataMgr) {
//var padType = metadataMgr.getMetadata().type;
2020-06-03 17:29:38 +00:00
var priv = metadataMgr.getPrivateData();
var origin = priv.origin;
// TODO link to the most recent changelog/release notes
// https://github.com/xwiki-labs/cryptpad/releases/latest/ ?
Messages.info_imprintFlavour = "Legally mandated information about this service's operators can be found <a>here</a>."; // XXX
Messages.info_privacyFlavour = "Our <a>privacy policy</a> describes how we treat your data."; // XXX
Messages.info_faqFlavour = "Consult our <a>FAQ</a> for answers to common questions."; // XXX
var template = function (line, link) {
if (!line || !link) { return; }
var p = $('<p>').html(line)[0];
var sub = link.cloneNode(true);
/* This is a hack to make relative URLs point to the main domain
instead of the sandbox domain. It will break if the admins have specified
some less common URL formats for their customizable links, such as if they've
used a protocal-relative absolute URL. The URL API isn't quite safe to use
because of IE (thanks, Bill). */
var href = sub.getAttribute('href');
if (/^\//.test(href)) { sub.setAttribute('href', origin + href); }
var a = p.querySelector('a');
if (!a) { return; }
sub.innerText = a.innerText;
p.replaceChild(sub, a);
return p;
};
var legalLine = template(Messages.info_imprintFlavour, Pages.imprintLink);
var privacyLine = template(Messages.info_privacyFlavour, Pages.privacyLink);
var faqLine = template(Messages.info_faqFlavour, Pages.faqLink);
var content = h('div.cp-info-menu-container', [
2020-06-03 17:29:38 +00:00
h('h6', Pages.versionString),
h('hr'),
legalLine,
privacyLine,
2020-06-03 17:29:38 +00:00
faqLine,
]);
var buttons = [
{
className: 'primary',
name: Messages.filePicker_close,
onClick: function () {},
keys: [27],
},
];
var modal = UI.dialog.customModal(content, {buttons: buttons });
UI.openCustomModal(modal);
};
UIElements.createUserAdminMenu = function (Common, config) {
2017-09-06 16:26:10 +00:00
var metadataMgr = Common.getMetadataMgr();
2017-08-18 16:43:04 +00:00
2017-11-17 17:20:45 +00:00
var displayNameCls = config.displayNameCls || 'cp-toolbar-user-name';
2017-08-18 16:43:04 +00:00
var $displayedName = $('<span>', {'class': displayNameCls});
var priv = metadataMgr.getPrivateData();
var accountName = priv.accountName;
var origin = priv.origin;
2017-08-18 16:43:04 +00:00
var padType = metadataMgr.getMetadata().type;
2017-11-17 17:20:45 +00:00
var $userName = $('<span>');
2017-08-18 16:43:04 +00:00
var options = [];
if (config.displayNameCls) {
var $userAdminContent = $('<p>');
if (accountName) {
2017-11-17 17:20:45 +00:00
var $userAccount = $('<span>').append(Messages.user_accountName + ': ');
$userAdminContent.append($userAccount).append(Util.fixHTML(accountName));
2017-08-18 16:43:04 +00:00
$userAdminContent.append($('<br>'));
}
2018-01-23 13:24:40 +00:00
if (config.displayName && !AppConfig.disableProfile) {
2017-08-18 16:43:04 +00:00
// Hide "Display name:" in read only mode
$userName.append(Messages.user_displayName + ': ');
$userName.append($displayedName);
}
$userAdminContent.append($userName);
options.push({
tag: 'p',
2017-09-25 09:59:05 +00:00
attributes: {'class': 'cp-toolbar-account'},
2017-08-18 16:43:04 +00:00
content: $userAdminContent.html()
});
}
2019-08-23 14:19:45 +00:00
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'href': origin+'/index.html',
'class': 'fa fa-home'
},
content: h('span', Messages.homePage)
});
if (padType !== 'drive' || (!accountName && priv.newSharedFolder)) {
2017-08-18 16:43:04 +00:00
options.push({
tag: 'a',
attributes: {
'target': '_blank',
2018-03-22 14:19:14 +00:00
'href': origin+'/drive/',
'class': 'fa fa-hdd-o'
2017-08-18 16:43:04 +00:00
},
2019-10-01 08:57:46 +00:00
content: h('span', Messages.type.drive)
});
}
if (padType !== 'teams' && accountName) {
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'href': origin+'/teams/',
'class': 'fa fa-users'
},
content: h('span', Messages.type.teams)
2017-08-18 16:43:04 +00:00
});
}
if (padType !== 'contacts' && accountName) {
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'href': origin+'/contacts/',
'class': 'cptools cptools-contacts'
},
content: h('span', Messages.type.contacts)
});
}
2019-08-23 14:19:45 +00:00
options.push({ tag: 'hr' });
2017-08-18 16:43:04 +00:00
// Add the change display name button if not in read only mode
/*
2018-01-23 13:24:40 +00:00
if (config.changeNameButtonCls && config.displayChangeName && !AppConfig.disableProfile) {
2017-08-18 16:43:04 +00:00
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': config.changeNameButtonCls + ' fa fa-user'},
content: h('span', Messages.user_rename)
2017-08-18 16:43:04 +00:00
});
}*/
2018-01-23 13:24:40 +00:00
if (accountName && !AppConfig.disableProfile) {
2017-08-18 16:43:04 +00:00
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': 'cp-toolbar-menu-profile fa fa-user-circle'},
content: h('span', Messages.profileButton),
action: function () {
if (padType) {
window.open(origin+'/profile/');
} else {
window.parent.location = origin+'/profile/';
}
},
2017-08-18 16:43:04 +00:00
});
}
if (padType !== 'settings') {
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': 'cp-toolbar-menu-settings fa fa-cog'},
content: h('span', Messages.settingsButton),
action: function () {
if (padType) {
window.open(origin+'/settings/');
} else {
window.parent.location = origin+'/settings/';
}
},
2017-08-18 16:43:04 +00:00
});
}
options.push({ tag: 'hr' });
2019-03-27 16:00:28 +00:00
// Add administration panel link if the user is an admin
if (priv.edPublic && Array.isArray(Config.adminKeys) && Config.adminKeys.indexOf(priv.edPublic) !== -1) {
options.push({
tag: 'a',
attributes: {'class': 'cp-toolbar-menu-admin fa fa-cogs'},
content: h('span', Messages.adminPage || 'Admin'),
action: function () {
if (padType) {
window.open(origin+'/admin/');
} else {
window.parent.location = origin+'/admin/';
}
},
2019-03-27 16:00:28 +00:00
});
}
if (padType !== 'support' && accountName && Config.supportMailbox) {
options.push({
tag: 'a',
attributes: {'class': 'cp-toolbar-menu-support fa fa-life-ring'},
content: h('span', Messages.supportPage || 'Support'),
action: function () {
if (padType) {
window.open(origin+'/support/');
} else {
window.parent.location = origin+'/support/';
}
},
});
}
options.push({ tag: 'hr' });
if (Config.allowSubscriptions) {
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'href': priv.plan ? priv.accounts.upgradeURL : origin+'/features.html',
'class': 'fa fa-star-o'
},
content: h('span', priv.plan ? Messages.settings_cat_subscription : Messages.pricing)
});
}
if (!priv.plan && !Config.removeDonateButton) {
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'rel': 'noopener',
'href': priv.accounts.donateURL,
'class': 'fa fa-gift'
},
content: h('span', Messages.crowdfunding_button2)
});
}
if (AppConfig.surveyURL) {
options.push({
tag: 'a',
attributes: {
'target': '_blank',
'rel': 'noopener',
'href': AppConfig.surveyURL,
2019-10-15 16:03:53 +00:00
'class': 'cp-toolbar-survey fa fa-graduation-cap'
},
content: h('span', Messages.survey),
action: function () {
Feedback.send('SURVEY_CLICKED');
},
});
}
2020-06-03 17:29:38 +00:00
Messages.user_about = 'About CryptPad'; // XXX
options.push({
tag: 'a',
attributes: {
'class': 'cp-toolbar-about fa fa-info',
},
content: h('span', Messages.user_about),
action: function () {
UIElements.displayInfoMenu(Common, metadataMgr);
},
});
2019-08-23 14:19:45 +00:00
options.push({ tag: 'hr' });
2017-08-18 16:43:04 +00:00
// Add login or logout button depending on the current status
if (priv.loggedIn) {
options.push({
tag: 'a',
attributes: {
'class': 'cp-toolbar-menu-logout-everywhere fa fa-plug',
},
content: h('span', Messages.logoutEverywhere),
action: function () {
Common.getSframeChannel().query('Q_LOGOUT_EVERYWHERE', null, function () {
window.parent.location = origin + '/';
});
},
});
2017-08-18 16:43:04 +00:00
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': 'cp-toolbar-menu-logout fa fa-sign-out'},
content: h('span', Messages.logoutButton),
action: function () {
Common.logout(function () {
window.parent.location = origin+'/';
});
},
2017-08-18 16:43:04 +00:00
});
} else {
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': 'cp-toolbar-menu-login fa fa-sign-in'},
content: h('span', Messages.login_login),
action: function () {
Common.setLoginRedirect(function () {
window.parent.location = origin+'/login/';
});
},
2017-08-18 16:43:04 +00:00
});
options.push({
tag: 'a',
2018-03-22 14:19:14 +00:00
attributes: {'class': 'cp-toolbar-menu-register fa fa-user-plus'},
content: h('span', Messages.login_register),
action: function () {
Common.setLoginRedirect(function () {
window.parent.location = origin+'/register/';
});
},
2017-08-18 16:43:04 +00:00
});
}
var $icon = $('<span>', {'class': 'fa fa-user-secret'});
//var $userbig = $('<span>', {'class': 'big'}).append($displayedName.clone());
var $userButton = $('<div>').append($icon);//.append($userbig);
if (accountName) {
$userButton = $('<div>').append(accountName);
}
/*if (account && config.displayNameCls) {
$userbig.append($('<span>', {'class': 'account-name'}).text('(' + accountName + ')'));
} else if (account) {
// If no display name, do not display the parentheses
$userbig.append($('<span>', {'class': 'account-name'}).text(accountName));
}*/
var dropdownConfigUser = {
text: $userButton.html(), // Button initial text
options: options, // Entries displayed in the menu
left: true, // Open to the left of the button
container: config.$initBlock, // optional
feedback: "USER_ADMIN",
2017-11-23 11:28:49 +00:00
common: Common
2017-08-18 16:43:04 +00:00
};
var $userAdmin = UIElements.createDropdown(dropdownConfigUser);
2017-08-18 16:43:04 +00:00
/*
// Uncomment these lines to have a language selector in the admin menu
// FIXME clicking on the inner menu hides the outer one
var $lang = UIElements.createLanguageSelector(Common);
$userAdmin.find('.cp-dropdown-content').append($lang);
*/
2017-08-18 16:43:04 +00:00
var $displayName = $userAdmin.find('.'+displayNameCls);
var $avatar = $userAdmin.find('> button .cp-dropdown-button-title');
2017-09-08 13:54:54 +00:00
var loadingAvatar;
var to;
var oldUrl = '';
2017-08-18 16:43:04 +00:00
var updateButton = function () {
var myData = metadataMgr.getUserData();
2017-08-18 16:43:04 +00:00
if (!myData) { return; }
2017-09-08 13:54:54 +00:00
if (loadingAvatar) {
// Try again in 200ms
window.clearTimeout(to);
to = window.setTimeout(updateButton, 200);
return;
}
loadingAvatar = true;
2017-08-18 16:43:04 +00:00
var newName = myData.name;
var url = myData.avatar;
$displayName.text(newName || Messages.anonymous);
2017-08-30 10:26:11 +00:00
if (accountName && oldUrl !== url) {
$avatar.html('');
Common.displayAvatar($avatar, url,
newName || Messages.anonymous, function ($img) {
2017-08-30 10:26:11 +00:00
oldUrl = url;
2018-03-22 14:19:14 +00:00
$userAdmin.find('> button').removeClass('cp-avatar');
if ($img) { $userAdmin.find('> button').addClass('cp-avatar'); }
2017-09-08 13:54:54 +00:00
loadingAvatar = false;
});
2017-09-08 13:54:54 +00:00
return;
}
2017-09-08 13:54:54 +00:00
loadingAvatar = false;
2017-08-18 16:43:04 +00:00
};
metadataMgr.onChange(updateButton);
updateButton();
return $userAdmin;
};
// Provide $container if you want to put the generated block in another element
// Provide $initBlock if you already have the menu block and you want the content inserted in it
UIElements.createLanguageSelector = function (common, $container, $initBlock) {
var options = [];
var languages = Messages._languages;
var keys = Object.keys(languages).sort();
keys.forEach(function (l) {
options.push({
tag: 'a',
attributes: {
'class': 'cp-language-value',
'data-value': l,
'href': '#',
},
content: languages[l] // Pretty name of the language value
});
});
var dropdownConfig = {
text: Messages.language, // Button initial text
options: options, // Entries displayed in the menu
//left: true, // Open to the left of the button
container: $initBlock, // optional
2017-11-23 11:28:49 +00:00
isSelect: true,
common: common
};
var $block = UIElements.createDropdown(dropdownConfig);
$block.attr('id', 'cp-language-selector');
if ($container) {
$block.appendTo($container);
}
Language.initSelector($block, common);
return $block;
};
2018-01-12 08:48:40 +00:00
UIElements.createNewPadModal = function (common) {
// if in drive, show new pad modal instead
if ($(".cp-app-drive-element-row.cp-app-drive-new-ghost").length !== 0) {
return void $(".cp-app-drive-element-row.cp-app-drive-new-ghost").click();
}
2020-04-06 09:42:47 +00:00
var modal = UI.createModal({
2018-01-12 08:48:40 +00:00
id: 'cp-app-toolbar-creation-dialog',
$body: $('body')
});
2020-04-06 09:42:47 +00:00
var $modal = modal.$modal;
2018-01-12 08:48:40 +00:00
var $title = $('<h3>').text(Messages.fm_newFile);
var $description = $('<p>').html(Messages.creation_newPadModalDescription);
2018-01-12 08:48:40 +00:00
$modal.find('.cp-modal').append($title);
$modal.find('.cp-modal').append($description);
var $advanced;
var $container = $('<div>');
var i = 0;
2018-09-17 12:39:52 +00:00
var types = AppConfig.availablePadTypes.filter(function (p) {
2018-01-12 08:48:40 +00:00
if (p === 'drive') { return; }
2019-10-01 08:32:37 +00:00
if (p === 'teams') { return; }
2018-01-12 08:48:40 +00:00
if (p === 'contacts') { return; }
if (p === 'todo') { return; }
if (p === 'file') { return; }
if (!common.isLoggedIn() && AppConfig.registeredOnlyTypes &&
AppConfig.registeredOnlyTypes.indexOf(p) !== -1) { return; }
2018-09-17 12:39:52 +00:00
return true;
});
types.forEach(function (p) {
2018-01-12 08:48:40 +00:00
var $element = $('<li>', {
'class': 'cp-icons-element',
'id': 'cp-newpad-icons-'+ (i++)
2018-01-12 08:48:40 +00:00
}).prepend(UI.getIcon(p)).appendTo($container);
$element.append($('<span>', {'class': 'cp-icons-name'})
.text(Messages.type[p]));
$element.attr('data-type', p);
$element.click(function () {
$modal.hide();
2018-03-19 15:56:09 +00:00
if ($advanced && Util.isChecked($advanced)) {
common.sessionStorage.put(Constants.displayPadCreationScreen, true, function (){
2018-01-12 08:48:40 +00:00
common.openURL('/' + p + '/');
});
return;
}
common.sessionStorage.put(Constants.displayPadCreationScreen, "", function () {
common.openURL('/' + p + '/');
});
});
});
var selected = -1;
var next = function () {
2018-09-17 12:39:52 +00:00
selected = ++selected % types.length;
$container.find('.cp-icons-element-selected').removeClass('cp-icons-element-selected');
$container.find('#cp-newpad-icons-'+selected).addClass('cp-icons-element-selected');
};
$modal.off('keydown');
$modal.keydown(function (e) {
if (e.which === 9) {
e.preventDefault();
e.stopPropagation();
next();
return;
}
if (e.which === 13) {
if ($container.find('.cp-icons-element-selected').length === 1) {
$container.find('.cp-icons-element-selected').click();
}
return;
}
if (e.which === 32 && $advanced) {
$advanced.prop('checked', !$advanced.prop('checked'));
$modal.focus();
e.stopPropagation();
e.preventDefault();
}
});
2018-01-12 08:48:40 +00:00
2020-03-27 14:47:59 +00:00
$modal.find('.cp-modal').append($container);
window.setTimeout(function () {
2020-04-06 09:42:47 +00:00
modal.show();
$modal.focus();
});
2018-01-12 08:48:40 +00:00
};
2020-04-07 12:27:44 +00:00
UIElements.openFilePicker = function (common, types, cb) {
var sframeChan = common.getSframeChannel();
2020-04-07 12:27:44 +00:00
sframeChan.query("Q_FILE_PICKER_OPEN", types, function (err, data) {
if (err) { return; }
cb(data);
});
};
2017-09-05 09:35:15 +00:00
2018-03-22 16:01:01 +00:00
UIElements.openTemplatePicker = function (common, force) {
2017-09-05 09:35:15 +00:00
var metadataMgr = common.getMetadataMgr();
var type = metadataMgr.getMetadataLazy().type;
var sframeChan = common.getSframeChannel();
var focus;
2018-04-13 16:52:55 +00:00
var pickerCfgInit = {
types: [type],
where: ['template'],
hidden: true
};
2018-04-13 16:52:55 +00:00
var pickerCfg = {
types: [type],
where: ['template'],
};
var onConfirm = function (yes) {
if (!yes) {
if (focus) { focus.focus(); }
return;
}
delete pickerCfg.hidden;
var first = true; // We can only pick a template once (for a new document)
2020-04-07 12:27:44 +00:00
common.openFilePicker(pickerCfg, function (data) {
if (data.type === type && first) {
UI.addLoadingScreen({hideTips: true});
var chatChan = common.getPadChat();
var cursorChan = common.getCursorChannel();
sframeChan.query('Q_TEMPLATE_USE', {
href: data.href,
chat: chatChan,
cursor: cursorChan
}, function () {
first = false;
UI.removeLoadingScreen();
Feedback.send('TEMPLATE_USED');
});
if (focus) { focus.focus(); }
return;
2017-09-05 09:35:15 +00:00
}
2020-04-07 12:27:44 +00:00
});
2017-09-05 09:35:15 +00:00
};
sframeChan.query("Q_TEMPLATE_EXIST", type, function (err, data) {
if (data) {
2018-04-13 16:52:55 +00:00
common.openFilePicker(pickerCfgInit);
focus = document.activeElement;
2018-03-22 16:01:01 +00:00
if (force) { return void onConfirm(true); }
UI.confirm(Messages.useTemplate, onConfirm, {
ok: Messages.useTemplateOK,
cancel: Messages.useTemplateCancel,
});
2018-03-22 16:01:01 +00:00
} else if (force) {
UI.alert(Messages.template_empty);
}
});
};
UIElements.setExpirationValue = function (val, $expire) {
if (val && typeof (val) === "number") {
2018-03-13 10:31:08 +00:00
$expire.find('#cp-creation-expire').attr('checked', true).trigger('change');
$expire.find('#cp-creation-expire-true').attr('checked', true);
if (val % (3600 * 24 * 30) === 0) {
$expire.find('#cp-creation-expire-unit').val("month");
$expire.find('#cp-creation-expire-val').val(val / (3600 * 24 * 30));
return;
}
if (val % (3600 * 24) === 0) {
$expire.find('#cp-creation-expire-unit').val("day");
$expire.find('#cp-creation-expire-val').val(val / (3600 * 24));
return;
}
if (val % 3600 === 0) {
$expire.find('#cp-creation-expire-unit').val("hour");
$expire.find('#cp-creation-expire-val').val(val / 3600);
return;
}
// if we're here, it means we don't have a valid value so we should check unlimited
$expire.find('#cp-creation-expire-false').attr('checked', true);
}
};
UIElements.getPadCreationScreen = function (common, cfg, appCfg, cb) {
2019-02-05 16:07:19 +00:00
appCfg = appCfg || {};
2017-12-07 17:51:50 +00:00
if (!common.isLoggedIn()) { return void cb(); }
var sframeChan = common.getSframeChannel();
var metadataMgr = common.getMetadataMgr();
var privateData = metadataMgr.getPrivateData();
2017-12-11 11:19:44 +00:00
var type = metadataMgr.getMetadataLazy().type;
var fromFileData = privateData.fromFileData;
2017-12-07 17:51:50 +00:00
var $body = $('body');
2017-12-11 11:19:44 +00:00
var $creationContainer = $('<div>', { id: 'cp-creation-container' }).appendTo($body);
2018-04-11 16:56:03 +00:00
var urlArgs = (Config.requireConf && Config.requireConf.urlArgs) || '';
var l = h('div.cp-creation-logo', h('img', { src: '/customize/loading-logo.png?' + urlArgs }));
2018-04-11 16:56:03 +00:00
$(l).appendTo($creationContainer);
2018-03-02 17:33:43 +00:00
var $creation = $('<div>', { id: 'cp-creation', tabindex: 1 }).appendTo($creationContainer);
2017-12-11 11:19:44 +00:00
// Title
2018-04-16 13:57:18 +00:00
//var colorClass = 'cp-icon-color-'+type;
2018-04-11 16:56:03 +00:00
//$creation.append(h('h2.cp-creation-title', Messages.newButtonTitle));
var newPadH3Title = Messages['button_new' + type];
$creation.append(h('h3.cp-creation-title', newPadH3Title));
2018-03-19 15:17:19 +00:00
//$creation.append(h('h2.cp-creation-title.'+colorClass, Messages.newButtonTitle));
2017-12-11 11:19:44 +00:00
// Deleted pad warning
if (metadataMgr.getPrivateData().isDeleted) {
2018-03-13 10:31:08 +00:00
$creation.append(h('div.cp-creation-deleted-container',
h('div.cp-creation-deleted', Messages.creation_404)
));
2017-12-11 11:19:44 +00:00
}
var origin = privateData.origin;
2018-03-13 10:31:08 +00:00
var createHelper = function (href, text) {
2018-04-11 16:56:03 +00:00
var q = h('a.cp-creation-help.fa.fa-question-circle', {
2020-04-29 10:30:59 +00:00
'data-cptippy-html': true,
2018-03-13 10:31:08 +00:00
title: text,
href: origin + href,
2018-04-12 17:08:08 +00:00
target: "_blank",
'data-tippy-placement': "right"
2017-12-11 11:19:44 +00:00
});
return q;
2017-12-07 17:51:50 +00:00
};
2017-12-11 11:19:44 +00:00
2019-09-12 15:54:50 +00:00
// Team pad
var team;
var teamExists = privateData.teams && Object.keys(privateData.teams).length;
2019-09-24 16:09:07 +00:00
var teamValue;
2019-09-12 15:54:50 +00:00
// storeInTeam can be
// * a team ID ==> store in the team drive, and the team will be the owner
// * -1 ==> store in the user drive, and the user will be the owner
// * undefined ==> ask
2019-10-01 08:59:26 +00:00
if (teamExists) {
2019-09-24 16:09:07 +00:00
var teams = Object.keys(privateData.teams).map(function (id) {
var data = privateData.teams[id];
var avatar = h('span.cp-creation-team-avatar.cp-avatar');
common.displayAvatar($(avatar), data.avatar, data.name);
2019-09-24 16:09:07 +00:00
return h('div.cp-creation-team', {
'data-id': id,
title: data.name,
},[
avatar,
h('span.cp-creation-team-name', data.name)
]);
2019-09-12 15:54:50 +00:00
});
2019-09-24 16:09:07 +00:00
teams.unshift(h('div.cp-creation-team', {
'data-id': '-1',
title: Messages.settings_cat_drive
}, [
h('span.cp-creation-team-avatar.fa.fa-hdd-o'),
h('span.cp-creation-team-name', Messages.settings_cat_drive)
]));
team = h('div.cp-creation-teams', [
Messages.team_pcsSelectLabel,
2019-09-24 16:09:07 +00:00
h('div.cp-creation-teams-grid', teams),
createHelper('#', Messages.team_pcsSelectHelp)
2019-09-12 15:54:50 +00:00
]);
2019-09-24 16:09:07 +00:00
var $team = $(team);
$team.find('.cp-creation-team').click(function () {
if ($(this).hasClass('cp-selected')) {
teamValue = undefined;
return void $(this).removeClass('cp-selected');
}
$team.find('.cp-creation-team').removeClass('cp-selected');
$(this).addClass('cp-selected');
teamValue = $(this).attr('data-id');
});
2019-09-12 15:54:50 +00:00
if (privateData.storeInTeam) {
2019-09-24 16:09:07 +00:00
$team.find('[data-id="'+privateData.storeInTeam+'"]').addClass('cp-selected');
teamValue = privateData.storeInTeam;
2019-09-12 15:54:50 +00:00
}
}
2017-12-11 11:19:44 +00:00
// Owned pads
// Default is Owned pad
2017-12-11 11:19:44 +00:00
var owned = h('div.cp-creation-owned', [
2018-04-16 17:07:54 +00:00
UI.createCheckbox('cp-creation-owned', Messages.creation_owned, true),
2018-03-13 10:31:08 +00:00
createHelper('/faq.html#keywords-owned', Messages.creation_owned1)
2017-12-11 11:19:44 +00:00
]);
2017-12-11 11:19:44 +00:00
// Life time
var expire = h('div.cp-creation-expire', [
2018-04-16 17:07:54 +00:00
UI.createCheckbox('cp-creation-expire', Messages.creation_expire, false),
2018-04-11 16:56:03 +00:00
h('span.cp-creation-expire-picker.cp-creation-slider', [
2018-03-13 10:31:08 +00:00
h('input#cp-creation-expire-val', {
type: "number",
min: 1,
max: 100,
value: 3
}),
h('select#cp-creation-expire-unit', [
h('option', { value: 'hour' }, Messages.creation_expireHours),
h('option', { value: 'day' }, Messages.creation_expireDays),
h('option', {
value: 'month',
selected: 'selected'
}, Messages.creation_expireMonths)
2017-12-11 11:19:44 +00:00
])
2018-04-11 16:56:03 +00:00
]),
createHelper('/faq.html#keywords-expiring', Messages.creation_expire2),
2017-12-11 11:19:44 +00:00
]);
2018-03-13 10:31:08 +00:00
// Password
var password = h('div.cp-creation-password', [
2018-04-27 15:23:23 +00:00
UI.createCheckbox('cp-creation-password', Messages.creation_password, false),
h('span.cp-creation-password-picker.cp-creation-slider', [
UI.passwordInput({id: 'cp-creation-password-val'})
/*h('input#cp-creation-password-val', {
type: "text" // TODO type password with click to show
}),*/
]),
2018-04-27 15:23:23 +00:00
//createHelper('#', "TODO: password protection adds another layer of security ........") // TODO
]);
2018-03-20 10:05:43 +00:00
2018-04-17 16:23:58 +00:00
var right = h('span.fa.fa-chevron-right.cp-creation-template-more');
var left = h('span.fa.fa-chevron-left.cp-creation-template-more');
2018-03-13 10:31:08 +00:00
var templates = h('div.cp-creation-template', [
2018-04-17 16:23:58 +00:00
left,
2018-03-13 10:31:08 +00:00
h('div.cp-creation-template-container', [
h('span.fa.fa-circle-o-notch.fa-spin.fa-4x.fa-fw')
2018-03-20 10:05:43 +00:00
]),
2018-04-17 16:23:58 +00:00
right
2018-03-13 10:31:08 +00:00
]);
2018-04-11 16:56:03 +00:00
var createDiv = h('div.cp-creation-create');
var $create = $(createDiv);
2018-03-13 10:31:08 +00:00
$(h('div#cp-creation-form', [
2019-09-12 15:54:50 +00:00
team,
2018-03-13 10:31:08 +00:00
owned,
expire,
password,
2018-04-11 16:56:03 +00:00
templates,
createDiv
2018-03-13 10:31:08 +00:00
])).appendTo($creation);
// Display templates
2018-04-17 16:23:58 +00:00
var selected = 0; // Selected template in the list (highlighted)
var TEMPLATES_DISPLAYED = 4; // Max templates displayed per page
var next = function () {}; // Function called when pressing tab to highlight the next template
var i = 0; // Index of the first template displayed in the current page
2018-03-13 10:31:08 +00:00
sframeChan.query("Q_CREATE_TEMPLATES", type, function (err, res) {
if (!res.data || !Array.isArray(res.data)) {
return void console.error("Error: get the templates list");
}
2018-04-17 16:23:58 +00:00
var allData = res.data.slice().sort(function (a, b) {
2018-04-13 16:52:55 +00:00
if (a.used === b.used) {
// Sort by name
if (a.name === b.name) { return 0; }
return a.name < b.name ? -1 : 1;
}
return b.used - a.used;
2018-03-13 10:31:08 +00:00
});
if (!appCfg.noTemplates) {
allData.unshift({
name: Messages.creation_newTemplate,
id: -1,
//icon: h('span.fa.fa-bookmark')
icon: h('span.cptools.cptools-new-template')
});
}
2018-04-17 16:23:58 +00:00
allData.unshift({
name: Messages.creation_noTemplate,
2018-03-13 10:31:08 +00:00
id: 0,
2018-07-28 09:37:38 +00:00
//icon: h('span.fa.fa-file')
icon: UI.getFileIcon({type: type})
2018-03-13 10:31:08 +00:00
});
2018-04-17 16:23:58 +00:00
var redraw = function (index) {
if (index < 0) { i = 0; }
else if (index > allData.length - 1) { return; }
else { i = index; }
var data = allData.slice(i, i + TEMPLATES_DISPLAYED);
var $container = $(templates).find('.cp-creation-template-container').html('');
data.forEach(function (obj, idx) {
var name = obj.name;
var $span = $('<span>', {
'class': 'cp-creation-template-element',
'title': name,
}).appendTo($container);
$span.data('id', obj.id);
if (idx === selected) { $span.addClass('cp-creation-template-selected'); }
if (!obj.thumbnail) {
$span.append(obj.icon || h('span.cptools.cptools-template'));
}
2018-04-17 16:23:58 +00:00
$('<span>', {'class': 'cp-creation-template-element-name'}).text(name)
.appendTo($span);
$span.click(function () {
$container.find('.cp-creation-template-selected')
.removeClass('cp-creation-template-selected');
$span.addClass('cp-creation-template-selected');
selected = idx;
});
2018-03-13 10:31:08 +00:00
2018-04-17 16:23:58 +00:00
// Add thumbnail if it exists
if (obj.thumbnail) {
common.addThumbnail(obj.thumbnail, $span, function () {});
}
});
$(right).off('click').removeClass('hidden').click(function () {
selected = 0;
redraw(i + TEMPLATES_DISPLAYED);
});
if (i >= allData.length - TEMPLATES_DISPLAYED ) { $(right).addClass('hidden'); }
$(left).off('click').removeClass('hidden').click(function () {
selected = TEMPLATES_DISPLAYED - 1;
redraw(i - TEMPLATES_DISPLAYED);
});
if (i < TEMPLATES_DISPLAYED) { $(left).addClass('hidden'); }
};
if (fromFileData) {
var todo = function (thumbnail) {
allData = [{
name: fromFileData.title,
id: 0,
thumbnail: thumbnail,
icon: h('span.cptools.cptools-file'),
}];
redraw(0);
};
todo();
sframeChan.query("Q_GET_FILE_THUMBNAIL", null, function (err, res) {
if (err || (res && res.error)) { return; }
todo(res.data);
});
}
else {
redraw(0);
}
2018-04-17 16:23:58 +00:00
// Change template selection when Tab is pressed
next = function (revert) {
var max = $creation.find('.cp-creation-template-element').length;
if (selected + 1 === max && !revert) {
selected = i + TEMPLATES_DISPLAYED < allData.length ? 0 : max;
return void redraw(i + TEMPLATES_DISPLAYED);
2018-03-13 10:31:08 +00:00
}
2018-04-17 16:23:58 +00:00
if (selected === 0 && revert) {
selected = i - TEMPLATES_DISPLAYED >= 0 ? TEMPLATES_DISPLAYED - 1 : 0;
return void redraw(i - TEMPLATES_DISPLAYED);
}
selected = revert ?
(--selected < 0 ? 0 : selected) :
++selected >= max ? max-1 : selected;
$creation.find('.cp-creation-template-element')
.removeClass('cp-creation-template-selected');
$($creation.find('.cp-creation-template-element').get(selected))
.addClass('cp-creation-template-selected');
};
2018-03-13 10:31:08 +00:00
2018-04-17 16:23:58 +00:00
});
2018-03-13 10:31:08 +00:00
// Display expiration form when checkbox checked
$creation.find('#cp-creation-expire').on('change', function () {
if ($(this).is(':checked')) {
$creation.find('.cp-creation-expire-picker:not(.active)').addClass('active');
2018-04-11 16:56:03 +00:00
$creation.find('.cp-creation-expire:not(.active)').addClass('active');
2018-03-13 10:31:08 +00:00
$creation.find('#cp-creation-expire-val').focus();
return;
}
$creation.find('.cp-creation-expire-picker').removeClass('active');
2018-04-11 16:56:03 +00:00
$creation.find('.cp-creation-expire').removeClass('active');
2018-03-13 10:31:08 +00:00
$creation.focus();
});
2018-05-17 14:10:53 +00:00
// Display password form when checkbox checked
$creation.find('#cp-creation-password').on('change', function () {
if ($(this).is(':checked')) {
$creation.find('.cp-creation-password-picker:not(.active)').addClass('active');
$creation.find('.cp-creation-password:not(.active)').addClass('active');
$creation.find('#cp-creation-password-val').focus();
return;
}
$creation.find('.cp-creation-password-picker').removeClass('active');
$creation.find('.cp-creation-password').removeClass('active');
2018-03-13 10:31:08 +00:00
$creation.focus();
});
// Keyboard shortcuts
$creation.find('#cp-creation-expire-val').keydown(function (e) {
if (e.which === 9) {
e.stopPropagation();
}
});
$creation.find('#cp-creation-expire-unit').keydown(function (e) {
if (e.which === 9 && e.shiftKey) {
e.stopPropagation();
}
});
2017-12-07 17:51:50 +00:00
2018-03-13 10:31:08 +00:00
// Initial values
if (!cfg.owned && typeof cfg.owned !== "undefined") {
$creation.find('#cp-creation-owned').prop('checked', false);
2018-03-13 10:31:08 +00:00
}
UIElements.setExpirationValue(cfg.expire, $creation);
2017-12-07 17:51:50 +00:00
// Create the pad
2018-03-14 11:12:24 +00:00
var getFormValues = function () {
2017-12-11 11:19:44 +00:00
// Type of pad
2018-03-13 10:31:08 +00:00
var ownedVal = $('#cp-creation-owned').is(':checked') ? 1 : 0;
2017-12-11 11:19:44 +00:00
// Life time
var expireVal = 0;
2018-03-13 10:31:08 +00:00
if($('#cp-creation-expire').is(':checked')) {
2017-12-11 11:19:44 +00:00
var unit = 0;
switch ($('#cp-creation-expire-unit').val()) {
case "hour" : unit = 3600; break;
case "day" : unit = 3600 * 24; break;
case "month": unit = 3600 * 24 * 30; break;
default: unit = 0;
}
expireVal = ($('#cp-creation-expire-val').val() || 0) * unit;
}
// Password
var passwordVal = $('#cp-creation-password').is(':checked') ?
$('#cp-creation-password-val').val() : undefined;
2017-12-11 11:19:44 +00:00
2018-03-13 10:31:08 +00:00
var $template = $creation.find('.cp-creation-template-selected');
2018-03-14 11:12:24 +00:00
var templateId = $template.data('id') || undefined;
2019-09-12 15:54:50 +00:00
// Team
var team;
2019-09-24 16:09:07 +00:00
if (teamValue) {
team = privateData.teams[teamValue] || {};
team.id = Number(teamValue);
2019-09-12 15:54:50 +00:00
}
2018-03-13 10:31:08 +00:00
2018-02-28 16:02:35 +00:00
return {
2017-12-11 11:19:44 +00:00
owned: ownedVal,
password: passwordVal,
2017-12-11 11:19:44 +00:00
expire: expireVal,
2019-09-12 15:54:50 +00:00
templateId: templateId,
team: team
2018-02-28 16:02:35 +00:00
};
};
2018-03-13 10:31:08 +00:00
var create = function () {
var val = getFormValues();
2018-02-28 16:02:35 +00:00
2018-03-19 15:17:19 +00:00
common.setAttribute(['general', 'creation', 'owned'], val.owned, function (e) {
if (e) { return void console.error(e); }
});
common.setAttribute(['general', 'creation', 'expire'], val.expire, function (e) {
if (e) { return void console.error(e); }
});
2017-12-07 17:51:50 +00:00
2019-03-05 10:17:31 +00:00
if (val.expire) {
Feedback.send('EXPIRING_PAD-'+val.expire);
}
2018-03-13 10:31:08 +00:00
$creationContainer.remove();
common.createPad(val, function () {
2017-12-07 17:51:50 +00:00
cb();
});
};
var $button = $('<button>').text(Messages.creation_create).appendTo($create);
$button.addClass('cp-creation-button-selected');
2017-12-11 11:19:44 +00:00
$button.click(function () {
create();
});
2018-03-02 17:33:43 +00:00
$creation.keydown(function (e) {
if (e.which === 9) {
e.preventDefault();
e.stopPropagation();
2018-03-13 10:31:08 +00:00
next(e.shiftKey);
2018-03-02 17:33:43 +00:00
return;
}
if (e.which === 13) {
2018-03-13 10:31:08 +00:00
$button.click();
2018-03-02 17:33:43 +00:00
return;
}
});
$creation.focus();
2017-12-07 17:51:50 +00:00
};
UIElements.onServerError = function (common, err, toolbar, cb) {
2020-03-11 17:00:46 +00:00
//if (["EDELETED", "EEXPIRED", "ERESTRICTED"].indexOf(err.type) === -1) { return; }
2020-01-09 16:30:15 +00:00
var priv = common.getMetadataMgr().getPrivateData();
var msg = err.type;
if (err.type === 'EEXPIRED') {
msg = Messages.expiredError;
if (err.loaded) {
msg += Messages.errorCopy;
}
2020-03-06 11:55:20 +00:00
if (toolbar && typeof toolbar.deleted === "function") { toolbar.deleted(); }
} else if (err.type === 'EDELETED') {
2020-01-09 16:30:15 +00:00
if (priv.burnAfterReading) { return void cb(); }
msg = Messages.deletedError;
if (err.loaded) {
msg += Messages.errorCopy;
}
2020-03-06 11:55:20 +00:00
if (toolbar && typeof toolbar.deleted === "function") { toolbar.deleted(); }
2020-02-20 11:12:22 +00:00
} else if (err.type === 'ERESTRICTED') {
msg = Messages.restrictedError;
2020-03-06 11:55:20 +00:00
if (toolbar && typeof toolbar.failed === "function") { toolbar.failed(true); }
}
2020-01-13 15:43:44 +00:00
var sframeChan = common.getSframeChannel();
sframeChan.event('EV_SHARE_OPEN', {hidden: true});
2020-02-20 11:12:22 +00:00
UI.errorLoadingScreen(msg, Boolean(err.loaded), Boolean(err.loaded));
(cb || function () {})();
};
UIElements.displayPasswordPrompt = function (common, cfg, isError) {
var error;
if (isError) { error = setHTML(h('p.cp-password-error'), Messages.password_error); }
var info = h('p.cp-password-info', Messages.password_info);
var password = UI.passwordInput({placeholder: Messages.password_placeholder});
var $password = $(password);
var button = h('button', Messages.password_submit);
cfg = cfg || {};
if (cfg.value && !isError) {
$password.find('.cp-password-input').val(cfg.value);
}
var submit = function () {
var value = $password.find('.cp-password-input').val();
UI.addLoadingScreen();
common.getSframeChannel().query('Q_PAD_PASSWORD_VALUE', value, function (err, data) {
if (!data) {
UIElements.displayPasswordPrompt(common, cfg, true);
}
});
};
$password.find('.cp-password-input').on('keydown', function (e) { if (e.which === 13) { submit(); } });
$(button).on('click', function () { submit(); });
var block = h('div#cp-loading-password-prompt', [
error,
info,
h('p.cp-password-form', [
password,
button
])
]);
UI.errorLoadingScreen(block);
$password.find('.cp-password-input').focus();
};
2020-01-09 16:30:15 +00:00
UIElements.displayBurnAfterReadingPage = function (common, cb) {
2020-01-20 15:45:19 +00:00
var info = h('p.cp-password-info', Messages.burnAfterReading_warningAccess);
2020-01-17 13:24:31 +00:00
var button = h('button.primary', Messages.burnAfterReading_proceed);
2020-01-09 16:30:15 +00:00
$(button).on('click', function () {
cb();
});
var block = h('div#cp-loading-burn-after-reading', [
info,
button
]);
UI.errorLoadingScreen(block);
};
UIElements.getBurnAfterReadingWarning = function (common) {
var priv = common.getMetadataMgr().getPrivateData();
if (!priv.burnAfterReading) { return; }
2020-01-17 13:24:31 +00:00
return h('div.alert.alert-danger.cp-burn-after-reading', Messages.burnAfterReading_warningDeleted);
2020-01-09 16:30:15 +00:00
};
2018-09-13 16:47:46 +00:00
var crowdfundingState = false;
2020-02-03 14:14:52 +00:00
UIElements.displayCrowdfunding = function (common, force) {
2018-09-13 16:47:46 +00:00
if (crowdfundingState) { return; }
var priv = common.getMetadataMgr().getPrivateData();
2020-02-03 14:14:52 +00:00
var todo = function () {
crowdfundingState = true;
// Display the popup
var text = Messages.crowdfunding_popup_text;
var yes = h('button.cp-corner-primary', [
h('span.fa.fa-external-link'),
'OpenCollective'
]);
var no = h('button.cp-corner-cancel', Messages.crowdfunding_popup_no);
var actions = h('div', [no, yes]);
var dontShowAgain = function () {
common.setAttribute(['general', 'crowdfunding'], false);
Feedback.send('CROWDFUNDING_NEVER');
};
var modal = UI.cornerPopup(text, actions, '', {
2020-02-03 14:14:52 +00:00
big: true,
alt: true,
dontShowAgain: dontShowAgain
});
$(yes).click(function () {
modal.delete();
common.openURL(priv.accounts.donateURL);
Feedback.send('CROWDFUNDING_YES');
});
$(modal.popup).find('a').click(function (e) {
e.stopPropagation();
e.preventDefault();
modal.delete();
common.openURL(priv.accounts.donateURL);
Feedback.send('CROWDFUNDING_LINK');
});
$(no).click(function () {
modal.delete();
Feedback.send('CROWDFUNDING_NO');
});
};
if (force) {
crowdfundingState = true;
return void todo();
}
if (AppConfig.disableCrowdfundingMessages) { return; }
if (priv.plan) { return; }
2018-09-13 16:47:46 +00:00
crowdfundingState = true;
2020-02-03 14:14:52 +00:00
common.getAttribute(['general', 'crowdfunding'], function (err, val) {
if (err || val === false) { return; }
common.getSframeChannel().query('Q_GET_PINNED_USAGE', null, function (err, obj) {
var quotaMb = obj.quota / (1024 * 1024);
if (quotaMb < 10) { return; }
todo();
2018-09-13 16:47:46 +00:00
});
2020-02-03 14:14:52 +00:00
});
2018-09-13 16:47:46 +00:00
};
2018-08-27 12:58:09 +00:00
var storePopupState = false;
var autoStoreModal = {};
UIElements.displayStorePadPopup = function (common, data) {
2018-08-27 12:58:09 +00:00
if (storePopupState) { return; }
storePopupState = true;
if (data && data.stored) { return; } // We won't display the popup for dropped files
var priv = common.getMetadataMgr().getPrivateData();
2018-08-27 12:58:09 +00:00
2020-01-09 16:30:15 +00:00
// This pad will be deleted automatically, it shouldn't be stored
if (priv.burnAfterReading) { return; }
var typeMsg = priv.pathname.indexOf('/file/') !== -1 ? Messages.autostore_file :
priv.pathname.indexOf('/drive/') !== -1 ? Messages.autostore_sf :
Messages.autostore_pad;
var text = Messages._getKey('autostore_notstored', [typeMsg]);
var footer = Messages.autostore_settings;
2018-08-27 12:58:09 +00:00
var hide = h('button.cp-corner-cancel', Messages.autostore_hide);
var store = h('button.cp-corner-primary', Messages.autostore_store);
2020-02-03 14:14:52 +00:00
var actions = h('div', [hide, store]);
2018-08-27 12:58:09 +00:00
var initialHide = data && data.autoStore && data.autoStore === -1;
var modal = UI.cornerPopup(text, actions, footer, {hidden: initialHide});
// Once the store pad popup is created, put the crowdfunding one in the queue
UIElements.displayCrowdfunding(common);
autoStoreModal[priv.channel] = modal;
$(modal.popup).find('.cp-corner-footer a').click(function (e) {
e.preventDefault();
common.openURL('/settings/');
});
2018-08-27 12:58:09 +00:00
$(hide).click(function () {
delete autoStoreModal[priv.channel];
2018-08-27 12:58:09 +00:00
modal.delete();
});
2019-08-08 15:21:00 +00:00
var waitingForStoringCb = false;
2018-08-27 12:58:09 +00:00
$(store).click(function () {
if (waitingForStoringCb) { return; }
waitingForStoringCb = true;
2018-08-27 12:58:09 +00:00
common.getSframeChannel().query("Q_AUTOSTORE_STORE", null, function (err, obj) {
waitingForStoringCb = false;
var error = err || (obj && obj.error);
if (error) {
if (error === 'E_OVER_LIMIT') {
return void UI.warn(Messages.pinLimitReached);
}
return void UI.warn(Messages.autostore_error);
2018-08-27 12:58:09 +00:00
}
$(document).trigger('cpPadStored');
delete autoStoreModal[priv.channel];
modal.delete();
UI.log(Messages.autostore_saved);
2018-08-27 12:58:09 +00:00
});
});
};
2019-05-21 16:43:11 +00:00
UIElements.displayFriendRequestModal = function (common, data) {
var msg = data.content.msg;
2020-02-05 12:33:32 +00:00
var userData = msg.content.user;
var text = Messages._getKey('contacts_request', [Util.fixHTML(userData.displayName)]);
2019-05-22 16:03:52 +00:00
var todo = function (yes) {
2019-05-21 16:43:11 +00:00
common.getSframeChannel().query("Q_ANSWER_FRIEND_REQUEST", {
data: data,
value: yes
}, function (err, obj) {
var error = err || (obj && obj.error);
if (error) {
return void UI.warn(error);
}
if (yes) {
UI.log(Messages.contacts_added);
} else {
UI.log(Messages.contacts_rejected);
}
2019-05-21 16:43:11 +00:00
});
2019-05-22 16:03:52 +00:00
};
var content = h('div.cp-share-modal', [
setHTML(h('p'), text),
2019-05-22 16:03:52 +00:00
]);
2019-09-20 13:27:20 +00:00
UI.proposal(content, todo);
2019-05-21 16:43:11 +00:00
};
UIElements.displayAddOwnerModal = function (common, data) {
var priv = common.getMetadataMgr().getPrivateData();
var sframeChan = common.getSframeChannel();
var msg = data.content.msg;
2019-08-30 15:36:27 +00:00
var name = Util.fixHTML(msg.content.user.displayName) || Messages.anonymous;
var title = Util.fixHTML(msg.content.title);
var text = Messages._getKey('owner_add', [name, title]);
var link = h('a', {
href: '#'
}, Messages.requestEdit_viewPad);
$(link).click(function (e) {
e.preventDefault();
e.stopPropagation();
2019-08-30 15:36:27 +00:00
if (msg.content.password) {
common.sessionStorage.put('newPadPassword', msg.content.password, function () {
common.openURL(msg.content.href);
});
return;
}
common.openURL(msg.content.href);
});
var div = h('div', [
UI.setHTML(h('p'), text),
link
]);
2019-08-30 15:36:27 +00:00
var answer = function (yes) {
common.mailbox.sendTo("ADD_OWNER_ANSWER", {
channel: msg.content.channel,
href: msg.content.href,
password: msg.content.password,
title: msg.content.title,
2020-02-05 12:33:32 +00:00
answer: yes
2019-08-30 15:36:27 +00:00
}, {
channel: msg.content.user.notifications,
curvePublic: msg.content.user.curvePublic
});
common.mailbox.dismiss(data, function (err) {
console.log(err);
});
};
var todo = function (yes) {
if (yes) {
2019-08-30 15:36:27 +00:00
// ACCEPT
sframeChan.query('Q_SET_PAD_METADATA', {
2019-08-30 15:36:27 +00:00
channel: msg.content.channel,
command: 'ADD_OWNERS',
value: [priv.edPublic]
}, function (err, res) {
err = err || (res && res.error);
if (err) {
2019-09-03 13:28:23 +00:00
var text = err === "INSUFFICIENT_PERMISSIONS" ? Messages.fm_forbidden
: Messages.error;
return void UI.warn(text);
}
UI.log(Messages.saved);
2019-08-30 15:36:27 +00:00
// Send notification to the sender
answer(true);
var data = JSON.parse(JSON.stringify(msg.content));
data.metadata = res;
// Add the pad to your drive
2019-09-03 13:11:23 +00:00
// This command will also add your mailbox to the metadata log
// The callback is called when the pad is stored, independantly of the metadata command
sframeChan.query('Q_ACCEPT_OWNERSHIP', data, function (err, res) {
if (err || (res && res.error)) {
return void console.error(err | res.error);
}
UI.log(Messages.saved);
if (autoStoreModal[data.channel]) {
autoStoreModal[data.channel].delete();
delete autoStoreModal[data.channel];
}
});
2019-08-30 15:36:27 +00:00
// Remove yourself from the pending owners
sframeChan.query('Q_SET_PAD_METADATA', {
channel: msg.content.channel,
command: 'RM_PENDING_OWNERS',
value: [priv.edPublic]
}, function (err, res) {
err = err || (res && res.error);
if (err) {
console.error(err);
}
});
});
return;
}
2019-08-30 15:36:27 +00:00
// DECLINE
// Remove yourself from the pending owners
sframeChan.query('Q_SET_PAD_METADATA', {
channel: msg.content.channel,
command: 'RM_PENDING_OWNERS',
value: [priv.edPublic]
}, function (err, res) {
err = err || (res && res.error);
if (err) {
console.error(err);
}
// Send notification to the sender
answer(false);
});
};
2019-09-20 13:27:20 +00:00
UI.proposal(div, todo);
};
2019-09-30 13:17:26 +00:00
UIElements.displayAddTeamOwnerModal = function (common, data) {
var priv = common.getMetadataMgr().getPrivateData();
var sframeChan = common.getSframeChannel();
var msg = data.content.msg;
var name = Util.fixHTML(msg.content.user.displayName) || Messages.anonymous;
var title = Util.fixHTML(msg.content.title);
2019-09-30 14:39:47 +00:00
var text = Messages._getKey('owner_team_add', [name, title]);
2019-09-30 13:17:26 +00:00
var div = h('div', [
UI.setHTML(h('p'), text),
]);
var answer = function (yes) {
common.mailbox.sendTo("ADD_OWNER_ANSWER", {
teamChannel: msg.content.teamChannel,
title: msg.content.title,
2020-02-05 12:33:32 +00:00
answer: yes
2019-09-30 13:17:26 +00:00
}, {
channel: msg.content.user.notifications,
curvePublic: msg.content.user.curvePublic
});
common.mailbox.dismiss(data, function (err) {
if (err) { console.log(err); }
});
};
var module = common.makeUniversal('team');
var addOwner = function (chan, waitFor, cb) {
// Remove yourself from the pending owners
sframeChan.query('Q_SET_PAD_METADATA', {
channel: chan,
command: 'ADD_OWNERS',
value: [priv.edPublic]
}, function (err, res) {
err = err || (res && res.error);
if (!err) { return; }
waitFor.abort();
cb(err);
});
};
var removePending = function (chan, waitFor, cb) {
// Remove yourself from the pending owners
sframeChan.query('Q_SET_PAD_METADATA', {
channel: chan,
command: 'RM_PENDING_OWNERS',
value: [priv.edPublic]
}, waitFor(function (err, res) {
err = err || (res && res.error);
if (!err) { return; }
waitFor.abort();
cb(err);
}));
};
var changeAll = function (add, _cb) {
var f = add ? addOwner : removePending;
var cb = Util.once(_cb);
NThen(function (waitFor) {
f(msg.content.teamChannel, waitFor, cb);
f(msg.content.chatChannel, waitFor, cb);
f(msg.content.rosterChannel, waitFor, cb);
}).nThen(function () { cb(); });
};
var todo = function (yes) {
if (yes) {
// ACCEPT
changeAll(true, function (err) {
if (err) {
console.error(err);
var text = err === "INSUFFICIENT_PERMISSIONS" ? Messages.fm_forbidden
: Messages.error;
return void UI.warn(text);
}
UI.log(Messages.saved);
// Send notification to the sender
answer(true);
// Mark ourselves as "owner" in our local team data
module.execCommand("ANSWER_OWNERSHIP", {
teamChannel: msg.content.teamChannel,
answer: true
}, function (obj) {
if (obj && obj.error) { console.error(obj.error); }
});
// Remove yourself from the pending owners
changeAll(false, function (err) {
if (err) { console.error(err); }
});
});
return;
}
// DECLINE
// Remove yourself from the pending owners
changeAll(false, function (err) {
if (err) { console.error(err); }
// Send notification to the sender
answer(false);
// Set our role back to ADMIN
module.execCommand("ANSWER_OWNERSHIP", {
teamChannel: msg.content.teamChannel,
answer: false
}, function (obj) {
if (obj && obj.error) { console.error(obj.error); }
});
});
};
UI.proposal(div, todo);
};
2019-09-20 13:27:20 +00:00
UIElements.getVerifiedFriend = function (common, curve, name) {
var priv = common.getMetadataMgr().getPrivateData();
var verified = h('p');
var $verified = $(verified);
if (priv.friends && priv.friends[curve]) {
$verified.addClass('cp-notifications-requestedit-verified');
var f = priv.friends[curve];
$verified.append(h('span.fa.fa-certificate'));
var $avatar = $(h('span.cp-avatar')).appendTo($verified);
$verified.append(h('p', Messages._getKey('isContact', [f.displayName])));
2019-09-20 13:27:20 +00:00
common.displayAvatar($avatar, f.avatar, f.displayName);
} else {
$verified.append(Messages._getKey('isNotContact', [name]));
2019-09-20 13:27:20 +00:00
}
return verified;
};
UIElements.displayInviteTeamModal = function (common, data) {
var msg = data.content.msg;
var name = Util.fixHTML(msg.content.user.displayName) || Messages.anonymous;
var teamName = Util.fixHTML(Util.find(msg, ['content', 'team', 'metadata', 'name']) || '');
var verified = UIElements.getVerifiedFriend(common, msg.author, name);
var text = Messages._getKey('team_invitedToTeam', [name, teamName]);
2019-09-20 13:27:20 +00:00
var div = h('div', [
UI.setHTML(h('p'), text),
verified
]);
var module = common.makeUniversal('team');
var answer = function (yes) {
common.mailbox.sendTo("INVITE_TO_TEAM_ANSWER", {
answer: yes,
teamChannel: msg.content.team.channel,
2020-02-05 12:33:32 +00:00
teamName: teamName
2019-09-20 13:27:20 +00:00
}, {
channel: msg.content.user.notifications,
curvePublic: msg.content.user.curvePublic
});
common.mailbox.dismiss(data, function (err) {
console.log(err);
});
};
2019-09-24 14:55:05 +00:00
var MAX_TEAMS_SLOTS = Constants.MAX_TEAMS_SLOTS;
2019-09-20 13:27:20 +00:00
var todo = function (yes) {
2019-09-24 14:55:05 +00:00
var priv = common.getMetadataMgr().getPrivateData();
var numberOfTeams = Object.keys(priv.teams || {}).length;
2019-09-20 13:27:20 +00:00
if (yes) {
2019-09-24 14:55:05 +00:00
if (numberOfTeams >= MAX_TEAMS_SLOTS) {
return void UI.alert(Messages._getKey('team_maxTeams', [MAX_TEAMS_SLOTS]));
}
2019-09-20 13:27:20 +00:00
// ACCEPT
module.execCommand('JOIN_TEAM', {
team: msg.content.team
}, function (obj) {
2019-10-08 09:10:03 +00:00
if (obj && obj.error) {
if (obj.error === 'ENOENT') {
common.mailbox.dismiss(data, function () {});
return void UI.alert(Messages.deletedError);
}
return void UI.warn(Messages.error);
}
2019-09-20 13:27:20 +00:00
answer(true);
2019-10-08 09:10:03 +00:00
if (priv.app !== 'teams') { common.openURL('/teams/'); }
2019-09-20 13:27:20 +00:00
});
return;
}
// DECLINE
answer(false);
};
UI.proposal(div, todo);
};
2020-04-28 13:54:12 +00:00
var insertTextAtCursor = function (text) {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
range.deleteContents();
var node = document.createTextNode(text);
range.insertNode(node);
2020-04-28 16:16:21 +00:00
for (var position = 0; position !== text.length; position++) {
2020-04-28 13:54:12 +00:00
selection.modify("move", "right", "character");
2020-04-28 16:16:21 +00:00
}
};
2020-04-28 13:54:12 +00:00
var getSource = {};
getSource['contacts'] = function (common, sources) {
var priv = common.getMetadataMgr().getPrivateData();
Object.keys(priv.friends || {}).forEach(function (key) {
if (key === 'me') { return; }
var f = priv.friends[key];
if (!f.curvePublic || sources[f.curvePublic]) { return; }
sources[f.curvePublic] = {
avatar: f.avatar,
name: f.displayName,
curvePublic: f.curvePublic,
profile: f.profile,
notifications: f.notifications
};
});
};
UIElements.addMentions = function (common, options) {
if (!options.$input) { return; }
var $t = options.$input;
var getValue = function () { return $t.val(); };
var setValue = function (val) { $t.val(val); };
var div = false;
if (options.contenteditable) {
div = true;
getValue = function () { return $t.html(); };
setValue = function () {}; // Not used, we insert data at the node level
$t.on('paste', function (e) {
try {
insertTextAtCursor(e.originalEvent.clipboardData.getData('text'));
e.preventDefault();
2020-04-28 16:16:21 +00:00
} catch (err) { console.error(err); }
2020-04-28 13:54:12 +00:00
});
// Fix backspace with "contenteditable false" children
$t.on('keydown', function (e) {
if (e.which !== 8 && e.which !== 46) { return; } // Backspace or del
var sel = document.getSelection();
if (sel.anchorNode.nodeType !== Node.TEXT_NODE) { return; } // text nodes only
// Only fix node located after mentions
var n = sel.anchorNode;
var prev = n && n.previousSibling;
// Check if our caret is just after a mention
if (!prev || !prev.classList || !prev.classList.contains('cp-mentions')) { return; }
// Del: if we're at offset 0, make sure we won't delete the text node
if (e.which === 46) {
if (!sel.anchorOffset && sel.anchorNode.length === 1) {
sel.anchorNode.nodeValue = " ";
e.preventDefault();
}
return;
}
// Backspace
// If we're not at offset 0, make sure we won't delete the text node
if (e.which === 8 && sel.anchorOffset) {
if (sel.anchorNode.length === 1) {
sel.anchorNode.nodeValue = " ";
e.preventDefault();
}
return;
}
// If we're at offset 0, We're just after a mention: delete it
prev.parentElement.removeChild(prev);
e.preventDefault();
});
}
// Add the sources
// NOTE: Sources must have a "name". They can have an "avatar".
var sources = options.sources || {};
if (!getSource[options.type]) { return; }
getSource[options.type](common, sources);
// Sort autocomplete result by label
var sort = function (a, b) {
var _a = a.label.toLowerCase();
var _b = b.label.toLowerCase();
if (_a.label < _b.label) { return -1; }
if (_b.label < _a.label) { return 1; }
return 0;
};
// Get the text between the last @ before the cursor and the cursor
var extractLast = function (term, offset) {
2020-04-28 16:16:21 +00:00
offset = typeof(offset) !== "undefined" ? offset : $t[0].selectionStart;
2020-04-28 13:54:12 +00:00
var startOffset = term.slice(0,offset).lastIndexOf('@');
return term.slice(startOffset+1, offset);
};
// Insert the autocomplete value in the input field
var insertValue = function (value, offset, content) {
2020-04-28 16:16:21 +00:00
offset = typeof(offset) !== "undefined" ? offset : $t[0].selectionStart;
content = content || getValue();
2020-04-28 13:54:12 +00:00
var startOffset = content.slice(0,offset).lastIndexOf('@');
var length = offset - startOffset;
if (length <= 0) { return; }
var result = content.slice(0,startOffset) + value + content.slice(offset);
if (content) {
return {
result: result,
startOffset: startOffset
};
}
setValue(result);
};
// Set the value to receive from the autocomplete
var toInsert = function (data, key) {
var name = data.name.replace(/[^a-zA-Z0-9]+/g, "-");
return "[@"+name+"|"+key+"]";
};
// Fix the functions when suing a contenteditable div
if (div) {
var _extractLast = extractLast;
// Use getSelection to get the cursor position in contenteditable
extractLast = function () {
var sel = document.getSelection();
if (sel.anchorNode.nodeType !== Node.TEXT_NODE) { return; }
return _extractLast(sel.anchorNode.nodeValue, sel.anchorOffset);
};
var _insertValue = insertValue;
insertValue = function (value) {
// Get the selected node
var sel = document.getSelection();
if (sel.anchorNode.nodeType !== Node.TEXT_NODE) { return; }
var node = sel.anchorNode;
// Remove the "term"
var insert =_insertValue("", sel.anchorOffset, node.nodeValue);
if (insert) {
node.nodeValue = insert.result;
}
var breakAt = insert ? insert.startOffset : sel.anchorOffset;
var el;
if (typeof(value) === "string") { el = document.createTextNode(value); }
else { el = value; }
node.parentNode.insertBefore(el, node.splitText(breakAt));
var next = el.nextSibling;
if (!next) {
next = document.createTextNode(" ");
el.parentNode.appendChild(next);
} else if (next.nodeType === Node.TEXT_NODE && !next.nodeValue) {
next.nodeValue = " ";
}
var range = document.createRange();
range.setStart(next, 0);
range.setEnd(next, 0);
2020-04-28 16:16:21 +00:00
var newSel = window.getSelection();
newSel.removeAllRanges();
newSel.addRange(range);
2020-04-28 13:54:12 +00:00
};
// Inserting contacts into contenteditable: use mention UI
if (options.type === "contacts") {
2020-04-28 16:16:21 +00:00
toInsert = function (data) {
2020-04-28 13:54:12 +00:00
var avatar = h('span.cp-avatar', {
contenteditable: false
});
common.displayAvatar($(avatar), data.avatar, data.name);
return h('span.cp-mentions', {
'data-curve': data.curvePublic,
'data-notifications': data.notifications,
'data-profile': data.profile,
'data-name': Util.fixHTML(data.name),
'data-avatar': data.avatar || "",
}, [
avatar,
h('span.cp-mentions-name', {
contenteditable: false
}, data.name)
]);
};
}
}
// don't navigate away from the field on tab when selecting an item
$t.on("keydown", function(e) {
// Tab or enter
if ((e.which === 13 || e.which === 9)) {
try {
var visible = $t.autocomplete("instance").menu.activeMenu.is(':visible');
if (visible) {
e.preventDefault();
e.stopPropagation();
}
2020-04-29 14:45:42 +00:00
} catch (err) { console.error(err, $t); }
2020-04-28 13:54:12 +00:00
}
}).autocomplete({
minLength: 0,
source: function(data, cb) {
var term = data.term;
var results = [];
if (term.indexOf("@") >= 0) {
term = extractLast(data.term) || '';
results = Object.keys(sources).filter(function (key) {
var data = sources[key];
return data.name.toLowerCase().indexOf(term.toLowerCase()) !== -1;
}).map(function (key) {
var data = sources[key];
return {
label: data.name,
value: key
};
});
results.sort(sort);
}
cb(results);
// Set max-height to the autocomplete dropdown
try {
var max = window.innerHeight;
var pos = $t[0].getBoundingClientRect();
var menu = $t.autocomplete("instance").menu.activeMenu;
menu.css({
'overflow-y': 'auto',
'max-height': (max-pos.bottom)+'px'
});
} catch (e) {}
2020-04-28 13:54:12 +00:00
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
// add the selected item
var key = ui.item.value;
var data = sources[key];
var value = toInsert(data, key);
insertValue(value);
return false;
}
}).autocomplete( "instance" )._renderItem = function( ul, item ) {
var key = item.value;
var obj = sources[key];
if (!obj) { return; }
var avatar = h('span.cp-avatar');
common.displayAvatar($(avatar), obj.avatar, obj.name);
var li = h('li.cp-autocomplete-value', [
avatar,
h('span', obj.name)
]);
return $(li).appendTo(ul);
};
};
return UIElements;
2017-08-18 16:43:04 +00:00
});