markdown = require('./markdown.coffee') ### Salet interface configuration. In a typical MVC structure, this is the View. Only it knows about the DOM structure. Other modules just use its API or prepare the HTML for insertion. You don't need to call this module from the game directly. The abstraction goal here is to provide the author with a freedom to style his game as he wants to. The save and erase buttons are not necessary buttons, but they could be something else entirely. (That's why IDs are hardcoded.) There is only one instance of this class, and it's stored as `salet.view`. ### assert = (msg, assertion) -> console.assert assertion, msg way_to = (content, ref) -> return "#{content}" addClass = (element, className) -> if (element.classList) element.classList.add(className) else element.className += ' ' + className class SaletView init: (salet) => $("#page").on("click", "a", (event) -> event.preventDefault() a = $(this) href = a.attr('href') if a.hasClass("once") || href.match(/[?&]once[=&]?/) salet.view.clearLinks(href) if href.match(salet.linkRe) salet.processClick(href) ) $("#inventory").on("click", "a", (event) -> event.preventDefault() alert("Not done yet") ) $("#load").on("click", "a", (event) -> window.location.reload() ) if (@hasLocalStorage()) $("#erase").click((event) -> event.preventDefault() return salet.eraseSave() ) $("#save").click((event) -> event.preventDefault() return salet.saveGame() ) disableSaving: () -> $("#save").addClass('disabled') enableSaving: () -> $("#save").removeClass('disabled') enableErasing: () -> $("#erase").removeClass('disabled') disableErasing: () -> $("#erase").addClass('disabled') enableLoading: () -> $("#load").removeClass('disabled') disableLoading: () -> $("#load").addClass('disabled') # Scrolls the top of the screen to the specified point scrollTopTo: (value) -> $('html,body').animate({scrollTop: value}, 500) # Scrolls the bottom of the screen to the specified point scrollBottomTo: (value) -> scrollTopTo(value - $(window).height()) # Scrolls all the way to the bottom of the screen scrollToBottom: () -> scrollTopTo($('html').height() - $(window).height()); ### Removes all content from the page, clearing the main content area. If an elementSelector is given, then only that selector will be cleared. Note that all content from the cleared element is removed, but the element itself remains, ready to be filled again using @write. ### clearContent: (elementSelector = "#content") -> if (elementSelector == "#content") # empty the intro with the content intro = document.getElementById("intro") if intro? intro.innerHTML = "" document.querySelector(elementSelector).innerHTML = "" prepareContent: (content) -> if typeof content == "function" content = content() if content instanceof jQuery content = content[0].outerHTML return content.toString() # Write content to current room write: (content, elementSelector = "#current-room") => if not content? or content == "" return content = @prepareContent(content) block = document.querySelector(elementSelector) if block block.innerHTML = block.innerHTML + markdown(content) else # most likely this is the starting room block = document.getElementById("content") block.innerHTML = content # Append content to a block. Does not replace the old content. append: (content, elementSelector = "#content") => if content == "" return content = @prepareContent(content) block = document.querySelector(elementSelector) block.innerHTML = block.innerHTML + markdown(content) # Replaces the text in the given block with the given text. # !! Does not call markdown on the provided text. !! replace: (content, elementSelector) => if content == "" return content = @prepareContent(content) block = document.querySelector(elementSelector) block.innerHTML = content ### Turns any links that target the given href into plain text. This can be used to remove action options when an action is no longer available. It is used automatically when you give a link the 'once' class. ### clearLinks: (code) -> 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 block with the situation choices in the given order. The contents of each choice will be a link to the situation, the text of the link will be given by the situation's outputText property. Note that the canChoose function is called, and if it returns false, then the text will appear, but the link will not be clickable. Although canChoose is honored, canView and displayOrder are not. If you need to honor these, you should either do so manually, ot else use the `getSituationIdChoices` method to return an ordered list of valid viewable situation ids. ### writeChoices: (salet, listOfIds) => if (not listOfIds? or listOfIds.length == 0) return currentRoom = salet.getCurrentRoom() $options = $("