0 votes
asked by (2.4k points)

Hey there, 

I still haven't found an answer for my last question.

I don't want to spam or anything, but it's been some days already, and I'm affraid I've been forgotten :(

Thanks a lot !

1 Answer

0 votes
answered by (57.1k points)
edited by

Here's an updated version that saves its own state:

//intialize namespace
setup.messageMacro = {};

// default text option:
setup.messageMacro.default = 'Help';
setup.messageMacro.state = false;

// <<message>> macro
Macro.add('message', {
    tags    : null,
    handler : function () {
        var message  = this.payload[0].contents;
        var $wrapper = $(document.createElement('span'));
        var $link    = $(document.createElement(this.args.includes('btn') ? 'button' : 'a'));
        var $content = $(document.createElement('span'));

        $link
            .wiki(this.args.length > 0 && this.args[0] !== 'btn' ? this.args[0] : setup.messageMacro.default)
            .ariaClick(function () {
                if ($wrapper.hasClass('open')) {
                    $content
                        .css('display', 'none')
                        .empty();
                    setup.messageMacro.state = false;
                }
                else {
                    $content
                        .css('display', 'block')
                        .wiki(message);
                    setup.messageMacro.state = true;
                }
                    
                $wrapper.toggleClass('open');
            });

        $wrapper
            .attr('id', 'macro-' + this.name + '-' + this.args.join('').replace(/[^A-Za-z0-9]/g, ''))
            .addClass('message-text')
            .append($link)
            .append($content)
            .appendTo(this.output);
            
            if (setup.messageMacro.state) {
                $link.trigger('click');
            }
    }
});

 

commented by (2.4k points)
Thanks !
It works well with one <<message>> displayed, but if I have something like

 

<<message "Statistics">> Statistics <</message>>

<<message "Objects">> Objects <</message>>

 

Then whenever one is opened, both open on the next passage ? Would there be a way for it to remember which <<message>> has been opened, and which hasn't ?

Thanks again Chapel !
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...