From c292f9f549b3fb9f6293a8c4930b86da89d23ec1 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 4 Jul 2016 15:21:23 +0200 Subject: [PATCH] move home page scripts into an amd module --- customize.dist/main.js | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 customize.dist/main.js diff --git a/customize.dist/main.js b/customize.dist/main.js new file mode 100644 index 000000000..3f326117d --- /dev/null +++ b/customize.dist/main.js @@ -0,0 +1,95 @@ +define([ + '/customize/DecorateToolbar.js', + '/common/cryptpad-common.js', + '/bower_components/lil-uri/uri.min.js', + '/bower_components/jquery/dist/jquery.min.js' +], function (DecorateToolbar, Cryptpad, LilUri) { + var $ = window.$; + DecorateToolbar.main($('#bottom-bar')); + + var $table = $('table.scroll'); + var $tbody = $table.find('tbody'); + var $tryit = $('#tryit'); + var now = new Date(); + var hasRecent = false; + + var memorySpan = Cryptpad.timeframe; // thirty days + + var forgetPad = Cryptpad.forgetPad; + + var padTypes = { + '/pad/': 'Pad', + '/code/': 'Code' + }; + + var truncateTitle = function (title, len) { + if (typeof(title) === 'string' && title.length > len) { + return title.slice(0, len) + '…'; + } + return title; + }; + + var recentPads = Cryptpad.getRecentPads(); + recentPads.sort(Cryptpad.mostRecent); + + var makeRecentPadsTable = function () { + if (!recentPads.length) { return; } + recentPads.some(function (pad, index) { + if (!pad) { return; } + + console.log(pad); + + // don't link to old pads + if (now.getTime() - new Date(pad.atime).getTime() > memorySpan) { return true; } + + 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 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; + $tbody.append('' + + '' + name + '' + + //'' + title + '' + + '' + truncateTitle(pad.title, 48) + '' + + '' + created + '' + // created + '' + date + '' + + '✖'+ + ''); + + var $row = $('#'+id); + $row.find('.remove').click(function () { + forgetPad(pad.href); + $row.fadeOut(750, function () { + $row.remove(); + if (!$table.find('tr').find('td').length) { + $table.remove(); + $tryit.text("Try it out!"); + } + }); + }); + }); + }; + + if (recentPads.length) { + recentPads.sort(Cryptpad.mostRecent); + makeRecentPadsTable(); + } + if (hasRecent) { + $('table').attr('style', ''); + $tryit.text('Your Recent pads (stored only in browser)'); + } +}); +