2012-01-23 18:31:17 +00:00
|
|
|
/*\
|
|
|
|
title: js/macros/slider.js
|
|
|
|
|
2012-03-04 13:20:38 +00:00
|
|
|
!Introduction
|
|
|
|
The slider macro is used to selectively reveal a chunk of text. By default, it renders as a button that may be clicked or touched to reveal the enclosed text.
|
|
|
|
|
|
|
|
The enclosed text can be a string of WikiText or be taken from a target tiddler.
|
|
|
|
!!Parameters
|
|
|
|
|`state` //(defaults to 1st parameter)// |The title of the tiddler to contain the current state of the slider |
|
|
|
|
|`default` |The initial state of the slider, either `open` or `closed` |
|
|
|
|
|`content` |The WikiText to be enclosed in the slider. Overrides the `target` parameter, if present |
|
|
|
|
|`target` //(defaults to 2nd parameter)// |The title of the tiddler that contains the enclosed text. Ignored if the `content` parameter is specified |
|
|
|
|
|`label` //(defaults to 3rd parameter)// |The plain text to be displayed as the label for the slider button |
|
|
|
|
|`tooltip` //(defaults to 4th parameter)// |The plain text tooltip to be displayed when the mouse hovers over the slider button |
|
|
|
|
!!Examples
|
|
|
|
A minimal slider:
|
|
|
|
{{{
|
|
|
|
<<slider target:MyTiddler>>
|
|
|
|
}}}
|
|
|
|
!!Notes
|
|
|
|
The slider is a good study example of a simple interactive macro.
|
2012-01-23 18:31:17 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-02-16 20:38:10 +00:00
|
|
|
var Renderer = require("../Renderer.js").Renderer,
|
2012-02-17 14:11:25 +00:00
|
|
|
Dependencies = require("../Dependencies.js").Dependencies,
|
2012-02-17 12:36:39 +00:00
|
|
|
Tiddler = require("../Tiddler.js").Tiddler,
|
2012-02-06 11:31:23 +00:00
|
|
|
utils = require("../Utils.js");
|
2012-01-23 18:31:17 +00:00
|
|
|
|
|
|
|
exports.macro = {
|
|
|
|
name: "slider",
|
|
|
|
types: ["text/html","text/plain"],
|
|
|
|
params: {
|
2012-02-19 17:24:01 +00:00
|
|
|
state: {byPos: 0, type: "tiddler"},
|
2012-03-04 13:20:38 +00:00
|
|
|
"default": {byName: true, type: "text"},
|
|
|
|
target: {byPos: 1, type: "tiddler"},
|
2012-02-17 17:32:32 +00:00
|
|
|
label: {byPos: 2, type: "text"},
|
2012-02-22 21:39:59 +00:00
|
|
|
tooltip: {byPos: 3, type: "text"},
|
|
|
|
content: {byName: true, type: "text"}
|
2012-01-23 18:31:17 +00:00
|
|
|
},
|
2012-02-02 17:48:09 +00:00
|
|
|
events: {
|
2012-02-21 21:57:30 +00:00
|
|
|
click: function(event) {
|
2012-02-19 17:24:01 +00:00
|
|
|
if(event.target === event.currentTarget.firstChild.firstChild) {
|
|
|
|
var el = event.currentTarget.firstChild.firstChild.nextSibling,
|
2012-02-21 21:57:30 +00:00
|
|
|
stateTiddler = this.params.state ? this.store.getTiddler(this.params.state) : null;
|
|
|
|
stateTiddler = stateTiddler || new Tiddler({title: this.params.state, text: ""});
|
2012-02-19 17:24:01 +00:00
|
|
|
var isOpen = stateTiddler.text.trim() === "open";
|
2012-02-21 21:57:30 +00:00
|
|
|
this.store.addTiddler(new Tiddler(stateTiddler,{text: isOpen ? "closed" : "open"}));
|
2012-02-19 17:24:01 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-02 17:48:09 +00:00
|
|
|
}
|
|
|
|
},
|
2012-02-21 21:57:30 +00:00
|
|
|
execute: function() {
|
|
|
|
var isOpen = this.params.state ? this.store.getTiddlerText(this.params.state,"").trim() === "open" : true,
|
2012-03-04 13:20:38 +00:00
|
|
|
target = this.params.target,
|
2012-02-22 21:39:59 +00:00
|
|
|
sliderContent;
|
|
|
|
if(this.params.hasOwnProperty("content")) {
|
2012-03-03 18:39:13 +00:00
|
|
|
sliderContent = this.store.parseText("text/x-tiddlywiki",this.params.content).nodes;
|
2012-02-22 21:39:59 +00:00
|
|
|
} else {
|
|
|
|
sliderContent = [Renderer.MacroNode(
|
|
|
|
"tiddler",
|
|
|
|
{target: target},
|
|
|
|
null,
|
|
|
|
this.store)];
|
|
|
|
}
|
2012-02-21 21:57:30 +00:00
|
|
|
var content = Renderer.SliderNode(this.params.state,
|
|
|
|
this.params.label ? this.params.label : target,
|
|
|
|
this.params.tooltip,
|
2012-02-17 12:36:39 +00:00
|
|
|
isOpen,
|
2012-02-22 21:39:59 +00:00
|
|
|
sliderContent);
|
2012-02-21 21:57:30 +00:00
|
|
|
content.execute(this.parents,this.store.getTiddler(this.tiddlerText));
|
2012-02-16 20:38:10 +00:00
|
|
|
return [content];
|
2012-02-19 17:24:01 +00:00
|
|
|
},
|
2012-02-22 23:27:21 +00:00
|
|
|
refreshInDom: function(changes) {
|
2012-02-22 22:47:42 +00:00
|
|
|
if(this.params.target && changes.hasOwnProperty(this.params.target)) {
|
2012-02-19 17:24:01 +00:00
|
|
|
// If the target has changed, re-render the macro
|
2012-02-22 22:47:42 +00:00
|
|
|
} else {
|
|
|
|
if (this.params.state && changes.hasOwnProperty(this.params.state)) {
|
|
|
|
// If it was just the state tiddler that's changed, set the display appropriately
|
|
|
|
var el = this.domNode.firstChild.firstChild.nextSibling,
|
|
|
|
isOpen = this.store.getTiddlerText(this.params.state,"").trim() === "open";
|
|
|
|
el.style.display = isOpen ? "block" : "none";
|
|
|
|
}
|
|
|
|
// Refresh any children
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].refreshInDom(changes);
|
|
|
|
}
|
2012-02-19 17:24:01 +00:00
|
|
|
}
|
2012-01-23 18:31:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|