2013-10-12 16:05:13 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/reveal.js
|
2013-10-12 16:05:13 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-12 16:05:13 +00:00
|
|
|
|
|
|
|
Reveal widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-12 16:05:13 +00:00
|
|
|
|
2022-12-01 21:16:44 +00:00
|
|
|
var Popup = require("$:/core/modules/utils/dom/popup.js");
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
var RevealWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
2015-03-10 12:44:47 +00:00
|
|
|
var tag = this.parseTreeNode.isBlock ? "div" : "span";
|
2015-03-10 16:59:25 +00:00
|
|
|
if(this.revealTag && $tw.config.htmlUnsafeElements.indexOf(this.revealTag) === -1) {
|
2015-03-10 12:44:47 +00:00
|
|
|
tag = this.revealTag;
|
|
|
|
}
|
|
|
|
var domNode = this.document.createElement(tag);
|
2020-12-11 15:36:00 +00:00
|
|
|
this.domNode = domNode;
|
|
|
|
this.assignDomNodeClasses();
|
2015-03-23 15:28:26 +00:00
|
|
|
if(this.style) {
|
|
|
|
domNode.setAttribute("style",this.style);
|
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
2013-10-14 22:32:01 +00:00
|
|
|
if(!domNode.isTiddlyWikiFakeDom && this.type === "popup" && this.isOpen) {
|
|
|
|
this.positionPopup(domNode);
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.addClass(domNode,"tc-popup"); // Make sure that clicks don't dismiss popups within the revealed content
|
2013-10-14 22:32:01 +00:00
|
|
|
}
|
2013-10-15 20:06:52 +00:00
|
|
|
if(!this.isOpen) {
|
2014-08-30 19:44:26 +00:00
|
|
|
domNode.setAttribute("hidden","true");
|
2013-10-15 20:06:52 +00:00
|
|
|
}
|
2013-10-12 16:05:13 +00:00
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
2013-10-14 22:32:01 +00:00
|
|
|
RevealWidget.prototype.positionPopup = function(domNode) {
|
|
|
|
domNode.style.position = "absolute";
|
|
|
|
domNode.style.zIndex = "1000";
|
2019-07-04 14:58:27 +00:00
|
|
|
var left,top;
|
2013-10-14 22:32:01 +00:00
|
|
|
switch(this.position) {
|
|
|
|
case "left":
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left - domNode.offsetWidth;
|
|
|
|
top = this.popup.top;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
|
|
|
case "above":
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left;
|
|
|
|
top = this.popup.top - domNode.offsetHeight;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
|
|
|
case "aboveright":
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left + this.popup.width;
|
|
|
|
top = this.popup.top + this.popup.height - domNode.offsetHeight;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
2020-11-21 17:19:52 +00:00
|
|
|
case "belowright":
|
|
|
|
left = this.popup.left + this.popup.width;
|
|
|
|
top = this.popup.top + this.popup.height;
|
2021-05-30 18:20:17 +00:00
|
|
|
break;
|
2013-10-14 22:32:01 +00:00
|
|
|
case "right":
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left + this.popup.width;
|
|
|
|
top = this.popup.top;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
|
|
|
case "belowleft":
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left + this.popup.width - domNode.offsetWidth;
|
|
|
|
top = this.popup.top + this.popup.height;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
2020-11-21 17:19:52 +00:00
|
|
|
case "aboveleft":
|
|
|
|
left = this.popup.left - domNode.offsetWidth;
|
|
|
|
top = this.popup.top - domNode.offsetHeight;
|
2021-05-30 18:20:17 +00:00
|
|
|
break;
|
2013-10-14 22:32:01 +00:00
|
|
|
default: // Below
|
2019-07-04 14:58:27 +00:00
|
|
|
left = this.popup.left;
|
|
|
|
top = this.popup.top + this.popup.height;
|
2013-10-14 22:32:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-07-04 14:58:27 +00:00
|
|
|
if(!this.positionAllowNegative) {
|
|
|
|
left = Math.max(0,left);
|
|
|
|
top = Math.max(0,top);
|
|
|
|
}
|
2022-12-01 21:16:44 +00:00
|
|
|
if (this.popup.absolute) {
|
|
|
|
// Traverse the offsetParent chain and correct the offset to make it relative to the parent node.
|
|
|
|
for (var offsetParentDomNode = domNode.offsetParent; offsetParentDomNode; offsetParentDomNode = offsetParentDomNode.offsetParent) {
|
|
|
|
left -= offsetParentDomNode.offsetLeft;
|
|
|
|
top -= offsetParentDomNode.offsetTop;
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 14:58:27 +00:00
|
|
|
domNode.style.left = left + "px";
|
|
|
|
domNode.style.top = top + "px";
|
2013-10-14 22:32:01 +00:00
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
|
|
|
this.state = this.getAttribute("state");
|
2015-03-10 12:44:47 +00:00
|
|
|
this.revealTag = this.getAttribute("tag");
|
2013-10-12 16:05:13 +00:00
|
|
|
this.type = this.getAttribute("type");
|
|
|
|
this.text = this.getAttribute("text");
|
|
|
|
this.position = this.getAttribute("position");
|
2019-07-04 14:58:27 +00:00
|
|
|
this.positionAllowNegative = this.getAttribute("positionAllowNegative") === "yes";
|
2020-12-11 15:36:00 +00:00
|
|
|
// class attribute handled in assignDomNodeClasses()
|
2015-03-23 15:28:26 +00:00
|
|
|
this.style = this.getAttribute("style","");
|
2013-10-12 16:05:13 +00:00
|
|
|
this["default"] = this.getAttribute("default","");
|
|
|
|
this.animate = this.getAttribute("animate","no");
|
2014-02-12 08:32:19 +00:00
|
|
|
this.retain = this.getAttribute("retain","no");
|
2013-11-02 09:21:11 +00:00
|
|
|
this.openAnimation = this.animate === "no" ? undefined : "open";
|
|
|
|
this.closeAnimation = this.animate === "no" ? undefined : "close";
|
2020-11-21 17:19:52 +00:00
|
|
|
this.updatePopupPosition = this.getAttribute("updatePopupPosition","no") === "yes";
|
2013-10-12 16:05:13 +00:00
|
|
|
// Compute the title of the state tiddler and read it
|
2018-11-18 19:16:46 +00:00
|
|
|
this.stateTiddlerTitle = this.state;
|
|
|
|
this.stateTitle = this.getAttribute("stateTitle");
|
|
|
|
this.stateField = this.getAttribute("stateField");
|
|
|
|
this.stateIndex = this.getAttribute("stateIndex");
|
2013-10-12 16:05:13 +00:00
|
|
|
this.readState();
|
|
|
|
// Construct the child widgets
|
|
|
|
var childNodes = this.isOpen ? this.parseTreeNode.children : [];
|
2013-11-01 17:23:08 +00:00
|
|
|
this.hasChildNodes = this.isOpen;
|
2013-10-12 16:05:13 +00:00
|
|
|
this.makeChildWidgets(childNodes);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read the state tiddler
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype.readState = function() {
|
|
|
|
// Read the information from the state tiddler
|
2019-03-11 09:04:17 +00:00
|
|
|
var state,
|
2019-03-20 16:50:34 +00:00
|
|
|
defaultState = this["default"];
|
2019-02-01 16:53:29 +00:00
|
|
|
if(this.stateTitle) {
|
|
|
|
var stateTitleTiddler = this.wiki.getTiddler(this.stateTitle);
|
|
|
|
if(this.stateField) {
|
2019-03-11 09:04:17 +00:00
|
|
|
state = stateTitleTiddler ? stateTitleTiddler.getFieldString(this.stateField) || defaultState : defaultState;
|
2019-02-01 16:53:29 +00:00
|
|
|
} else if(this.stateIndex) {
|
2019-03-11 09:04:17 +00:00
|
|
|
state = stateTitleTiddler ? this.wiki.extractTiddlerDataItem(this.stateTitle,this.stateIndex) || defaultState : defaultState;
|
2019-02-01 16:53:29 +00:00
|
|
|
} else if(stateTitleTiddler) {
|
2019-03-11 09:04:17 +00:00
|
|
|
state = this.wiki.getTiddlerText(this.stateTitle) || defaultState;
|
2019-02-01 16:53:29 +00:00
|
|
|
} else {
|
2019-03-11 09:04:17 +00:00
|
|
|
state = defaultState;
|
2019-02-01 16:53:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state = this.stateTiddlerTitle ? this.wiki.getTextReference(this.state,this["default"],this.getVariable("currentTiddler")) : this["default"];
|
|
|
|
}
|
2018-11-18 19:31:53 +00:00
|
|
|
if(state === null) {
|
|
|
|
state = this["default"];
|
|
|
|
}
|
2014-11-24 06:38:58 +00:00
|
|
|
switch(this.type) {
|
|
|
|
case "popup":
|
|
|
|
this.readPopupState(state);
|
|
|
|
break;
|
|
|
|
case "match":
|
2019-05-10 07:47:00 +00:00
|
|
|
this.isOpen = this.text === state;
|
2014-11-24 06:38:58 +00:00
|
|
|
break;
|
|
|
|
case "nomatch":
|
2019-05-10 07:47:00 +00:00
|
|
|
this.isOpen = this.text !== state;
|
2014-11-24 06:38:58 +00:00
|
|
|
break;
|
2018-03-22 16:51:02 +00:00
|
|
|
case "lt":
|
2018-03-22 20:37:06 +00:00
|
|
|
this.isOpen = !!(this.compareStateText(state) < 0);
|
2018-03-22 16:51:02 +00:00
|
|
|
break;
|
|
|
|
case "gt":
|
2018-03-22 20:37:06 +00:00
|
|
|
this.isOpen = !!(this.compareStateText(state) > 0);
|
2018-03-22 16:51:02 +00:00
|
|
|
break;
|
|
|
|
case "lteq":
|
2018-03-22 20:37:06 +00:00
|
|
|
this.isOpen = !(this.compareStateText(state) > 0);
|
2018-03-22 16:51:02 +00:00
|
|
|
break;
|
|
|
|
case "gteq":
|
2018-03-22 20:37:06 +00:00
|
|
|
this.isOpen = !(this.compareStateText(state) < 0);
|
2018-03-22 16:51:02 +00:00
|
|
|
break;
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-22 16:51:02 +00:00
|
|
|
RevealWidget.prototype.compareStateText = function(state) {
|
2018-03-22 20:37:06 +00:00
|
|
|
return state.localeCompare(this.text,undefined,{numeric: true,sensitivity: "case"});
|
2013-10-12 16:05:13 +00:00
|
|
|
};
|
2018-03-22 20:37:06 +00:00
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
RevealWidget.prototype.readPopupState = function(state) {
|
2022-12-01 21:16:44 +00:00
|
|
|
this.popup = Popup.parseCoordinates(state);
|
2013-10-12 16:05:13 +00:00
|
|
|
// Check if the state matches the location regexp
|
2022-12-01 21:16:44 +00:00
|
|
|
if(this.popup) {
|
2013-10-12 16:05:13 +00:00
|
|
|
// If so, we're open
|
|
|
|
this.isOpen = true;
|
|
|
|
} else {
|
|
|
|
// If not, we're closed
|
|
|
|
this.isOpen = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-11 15:36:00 +00:00
|
|
|
RevealWidget.prototype.assignDomNodeClasses = function() {
|
|
|
|
var classes = this.getAttribute("class","").split(" ");
|
|
|
|
classes.push("tc-reveal");
|
|
|
|
this.domNode.className = classes.join(" ");
|
|
|
|
};
|
|
|
|
|
2013-10-12 16:05:13 +00:00
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2023-06-21 16:13:33 +00:00
|
|
|
if(changedAttributes.state || changedAttributes.type || changedAttributes.text || changedAttributes.position || changedAttributes.positionAllowNegative || changedAttributes["default"] || changedAttributes.animate || changedAttributes.stateTitle || changedAttributes.stateField || changedAttributes.stateIndex) {
|
2013-10-12 16:05:13 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
2020-12-11 23:24:27 +00:00
|
|
|
} else {
|
2018-07-20 16:07:48 +00:00
|
|
|
var currentlyOpen = this.isOpen;
|
2018-07-31 12:30:00 +00:00
|
|
|
this.readState();
|
2019-04-06 09:27:37 +00:00
|
|
|
if(this.isOpen !== currentlyOpen) {
|
2014-02-12 08:32:19 +00:00
|
|
|
if(this.retain === "yes") {
|
|
|
|
this.updateState();
|
|
|
|
} else {
|
|
|
|
this.refreshSelf();
|
2018-07-20 16:07:48 +00:00
|
|
|
return true;
|
2014-02-12 08:32:19 +00:00
|
|
|
}
|
2021-08-04 16:00:42 +00:00
|
|
|
} else if(this.type === "popup" && this.isOpen && this.updatePopupPosition && (changedTiddlers[this.state] || changedTiddlers[this.stateTitle])) {
|
2020-12-11 15:36:00 +00:00
|
|
|
this.positionPopup(this.domNode);
|
2013-11-01 17:23:08 +00:00
|
|
|
}
|
2020-12-11 23:24:27 +00:00
|
|
|
if(changedAttributes.style) {
|
|
|
|
this.domNode.style = this.getAttribute("style","");
|
|
|
|
}
|
|
|
|
if(changedAttributes["class"]) {
|
|
|
|
this.assignDomNodeClasses();
|
2021-05-30 18:20:17 +00:00
|
|
|
}
|
2018-07-20 16:07:48 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
2013-11-01 17:23:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Called by refresh() to dynamically show or hide the content
|
|
|
|
*/
|
|
|
|
RevealWidget.prototype.updateState = function() {
|
2018-05-02 14:31:26 +00:00
|
|
|
var self = this;
|
2013-11-01 17:23:08 +00:00
|
|
|
// Read the current state
|
|
|
|
this.readState();
|
|
|
|
// Construct the child nodes if needed
|
|
|
|
var domNode = this.domNodes[0];
|
|
|
|
if(this.isOpen && !this.hasChildNodes) {
|
|
|
|
this.hasChildNodes = true;
|
|
|
|
this.makeChildWidgets(this.parseTreeNode.children);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
}
|
|
|
|
// Animate our DOM node
|
|
|
|
if(!domNode.isTiddlyWikiFakeDom && this.type === "popup" && this.isOpen) {
|
|
|
|
this.positionPopup(domNode);
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.addClass(domNode,"tc-popup"); // Make sure that clicks don't dismiss popups within the revealed content
|
2013-11-12 22:02:43 +00:00
|
|
|
|
2013-11-01 17:23:08 +00:00
|
|
|
}
|
|
|
|
if(this.isOpen) {
|
|
|
|
domNode.removeAttribute("hidden");
|
2013-11-02 09:21:11 +00:00
|
|
|
$tw.anim.perform(this.openAnimation,domNode);
|
2013-11-01 17:23:08 +00:00
|
|
|
} else {
|
2013-11-02 09:21:11 +00:00
|
|
|
$tw.anim.perform(this.closeAnimation,domNode,{callback: function() {
|
2018-05-02 14:31:26 +00:00
|
|
|
//make sure that the state hasn't changed during the close animation
|
|
|
|
self.readState()
|
|
|
|
if(!self.isOpen) {
|
|
|
|
domNode.setAttribute("hidden","true");
|
|
|
|
}
|
2018-11-18 19:16:46 +00:00
|
|
|
}});
|
2013-10-12 16:05:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.reveal = RevealWidget;
|
|
|
|
|
|
|
|
})();
|