From b6696ff9818476277bb04380859ebc131fda1127 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Mon, 1 Feb 2016 16:54:15 +0700 Subject: [PATCH] Fixed once links. Looks finished (except for saving) --- lib/salet.coffee | 10 +++++----- lib/view.coffee | 41 ++++++++++++++--------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/lib/salet.coffee b/lib/salet.coffee index c3d7784..ebfa935 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -20,10 +20,6 @@ assert = (msg, assertion) -> console.assert assertion, msg class Character inventory: [] -# Regular expression to catch every link action. -# Salet's default is a general URL-safe expression. -linkRe = /^([0-9A-Za-z_-]+|\.)(\/([0-9A-Za-z_-]+))?$/ - ### This is the control structure, it has minimal amount of data and this data is volatile anyway (as in, it won't get saved). @@ -42,6 +38,10 @@ class Salet # The unique id of the starting room. start: "start" + # Regular expression to catch every link action. + # Salet's default is a general URL-safe expression. + linkRe: /^([0-9A-Za-z_-]+|\.)(\/([0-9A-Za-z_-]+))?$/ + ### This function is called at the start of the game. It is normally overridden to provide initial character creation @@ -264,7 +264,7 @@ class Salet each one, and processLink manages this. ### processOneLink: (code) -> - match = code.match(linkRe) + match = code.match(@linkRe) assert(match, "link_not_valid".l({link:code})) situation = match[1] diff --git a/lib/view.coffee b/lib/view.coffee index 95fa947..0c12d8f 100644 --- a/lib/view.coffee +++ b/lib/view.coffee @@ -26,7 +26,12 @@ class SaletView init: (salet) -> $("#content, #ways").on("click", "a", (event) -> event.preventDefault() - salet.processClick($(this).attr("href")) + a = $(this) + href = a.attr('href') + if (href.match(salet.linkRe)) + if (a.hasClass("once") || href.match(/[?&]once[=&]?/)) + salet.view.clearLinks(href) + salet.processClick(href) ) $("#inventory").on("click", "a", (event) -> event.preventDefault() @@ -111,8 +116,11 @@ class SaletView a link the 'once' class. ### clearLinks: (code) -> - for a in $("a[href='" + code + "']") - a.replaceWith($("").addClass("ex_link").html(a.html())) + for a in $("#content").find("a[href='" + code + "']") + html = a.innerHTML + a = $(a) + a.replaceWith($("").addClass("ex_link").html(html)) + return true ### Given a list of situation ids, this outputs a standard option @@ -249,7 +257,9 @@ class SaletView min_key.push(node.key) if min < Infinity for node in min_key - addClass(document.getElementById("waylink-#{node}"), "destination") + waylink = document.getElementById("waylink-#{node}") + if waylink + addClass(waylink, "destination") pictureTag = (picture) -> extension = picture.substr((~-picture.lastIndexOf(".") >>> 0) + 2) @@ -262,27 +272,4 @@ class SaletView """ return "Room illustration" - # Returns HTML from the given content with the non-raw links wired up. - augmentLinks: (content) -> - output = $(content) - - # Wire up the links for regular tags. - for index, element in output.find("a") - a = $(element) - href = a.attr('href') - if (!a.hasClass("raw")|| href.match(/[?&]raw[=&]?/)) - if (href.match(linkRe)) - a.click((event) -> - event.preventDefault() - - # If we're a once-click, remove all matching links. - if (a.hasClass("once") || href.match(/[?&]once[=&]?/)) - @view.clearLinks(href) - - processClick(href) - return false - ) - else - a.addClass("raw") - return output module.exports = SaletView