2012-06-13 08:12:09 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/macros/reveal.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
|
|
|
The reveal macro shows or hides content according to the value of the text in a specified tiddler.
|
|
|
|
|
2012-07-13 22:39:20 +00:00
|
|
|
The parameters are:
|
|
|
|
|
|
|
|
* ''state'' - text reference where the hide/reveal state is stored
|
|
|
|
* ''type'' - type of the hide/reveal state:
|
|
|
|
** //popup// - a popup - the state tiddler should contain the page coordinates of the button that triggered the popup
|
|
|
|
** //match// - reveals if the state tiddler matches the match text
|
2012-10-16 22:06:45 +00:00
|
|
|
** //nomatch// - reveals if the state tiddler does not match the match text
|
2012-07-13 22:39:20 +00:00
|
|
|
* ''position'' - popup position: //left//, //above//, //right//, //below// or //belowleft//
|
|
|
|
* ''text'' - match text
|
|
|
|
* ''qualifyTiddlerTitles'' - if present, causes the title of the state tiddler to be qualified with the current tiddler stack
|
|
|
|
* ''default'' - default hide/reveal state: `open` if visible, otherwise hidden
|
2012-10-16 22:06:45 +00:00
|
|
|
* ''class'' - CSS class(es) to be assigned to the revealed element
|
2012-07-13 22:39:20 +00:00
|
|
|
|
2012-06-13 08:12:09 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "reveal",
|
|
|
|
params: {
|
|
|
|
state: {byPos: 0, type: "tiddler"},
|
2012-06-13 12:19:55 +00:00
|
|
|
type: {byName: true, type: "text"},
|
2012-07-13 22:39:20 +00:00
|
|
|
text: {byName: true, type: "text"},
|
2012-06-13 12:19:55 +00:00
|
|
|
position: {byName: true, type: "text"},
|
2012-06-13 08:12:09 +00:00
|
|
|
qualifyTiddlerTitles: {byName: true, type: "text"},
|
|
|
|
"default": {byName: true, type: "text"},
|
|
|
|
"class": {byName: true, type: "text"}
|
2012-06-14 16:15:38 +00:00
|
|
|
}
|
2012-06-13 08:12:09 +00:00
|
|
|
};
|
|
|
|
|
2012-06-13 12:19:55 +00:00
|
|
|
exports.readState = function() {
|
|
|
|
// Start with the default value for being open or closed
|
|
|
|
if(this.hasParameter("default")) {
|
|
|
|
this.isOpen = this.params["default"] === "open";
|
|
|
|
}
|
|
|
|
// Read the information from the state tiddler
|
2012-06-19 15:47:35 +00:00
|
|
|
if(this.stateTextRef) {
|
|
|
|
var state = this.wiki.getTextReference(this.stateTextRef,"",this.tiddlerTitle);
|
|
|
|
switch(this.params.type) {
|
|
|
|
case "popup":
|
|
|
|
this.readPopupState(state);
|
|
|
|
break;
|
2012-07-13 22:39:20 +00:00
|
|
|
case "match":
|
|
|
|
this.readMatchState(state);
|
|
|
|
break;
|
2012-07-15 21:34:48 +00:00
|
|
|
case "nomatch":
|
|
|
|
this.readMatchState(state);
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
break;
|
2012-06-13 08:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-13 12:19:55 +00:00
|
|
|
};
|
|
|
|
|
2012-07-13 22:39:20 +00:00
|
|
|
exports.readMatchState = function(state) {
|
|
|
|
this.isOpen = state === this.params.text;
|
|
|
|
};
|
|
|
|
|
2012-06-13 12:19:55 +00:00
|
|
|
exports.readPopupState = function(state) {
|
|
|
|
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/,
|
|
|
|
match = popupLocationRegExp.exec(state);
|
|
|
|
// Check if the state matches the location regexp
|
|
|
|
if(match) {
|
|
|
|
// If so, we're open
|
|
|
|
this.isOpen = true;
|
|
|
|
// Get the location
|
|
|
|
this.popup = {
|
|
|
|
left: parseFloat(match[1]),
|
|
|
|
top: parseFloat(match[2]),
|
|
|
|
width: parseFloat(match[3]),
|
|
|
|
height: parseFloat(match[4])
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// If not, we're closed
|
|
|
|
this.isOpen = false;
|
2012-06-13 08:12:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-13 15:21:08 +00:00
|
|
|
exports.handleEvent = function(event) {
|
2012-07-13 22:39:20 +00:00
|
|
|
if(event.type === "click" && this.params.type === "popup") {
|
2012-06-13 15:21:08 +00:00
|
|
|
// Cancel the popup if we get a click on it
|
2012-06-19 15:47:35 +00:00
|
|
|
if(this.stateTextRef) {
|
|
|
|
this.wiki.deleteTextReference(this.stateTextRef);
|
|
|
|
}
|
2012-06-20 16:40:08 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2012-06-13 15:21:08 +00:00
|
|
|
}
|
2012-06-20 16:40:08 +00:00
|
|
|
return true;
|
2012-06-13 15:21:08 +00:00
|
|
|
};
|
|
|
|
|
2012-06-13 08:12:09 +00:00
|
|
|
exports.executeMacro = function() {
|
2012-06-19 15:47:35 +00:00
|
|
|
this.stateTextRef = this.params.state;
|
2012-06-13 08:12:09 +00:00
|
|
|
if(this.hasParameter("qualifyTiddlerTitles")) {
|
2012-06-19 15:47:35 +00:00
|
|
|
this.stateTextRef = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + this.stateTextRef;
|
2012-06-13 08:12:09 +00:00
|
|
|
}
|
2012-06-13 12:19:55 +00:00
|
|
|
this.readState();
|
2012-06-13 08:12:09 +00:00
|
|
|
var attributes = {
|
2012-06-13 12:19:55 +00:00
|
|
|
"class": ["tw-reveal"],
|
2012-06-13 14:37:07 +00:00
|
|
|
style: {}
|
2012-06-13 08:12:09 +00:00
|
|
|
};
|
|
|
|
if(this.hasParameter("class")) {
|
|
|
|
attributes["class"].push(this.params["class"]);
|
|
|
|
}
|
|
|
|
if(this.classes) {
|
|
|
|
$tw.utils.pushTop(attributes["class"],this.classes);
|
|
|
|
}
|
2012-06-13 14:37:07 +00:00
|
|
|
switch(this.params.type) {
|
|
|
|
case "popup":
|
|
|
|
attributes.style.position = "absolute";
|
|
|
|
attributes["class"].push("tw-popup");
|
|
|
|
break;
|
|
|
|
}
|
2012-07-15 22:06:24 +00:00
|
|
|
attributes.style = {display: this.isOpen ? (this.isBlock ? "block" : "inline") : "none"};
|
2012-07-13 22:39:20 +00:00
|
|
|
var child = $tw.Tree.Element(this.isBlock ? "div" : "span",attributes,this.isOpen ? this.content : [],{
|
2012-06-14 16:15:38 +00:00
|
|
|
events: ["click"],
|
|
|
|
eventHandler: this
|
|
|
|
});
|
2012-06-13 08:12:09 +00:00
|
|
|
child.execute(this.parents,this.tiddlerTitle);
|
|
|
|
return child;
|
|
|
|
};
|
|
|
|
|
2012-06-19 17:21:58 +00:00
|
|
|
exports.postRenderInDom = function() {
|
2012-06-13 12:19:55 +00:00
|
|
|
switch(this.params.type) {
|
|
|
|
case "popup":
|
|
|
|
if(this.isOpen) {
|
|
|
|
this.child.domNode.style.position = "absolute";
|
2012-06-13 14:37:07 +00:00
|
|
|
this.child.domNode.style.zIndex = "1000";
|
2012-06-13 12:19:55 +00:00
|
|
|
switch(this.params.position) {
|
|
|
|
case "left":
|
|
|
|
this.child.domNode.style.left = (this.popup.left - this.child.domNode.offsetWidth) + "px";
|
|
|
|
this.child.domNode.style.top = this.popup.top + "px";
|
|
|
|
break;
|
|
|
|
case "above":
|
|
|
|
this.child.domNode.style.left = this.popup.left + "px";
|
|
|
|
this.child.domNode.style.top = (this.popup.top - this.child.domNode.offsetHeight) + "px";
|
|
|
|
break;
|
|
|
|
case "right":
|
|
|
|
this.child.domNode.style.left = (this.popup.left + this.popup.width) + "px";
|
|
|
|
this.child.domNode.style.top = this.popup.top + "px";
|
|
|
|
break;
|
2012-06-21 08:11:04 +00:00
|
|
|
case "belowleft":
|
|
|
|
this.child.domNode.style.left = (this.popup.left + this.popup.width - this.child.domNode.offsetWidth) + "px";
|
|
|
|
this.child.domNode.style.top = (this.popup.top + this.popup.height) + "px";
|
|
|
|
break;
|
2012-06-13 12:19:55 +00:00
|
|
|
default: // Below
|
|
|
|
this.child.domNode.style.left = this.popup.left + "px";
|
|
|
|
this.child.domNode.style.top = (this.popup.top + this.popup.height) + "px";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-06-13 08:12:09 +00:00
|
|
|
};
|
|
|
|
|
2012-06-19 17:21:58 +00:00
|
|
|
exports.refreshInDom = function(changes) {
|
|
|
|
var needChildrenRefresh = true, // Avoid refreshing the children nodes if we don't need to
|
|
|
|
t;
|
|
|
|
// Re-read the open state
|
|
|
|
this.readState();
|
|
|
|
// Render the children if we're open and we don't have any children yet
|
|
|
|
if(this.isOpen && this.child.children.length === 0) {
|
|
|
|
// Install the children and execute them
|
|
|
|
this.child.children = this.content;
|
|
|
|
for(t=0; t<this.child.children.length; t++) {
|
|
|
|
this.child.children[t].execute(this.parents,this.tiddlerTitle);
|
|
|
|
this.child.children[t].renderInDom(this.child.domNode);
|
|
|
|
}
|
|
|
|
needChildrenRefresh = false; // Don't refresh the children if we've just created them
|
|
|
|
}
|
|
|
|
// Refresh the children
|
|
|
|
if(needChildrenRefresh) {
|
|
|
|
for(t=0; t<this.child.children.length; t++) {
|
|
|
|
this.child.children[t].refreshInDom(changes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set the visibility of the children
|
2012-07-15 22:06:24 +00:00
|
|
|
this.child.domNode.style.display = this.isOpen ? (this.isBlock ? "block" : "inline") : "none";
|
2012-06-19 17:21:58 +00:00
|
|
|
// Position the content if required
|
|
|
|
this.postRenderInDom();
|
|
|
|
};
|
|
|
|
|
2012-06-13 08:12:09 +00:00
|
|
|
})();
|