1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-10-31 23:53:00 +00:00

New popup cancelling mechanism

This commit is contained in:
Jeremy Ruston
2012-06-19 16:47:35 +01:00
parent 08d7a12d04
commit 5103f1585b
4 changed files with 94 additions and 71 deletions

View File

@@ -30,47 +30,42 @@ exports.dispatchMessage = function(event) {
};
exports.triggerPopup = function(event,cancel) {
// Get the title of the popup state tiddler
var title = this.params.popup;
// Get the textref of the popup state tiddler
var textRef = this.params.popup;
if(this.hasParameter("qualifyTiddlerTitles") && this.params.qualifyTiddlerTitles === "yes") {
title = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + title;
textRef = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + textRef;
}
// Get the popup state tiddler and the the text value
var tiddler = this.wiki.getTiddler(title),
value = tiddler ? tiddler.fields.text : "";
// Check for cancelling
if(cancel) {
value = "";
this.wiki.deleteTextReference(textRef,this.tiddlerTitle);
} else {
// Get the current popup state tiddler
var value = this.wiki.getTextReference(textRef,"",this.tiddlerTitle);
// Check if the popup is open by checking whether it matches "(<x>,<y>)"
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/;
if(popupLocationRegExp.test(value)) {
value = "";
this.wiki.deleteTextReference(textRef,this.tiddlerTitle);
} else {
// Set the position if we're opening it
value = "(" + this.child.domNode.offsetLeft + "," + this.child.domNode.offsetTop + "," + this.child.domNode.offsetWidth + "," + this.child.domNode.offsetHeight + ")";
this.wiki.setTextReference(textRef,
"(" + this.child.domNode.offsetLeft + "," + this.child.domNode.offsetTop + "," +
this.child.domNode.offsetWidth + "," + this.child.domNode.offsetHeight + ")",
this.tiddlerTitle,true);
$tw.popupper.popup(textRef);
}
}
// Update the state tiddler
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: title, text: value}),true);
};
exports.handleEvent = function(event) {
switch(event.type) {
case "click":
if(this.hasParameter("message")) {
this.dispatchMessage(event);
}
if(this.hasParameter("popup")) {
this.triggerPopup(event);
}
event.preventDefault();
return false;
case "tw-cancel-popup":
if(this.hasParameter("popup") && this.child.domNode !== event.targetOfCancel && !$tw.utils.domContains(this.child.domNode,event.targetOfCancel)) {
this.triggerPopup(event,true);
}
break;
if(event.type === "click") {
if(this.hasParameter("message")) {
this.dispatchMessage(event);
}
if(this.hasParameter("popup")) {
this.triggerPopup(event);
}
event.preventDefault();
return false;
}
return true;
};
@@ -87,7 +82,7 @@ exports.executeMacro = function() {
this.content[t].execute(this.parents,this.tiddlerTitle);
}
return $tw.Tree.Element("button",attributes,this.content,{
events: ["click","tw-cancel-popup"],
events: ["click"],
eventHandler: this
});
};

View File

@@ -30,15 +30,12 @@ exports.readState = function() {
this.isOpen = this.params["default"] === "open";
}
// Read the information from the state tiddler
if(this.stateTitle) {
var stateTiddler = this.wiki.getTiddler(this.stateTitle);
if(stateTiddler) {
var state = stateTiddler ? stateTiddler.fields.text : "";
switch(this.params.type) {
case "popup":
this.readPopupState(state);
break;
}
if(this.stateTextRef) {
var state = this.wiki.getTextReference(this.stateTextRef,"",this.tiddlerTitle);
switch(this.params.type) {
case "popup":
this.readPopupState(state);
break;
}
}
};
@@ -66,15 +63,16 @@ exports.readPopupState = function(state) {
exports.handleEvent = function(event) {
if(event.type === "click") {
// Cancel the popup if we get a click on it
var tiddler = this.wiki.getTiddler(this.stateTitle);
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.stateTitle, text: ""}),true);
if(this.stateTextRef) {
this.wiki.deleteTextReference(this.stateTextRef);
}
}
};
exports.executeMacro = function() {
this.stateTitle = this.params.state;
this.stateTextRef = this.params.state;
if(this.hasParameter("qualifyTiddlerTitles")) {
this.stateTitle = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + this.stateTitle;
this.stateTextRef = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + this.stateTextRef;
}
this.readState();
var attributes = {
@@ -105,10 +103,8 @@ exports.executeMacro = function() {
exports.refreshInDom = function(changes) {
var needChildrenRefresh = true, // Avoid refreshing the children nodes if we don't need to
t;
// If the state tiddler has changed then reset the open state
if($tw.utils.hop(changes,this.stateTitle)) {
this.readState();
}
// 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