1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-12 03:00:28 +00:00
TiddlyWiki5/core/modules/utils/dom/popup.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2012-06-19 15:47:35 +00:00
/*\
2012-07-14 14:57:36 +00:00
title: $:/core/modules/utils/dom/popup.js
2012-06-19 15:47:35 +00:00
type: application/javascript
module-type: utils
Module that creates a $tw.utils.Popup object prototype that manages popups in the browser
2012-06-19 15:47:35 +00:00
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Creates a Popup object with these options:
2012-06-19 15:47:35 +00:00
rootElement: the DOM element to which the popup zapper should be attached
*/
var Popup = function(options) {
2012-06-19 15:47:35 +00:00
options = options || {};
this.rootElement = options.rootElement || document.body;
};
2012-12-31 18:36:39 +00:00
Popup.prototype.show = function(options) {
2012-06-19 15:47:35 +00:00
this.cancel();
2012-12-31 18:36:39 +00:00
this.title = options.title;
this.wiki = options.wiki;
this.anchorDomNode = options.domNode;
$tw.utils.addClass(this.anchorDomNode,"tc-popup");
2013-03-19 16:44:32 +00:00
this.rootElement.addEventListener("click",this,false);
2012-06-19 15:47:35 +00:00
};
Popup.prototype.handleEvent = function(event) {
// Dismiss the popup if we get a click on an element that doesn't have .tc-popup class
if(event.type === "click") {
var node = event.target;
while(node && !$tw.utils.hasClass(node,"tc-popup")) {
node = node.parentNode;
}
if(!node) {
this.cancel();
}
2012-06-19 15:47:35 +00:00
}
2012-06-19 15:56:29 +00:00
};
2012-06-19 15:47:35 +00:00
Popup.prototype.cancel = function() {
if(this.anchorDomNode) {
$tw.utils.removeClass(this.anchorDomNode,"tc-popup");
this.anchorDomNode = null;
}
2013-03-19 16:44:32 +00:00
this.rootElement.removeEventListener("click",this,false);
2012-12-31 18:36:39 +00:00
if(this.title) {
this.wiki.deleteTiddler(this.title);
this.title = null;
2012-06-19 15:47:35 +00:00
}
};
2012-11-06 14:11:11 +00:00
/*
Trigger a popup open or closed. Parameters are in a hashmap:
2012-12-31 18:36:39 +00:00
title: title of the tiddler where the popup details are stored
2012-11-06 14:11:11 +00:00
domNode: dom node to which the popup will be positioned
wiki: wiki
force: if specified, forces the popup state to true or false
2012-11-06 14:11:11 +00:00
*/
Popup.prototype.triggerPopup = function(options) {
// Get the current popup state tiddler
2012-12-31 18:36:39 +00:00
var value = options.wiki.getTextReference(options.title,"");
2012-11-06 14:11:11 +00:00
// Check if the popup is open by checking whether it matches "(<x>,<y>)"
var state = !this.readPopupState(options.title,value);
if("force" in options) {
state = options.force;
}
if(state) {
2012-11-06 14:11:11 +00:00
// Set the position if we're opening it
2012-12-31 18:36:39 +00:00
this.cancel();
options.wiki.setTextReference(options.title,
2012-11-06 14:11:11 +00:00
"(" + options.domNode.offsetLeft + "," + options.domNode.offsetTop + "," +
2012-12-31 18:36:39 +00:00
options.domNode.offsetWidth + "," + options.domNode.offsetHeight + ")");
this.show(options);
} else {
this.cancel();
2012-11-06 14:11:11 +00:00
}
};
/*
Returns true if the specified title and text identifies an active popup
*/
Popup.prototype.readPopupState = function(title,text) {
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/,
result = false;
if(this.title === title) {
result = popupLocationRegExp.test(text);
}
return result;
};
exports.Popup = Popup;
2012-06-19 15:47:35 +00:00
})();