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

72 lines
1.9 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;
this.rootElement.addEventListener("click",this,true);
2012-06-19 15:47:35 +00:00
};
Popup.prototype.handleEvent = function(event) {
if(event.type === "click" && !$tw.utils.domContains(this.anchorDomNode,event.target)) {
2012-06-19 15:47:35 +00:00
this.cancel();
}
2012-06-19 15:56:29 +00:00
};
2012-06-19 15:47:35 +00:00
Popup.prototype.cancel = function() {
this.rootElement.removeEventListener("click",this,true);
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
*/
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 popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/;
if(popupLocationRegExp.test(value)) {
this.cancel();
} else {
// 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);
2012-11-06 14:11:11 +00:00
}
};
exports.Popup = Popup;
2012-06-19 15:47:35 +00:00
})();