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
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
/*
|
2012-07-14 14:52:35 +00:00
|
|
|
Creates a Popup object with these options:
|
2012-06-19 15:47:35 +00:00
|
|
|
wiki: the wiki to use for resolving tiddler titles
|
|
|
|
rootElement: the DOM element to which the popup zapper should be attached
|
|
|
|
*/
|
2012-07-14 14:52:35 +00:00
|
|
|
var Popup = function(options) {
|
2012-06-19 15:47:35 +00:00
|
|
|
options = options || {};
|
|
|
|
this.wiki = options.wiki;
|
|
|
|
this.rootElement = options.rootElement || document.body;
|
|
|
|
this.popupTextRef = null;
|
|
|
|
};
|
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
Popup.prototype.popup = function(stateTextRef) {
|
2012-06-19 15:47:35 +00:00
|
|
|
this.cancel();
|
|
|
|
this.popupTextRef = stateTextRef;
|
|
|
|
this.rootElement.addEventListener("click",this,true);
|
|
|
|
};
|
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
Popup.prototype.handleEvent = function(event) {
|
2012-06-19 15:47:35 +00:00
|
|
|
if(event.type === "click") {
|
2012-06-19 15:50:10 +00:00
|
|
|
this.rootElement.removeEventListener("click",this,true);
|
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
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
Popup.prototype.cancel = function() {
|
2012-06-19 15:47:35 +00:00
|
|
|
if(this.popupTextRef) {
|
|
|
|
this.wiki.deleteTextReference(this.popupTextRef);
|
|
|
|
this.popupTextRef = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-06 14:11:11 +00:00
|
|
|
/*
|
|
|
|
Trigger a popup open or closed. Parameters are in a hashmap:
|
|
|
|
textRef: text reference where the popup details are stored
|
|
|
|
domNode: dom node to which the popup will be positioned
|
|
|
|
qualifyTiddlerTitles: "yes" if state tiddler titles are to be qualified
|
|
|
|
contextTiddlerTitle: title of tiddler providing context for text references
|
|
|
|
contextParents: parent stack
|
|
|
|
wiki: wiki
|
|
|
|
*/
|
|
|
|
Popup.prototype.triggerPopup = function(options) {
|
|
|
|
// Get the textref of the popup state tiddler
|
|
|
|
var textRef = options.textRef;
|
|
|
|
if(options.qualifyTiddlerTitles === "yes") {
|
2012-11-11 15:03:34 +00:00
|
|
|
textRef = textRef + "(" + options.contextParents.join(",") + "," + options.contextTiddlerTitle + ")";
|
2012-11-06 14:11:11 +00:00
|
|
|
}
|
|
|
|
// Get the current popup state tiddler
|
|
|
|
var value = options.wiki.getTextReference(textRef,"",options.contextTiddlerTitle);
|
|
|
|
// 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
|
|
|
|
options.wiki.setTextReference(textRef,
|
|
|
|
"(" + options.domNode.offsetLeft + "," + options.domNode.offsetTop + "," +
|
|
|
|
options.domNode.offsetWidth + "," + options.domNode.offsetHeight + ")",
|
|
|
|
options.contextTiddlerTitle,true);
|
|
|
|
this.popup(textRef);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
exports.Popup = Popup;
|
2012-06-19 15:47:35 +00:00
|
|
|
|
|
|
|
})();
|