Account portal, download page, non-standard module docs

This ends v1 on the website. Docs archive still available through a
sidebar nav link in the docs.
This commit is contained in:
Matthew Holt 2020-07-16 15:51:46 -06:00
parent 49ed10d267
commit 4f6d355a97
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
37 changed files with 1678 additions and 56 deletions

View file

@ -0,0 +1,63 @@
if (!loggedIn()
&& window.location.pathname != '/account/login'
&& window.location.pathname != '/account/create'
&& window.location.pathname != '/account/verify'
&& window.location.pathname != '/account/reset-password') {
window.location = '/account/login?redir='+encodeURIComponent(window.location);
}
$(function() {
// highlight current page in left nav
var $currentPageLink = $('.container > nav a[href="'+window.location.pathname+'"]');
$currentPageLink.addClass('current');
// shortcut any logout links to make the POST directly
$('a[href="/account/logout"]').click(function() {
logout();
return false;
});
});
function loggedIn() {
return document.cookie.indexOf('user=') > -1;
}
function logout() {
$.post('/api/logout').done(function() {
window.location = '/';
}).fail(function(jqxhr, status, error) {
document.cookie = 'user=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
swal({
icon: "error",
title: error,
content: errorContent(jqxhr)
}).then(function() {
window.location = '/account/'
});
});
}
function errorContent(jqxhr) {
var div = document.createElement('div');
var p1 = document.createElement('p');
p1.appendChild(document.createTextNode("Sorry, something went wrong:"));
div.appendChild(p1);
var p2 = document.createElement('p');
var p2b = document.createElement('b');
p2b.appendChild(document.createTextNode(jqxhr.responseJSON ? jqxhr.responseJSON.error.message : jqxhr.status + " " + jqxhr.statusText));
p2.appendChild(p2b)
div.appendChild(p2);
if (jqxhr.responseJSON) {
var p3 = document.createElement('p');
p3.appendChild(document.createTextNode("Please include this error ID if reporting:"));
p3.appendChild(document.createElement('br'));
p3.appendChild(document.createTextNode(jqxhr.responseJSON.error.id));
div.appendChild(p3);
}
return div;
}

View file

@ -0,0 +1,28 @@
if (loggedIn()) window.location = '/account/';
$(function() {
$('form input').first().focus();
$('form').submit(function(event) {
$('#submit').prop('disabled', true);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
swal({
icon: "success",
title: "Check your email",
text: "We've sent you an email with a link that expires in 48 hours. Please verify your account before you can use it."
}).then(function() {
window.location = '/account/verify';
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
titleText: "Error",
content: errorContent(jqxhr)
});
$('#submit').prop('disabled', false);
});
return false;
});
});

View file

@ -0,0 +1,125 @@
// download package list as soon as possible
$.get("/api/user-packages").done(function(json) {
var packageList = json.result;
// wait until the DOM has finished loading before rendering the results
$(function() {
// trying out this fancy new syntax:
// https://twitter.com/joshmanders/status/1282395540970496001
packageList.forEach(pkg => {
var $tdPath = $('<td><input type="text" name="path" maxlength="255"></td>');
var $tdListed = $('<td class="text-center"><input type="checkbox" name="listed"></td>');
var $tdAvail = $('<td class="text-center"><input type="checkbox" name="available"></td>');
var $tdDownloads = $('<td>');
var $tdLinks = $('<td><a href="javascript:" class="rescan-package">Rescan</a> &nbsp; <a href="javascript:" class="delete-package">Delete</a></td>');
if (pkg.listed) {
$('input', $tdListed).prop('checked', true);
}
if (pkg.available) {
$('input', $tdAvail).prop('checked', true);
}
var $pathInput = $('input', $tdPath);
$pathInput.val(pkg.path).attr('size', pkg.path.length);
var $tr = $('<tr data-package-id="'+pkg.id+'"></tr>');
$tr.append($tdPath)
.append($tdListed)
.append($tdAvail)
.append($tdDownloads)
.append($tdLinks);
$('#user-packages').append($tr);
// scroll package paths to the left so if they get
// cut off, the leaf package name is still visible
$pathInput.scrollLeft($pathInput.width());
});
});
});
$(function() {
// update packages when fields change
$('#user-packages').on('change', 'input', function() {
$tr = $(this).closest('tr');
$('input', $tr).prop('disabled', true);
$.post('/api/update-package', {
id: $tr.data('package-id'),
listed: $('[name=listed]', $tr).prop('checked') ? 1 : 0,
available: $('[name=available]', $tr).prop('checked') ? 1 : 0,
path: $('[name=path]', $tr).val()
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: "Could not save changes",
content: errorContent(jqxhr)
});
}).always(function() {
$('input', $tr).prop('disabled', false);
});
});
// rescan package
$('#user-packages').on('click', '.rescan-package', function() {
if ($(this).hasClass('disabled')) return;
$tr = $(this).closest('tr');
$('a', $tr).addClass('disabled');
$.post('/api/rescan-package', {
package_id: $tr.data('package-id')
}).done(function(jqxhr, status, error) {
swal({
icon: "success",
title: "Rescan Complete",
text: "Package has been re-scanned and its documentation has been updated."
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: "Rescan failed",
content: errorContent(jqxhr)
});
}).always(function() {
$('a', $tr).removeClass('disabled');
});
});
// delete package
$('#user-packages').on('click', '.delete-package', function() {
if ($(this).hasClass('disabled')) return;
swal({
title: "Delete package?",
text: "Deleting the package will remove it from our website.",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((willDelete) => {
// abort if user cancelled
if (!willDelete) return;
$tr = $(this).closest('tr');
$('input', $tr).prop('disabled', true);
$.post('/api/delete-package', {
id: $tr.data('package-id')
}).done(function(jqxhr, status, error) {
$tr.remove();
swal({
icon: "success",
title: "Package deleted"
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: "Delete failed",
content: errorContent(jqxhr)
});
}).always(function() {
$('input', $tr).prop('disabled', false);
});
});
});
});

