2012-12-31 18:36:39 +00:00
|
|
|
/*\
|
2013-05-31 12:23:25 +00:00
|
|
|
title: $:/core/modules/widgets/reveal.js
|
2012-12-31 18:36:39 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Implements the reveal widget.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var RevealWidget = function(renderer) {
|
2012-12-31 18:36:39 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
2013-01-03 16:27:55 +00:00
|
|
|
this.generate();
|
2012-12-31 18:36:39 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
RevealWidget.prototype.generate = function() {
|
2012-12-31 18:36:39 +00:00
|
|
|
// Get the parameters from the attributes
|
|
|
|
this.state = this.renderer.getAttribute("state");
|
|
|
|
this.type = this.renderer.getAttribute("type");
|
|
|
|
this.text = this.renderer.getAttribute("text");
|
|
|
|
this.position = this.renderer.getAttribute("position");
|
|
|
|
this["default"] = this.renderer.getAttribute("default");
|
|
|
|
this.qualifyTiddlerTitles = this.renderer.getAttribute("qualifyTiddlerTitles");
|
|
|
|
this["class"] = this.renderer.getAttribute("class");
|
2013-05-28 15:47:16 +00:00
|
|
|
this.animate = this.renderer.getAttribute("animate","no");
|
2012-12-31 18:36:39 +00:00
|
|
|
// Compute the title of the state tiddler and read it
|
|
|
|
this.stateTitle = this.state;
|
|
|
|
if(this.qualifyTiddlerTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
this.stateTitle = this.stateTitle + "-" + this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2012-12-31 18:36:39 +00:00
|
|
|
}
|
|
|
|
this.readState();
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set up the element attributes
|
|
|
|
var classes = ["tw-reveal"],
|
|
|
|
styles = [];
|
2012-12-31 18:36:39 +00:00
|
|
|
if(this["class"]) {
|
2013-01-03 16:27:55 +00:00
|
|
|
$tw.utils.pushTop(classes,this["class"]);
|
2012-12-31 18:36:39 +00:00
|
|
|
}
|
2013-05-19 11:11:18 +00:00
|
|
|
if(this.isOpen) {
|
|
|
|
$tw.utils.pushTop(classes,"tw-reveal-open");
|
|
|
|
}
|
2012-12-31 18:36:39 +00:00
|
|
|
switch(this.type) {
|
|
|
|
case "popup":
|
2013-01-03 16:27:55 +00:00
|
|
|
styles.push("position:absolute;");
|
|
|
|
classes.push("tw-popup");
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
styles.push("display:" + (this.isOpen ? (this.renderer.parseTreeNode.isBlock ? "block" : "inline") : "none") + ";");
|
|
|
|
// Set the element
|
|
|
|
this.tag = "div";
|
|
|
|
this.attributes = {
|
|
|
|
"class": classes.join(" "),
|
|
|
|
style: styles.join("")
|
|
|
|
};
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.isOpen ? this.renderer.parseTreeNode.children : []);
|
2013-01-03 16:27:55 +00:00
|
|
|
this.events = [{name: "click", handlerObject: this, handlerMethod: "handleClickEvent"}];
|
2012-12-31 18:36:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read the state tiddler
|
|
|
|
*/
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.readState = function() {
|
2012-12-31 18:36:39 +00:00
|
|
|
// Read the information from the state tiddler
|
|
|
|
if(this.stateTitle) {
|
2013-05-15 16:32:17 +00:00
|
|
|
var state = this.renderer.renderTree.wiki.getTextReference(this.stateTitle,this["default"],this.renderer.tiddlerTitle);
|
2012-12-31 18:36:39 +00:00
|
|
|
switch(this.type) {
|
|
|
|
case "popup":
|
|
|
|
this.readPopupState(state);
|
|
|
|
break;
|
|
|
|
case "match":
|
|
|
|
this.readMatchState(state);
|
|
|
|
break;
|
|
|
|
case "nomatch":
|
|
|
|
this.readMatchState(state);
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.readMatchState = function(state) {
|
2012-12-31 18:36:39 +00:00
|
|
|
this.isOpen = state === this.text;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.readPopupState = function(state) {
|
2012-12-31 18:36:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.handleClickEvent = function(event) {
|
2012-12-31 18:36:39 +00:00
|
|
|
if(event.type === "click" && this.type === "popup") {
|
|
|
|
// Cancel the popup if we get a click on it
|
|
|
|
if(this.stateTitle) {
|
|
|
|
this.renderer.renderTree.wiki.deleteTextReference(this.stateTitle);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2013-05-28 15:28:38 +00:00
|
|
|
var self = this;
|
2012-12-31 18:36:39 +00:00
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
2013-01-01 16:14:42 +00:00
|
|
|
if(changedAttributes.state || changedAttributes.type || changedAttributes.text || changedAttributes.position || changedAttributes["default"] || changedAttributes.qualifyTiddlerTitles || changedAttributes["class"]) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-31 18:36:39 +00:00
|
|
|
} else {
|
2013-01-01 16:14:42 +00:00
|
|
|
var needChildrenRefresh = true; // Avoid refreshing the children nodes if we don't need to
|
|
|
|
// Get the open state
|
2013-05-28 15:28:38 +00:00
|
|
|
var previousState = this.isOpen
|
2013-01-01 16:14:42 +00:00
|
|
|
this.readState();
|
|
|
|
// Construct the child nodes if required
|
2013-01-03 16:27:55 +00:00
|
|
|
if(this.isOpen && this.children.length === 0) {
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.renderer.parseTreeNode.children);
|
2013-01-03 16:27:55 +00:00
|
|
|
var parentNode = this.renderer.domNode;
|
|
|
|
$tw.utils.each(this.children,function(child) {
|
2013-01-01 16:14:42 +00:00
|
|
|
parentNode.appendChild(child.renderInDom());
|
|
|
|
});
|
|
|
|
needChildrenRefresh = false;
|
|
|
|
}
|
|
|
|
// Refresh any child nodes
|
|
|
|
if(needChildrenRefresh) {
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-05-28 15:28:38 +00:00
|
|
|
// Animate the opening or closing
|
|
|
|
if(this.isOpen !== previousState) {
|
2013-05-28 15:47:16 +00:00
|
|
|
if(this.animate !== "no") {
|
|
|
|
if(this.isOpen) {
|
|
|
|
this.renderer.domNode.style.display = this.renderer.parseTreeNode.isBlock ? "block" : "inline";
|
|
|
|
$tw.anim.perform("open",this.renderer.domNode);
|
|
|
|
} else {
|
|
|
|
$tw.anim.perform("close",this.renderer.domNode,{callback: function() {
|
|
|
|
self.renderer.domNode.style.display = "none";
|
|
|
|
}});
|
|
|
|
}
|
2013-05-28 15:28:38 +00:00
|
|
|
} else {
|
2013-05-28 15:47:16 +00:00
|
|
|
this.renderer.domNode.style.display = this.isOpen ? (this.renderer.parseTreeNode.isBlock ? "block" : "inline") : "none";
|
2013-05-28 15:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 11:11:18 +00:00
|
|
|
// Add or remove the tw-reveal-open class
|
|
|
|
$tw.utils.toggleClass(this.renderer.domNode,"tw-reveal-open",this.isOpen);
|
2012-12-31 18:36:39 +00:00
|
|
|
}
|
|
|
|
// Position the content if required
|
2013-01-03 16:27:55 +00:00
|
|
|
if(this.isOpen) {
|
|
|
|
this.postRenderInDom();
|
|
|
|
}
|
2012-12-31 18:36:39 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
RevealWidget.prototype.postRenderInDom = function() {
|
2012-12-31 18:36:39 +00:00
|
|
|
switch(this.type) {
|
|
|
|
case "popup":
|
|
|
|
if(this.isOpen) {
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.position = "absolute";
|
|
|
|
this.renderer.domNode.style.zIndex = "1000";
|
2012-12-31 18:36:39 +00:00
|
|
|
switch(this.position) {
|
|
|
|
case "left":
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = (this.popup.left - this.renderer.domNode.offsetWidth) + "px";
|
|
|
|
this.renderer.domNode.style.top = this.popup.top + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
case "above":
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = this.popup.left + "px";
|
|
|
|
this.renderer.domNode.style.top = (this.popup.top - this.renderer.domNode.offsetHeight) + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
case "aboveright":
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = (this.popup.left + this.popup.width) + "px";
|
|
|
|
this.renderer.domNode.style.top = (this.popup.top + this.popup.height - this.renderer.domNode.offsetHeight) + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
case "right":
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = (this.popup.left + this.popup.width) + "px";
|
|
|
|
this.renderer.domNode.style.top = this.popup.top + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
case "belowleft":
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = (this.popup.left + this.popup.width - this.renderer.domNode.offsetWidth) + "px";
|
|
|
|
this.renderer.domNode.style.top = (this.popup.top + this.popup.height) + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
default: // Below
|
2013-01-03 16:27:55 +00:00
|
|
|
this.renderer.domNode.style.left = this.popup.left + "px";
|
|
|
|
this.renderer.domNode.style.top = (this.popup.top + this.popup.height) + "px";
|
2012-12-31 18:36:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.reveal = RevealWidget;
|
|
|
|
|
2012-12-31 18:36:39 +00:00
|
|
|
})();
|