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
|
|
|
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.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;
|
2013-01-01 16:14:42 +00:00
|
|
|
this.anchorDomNode = options.domNode;
|
2013-03-19 16:44:32 +00:00
|
|
|
this.rootElement.addEventListener("click",this,false);
|
2012-06-19 15:47:35 +00:00
|
|
|
};
|
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
Popup.prototype.handleEvent = function(event) {
|
2013-10-23 16:41:12 +00:00
|
|
|
if(event.type === "click" && this.anchorDomNode !== event.target && !$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
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
Popup.prototype.cancel = function() {
|
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
|
2013-06-09 18:24:21 +00:00
|
|
|
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>)"
|
2013-10-14 15:56:13 +00:00
|
|
|
var state = !this.readPopupState(options.title,value);
|
2013-06-09 18:24:21 +00:00
|
|
|
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);
|
2013-06-09 18:24:21 +00:00
|
|
|
} else {
|
|
|
|
this.cancel();
|
2012-11-06 14:11:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-14 15:56:13 +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;
|
|
|
|
};
|
|
|
|
|
2012-07-14 14:52:35 +00:00
|
|
|
exports.Popup = Popup;
|
2012-06-19 15:47:35 +00:00
|
|
|
|
|
|
|
})();
|