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.
|
2012-03-04 21:58:33 +00:00
|
|
|
|
2012-03-05 10:18:22 +00:00
|
|
|
The current state of the slider can be stored as the string "open" or "closed" in a specified tiddler. If the value of that tiddler changes then the slider is automatically updated. If no state tiddler is specified then the state of the slider isn't retained, but the slider still works as expected.
|
2012-03-04 13:20:38 +00:00
|
|
|
!!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` |
|
2012-03-05 12:23:27 +00:00
|
|
|
|`class` |A CSS class to be applied to the slider root element |
|
2012-03-04 13:20:38 +00:00
|
|
|
|`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 |
|
2012-03-05 12:23:27 +00:00
|
|
|
!!Markup
|
|
|
|
The markup generated by the slider macro is:
|
|
|
|
{{{
|
|
|
|
<span class="tw-slider {user defined class}">
|
|
|
|
<a class="tw-slider-label">{slider label}</a>
|
|
|
|
<div class="tw-slider-body" style="display:{state}">{slider content}</div>
|
|
|
|
</span>
|
|
|
|
}}}
|
2012-03-04 13:20:38 +00:00
|
|
|
!!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
|
|
|
|
2012-03-05 12:04:06 +00:00
|
|
|
function getOpenState(macroNode) {
|
|
|
|
if(macroNode.params.hasOwnProperty("state")) {
|
|
|
|
var stateTiddler = macroNode.store.getTiddler(macroNode.params.state);
|
2012-03-04 21:58:33 +00:00
|
|
|
if(stateTiddler) {
|
|
|
|
return stateTiddler.text.trim() === "open";
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 12:04:06 +00:00
|
|
|
if(macroNode.params.hasOwnProperty("default")) {
|
|
|
|
return macroNode.params["default"] === "open";
|
2012-03-04 21:58:33 +00:00
|
|
|
}
|
|
|
|
return false;
|
2012-03-05 12:04:06 +00:00
|
|
|
}
|
2012-03-04 21:58:33 +00:00
|
|
|
|
2012-03-05 12:04:06 +00:00
|
|
|
function saveOpenState(macroNode) {
|
|
|
|
if(macroNode.params.hasOwnProperty("state")) {
|
|
|
|
var stateTiddler = macroNode.store.getTiddler(macroNode.params.state) ||
|
|
|
|
new Tiddler({title: macroNode.params.state, text: ""});
|
|
|
|
macroNode.store.addTiddler(new Tiddler(stateTiddler,{text: macroNode.isOpen ? "open" : "closed"}));
|
2012-03-04 21:58:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-05 12:04:06 +00:00
|
|
|
function getSliderContent(macroNode) {
|
|
|
|
if(macroNode.params.hasOwnProperty("content")) {
|
|
|
|
return macroNode.store.parseText("text/x-tiddlywiki",macroNode.params.content).nodes;
|
|
|
|
} else if(macroNode.params.hasOwnProperty("target")) {
|
2012-03-04 21:58:33 +00:00
|
|
|
return [Renderer.MacroNode(
|
|
|
|
"tiddler",
|
2012-03-05 12:04:06 +00:00
|
|
|
{target: macroNode.params.target},
|
2012-03-04 21:58:33 +00:00
|
|
|
null,
|
2012-03-05 12:04:06 +00:00
|
|
|
macroNode.store)];
|
2012-03-04 21:58:33 +00:00
|
|
|
} else {
|
|
|
|
return [Renderer.ErrorNode("No content specified for slider")];
|
|
|
|
}
|
2012-03-05 12:04:06 +00:00
|
|
|
}
|
2012-03-04 21:58:33 +00:00
|
|
|
|
2012-01-23 18:31:17 +00:00
|
|
|
exports.macro = {
|
|
|
|
name: "slider",
|
|
|
|
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"},
|
2012-03-05 12:23:27 +00:00
|
|
|
"class": {byName: true, type: "text"},
|
2012-03-04 13:20:38 +00:00
|
|
|
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-03-04 21:58:33 +00:00
|
|
|
if(event.target === this.domNode.firstChild.firstChild) {
|
|
|
|
this.isOpen = !this.isOpen;
|
2012-03-05 12:23:27 +00:00
|
|
|
if(!saveOpenState(this)) {
|
2012-03-04 21:58:33 +00:00
|
|
|
exports.macro.refreshInDom.call(this,{});
|
|
|
|
}
|
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() {
|
2012-03-05 12:23:27 +00:00
|
|
|
this.isOpen = getOpenState(this);
|
2012-03-04 21:58:33 +00:00
|
|
|
var sliderContent = [];
|
|
|
|
if(this.isOpen) {
|
2012-03-05 12:23:27 +00:00
|
|
|
sliderContent = getSliderContent(this);
|
|
|
|
}
|
|
|
|
var attributes = {
|
|
|
|
"class": ["tw-slider"]
|
|
|
|
};
|
2012-03-05 13:36:53 +00:00
|
|
|
if(this.hasParameter("class")) {
|
2012-03-05 12:23:27 +00:00
|
|
|
attributes["class"].push(this.params["class"]);
|
|
|
|
}
|
2012-03-05 13:36:53 +00:00
|
|
|
if(this.hasParameter("state")) {
|
2012-03-05 12:23:27 +00:00
|
|
|
attributes["data-tw-slider-type"] = this.params.state;
|
|
|
|
}
|
2012-03-05 13:36:53 +00:00
|
|
|
if(this.hasParameter("tooltip")) {
|
2012-03-05 12:23:27 +00:00
|
|
|
attributes.alt = this.params.tooltip;
|
|
|
|
attributes.title = this.params.tooltip;
|
2012-02-22 21:39:59 +00:00
|
|
|
}
|
2012-03-05 12:23:27 +00:00
|
|
|
var content = Renderer.ElementNode("span",
|
|
|
|
attributes,
|
|
|
|
[
|
|
|
|
Renderer.ElementNode("a",
|
|
|
|
{
|
|
|
|
"class": ["tw-slider-label"]
|
|
|
|
},[
|
|
|
|
Renderer.TextNode(this.params.label ? this.params.label : this.params.target)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
Renderer.ElementNode("div",
|
|
|
|
{
|
|
|
|
"class": ["tw-slider-body"],
|
|
|
|
"style": {"display": this.isOpen ? "block" : "none"}
|
|
|
|
},
|
|
|
|
sliderContent
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
2012-03-04 21:58:33 +00:00
|
|
|
content.execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
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-03-04 21:58:33 +00:00
|
|
|
var needContentRefresh = true; // Avoid refreshing the content nodes if we don't need to
|
|
|
|
// If the state tiddler has changed then reset the open state
|
2012-03-05 13:36:53 +00:00
|
|
|
if(this.hasParameter("state") && changes.hasOwnProperty(this.params.state)) {
|
2012-03-05 12:23:27 +00:00
|
|
|
this.isOpen = getOpenState(this);
|
2012-03-04 21:58:33 +00:00
|
|
|
}
|
|
|
|
// Render the content if the slider is open and we don't have any content yet
|
|
|
|
if(this.isOpen && this.content[0].children[1].children.length === 0) {
|
2012-03-05 13:36:53 +00:00
|
|
|
// Remove the existing dom node for the body
|
|
|
|
this.content[0].domNode.removeChild(this.content[0].children[1].domNode);
|
2012-03-04 21:58:33 +00:00
|
|
|
// Get the slider content and execute it
|
2012-03-05 12:23:27 +00:00
|
|
|
this.content[0].children[1].children = getSliderContent(this);
|
2012-03-04 21:58:33 +00:00
|
|
|
this.content[0].children[1].execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
2012-03-05 13:36:53 +00:00
|
|
|
this.content[0].children[1].renderInDom(this.content[0].domNode,null);
|
2012-03-04 21:58:33 +00:00
|
|
|
needContentRefresh = false; // Don't refresh the children if we've just created them
|
|
|
|
}
|
|
|
|
// Set the visibility of the slider content
|
2012-03-05 13:36:53 +00:00
|
|
|
this.content[0].children[1].domNode.style.display = this.isOpen ? "block" : "none";
|
2012-03-04 21:58:33 +00:00
|
|
|
// Refresh any children
|
|
|
|
if(needContentRefresh) {
|
2012-02-22 22:47:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|