From 1f24d7126c7ef94acd224f5a24dd134a4a7c8232 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 28 Jul 2016 17:44:40 +0200 Subject: [PATCH] big ugly commit that will be really hard to audit This changeset applies new styles to the poll. it also uses the new asynchronous wrappers around the localStorage api. this is necessary because we're migrating to a storage system that will use an async api. The changes to the poll just happened to coincide with the async stuff. My apologies to anyone who wants to read this whole thing --- customize.dist/main.js | 58 +++++---- www/code/main.js | 194 +++++++++++++++++++--------- www/common/cryptpad-common.js | 236 +++++++++++++++++++++------------- www/pad/main.js | 75 ++++++++--- www/poll/index.html | 10 +- www/poll/main.js | 168 ++++++++++++++++++++---- www/poll/table.js | 18 ++- 7 files changed, 535 insertions(+), 224 deletions(-) diff --git a/customize.dist/main.js b/customize.dist/main.js index a222aad16..cdb52219a 100644 --- a/customize.dist/main.js +++ b/customize.dist/main.js @@ -28,8 +28,6 @@ define([ var now = new Date(); var hasRecent = false; - var memorySpan = Cryptpad.timeframe; // thirty days - var forgetPad = Cryptpad.forgetPad; var padTypes = { @@ -45,23 +43,17 @@ define([ return title; }; - var recentPads = Cryptpad.getRecentPads(); - recentPads.sort(Cryptpad.mostRecent); - var fixHTML = function (html) { return html.replace(/ memorySpan) { return true; } - hasRecent = true; // split up the uri @@ -92,13 +84,19 @@ define([ }).text('✖').click(function () { Cryptpad.confirm(Messages.forgetPrompt + ' (' + fixHTML(shortTitle) + ')', function (yes) { if (!yes) { return; } - forgetPad(pad.href); - $row.fadeOut(750, function () { - $row.remove(); - if (!$table.find('tr').find('td').length) { - $table.remove(); - $tryit.text(Messages.tryIt); + 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); + } + }); }); }); }); @@ -116,13 +114,23 @@ define([ }); }; - if (recentPads.length) { - recentPads.sort(Cryptpad.mostRecent); - makeRecentPadsTable(); - } - if (hasRecent) { - $('table').attr('style', ''); - $tryit.text(Messages.recentPads); - } + 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', ''); + $tryit.text(Messages.recentPads); + } + //recentPads.sort(Cryptpad.mostRecent); + }); }); diff --git a/www/code/main.js b/www/code/main.js index 63c533ed0..d29ff3b19 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -166,21 +166,30 @@ define([ name: myUserName }; addToUserList(myData); - Cryptpad.setPadAttribute('username', myUserName); - onLocal(); + Cryptpad.setPadAttribute('username', myUserName, function (err, data) { + if (err) { + console.log("Couldn't set username"); + console.error(err); + return; + } + onLocal(); + }); }; - var getLastName = function () { - return Cryptpad.getPadAttribute('username') || ''; + var getLastName = function (cb) { + Cryptpad.getPadAttribute('username', function (err, userName) { + cb(err, userName || ''); + }); }; var createChangeName = function(id, $container) { var buttonElmt = $container.find('#'+id)[0]; - var lastName = getLastName(); - buttonElmt.addEventListener("click", function() { - Cryptpad.prompt(Messages.changeNamePrompt, lastName, function (newName) { - setName(newName); + getLastName(function (err, lastName) { + buttonElmt.addEventListener("click", function() { + Cryptpad.prompt(Messages.changeNamePrompt, lastName, function (newName) { + setName(newName); + }); }); }); }; @@ -228,8 +237,7 @@ define([ if (document.title === hash) { return getHeadingText() || hash; } else { - return document.title || getHeadingText() || - Cryptpad.getPadTitle() || hash; + return document.title || getHeadingText() || hash; } }; @@ -250,7 +258,6 @@ define([ }; var onInit = config.onInit = function (info) { - //Cryptpad.warn("Initializing realtime session..."); var $bar = $('#pad-iframe')[0].contentWindow.$('#cme_toolbox'); toolbarList = info.userList; var config = { @@ -309,12 +316,28 @@ define([ Cryptpad.prompt(Messages.renamePrompt, suggestion, function (title, ev) { if (title === null) { return; } - if (Cryptpad.causesNamingConflict(title)) { - Cryptpad.alert(Messages.renameConflict); - return; - } - Cryptpad.setPadTitle(title); - document.title = title; + + Cryptpad.causesNamingConflict(title, function (err, conflicts) { + if (err) { + console.log("Unable to determine if name caused a conflict"); + console.error(err); + return; + } + + if (conflicts) { + Cryptpad.alert(Messages.renameConflict); + return; + } + + Cryptpad.setPadTitle(title, function (err, data) { + if (err) { + console.log("unable to set pad title"); + console.log(err); + return; + } + document.title = title; + }); + }); }); }); $rightside.append($setTitle); @@ -330,65 +353,104 @@ define([ var href = window.location.href; Cryptpad.confirm(Messages.forgetPrompt, function (yes) { if (!yes) { return; } - Cryptpad.forgetPad(href); - document.title = window.location.hash.slice(1,9); + Cryptpad.forgetPad(href, function (err, data) { + if (err) { + console.log("unable to forget pad"); + console.error(err); + return; + } + document.title = window.location.hash.slice(1,9); + }); }); }); $rightside.append($forgetPad); - var lastLanguage = Cryptpad.getPadAttribute('language') || 'javascript'; + // TODO use cb + var configureLanguage = function (cb) { + // FIXME this is async so make it happen as early as possible + Cryptpad.getPadAttribute('language', function (err, lastLanguage) { + if (err) { + console.log("Unable to get pad language"); + } - /* Let the user select different syntax highlighting modes */ - var syntaxDropdown = ''; + lastLanguage = lastLanguage || 'javascript'; - setMode(lastLanguage); + /* Let the user select different syntax highlighting modes */ + var syntaxDropdown = ''; - /* Remember the user's last choice of theme using localStorage */ - var themeKey = 'CRYPTPAD_CODE_THEME'; - var lastTheme = localStorage.getItem(themeKey) || 'default'; + setMode(lastLanguage); - /* Let the user select different themes */ - var $themeDropdown = $('', { + title: 'color theme', + id: 'display-theme', + }); + Themes.forEach(function (o) { + $themeDropdown.append($('