cryptpad/customize.dist/main.js

156 lines
4.9 KiB
JavaScript
Raw Normal View History

define([
2016-07-12 21:53:58 +08:00
'/customize/messages.js',
'/customize/DecorateToolbar.js',
'/customize/application_config.js',
'/common/cryptpad-common.js',
'/bower_components/lil-uri/uri.min.js',
2016-07-06 21:14:41 +08:00
'/bower_components/jquery/dist/jquery.min.js',
2016-09-29 20:46:32 +08:00
], function (Messages, DecorateToolbar, Config, Cryptpad, LilUri) {
var $ = window.$;
2016-07-11 17:07:59 +08:00
var APP = window.APP = {
Cryptpad: Cryptpad,
};
DecorateToolbar.main($('#bottom-bar'));
Cryptpad.styleAlerts();
var padTypes = {
2016-09-16 00:35:09 +08:00
'/pad/': Messages.type.pad,
'/code/': Messages.type.code,
'/poll/': Messages.type.poll,
'/slide/': Messages.type.slide,
};
var $table = $('table.scroll');
var $tbody = $table.find('tbody');
var $tryit = $('#tryit');
var now = new Date();
var hasRecent = false;
var forgetPad = Cryptpad.forgetPad;
var displayCreateButtons = function () {
Config.availablePadTypes.forEach(function (el) {
$('#create-' + el).show();
});
};
var makeRecentPadsTable = function (recentPads) {
if (!recentPads.length) { return; }
$('tbody tr').each(function (i, e) {
if (!i) { return; }
$(this).remove();
});
recentPads.some(function (pad, index) {
if (!pad) { return; }
hasRecent = true;
// split up the uri
var uri = LilUri(pad.href);
// derive the name
var name = padTypes[uri.path()];
var title = pad.title || uri.parts.hash.slice(0,8);
var shortTitle = Cryptpad.truncate(pad.title, 48);
var date = new Date(pad.atime).toLocaleDateString();
var created = new Date(pad.ctime).toLocaleDateString();
if (date === now.toLocaleDateString()) {
date = new Date(pad.atime).toLocaleTimeString().replace(/ /g, '');
}
var id = 'pad-'+index;
2016-07-04 23:31:52 +08:00
var $row = $('<tr>', {
id: id
});
var $remove = $('<td>', {
'class': 'remove',
2016-09-16 00:35:09 +08:00
title: Messages.forget + " '"+shortTitle + "'"
}).text('✖').click(function () {
Cryptpad.confirm(Messages.forgetPrompt + ' (' + Cryptpad.fixHTML(shortTitle) + ')', function (yes) {
if (!yes) { return; }
forgetPad(pad.href, function (err, data) {
if (err) {
console.log("Unable to forget pad");
console.log(err);
return;
}
$row.fadeOut(750, function () {
$row.remove();
if (!$table.find('tr').find('td').length) {
$table.remove();
$tryit.text(Messages.tryIt);
}
});
});
});
});
var readOnly = false;
if (pad.href.indexOf('#') !== -1) {
var modeArray = pad.href.split('#')[1].split('/');
if (modeArray.length >= 3 && modeArray[2] === 'view') {
readOnly = true;
}
}
var readOnlyText = readOnly ? '(' + Messages.readonly + ') ' : '';
$row
.append($('<td>').text(name))
.append($('<td>').append(readOnlyText).append($('<a>', {
href: pad.href,
title: pad.title,
}).text(shortTitle)))
.append($('<td>').text(created))
.append($('<td>').text(date))
.append($remove);
$tbody.append($row);
});
};
var refreshTable = function () {
Cryptpad.getRecentPads(function (err, recentPads) {
if (err) {
console.log("unable to get recent pads");
console.error(err);
return;
}
if (recentPads.length) {
recentPads.sort(Cryptpad.mostRecent);
makeRecentPadsTable(recentPads);
}
if (hasRecent) {
$('table').attr('style', '');
// Race condition here, this is triggered before the localization in HTML
// so we have to remove the data-localization attr
$tryit.removeAttr('data-localization');
$tryit.text(Messages.recentPads);
}
});
};
displayCreateButtons();
Cryptpad.ready(function () {
console.log("ready");
refreshTable();
if (Cryptpad.store && Cryptpad.store.change) {
Cryptpad.store.change(function (data) {
if (data.key === 'CryptPad_RECENTPADS') {
refreshTable();
}
});
}
});
});