<label for="code">
Numeric code:
</label>
- <input id="code" type="text" autocomplete="off" placeholder="(Leave blank for random)" />
+ <input id="code" type="number" min="0" max="4294967295" autocomplete="off" placeholder="(Leave blank for random)" />
</div>
</div>
</form>
</p>
- <div id="images" style="display: none">
+ <div id="images">
</div>
<audio loop="" src="/zombo_words.mp3" type="audio/mpeg"></audio>
const name_input = document.querySelector("#name");
const name_dialog = document.querySelector("#name-dialog");
+ var informed_name;
const images = document.querySelector("#images");
const zombo_form = document.querySelector("#zombo-form");
const prompt = document.querySelector("#prompt");
socket.emit('set-name', name_input.value);
});
- socket.on('comment', function(comment) {
- const comments = document.querySelector("#" + comment.comments_id);
+ function add_comment(comments, comment) {
const dt = document.createElement('dt');
const dd = document.createElement('dd');
dt.textContent = comment.name + ':';
dd.textContent = comment.text;
comments.appendChild(dt);
comments.appendChild(dd);
+ }
+
+ socket.on('comment', function(comment) {
+ const comments = document.querySelector("#comments_" + comment.image_id);
+ add_comment(comments, comment);
});
socket.on('inform-name', (name) => {
console.log("Received inform-name event: " + name);
+
+ /* When we receive a name we store it in 3 places:
+ *
+ * * The informed_name variable (confirming what the server knows)
+ * * The name_input field (so the user will see that in their profile)
+ * * In localStorage (for use in a future session)
+ */
+ informed_name = name;
name_input.value = name;
+ localStorage.setItem('name', name);
});
socket.on('reset', () => {
images.replaceChildren();
+
+ /* Since the server is seeing us for the first time, let it
+ know our name (if we have one). */
+ const name = localStorage.getItem('name');
+ if (name) {
+ socket.emit('set-name', name);
+ return;
+ }
});
socket.on('image', (image) => {
const figure = document.createElement('figure');
+ figure.id = "image_" + image.id;
const img = document.createElement('img');
img.src = image.filename;
- figure.appendChild(img);
-
- const figcaption = document.createElement('figcaption');
- const caption_text = document.createTextNode(`${image.prompt} (${image.code}) `);
- figcaption.appendChild(caption_text);
-
- const reuse_button = document.createElement('button');
- reuse_button.appendChild(document.createTextNode("Reuse"));
- figcaption.appendChild(reuse_button);
- reuse_button.addEventListener('click', () => {
- prompt.value = image.prompt;
- window.scrollTo(0,0);
- });
+ if (image.link) {
+ const a = document.createElement('a');
+ a.href = image.link;
+ a.appendChild(img);
+ figure.appendChild(a);
+ } else {
+ figure.appendChild(img);
+ }
+
+ if (image.censored) {
+ img.style.display = "none";
+ }
+
+ if (image.prompt && image.code) {
+ const figcaption = document.createElement('figcaption');
+ const caption_text = document.createTextNode(`${image.prompt} (${image.code}) `);
+ figcaption.appendChild(caption_text);
+
+ const reuse_button = document.createElement('button');
+ reuse_button.appendChild(document.createTextNode("Reuse"));
+ figcaption.appendChild(reuse_button);
+
+ reuse_button.addEventListener('click', () => {
+ prompt.value = image.prompt;
+ window.scrollTo(0,0);
+ });
+
+ if (image.censored) {
+ figcaption.style.display = "none";
+ }
- figure.appendChild(figcaption);
+ figure.appendChild(figcaption);
+ }
const dl_comments = document.createElement('dl');
- const comments_id = "comments_" + image.index;
dl_comments.className = "comments";
- dl_comments.id = comments_id;
+ dl_comments.id = "comments_" + image.id;
+
+ image.comments.forEach((comment) => {
+ add_comment(dl_comments, comment);
+ });
+
figure.appendChild(dl_comments);
const comment_form = document.createElement('form');
figure.appendChild(comment_form);
comment_input.addEventListener('focus', () => {
- /* Do nothing if name is already set. */
- if (name_input.value)
+ /* If the server has informed us it has our name, we are done. */
+ if (informed_name)
+ return;
+
+ /* If server has no name, check local storage. */
+ const name = localStorage.getItem('name');
+ if (name) {
+ socket.emit('set-name', name);
return;
+ }
- /* Otherwise, bring up the modal dialog to set the name. */
+ /* Failing both, bring up the modal dialog to set the name. */
name_dialog.showModal();
});
comment_form.addEventListener('submit', function(e) {
e.preventDefault();
if (comment_input.value) {
- socket.emit('comment', {comments_id: comments_id,
+ socket.emit('comment', {image_id: image.id,
text: comment_input.value});
comment_input.value = '';
}
/* Re-display the form and hide spinner now that generation is over. */
clearTimeout(spinner_timeout);
hide_spinner();
- /* Also display all of the images. */
- images.style.display = "block";
});
function random_from(items) {