View file

@ -0,0 +1,24 @@
if (loggedIn()) window.location = '/account/';
$(function() {
$('form input').first().focus();
$('form').submit(function(event) {
$('#submit').prop('disabled', true);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
var qsParams = new URLSearchParams(window.location.search);
var destination = qsParams.get('redir');
window.location = destination ? destination : '/account/';
}).fail(function(jqxhr, msg, error) {
swal({
icon: "error",
title: "Bad credentials",
content: errorContent(jqxhr)
});
$('#submit').prop('disabled', false);
});
return false;
});
});

View file

@ -0,0 +1 @@
logout();

View file

@ -0,0 +1,27 @@
$(function() {
$('form input').first().focus();
$('form').submit(function(event) {
$('#submit').prop('disabled', true);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
swal({
icon: "success",
title: "It's yours",
text: "Package claimed. Its documentation is now available on our website and you are responsible for maintaining it. Thank you!"
}).then(function() {
// TODO: ...
// window.location = "/account/login";
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: error,
content: errorContent(jqxhr)
});
$('#submit').prop('disabled', false);
});
return false;
});
});

View file

@ -0,0 +1,80 @@
if (loggedIn()) window.location = '/account/';
$(function() {
var qsParams = new URLSearchParams(window.location.search);
var email = qsParams.get("email");
var token = qsParams.get("token");
$('input[name=email]').val(email);
$('input[name=token]').val(token);
if (email && token) showStep2();
$('form input:visible').first().focus();
$('#reset-password-step1').submit(function(event) {
$('button').prop('disabled', false);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
swal({
icon: "info",
title: "Check your email",
text: "If we have an account with that email address, we just sent you some instructions."
}).then(function() {
window.location = '/';
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: error,
content: errorContent(jqxhr)
});
$('button').prop('disabled', false);
});
return false;
});
$('#reset-password-step2').submit(function(event) {
$('button').prop('disabled', false);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
swal({
icon: "success",
title: "Reset completed",
text: "You may now log in with your new password."
}).then(function() {
window.location = '/account/login';
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: "Error",
content: errorContent(jqxhr)
});
$('button').prop('disabled', false);
});
return false;
});
$('#goto-step1').click(function(event) {
$('#reset-password-step2').hide('fast');
$('#reset-password-step1').show('fast', function() {
$('input:visible').first().focus();
});
return false;
});
$('#goto-step2').click(function(event) {
showStep2();
return false;
});
});
function showStep2() {
$('#reset-password-step1').hide('fast');
$('#reset-password-step2').show('fast', function() {
if ($('input[name=token]').val() != "")
$('input[name=password]').focus();
else
$('input:visible').first().focus();
});
}

View file

@ -0,0 +1,36 @@
if (loggedIn()) window.location = '/account/';
$(function() {
$('form input').first().focus();
$('form').submit(function(event) {
$('#submit').prop('disabled', true);
$.post($(this).prop("action"), $(this).serialize()).done(function() {
swal({
icon: "success",
title: "Account confirmed",
text: "Thank you. You may now log in and use your account!"
}).then(function() {
window.location = "/account/login";
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: error,
content: errorContent(jqxhr)
});
$('#submit').prop('disabled', false);
});
return false;
});
// if info is in the query string, fill use and submit it
var qsParams = new URLSearchParams(window.location.search);
var email = qsParams.get("email");
var acct = qsParams.get("code");
$('input[name=email]').val(email);
$('input[name=account_id]').val(acct);
if (email && acct) $('form').submit();
});

