mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-23 06:20:01 +00:00
Refactored slider macro
This commit is contained in:
parent
74de12f4d6
commit
8abf0049cf
@ -472,39 +472,6 @@ var SplitLabelNode = function(type,left,right,classes) {
|
|||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
Static method to construct a slider
|
|
||||||
*/
|
|
||||||
var SliderNode = function(type,label,tooltip,isOpen,children) {
|
|
||||||
var attributes = {
|
|
||||||
"class": "tw-slider",
|
|
||||||
"data-tw-slider-type": type
|
|
||||||
};
|
|
||||||
if(tooltip) {
|
|
||||||
attributes.alt = tooltip;
|
|
||||||
attributes.title = tooltip;
|
|
||||||
}
|
|
||||||
return new ElementNode("span",
|
|
||||||
attributes,
|
|
||||||
[
|
|
||||||
new ElementNode("a",
|
|
||||||
{
|
|
||||||
"class": ["tw-slider-label"]
|
|
||||||
},[
|
|
||||||
new TextNode(label)
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ElementNode("div",
|
|
||||||
{
|
|
||||||
"class": ["tw-slider-body"],
|
|
||||||
"style": {"display": isOpen ? "block" : "none"}
|
|
||||||
},
|
|
||||||
children
|
|
||||||
)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
Renderer.MacroNode = MacroNode;
|
Renderer.MacroNode = MacroNode;
|
||||||
Renderer.ElementNode = ElementNode;
|
Renderer.ElementNode = ElementNode;
|
||||||
Renderer.TextNode = TextNode;
|
Renderer.TextNode = TextNode;
|
||||||
@ -512,7 +479,6 @@ Renderer.EntityNode = EntityNode;
|
|||||||
Renderer.ErrorNode = ErrorNode;
|
Renderer.ErrorNode = ErrorNode;
|
||||||
Renderer.LabelNode = LabelNode;
|
Renderer.LabelNode = LabelNode;
|
||||||
Renderer.SplitLabelNode = SplitLabelNode;
|
Renderer.SplitLabelNode = SplitLabelNode;
|
||||||
Renderer.SliderNode = SliderNode;
|
|
||||||
|
|
||||||
exports.Renderer = Renderer;
|
exports.Renderer = Renderer;
|
||||||
|
|
||||||
|
@ -10,10 +10,19 @@ The current state of the slider can be stored as the string "open" or "closed" i
|
|||||||
!!Parameters
|
!!Parameters
|
||||||
|`state` //(defaults to 1st parameter)// |The title of the tiddler to contain the current state of the slider |
|
|`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` |
|
|`default` |The initial state of the slider, either `open` or `closed` |
|
||||||
|
|`class` |A CSS class to be applied to the slider root element |
|
||||||
|`content` |The WikiText to be enclosed in the slider. Overrides the `target` parameter, if present |
|
|`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 |
|
|`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 |
|
|`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 |
|
|`tooltip` //(defaults to 4th parameter)// |The plain text tooltip to be displayed when the mouse hovers over the slider button |
|
||||||
|
!!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>
|
||||||
|
}}}
|
||||||
!!Examples
|
!!Examples
|
||||||
A minimal slider:
|
A minimal slider:
|
||||||
{{{
|
{{{
|
||||||
@ -74,6 +83,7 @@ exports.macro = {
|
|||||||
params: {
|
params: {
|
||||||
state: {byPos: 0, type: "tiddler"},
|
state: {byPos: 0, type: "tiddler"},
|
||||||
"default": {byName: true, type: "text"},
|
"default": {byName: true, type: "text"},
|
||||||
|
"class": {byName: true, type: "text"},
|
||||||
target: {byPos: 1, type: "tiddler"},
|
target: {byPos: 1, type: "tiddler"},
|
||||||
label: {byPos: 2, type: "text"},
|
label: {byPos: 2, type: "text"},
|
||||||
tooltip: {byPos: 3, type: "text"},
|
tooltip: {byPos: 3, type: "text"},
|
||||||
@ -83,7 +93,7 @@ exports.macro = {
|
|||||||
click: function(event) {
|
click: function(event) {
|
||||||
if(event.target === this.domNode.firstChild.firstChild) {
|
if(event.target === this.domNode.firstChild.firstChild) {
|
||||||
this.isOpen = !this.isOpen;
|
this.isOpen = !this.isOpen;
|
||||||
if(!saveOpenState.call(this)) {
|
if(!saveOpenState(this)) {
|
||||||
exports.macro.refreshInDom.call(this,{});
|
exports.macro.refreshInDom.call(this,{});
|
||||||
}
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -94,16 +104,43 @@ exports.macro = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
execute: function() {
|
execute: function() {
|
||||||
this.isOpen = getOpenState.call(this);
|
this.isOpen = getOpenState(this);
|
||||||
var sliderContent = [];
|
var sliderContent = [];
|
||||||
if(this.isOpen) {
|
if(this.isOpen) {
|
||||||
sliderContent = getSliderContent.call(this);
|
sliderContent = getSliderContent(this);
|
||||||
}
|
}
|
||||||
var content = Renderer.SliderNode(this.params.state,
|
var attributes = {
|
||||||
this.params.label ? this.params.label : this.params.target,
|
"class": ["tw-slider"]
|
||||||
this.params.tooltip,
|
};
|
||||||
this.isOpen,
|
if(this.params.hasOwnProperty("class")) {
|
||||||
sliderContent);
|
attributes["class"].push(this.params["class"]);
|
||||||
|
}
|
||||||
|
if(this.params.hasOwnProperty("state")) {
|
||||||
|
attributes["data-tw-slider-type"] = this.params.state;
|
||||||
|
}
|
||||||
|
if(this.params.hasOwnProperty("tooltip")) {
|
||||||
|
attributes.alt = this.params.tooltip;
|
||||||
|
attributes.title = this.params.tooltip;
|
||||||
|
}
|
||||||
|
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
|
||||||
|
)
|
||||||
|
]
|
||||||
|
);
|
||||||
content.execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
content.execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
||||||
return [content];
|
return [content];
|
||||||
},
|
},
|
||||||
@ -111,12 +148,12 @@ exports.macro = {
|
|||||||
var needContentRefresh = true; // Avoid refreshing the content nodes if we don't need to
|
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 the state tiddler has changed then reset the open state
|
||||||
if(this.params.hasOwnProperty("state") && changes.hasOwnProperty(this.params.state)) {
|
if(this.params.hasOwnProperty("state") && changes.hasOwnProperty(this.params.state)) {
|
||||||
this.isOpen = getOpenState.call(this);
|
this.isOpen = getOpenState(this);
|
||||||
}
|
}
|
||||||
// Render the content if the slider is open and we don't have any content yet
|
// 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) {
|
if(this.isOpen && this.content[0].children[1].children.length === 0) {
|
||||||
// Get the slider content and execute it
|
// Get the slider content and execute it
|
||||||
this.content[0].children[1].children = getSliderContent.call(this);
|
this.content[0].children[1].children = getSliderContent(this);
|
||||||
this.content[0].children[1].execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
this.content[0].children[1].execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
||||||
// Replace the existing slider body DOM node
|
// Replace the existing slider body DOM node
|
||||||
this.domNode.firstChild.removeChild(this.domNode.firstChild.firstChild.nextSibling);
|
this.domNode.firstChild.removeChild(this.domNode.firstChild.firstChild.nextSibling);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user