Auto popup marcos

edited March 2015 in Help! with 2.0
I'm currently using some of TheMadExile's popup macro code in SugarCube, see below.

What I want to do additionally is to trigger a popup automatically. For e.g. notification of an important event on the screen that will popup the moment someone arrives on a passage instead of having to click to activate the popup. Is there a way to add this functionality in? Or does it exist in some combination of existing macros?

macros.add("popup", {
version: { major: 1, minor: 0, revision: 0 },
handler: function ()
{
if (this.args.length < 2)
{
var errors = [];
if (this.args.length < 1) { errors.push("link text"); }
if (this.args.length < 2) { errors.push("passage name"); }
return this.error("no " + errors.join(" or ") + " specified");
}

var el = document.createElement("a");
el.innerHTML = this.args[0];
el.className = "link-internal link-popup";
el.setAttribute("data-passage", this.args[1]);
UISystem.addClickHandler(el, null, function(evt) {
var dialog = document.getElementById("ui-body");
$(dialog)
.empty()
.addClass("dialog popup");
new Wikifier(dialog, tale.get(evt.target.getAttribute("data-passage")).processText().trim());
return true;
});
this.output.appendChild(el);
}
});

Comments

  • First, your startFn callback to UISystem.addClickHandler() is a tad verbose.  I'd suggest using UISystem.setup(), like so:

    UISystem.addClickHandler(el, null, function () {
    var dialog = UISystem.setup("popup");
    new Wikifier(dialog, tale.get(this.getAttribute("data-passage")).processText().trim());
    });

    As to automatically opening a dialog upon passage display, that seems like it could get a little irksome, from a player perspective, if done too frequently.  I don't know.  /shrug

    Regardless, you could do so via the following macro (since you're already using a macro for the other):

    /* Usage: <<autopopup "Some Pasage">> */
    macros.add("autopopup", {
    version : { major : 1, minor : 0, revision : 0 },
    handler : function () {
    if (this.args.length === 0) {
    return this.error("no passage name specified");
    }

    var dialog = UISystem.setup("popup");
    new Wikifier(dialog, tale.get(this.args[0]).processText().trim());
    UISystem.open();
    }
    });
  • Thanks for this!

    And don't worry, it definitely makes sense for the one specific instance am using it for. Won't be used for evil. ;) It is, however, necessary.
Sign In or Register to comment.