View file

@ -7,3 +7,27 @@ document.addEventListener('DOMContentLoaded', function() {
debug: false // Set debug to true if you want to inspect the dropdown
});
});
const caddyImportPath = 'github.com/caddyserver/caddy/v2';
function isStandard(packagePath) {
return packagePath.startsWith(caddyImportPath);
}
function substrBeforeLastDot(s) {
return s.substr(0, s.lastIndexOf('.'))
}
function substrAfterLastDot(s) {
return s.substr(s.lastIndexOf('.'))
}
function truncate(str, len) {
if (!str) return "";
var startLen = str.length;
str = str.substring(0, len);
if (startLen > len) {
str += "...";
}
return str;
}

View file

@ -4,6 +4,8 @@ var pageData = {}, pageDocs = {};
var $renderbox, $hovercard;
const nonStandardFlag = '<span class="nonstandard-flag" title="This module does not come with official Caddy distributions by default; it needs to be added to custom Caddy builds.">Non-standard</span>';
$(function() {
$renderbox = $('#renderbox');
$hovercard = $('#hovercard');
@ -61,7 +63,12 @@ $(function() {
for (var i = 0; i < pageData.namespaces[modNamespace].length; i++) {
var modInfo = pageData.namespaces[modNamespace][i];
var href = canTraverse() ? '.'+elemPath+'/'+modInfo.name+'/' : './'+modNamespace+'.'+modInfo.name;
$list.append('<a href="'+href+'" class="module-link">'+modInfo.name+'<span class="module-link-description">'+truncate(modInfo.docs, 115)+'</span></a>');
var content = '<a href="'+href+'" class="module-link"> '+modInfo.name;
if (!isStandard(modInfo.package)) {
content += nonStandardFlag;
}
content += '<span class="module-link-description">'+truncate(modInfo.docs, 115)+'</span></a>';
$list.append(content);
}
}
$('#hovercard-module-list').html($list);
@ -88,7 +95,7 @@ $(function() {
case "struct":
for (var j = 0; j < bcVal.struct_fields.length; j++) {
var sf = bcVal.struct_fields[j];
bcSiblings.push({name: sf.key, path: siblingPath})
bcSiblings.push({name: sf.key, path: siblingPath, isStandard: isStandard(bcVal.type_name)})
}
break;
@ -96,7 +103,7 @@ $(function() {
case "module_map":
for (var j = 0; j < pageData.namespaces[bcVal.module_namespace].length; j++) {
var mod = pageData.namespaces[bcVal.module_namespace][j];
bcSiblings.push({name: mod.name, path: siblingPath})
bcSiblings.push({name: mod.name, path: siblingPath, isStandard: isStandard(mod.package)})
}
}
@ -108,7 +115,12 @@ $(function() {
sibPath += "/";
}
sibPath += sib.name+"/";
$siblings.append('<a href="'+jsonDocsPathPrefix+sibPath+'">'+sib.name+'</a>');
var aTag = '<a href="'+jsonDocsPathPrefix+sibPath+'"';
if (!sib.isStandard) {
aTag += ' class="nonstandard" title="Non-standard module"';
}
aTag += '>'+sib.name+'</a>';
$siblings.append(aTag);
}
$('#hovercard-breadcrumb-siblings').html($siblings).show();
@ -147,6 +159,14 @@ function beginRendering(json) {
pageData = json;
console.log("DATA:", pageData);
// show notice if module is non-standard
if (!isStandard(pageData.structure.type_name)) {
var projectHref = 'https://'+pageData.structure.type_name;
projectHref = substrBeforeLastDot(projectHref);
$('.nonstandard-project-link').attr('href', projectHref).text(projectHref);
$('.nonstandard-notice').prepend(nonStandardFlag).show();
}
if (pageData.structure.doc) {
// for most types, just render their docs
$('#top-doc').html(markdown(pageData.structure.doc));
@ -308,16 +328,6 @@ function indent(nesting, $group) {
$group.append($span);
}
function truncate(str, len) {
if (!str) return "";
var startLen = str.length;
str = str.substring(0, len);
if (startLen > len) {
str += "...";
}
return str;
}
function makeSubmoduleList(path, value) {
while (value.elems) {
value = value.elems;
@ -330,7 +340,11 @@ function makeSubmoduleList(path, value) {
for (var j = 0; j < pageData.namespaces[value.module_namespace].length; j++) {
var submod = pageData.namespaces[value.module_namespace][j];
var href = canTraverse() ? '.'+path+'/'+submod.name+'/' : './'+value.module_namespace+'.'+submod.name;
submodList += '<li><a href="'+href+'">'+submod.name+'</li>';
var submodLink = '<a href="'+href+'">'+submod.name+'</a>';
if (!isStandard(submod.package)) {
submodLink += ' '+nonStandardFlag;
}
submodList += '<li>'+submodLink+'</li>';
}
}
submodList += '</ul>';

View file

@ -0,0 +1,221 @@
// download package list as soon as possible
$.get("/api/packages").done(function(json) {
var packageList = json.result;
// wait until the DOM has finished loading before rendering the results
$(function() {
const optpkgTemplate =
'<tr class="optpkg">'+
' <td><label><input type="checkbox"><span class="optpkg-name"></span></label></td>'+
' <td class="text-center"><input type="text" name="version" placeholder="latest" title="Package version" size="5"></td>'+
' <td class="optpkg-modules"></td>'+
'</tr>';
const optpkgModuleTemplate =
'<div class="optpkg-module">'+
' <a class="module-name" title="View docs"></a>'+
' <span class="module-description"></span>'+
'</div>';
for (var i = 0; i < packageList.length; i++) {
var pkg = packageList[i];
if (isStandard(pkg.path)) {
// not necessary to show, since these packages
// come with standard distribution
continue;
}
var $optpkg = $(optpkgTemplate);
$('.optpkg-name', $optpkg).text(pkg.path);
if (pkg.modules && pkg.modules.length > 0) {
for (var j = 0; j < pkg.modules.length; j++) {
var mod = pkg.modules[j];
var $mod = $(optpkgModuleTemplate);
$('.module-name', $mod).attr('href', '/docs/modules/'+mod.name).text(mod.name);
$('.module-description', $mod).text(truncate(mod.docs, 120));
$('.optpkg-modules', $optpkg).append($mod);
}
} else {
$('.optpkg-modules', $optpkg)
.addClass("optpkg-no-modules")
.text('This package does not add any modules. Either it is another kind of plugin (such as a config adapter) or this listing is in error.');
}
$('#optional-packages').append($optpkg);
}
});
}).fail(function(jqxhr, status, error) {
swal({
icon: "error",
title: "Unavailable",
content: $('<div>Sorry, the build server is down for maintenance right now. You can try again later or <a href="https://github.com/caddyserver/caddy/releases/latest">download pre-built Caddy binaries from GitHub</a> any time.</div>')[0]
});
$(function() {
disableFields(false);
});
});
$(function() {
autoPlatform();
downloadButtonHtml = $('#download').html();
// update the page, including the download link, when form fields change
$('#optional-packages').on('change', 'input[type=checkbox]', function() {
$(this).closest('.optpkg').toggleClass('selected');
updatePage();
});
$('#optional-packages').on('change keyup', 'input[name=version]', function() {
updatePage();
});
$('#platform').change(function() {
updatePage();
});
$('#download').click(function(event) {
if ($(this).hasClass('disabled')) {
return false;
}
disableFields(true);
fathom.trackGoal('U9K2UTFV', 0);
$.ajax($(this).attr('href'), { method: "HEAD" }).done(function(data, status, jqxhr) {
window.onbeforeunload = null; // disable exit confirmation before "redirecting" to download
window.location = jqxhr.getResponseHeader("Location");
}).fail(function(jqxhr, status, error) {
handleBuildError(jqxhr, status, error);
}).always(function() {
enableFields();
});
return false;
});
})
// autoPlatform choooses the platform in the list that best
// matches the user's browser, if it's available.
function autoPlatform() {
// assume 32-bit linux, then change OS and architecture if justified
var os = "linux", arch = "386", arm = "";
// change os
if (/Macintosh/i.test(navigator.userAgent)) {
os = "darwin";
} else if (/Windows/i.test(navigator.userAgent)) {
os = "windows";
} else if (/FreeBSD/i.test(navigator.userAgent)) {
os = "freebsd";
} else if (/OpenBSD/i.test(navigator.userAgent)) {
os = "openbsd";
}
// change architecture
if (os == "darwin" || /amd64|x64|x86_64|Win64|WOW64|i686|64-bit/i.test(navigator.userAgent)) {
arch = "amd64";
} else if (/arm64/.test(navigator.userAgent)) {
arch = "arm64";
} else if (/ ARM| armv/.test(navigator.userAgent)) {
arch = "arm";
}
// change arm version
if (arch == "arm") {
arm = "7"; // assume version 7 by default
if (/armv6/.test(navigator.userAgent)) {
arm = "6";
} else if (/armv5/.test(navigator.userAgent)) {
arm = "5";
}
}
var selString = os+"-"+arch;
if (arm != "") {
selString += "-"+arm;
}
$('#platform').val(selString);
updatePage();
}
function getDownloadLink() {
// get platform components
var platformParts = $('#platform').val().split("-");
var os = platformParts[0];
var arch = platformParts[1];
var arm = platformParts.length > 2 ? platformParts[2] : "";
var qs = new URLSearchParams();
if (os) qs.set("os", os);
if (arch) qs.set("arch", arch);
if (arm) qs.set("arm", arm);
// get plugins and their versions
$('#optional-packages .selected').each(function() {
// get package path
var p = $('.optpkg-name', this).text().trim();
// get package version, if user specified one
var ver = $('input[name=version]', this).val().trim();
if (ver) {
p += "@"+ver;
}
qs.append("p", p);
});
var idempotencyKey = Math.floor(Math.random() * 99999999999999);
qs.append("idempotency", idempotencyKey);
return "/api/download?"+qs.toString();
}
function handleBuildError(jqxhr, status, error) {
var $content = $('<div class="swal-custom-content">');
if (jqxhr.status == 502) {
swal({
icon: "error",
title: "Unavailable",
content: $content.html('Sorry, the build server is down for maintenance right now. You can try again later or <a href="https://github.com/caddyserver/caddy/releases/latest">download pre-built Caddy binaries from GitHub</a>.')[0]
});
} else {
swal({
icon: "error",
title: "Build failed",
content: $content.html('The two most common reasons are:<ol><li><b>A plugin is not compiling.</b> The developer must release a new version that compiles.</li><li><b>The build configuration is invalid.</b> If you specified any versions, make sure they are correct and <a href="https://golang.org/cmd/go/#hdr-Module_compatibility_and_semantic_versioning" target="_blank">within the same major version</a> as the path of the associated package.</li></ol>In the meantime, you can <a href="https://github.com/caddyserver/caddy/releases/latest">download Caddy from GitHub</a> without any plugins.')[0]
});
}
}
function updatePage() {
$('#package-count').text($('.optpkg.selected').length);
$('#download').attr('href', getDownloadLink());
}
function disableFields(building) {
$('#download, #optional-packages').addClass('disabled');
$('.download-bar select, #optional-packages input').prop('disabled', true);
if (building) {
$('#download').html('<div class="loader"></div> Building');
// prevent accidentally leaving the page during a build
window.onbeforeunload = function() {
return "Your custom build is in progress.";
};
} else {
$('#download').html('Builds Unavailable');
}
}
function enableFields() {
$('#download, #optional-packages').removeClass('disabled');
$('.download-bar select, #optional-packages input').prop('disabled', false);
$('#download').html(downloadButtonHtml);
// allow user to leave page easily
window.onbeforeunload = null;
}
var downloadButtonHtml; // to restore button to its original contents

View file

@ -9,7 +9,7 @@ setPageTitle();
$.get("/api/docs/config"+configPath, function(json) {
// wait until the DOM has finished loading before rendering the results
$(function() {
beginRendering(json);
beginRendering(json.result);
// establish the breadcrumb
var $bc = $('.breadcrumbs');

View file

@ -9,21 +9,24 @@ if (moduleID) {
$(function() {
$('#module-docs-container').show();
$('h1').text("Module "+moduleID);
beginRendering(json);
beginRendering(json.result);
});
});
} else {
// populate the module list
$.get("/api/modules", function(moduleList) {
$.get("/api/modules", function(json) {
var moduleList = json.result;
// wait until the DOM has finished loading before rendering the results
$(function() {
$('#module-list-container').show();
$table = $('#module-list');
for (modID in moduleList) {
var doc = moduleList[modID];
var val = moduleList[modID];
var standard = isStandard(val.type_name);
var $tr = $('<tr/>');
$tr.append('<td><a href="./'+modID+'" class="module-link">'+modID+'</a></td>');
$tr.append('<td>'+markdown(truncate(doc, 200))+'</td>');
$tr.append('<td><a href="./'+modID+'" class="module-link">'+modID+'</a>'+(standard ? '' : ' '+nonStandardFlag)+'</td>');
$tr.append('<td>'+markdown(truncate(val.doc, 200))+'</td>');
$table.append($tr);
}
});

1
src/resources/js/sweetalert.min.js vendored Normal file

File diff suppressed because one or more lines are too long