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}">
|
2012-03-05 15:14:24 +00:00
|
|
|
<a class="btn-info">{slider label}</a>
|
2012-03-05 12:23:27 +00:00
|
|
|
<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 15:14:24 +00:00
|
|
|
this.isOpen = getOpenState(this);
|
|
|
|
var sliderContent = [];
|
|
|
|
if(this.isOpen) {
|
|
|
|
sliderContent = getSliderContent(this);
|
|
|
|
}
|
|
|
|
var attributes = {
|
|
|
|
"class": ["tw-slider"]
|
|
|
|
};
|
|
|
|
if(this.hasParameter("class")) {
|
|
|
|
attributes["class"].push(this.params["class"]);
|
|
|
|
}
|
|
|
|
if(this.hasParameter("state")) {
|
|
|
|
attributes["data-tw-slider-type"] = this.params.state;
|
|
|
|
}
|
|
|
|
if(this.hasParameter("tooltip")) {
|
|
|
|
attributes.alt = this.params.tooltip;
|
|
|
|
attributes.title = this.params.tooltip;
|
|
|
|
}
|
|
|
|
var content = Renderer.ElementNode("span",
|
|
|
|
attributes,
|
|
|
|
[
|
|
|
|
Renderer.ElementNode("a",
|
|
|
|
{
|
|
|
|
"class": ["btn","btn-info"]
|
|
|
|
},[
|
|
|
|
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-29 13:30:22 +00:00
|
|
|
content.execute(this.parents,this.tiddlerTitle);
|
2012-03-05 15:14:24 +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-30 17:12:21 +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
|
|
|
|
if(this.hasParameter("state") && changes.hasOwnProperty(this.params.state)) {
|
|
|
|
this.isOpen = getOpenState(this);
|
|
|
|
}
|
|
|
|
// 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) {
|
|
|
|
// Remove the existing dom node for the body
|
|
|
|
this.content[0].domNode.removeChild(this.content[0].children[1].domNode);
|
|
|
|
// Get the slider content and execute it
|
|
|
|
this.content[0].children[1].children = getSliderContent(this);
|
|
|
|
this.content[0].children[1].execute(this.parents,this.tiddlerTitle);
|
|
|
|
this.content[0].children[1].renderInDom(this.content[0].domNode,null);
|
|
|
|
needContentRefresh = false; // Don't refresh the children if we've just created them
|
|
|
|
}
|
|
|
|
// Set the visibility of the slider content
|
|
|
|
this.content[0].children[1].domNode.style.display = this.isOpen ? "block" : "none";
|
|
|
|
// Refresh any children
|
|
|
|
if(needContentRefresh) {
|
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].refreshInDom(changes);
|
2012-02-22 22:47:42 +00:00
|
|
|
}
|
2012-02-19 17:24:01 +00:00
|
|
|
}
|
2012-01-23 18:31:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|