Browse Source

Initial commit

main
indigane 4 years ago committed by GitHub
commit
d801229795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      LICENSE
  2. 11
      README.md
  3. 375
      card-sort.html
  4. 78
      common-scripts.js
  5. 79
      index.html
  6. 21
      muuri-0.9.5.min.js
  7. 2
      pako-2.0.4.min.js

9
LICENSE

@ -0,0 +1,9 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTBILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org/>

11
README.md

@ -0,0 +1,11 @@
# Card Sort
A tool for conducting a virtual card sorting.
## Web
https://indigane.github.io/cardsort/
## Read about card sorting
https://en.wikipedia.org/wiki/Card_sorting

375
card-sort.html

@ -0,0 +1,375 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Card Sort</title>
<style>
/* Reset */
body, html {
margin: 0;
padding: 0;
font-family: sans-serif;
}
* {
box-sizing: border-box;
}
/* Layout */
.main-layout {
display: grid;
grid-template-columns: auto 1fr;
grid-template-rows: minmax(60px, min-content) 1fr;
gap: 10px 10px;
grid-template-areas:
"top top"
"left right";
margin: 10px;
}
.toolbar {
grid-area: top;
}
.uncategorized-cards {
position: relative;
grid-area: left;
}
.categories {
position: relative;
grid-area: right;
}
/* Muuri functional */
.grid {
position: relative;
}
.item {
display: block;
position: absolute;
}
.item.muuri-item-dragging {
z-index: 3;
}
.item.muuri-item-releasing {
z-index: 2;
}
.item.muuri-item-hidden {
z-index: 0;
}
.item-content {
position: relative;
width: 100%;
height: 100%;
}
/* Toolbar */
.toolbar {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr;
gap: 10px 10px;
align-items: center;
justify-items: center;
}
.toolbar button {
font-size: 24px;
cursor: pointer;
}
/* Uncategorized cards */
.uncategorized-cards {
/* padding: 5px; */
border-radius: 5px;
/* box-shadow: 0 2px 5px rgba(0, 0, 0, .5); */
width: 200px;
max-width: calc(50vw - 35px);
}
.uncategorized-cards .grid {
min-height: 100%;
}
.uncategorized-cards .item {
width: 100%;
}
.card {
margin: 5px;
padding: 5px 7px;
border-radius: 3px;
font-size: 18px;
background: #ee6;
box-shadow: 0 2px 2px rgba(172, 121, 0, 0.58);
cursor: grab;
}
.muuri-item-placeholder .card {
pointer-events: none;
background: transparent;
border: 2px dashed #bbb;
color: #999;
box-shadow: none;
border-radius: 7px;
}
/* Categories */
.category {
margin: 5px 15px 30px 5px;
padding: calc(5px + 5px);
background: #fff;
box-shadow: 0 2px 10px rgba(0, 0, 0, .1);
}
.category .title {
width: 100%;
max-width: 200px;
padding: 7px 5px;
margin-bottom: 10px;
border: 0;
border-bottom: 1px solid #bbf;
font-size: 20px;
color: inherit;
}
.category-cards {
}
.category-cards .grid {
min-height: 100px;
}
.category-cards .item {
width: 100%;
}
.category-controls {
margin-top: 5px;
}
.category-drag-handle {
width: 100%;
height: 16px;
background: radial-gradient(#ddd 35%, transparent 36%) 0 0;
background-color: #fff;
background-size: 8px 8px;
cursor: grab;
}
</style>
</head>
<body>
<div class="main-layout">
<div class="toolbar">
<button class="btn-create-category">Create a new category</button>
<button class="btn-share-result">Share result</button>
<button class="btn-export-result">Export result</button>
</div>
<div class="uncategorized-cards">
<div class="grid"></div>
</div>
<div class="categories">
<div class="grid"></div>
</div>
</div>
<template class="card-template">
<div class="item">
<div class="item-content">
<div class="card"></div>
</div>
</div>
</template>
<template class="category-template">
<div class="item">
<div class="item-content">
<div class="category">
<input class="title" placeholder="Type a name" />
<div class="category-cards">
<div class="grid"></div>
</div>
<div class="category-controls">
<div class="category-drag-handle"></div>
</div>
</div>
</div>
</div>
</template>
<script src="muuri-0.9.5.min.js"></script>
<script src="pako-2.0.4.min.js"></script>
<script src="common-scripts.js"></script>
<script>
let data = {};
const settings = {
allowCategoryEditing: true,
};
/* Card sorting */
const buttonCreateCategory = document.querySelector('.btn-create-category');
const buttonShareResult = document.querySelector('.btn-share-result');
const buttonExportResult = document.querySelector('.btn-export-result');
const cardTemplate = document.querySelector('.card-template');
const categoryTemplate = document.querySelector('.category-template');
const uncategorizedCardsGrid = new Muuri('.uncategorized-cards .grid', {
dragEnabled: true,
dragSort: () => cardGrids,
dragPlaceholder: {
enabled: true,
createElement: (item) => item.getElement().cloneNode(true),
},
dragAutoScroll: {
targets: [window],
sortDuringScroll: false,
},
});
const categoriesGrid = new Muuri('.categories .grid', {
dragEnabled: true,
dragSort: true,
dragHandle: '.category-drag-handle',
});
const cardGrids = [uncategorizedCardsGrid];
const debouncedUpdateData = debounce(updateData, 500);
initialize();
async function initialize() {
if ( ! window.location.hash) {
window.location.href = 'index.html';
return;
}
data = await loadFromString(window.location.hash.split('#').pop());
const cardTexts = data[uncategorizedKey];
Object.assign(settings, loadSettings(data));
if (settings.allowCategoryEditing === false) {
categoryTemplate.content.querySelector('.title').disabled = true;
}
buttonCreateCategory.addEventListener('click', () => createCategory());
for (const text of cardTexts) {
createCard(text);
}
const categories = Object.keys(data).filter(categoryName => !reservedKeys.includes(categoryName));
if (categories.length > 0) {
for (categoryName of categories) {
createCategory(categoryName);
}
}
else {
createCategory();
createCategory();
createCategory();
}
uncategorizedCardsGrid.on('dragEnd', function handleDragEnd(data) {
debouncedUpdateData();
categoriesGrid.refreshItems().layout();
});
buttonShareResult.addEventListener('click', shareResult);
buttonExportResult.addEventListener('click', exportResult);
}
function createCard(text, categoryGrid) {
const targetGrid = categoryGrid || uncategorizedCardsGrid;
const itemElement = document.importNode(cardTemplate.content.children[0], true);
itemElement.querySelector('.card').textContent = text;
targetGrid.show(
targetGrid.add([itemElement], {
active: false,
}),
{
onFinish: () => {
categoriesGrid.refreshItems().layout();
},
},
);
}
function createCategory(name, cards) {
const itemElement = document.importNode(categoryTemplate.content.children[0], true);
const titleInput = itemElement.querySelector('.title');
titleInput.addEventListener('input', function handleInput() {
debouncedUpdateData();
});
if (name) {
titleInput.value = name;
}
categoriesGrid.show(
categoriesGrid.add([itemElement], {
active: false
}),
{
onFinish: () => {
const categoryGrid = new Muuri(itemElement.querySelector('.grid'), {
dragEnabled: true,
dragSort: () => cardGrids,
dragPlaceholder: {
enabled: true,
createElement: (item) => item.getElement().cloneNode(true),
},
dragAutoScroll: {
targets: [window],
sortDuringScroll: false,
},
});
categoryGrid.on('dragEnd', function handleDragEnd(data) {
debouncedUpdateData();
categoriesGrid.refreshItems().layout();
});
cardGrids.push(categoryGrid);
const cardTexts = data[name] || [];
for (const text of cardTexts) {
createCard(text, categoryGrid);
}
},
}
);
}
function updateData() {
data = {};
for (const categoryElement of document.querySelectorAll('.category')) {
const categoryName = categoryElement.querySelector('.title').value;
data[categoryName] = [];
for (const cardElement of categoryElement.querySelectorAll('.card')) {
data[categoryName].push(cardElement.textContent);
}
}
data[uncategorizedKey] = [];
for (const cardElement of document.querySelectorAll('.uncategorized-cards .card')) {
data[uncategorizedKey].push(cardElement.textContent);
}
data[settingsFlagsKey] = saveSettings(settings);
}
async function shareResult() {
updateData();
const dataString = await saveToString(data);
const baseUrl = window.location.href.replace(regexRemoveBasenameFromUrl, '/');
const url = baseUrl + 'card-sort.html#' + dataString;
if (navigator.share) {
navigator.share({
title: '',
text: '',
url: url,
});
}
else if (navigator.clipboard) {
navigator.clipboard.writeText(url);
const originalText = buttonShareResult.textContent;
buttonShareResult.textContent = 'Link copied to clipboard';
clearTimeout(buttonShareResult._textTimeout);
buttonShareResult._textTimeout = setTimeout(() => {
buttonShareResult.textContent = originalText;
}, 5000);
}
else {
alert('Your browser does not support sharing. Please copy from the address bar instead.');
window.location.href = url;
}
}
function exportResult() {
updateData();
const cleanedData = {};
for ([key, value] of Object.entries(data)) {
if (reservedKeys.includes(key)) {
continue;
}
cleanedData[key] = value;
}
const serializedData = JSON.stringify(cleanedData, null, 2);
if (navigator.clipboard) {
navigator.clipboard.writeText(serializedData);
const originalText = buttonExportResult.textContent;
buttonExportResult.textContent = 'Data copied to clipboard';
clearTimeout(buttonExportResult._textTimeout);
buttonExportResult._textTimeout = setTimeout(() => {
buttonExportResult.textContent = originalText;
}, 5000);
}
else {
prompt('Your browser does not support clipboard. You may try copying the content from below.', serializedData);
}
}
</script>
</body>
</html>

78
common-scripts.js

@ -0,0 +1,78 @@
/* Settings flags */
const settingsFlagsKey = '?';
const settingsFlagsEnum = {
allowCategoryEditing: 1,
};
function loadSettings(data) {
const settings = {};
const flags = data[settingsFlagsKey];
settings.allowCategoryEditing = Boolean(settingsFlagsEnum.allowCategoryEditing & flags);
return settings;
}
function saveSettings(settings) {
let flags = 0;
for (const [settingName, isEnabled] of Object.entries(settings)) {
if (isEnabled) {
flags |= settingsFlagsEnum[settingName];
}
}
return flags;
}
/* Saving and loading */
const uncategorizedKey = '#';
const reservedKeys = [
settingsFlagsKey,
uncategorizedKey,
];
const regexRemoveBasenameFromUrl = /\/[^\/]*?$/;
async function uint8ArrayToBase64(data) {
const base64url = await new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(new Blob([data]));
});
return base64url.split(',', 2)[1];
}
async function base64ToUint8Array(base64string) {
const response = await fetch('data:;base64,' + base64string);
const blob = await response.blob();
return new Uint8Array(await blob.arrayBuffer());
}
async function saveToString(data) {
const jsonString = JSON.stringify(data);
const compressedData = pako.deflate(new TextEncoder().encode(jsonString));
const base64String = await uint8ArrayToBase64(compressedData);
const webSafeBase64String = base64String.replaceAll('+', '-').replaceAll('/', '_');
return webSafeBase64String;
}
async function loadFromString(webSafeBase64String) {
const base64String = webSafeBase64String.replaceAll('-', '+').replaceAll('_', '/');
const compressedData = await base64ToUint8Array(base64String);
const jsonString = new TextDecoder('utf8').decode(pako.inflate(compressedData));
const data = JSON.parse(jsonString);
return data;
}
/* Utilities */
function debounce(func, wait, immediate) {
let timeout;
return function debouncedFunc() {
const context = this;
const args = arguments;
const later = function () {
timeout = null;
if ( ! immediate) {
func.apply(context, args);
}
};
const callNow = immediate && ! timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}

79
index.html

@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Card Sort</title>
<style>
/* Reset */
body, html {
margin: 0;
padding: 0;
font-family: sans-serif;
}
* {
box-sizing: border-box;
}
</style>
</head>
<body>
<div>Enter card texts one per line or separated by commas</div>
<div>
<textarea class="card-text-input"></textarea>
</div>
<div>Enter category names one per line or separated by commas (optional)</div>
<div>
<textarea class="category-name-input"></textarea>
</div>
<div>
<label>
<input class="category-editing-checkbox" type="checkbox" checked />
<span>Allow category editing</span>
</label>
</div>
<div>
<button class="btn-create-link">Create a link for card sorting</button>
</div>
<div class="link-container"></div>
<script src="pako-2.0.4.min.js"></script>
<script src="common-scripts.js"></script>
<script>
const cardTextInput = document.querySelector('.card-text-input');
const categoryNameInput = document.querySelector('.category-name-input');
const categoryEditingCheckbox = document.querySelector('.category-editing-checkbox');
const linkContainer = document.querySelector('.link-container');
const buttonCreateLink = document.querySelector('.btn-create-link');
buttonCreateLink.addEventListener('click', createLink);
async function createLink() {
const cardTexts = textToArray(cardTextInput.value);
const categoryNames = textToArray(categoryNameInput.value);
const data = {};
for (const categoryName of categoryNames) {
data[categoryName] = [];
}
data[uncategorizedKey] = cardTexts;
const settings = {};
settings.allowCategoryEditing = categoryEditingCheckbox.checked;
data[settingsFlagsKey] = saveSettings(settings);
const dataString = await saveToString(data);
const baseUrl = window.location.href.replace(regexRemoveBasenameFromUrl, '/');
const url = baseUrl + 'card-sort.html#' + dataString;
linkContainer.innerHTML = `<a href="${url}">${url}</a>`;
}
function textToArray(text) {
let resultArray;
if (text.includes('\n')) {
resultArray = text.split('\n');
}
else {
resultArray = text.split(',');
}
resultArray = resultArray.map(item => item.trim());
return resultArray;
}
</script>
</body>
</html>

21
muuri-0.9.5.min.js vendored

File diff suppressed because one or more lines are too long

2
pako-2.0.4.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save