diff --git a/template/js/main.js b/template/js/main.js index 1d42387..f14bb29 100644 --- a/template/js/main.js +++ b/template/js/main.js @@ -56,16 +56,14 @@ situation('special-links', { # Special Links Undularity supports various special types of links, starting with - ${a().content('writer').once.writer('writerlink')} links. + ${a.content('writer').class('once').writer('writerlink').tag()} links. - Also notable are ${a().id('replacer-link').content('replacer').replacer('replacer-link')} + Also notable are ${a.id('replacer-link').content('replacer').replacer('replacer-link').tag()} links, which replace the content of a given id. And finally, we have ${ - a().once.content('inserter').inserter('inserter-link') - } links, which insert something into a specified element${ - span().id('inserter-link').here - }. + a.class("once").content('inserter').inserter('inserter-link').tag() + } links, which insert something into a specified element${span.id('inserter-link').tag()}. `, writers: { writerlink: "Writer links can only be clicked once.", @@ -81,7 +79,7 @@ situation('custom-actions', { You can define actions with custom effects that access the underlying Undum API. Try clicking - ${a().content('this link').action('specialaction')} for example. + ${a.content('this link').action('specialaction').tag()} for example. `, actions: { specialaction: function (character, system, from) { diff --git a/undularity/undularity.js b/undularity/undularity.js index 000c898..a17b1c7 100644 --- a/undularity/undularity.js +++ b/undularity/undularity.js @@ -174,7 +174,6 @@ UndularitySituation.prototype.enter = function (character, system, f) { */ UndularitySituation.prototype.act = function (character, system, action) { - console.log("Act called with action ", action); var actionClass, self = this; @@ -217,7 +216,7 @@ UndularitySituation.prototype.act = function (character, system, action) { They define a monadic interface: - a().id('my-link').content('link').writer('my-ref') + a.id('my-link').content('link').writer('my-ref').tag() -> link span().here -> @@ -230,51 +229,80 @@ UndularitySituation.prototype.act = function (character, system, action) { once: The 'once' special link class. A getter. */ - -var a = function () { - var once = "", - id = "", - content = ""; - var monad = { - writer: (ref) => - `${markdown.renderInline(content)}`, - replacer: (ref) => - `${markdown.renderInline(content)}`, - inserter: (ref) => - `${markdown.renderInline(content)}`, - action: (ref) => - `${markdown.renderInline(content)}`, - external: (href) => - `${markdown.renderInline(content)}`, - content: function (s) { content = s; return monad; }, - id: function (s) {id = `id="${s}"`; return monad; }, - get once () { once = "once"; return monad; } - }; - return monad; +var elementMonad = function (element) { + this.element = element; }; -var span = function (content) { - var elementClass = "", - id = "", - content = ""; - var monad = { - get here () { return `${content}`; }, - id: function (s) { - id = `id="${s}"`; - return monad; - }, - content: function (s) { - content = s; - return monad; - }, - class: function (s) { - elementClass = `class="${s}"`; - return monad; - } - }; - return monad; +var monadSetterGen = function (prop) { + return function (value) { + var newMonad = Object.create(this); + newMonad['_' + prop] = value; + return Object.freeze(newMonad); + } } +elementMonad.prototype.class = monadSetterGen("class"); +elementMonad.prototype.id = monadSetterGen("id"); +elementMonad.prototype.type = monadSetterGen("linkType"); +elementMonad.prototype.content = monadSetterGen("content"); +elementMonad.prototype.ref = monadSetterGen("ref"); +elementMonad.prototype.url = elementMonad.prototype.ref; +elementMonad.prototype.situation = elementMonad.prototype.ref; + +var linkTypeGen = function (type) { + return function (ref) { + return this.type(type).ref(ref); + } +} + +elementMonad.prototype.writer = linkTypeGen("writer"); +elementMonad.prototype.replacer = linkTypeGen("replacer"); +elementMonad.prototype.inserter = linkTypeGen("inserter"); +elementMonad.prototype.action = linkTypeGen("action"); + +elementMonad.prototype.tag = function () { + var classes = "", + classString = "", + idString = "", + hrefString= "", + contentString = ""; + + if (this._class) { + classes += this._class; + } + if (this._linkType) { + classes += (' ' + this._linkType); + } + if (classes) { + classString = ` class="${classes}"`; + } + + if (this._id) { + idString = ` id="${this._id}"`; + } + + if (this.element === "a") { + if (this._linkType) { + if (this._linkType === "action") { + hrefString = ` href="./${this._ref}"`; + } else { + hrefString = ` href="./_${this._linkType}_${this._ref}"`; + } + } else { + hrefString = ` href="${this._ref}"`; + } + } + + if (this._content) { + contentString = markdown.renderInline(this._content); + } + + return `<${this.element}${classString}${idString}${hrefString}>${contentString}`; +}; + +var a = Object.freeze(new elementMonad("a")); +var span = Object.freeze(new elementMonad("span")); + /* Quality definition function