diff src/admin.js @ 0:8f4df159f06b

start public repo
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 20:57:49 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/admin.js	Fri Jul 11 20:57:49 2025 -0600
@@ -0,0 +1,200 @@
+'use strict';
+
+let currentPulldown = null;
+let newPulldown = null;
+
+function clickMenu(clicked,display) {
+	//console.log("clickMenu");
+	let pulldown = clicked.parentNode.querySelector('[pulldown_menu]');
+	if( pulldown !== currentPulldown ) {
+		pulldown.style.display = display || "block";
+		newPulldown = pulldown;
+		window.onclick = function() {
+			//console.log("window.onclick");
+			if( currentPulldown ) {
+				currentPulldown.style.display = "none";
+				if( !newPulldown )
+					window.onclick = null;
+			}
+			currentPulldown = newPulldown;
+			newPulldown = null;
+		};
+		pulldown.scrollIntoViewIfNeeded(false);
+	}
+}
+
+function stopPropagation(event) {
+	event.stopPropagation();
+}
+
+window.addEventListener( 'load', function() {
+	for( let pulldown of document.querySelectorAll('[pulldown_menu]') ) {
+		pulldown.onclick = stopPropagation;
+	}
+} );
+
+function copyLink() {
+	// avoid the paranoid security nonsense with navigator.clipboard
+	let input = document.querySelector('input[clipboard]');
+	input.select();
+	document.execCommand('copy');
+	input.blur();
+
+	let span = document.querySelector('[pulldown_menu] span[copy]');
+	span.textContent = 'Copied!';
+	span.setAttribute('copy','copied');
+	setTimeout(function(){
+		span.textContent = 'Copy';
+		span.setAttribute('copy','');
+	} ,1000);
+}
+
+function logout() {
+	document.cookie = 'user=; Max-Age=0; path=/;';
+	document.cookie = 'password=; Max-Age=0; path=/;';
+	location = '/';
+}
+
+
+function uploadcareUrl(uuid) {
+	return "https://ucarecdn.com/" + uuid + "/-/quality/smart/";
+}
+
+if( typeof(uploadcare) !== 'undefined' ) {
+	uploadcare.publicKey = window.uploadcarePubKey;
+	uploadcare.imagesOnly = true;
+	uploadcare.maxFileSize = 10000000;
+	uploadcare.onError = function(status,text) {
+		let err = 'upload failed: ' + status;
+		if( text ) {
+			err += '\n' + text;
+		}
+		console.log(err);
+		ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
+	};
+}
+
+
+let dropSelector, iDragging;
+
+function indexOf(a,el) {
+	for( let i=0; i<a.length; i++ ) {
+		if( a[i] === el )
+			return i;
+	}
+	return -1;
+}
+
+if( typeof(dad) !== 'undefined' ) {
+	dad.onStart = function(event) {
+		let dragging = event.original;
+		iDragging = indexOf(dragging.parentNode.querySelectorAll(dropSelector),dragging);
+	}
+
+	dad.onEnter = function(event) {
+		let dropzone = event.dropzone
+		let original = event.original
+		let items = document.querySelectorAll(dropSelector);
+		let iDropzone = indexOf(items,dropzone);
+		let iOriginal = indexOf(items,original);
+		let where = iDropzone < iOriginal ? 'beforebegin' : 'afterend';
+		dropzone.insertAdjacentElement(where,original);
+	};
+}
+
+function date(time) {
+	document.write(new Date(time).toLocaleDateString());
+}
+
+function ajaxForm(url,form) {
+	let post = '';
+	for( let i=0; i<form.length; i++ ) {
+		let input = form[i];
+		let name = input.name;
+		if( name === '' )
+			continue;
+		let type = input.type;
+		if( (type==='radio' || type==='checkbox') && !input.checked )
+			continue;
+		post += name + '=' + encodeURIComponent(input.value) + '&';
+	}
+	ajax(url,post,{form:form});
+}
+
+function clearErrors(form) {
+	let divs = form.querySelectorAll('div[error]');
+	for( let i=0; i<divs.length; i++ ) {
+		divs[i].textContent = '';
+	}
+}
+
+function showError(form,field,message) {
+	clearErrors(form);
+	let err = form.querySelector('[error="'+field+'"]');
+	err.textContent = message;
+	err.scrollIntoViewIfNeeded(false);
+	err.setAttribute('flash','');
+	setTimeout(function(){err.removeAttribute('flash')},2000);
+}
+
+function showPassword(div) {
+	div.querySelector('img[show]').style.display = 'none';
+	div.querySelector('img[hide]').style.display = 'block';
+	let input = div.querySelector('input');
+	input.type = 'text';
+	input.focus();
+}
+function hidePassword(div) {
+	div.querySelector('img[show]').style.display = 'block';
+	div.querySelector('img[hide]').style.display = 'none';
+	let input = div.querySelector('input');
+	input.type = 'password';
+	input.focus();
+}
+
+
+function isEmpty(obj) {
+	for( let _ in obj ) {
+		return false;
+	}
+	return true;
+}
+
+function barChartHeight(rows) {
+	return 200 + 20*rows;
+}
+
+
+// Mixpanel
+if( window.UserEmail ) {
+	mixpanel.ours.identify(window.UserEmail);
+	mixpanel.ours.people.set({'$email':window.UserEmail,'$name':window.UserName});
+}
+
+
+// A/B tests
+
+function setAbTest(name,values) {
+	let props = {};
+	props[name] = cookies[name];
+	for( let i=1; i<=4; i++ ) {
+		props[name+'-null-'+i] = values[ Math.floor( Math.random() * values.length ) ];
+	}
+	mixpanel.ours.identify();
+	mixpanel.ours.people.set(props);
+}
+
+function removeAbTest(name) {
+	let a = [];
+	a.push(name);
+	for( let i=1; i<=4; i++ ) {
+		a.push(name+'-null-'+i);
+	}
+	mixpanel.ours.people.unset(a);
+}
+
+
+
+if( !location.pathname.match(/^\/private\//) ) {
+	fbTrack( 'track', 'PageView' );
+}