2012-06-20 21:16:41 +00:00
|
|
|
/*\
|
2012-07-14 14:57:36 +00:00
|
|
|
title: $:/core/modules/utils/dom.js
|
2012-06-20 21:16:41 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Various static DOM-related utility functions.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Determines whether element 'a' contains element 'b'
|
|
|
|
Code thanks to John Resig, http://ejohn.org/blog/comparing-document-position/
|
|
|
|
*/
|
|
|
|
exports.domContains = function(a,b) {
|
|
|
|
return a.contains ?
|
2014-01-03 10:54:00 +00:00
|
|
|
a !== b && a.contains(b) :
|
2012-06-20 21:16:41 +00:00
|
|
|
!!(a.compareDocumentPosition(b) & 16);
|
|
|
|
};
|
|
|
|
|
2012-12-13 21:31:41 +00:00
|
|
|
exports.removeChildren = function(node) {
|
|
|
|
while(node.hasChildNodes()) {
|
|
|
|
node.removeChild(node.firstChild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-20 21:16:41 +00:00
|
|
|
exports.hasClass = function(el,className) {
|
2014-07-28 13:19:52 +00:00
|
|
|
return el && el.className && el.className.toString().split(" ").indexOf(className) !== -1;
|
2012-06-20 21:16:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.addClass = function(el,className) {
|
|
|
|
var c = el.className.split(" ");
|
|
|
|
if(c.indexOf(className) === -1) {
|
|
|
|
c.push(className);
|
|
|
|
}
|
|
|
|
el.className = c.join(" ");
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.removeClass = function(el,className) {
|
|
|
|
var c = el.className.split(" "),
|
|
|
|
p = c.indexOf(className);
|
|
|
|
if(p !== -1) {
|
|
|
|
c.splice(p,1);
|
|
|
|
el.className = c.join(" ");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.toggleClass = function(el,className,status) {
|
|
|
|
if(status === undefined) {
|
|
|
|
status = !exports.hasClass(el,className);
|
|
|
|
}
|
|
|
|
if(status) {
|
|
|
|
exports.addClass(el,className);
|
|
|
|
} else {
|
|
|
|
exports.removeClass(el,className);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-22 07:36:29 +00:00
|
|
|
/*
|
|
|
|
Get the first parent element that has scrollbars or use the body as fallback.
|
|
|
|
*/
|
|
|
|
exports.getScrollContainer = function(el) {
|
|
|
|
var doc = el.ownerDocument;
|
|
|
|
while(el.parentNode) {
|
|
|
|
el = el.parentNode;
|
|
|
|
if(el.scrollTop) {
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return doc.body;
|
|
|
|
};
|
|
|
|
|
2012-06-20 21:20:48 +00:00
|
|
|
/*
|
2012-07-07 16:14:50 +00:00
|
|
|
Get the scroll position of the viewport
|
|
|
|
Returns:
|
|
|
|
{
|
|
|
|
x: horizontal scroll position in pixels,
|
|
|
|
y: vertical scroll position in pixels
|
|
|
|
}
|
2012-06-20 21:20:48 +00:00
|
|
|
*/
|
2012-07-07 16:14:50 +00:00
|
|
|
exports.getScrollPosition = function() {
|
|
|
|
if("scrollX" in window) {
|
|
|
|
return {x: window.scrollX, y: window.scrollY};
|
|
|
|
} else {
|
|
|
|
return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
|
2012-06-20 21:20:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-22 07:36:29 +00:00
|
|
|
/*
|
|
|
|
Adjust the height of a textarea to fit its content, preserving scroll position, and return the height
|
|
|
|
*/
|
|
|
|
exports.resizeTextAreaToFit = function(domNode,minHeight) {
|
|
|
|
// Get the scroll container and register the current scroll position
|
|
|
|
var container = $tw.utils.getScrollContainer(domNode),
|
|
|
|
scrollTop = container.scrollTop;
|
|
|
|
// Measure the specified minimum height
|
|
|
|
domNode.style.height = minHeight;
|
|
|
|
var measuredHeight = domNode.offsetHeight;
|
|
|
|
// Set its height to auto so that it snaps to the correct height
|
|
|
|
domNode.style.height = "auto";
|
|
|
|
// Calculate the revised height
|
|
|
|
var newHeight = Math.max(domNode.scrollHeight + domNode.offsetHeight - domNode.clientHeight,measuredHeight);
|
|
|
|
// Only try to change the height if it has changed
|
|
|
|
if(newHeight !== domNode.offsetHeight) {
|
|
|
|
domNode.style.height = newHeight + "px";
|
|
|
|
// Make sure that the dimensions of the textarea are recalculated
|
|
|
|
$tw.utils.forceLayout(domNode);
|
|
|
|
// Set the container to the position we registered at the beginning
|
|
|
|
container.scrollTop = scrollTop;
|
|
|
|
}
|
|
|
|
return newHeight;
|
|
|
|
};
|
|
|
|
|
2012-10-26 21:12:40 +00:00
|
|
|
/*
|
|
|
|
Gets the bounding rectangle of an element in absolute page coordinates
|
|
|
|
*/
|
|
|
|
exports.getBoundingPageRect = function(element) {
|
|
|
|
var scrollPos = $tw.utils.getScrollPosition(),
|
|
|
|
clientRect = element.getBoundingClientRect();
|
|
|
|
return {
|
|
|
|
left: clientRect.left + scrollPos.x,
|
|
|
|
width: clientRect.width,
|
|
|
|
right: clientRect.right + scrollPos.x,
|
|
|
|
top: clientRect.top + scrollPos.y,
|
|
|
|
height: clientRect.height,
|
|
|
|
bottom: clientRect.bottom + scrollPos.y
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-07-13 12:02:34 +00:00
|
|
|
/*
|
|
|
|
Saves a named password in the browser
|
|
|
|
*/
|
|
|
|
exports.savePassword = function(name,password) {
|
2014-07-06 09:11:21 +00:00
|
|
|
try {
|
|
|
|
if(window.localStorage) {
|
|
|
|
localStorage.setItem("tw5-password-" + name,password);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2013-10-29 22:21:25 +00:00
|
|
|
}
|
2012-07-13 12:02:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Retrieve a named password from the browser
|
|
|
|
*/
|
|
|
|
exports.getPassword = function(name) {
|
2014-07-06 09:11:21 +00:00
|
|
|
try {
|
|
|
|
return window.localStorage ? localStorage.getItem("tw5-password-" + name) : "";
|
|
|
|
} catch(e) {
|
|
|
|
return "";
|
|
|
|
}
|
2012-07-13 12:02:34 +00:00
|
|
|
};
|
|
|
|
|
2012-08-02 21:32:34 +00:00
|
|
|
/*
|
|
|
|
Force layout of a dom node and its descendents
|
|
|
|
*/
|
|
|
|
exports.forceLayout = function(element) {
|
|
|
|
var dummy = element.offsetWidth;
|
|
|
|
};
|
|
|
|
|
2012-10-15 16:18:32 +00:00
|
|
|
/*
|
|
|
|
Pulse an element for debugging purposes
|
|
|
|
*/
|
|
|
|
exports.pulseElement = function(element) {
|
|
|
|
// Event handler to remove the class at the end
|
|
|
|
element.addEventListener($tw.browser.animationEnd,function handler(event) {
|
|
|
|
element.removeEventListener($tw.browser.animationEnd,handler,false);
|
|
|
|
$tw.utils.removeClass(element,"pulse");
|
|
|
|
},false);
|
|
|
|
// Apply the pulse class
|
|
|
|
$tw.utils.removeClass(element,"pulse");
|
2012-11-06 17:21:56 +00:00
|
|
|
$tw.utils.forceLayout(element);
|
2012-10-15 16:18:32 +00:00
|
|
|
$tw.utils.addClass(element,"pulse");
|
|
|
|
};
|
|
|
|
|
2012-12-13 21:31:41 +00:00
|
|
|
/*
|
|
|
|
Attach specified event handlers to a DOM node
|
2013-09-14 12:47:51 +00:00
|
|
|
domNode: where to attach the event handlers
|
|
|
|
events: array of event handlers to be added (see below)
|
|
|
|
Each entry in the events array is an object with these properties:
|
|
|
|
handlerFunction: optional event handler function
|
|
|
|
handlerObject: optional event handler object
|
|
|
|
handlerMethod: optionally specifies object handler method name (defaults to `handleEvent`)
|
2012-12-13 21:31:41 +00:00
|
|
|
*/
|
|
|
|
exports.addEventListeners = function(domNode,events) {
|
|
|
|
$tw.utils.each(events,function(eventInfo) {
|
|
|
|
var handler;
|
|
|
|
if(eventInfo.handlerFunction) {
|
|
|
|
handler = eventInfo.handlerFunction;
|
|
|
|
} else if(eventInfo.handlerObject) {
|
|
|
|
if(eventInfo.handlerMethod) {
|
|
|
|
handler = function(event) {
|
|
|
|
eventInfo.handlerObject[eventInfo.handlerMethod].call(eventInfo.handlerObject,event);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
handler = eventInfo.handlerObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
domNode.addEventListener(eventInfo.name,handler,false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-22 07:36:29 +00:00
|
|
|
/*
|
|
|
|
Get the computed styles applied to an element as an array of strings of individual CSS properties
|
|
|
|
*/
|
|
|
|
exports.getComputedStyles = function(domNode) {
|
|
|
|
var textAreaStyles = window.getComputedStyle(domNode,null),
|
2016-05-17 21:45:06 +00:00
|
|
|
styleDefs = [],
|
|
|
|
name;
|
|
|
|
for(var t=0; t<textAreaStyles.length; t++) {
|
|
|
|
name = textAreaStyles[t];
|
2016-05-09 20:11:08 +00:00
|
|
|
styleDefs.push(name + ": " + textAreaStyles.getPropertyValue(name) + ";");
|
2016-05-17 21:45:06 +00:00
|
|
|
}
|
2016-04-22 07:36:29 +00:00
|
|
|
return styleDefs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Apply a set of styles passed as an array of strings of individual CSS properties
|
|
|
|
*/
|
|
|
|
exports.setStyles = function(domNode,styleDefs) {
|
|
|
|
domNode.style.cssText = styleDefs.join("");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copy the computed styles from a source element to a destination element
|
|
|
|
*/
|
|
|
|
exports.copyStyles = function(srcDomNode,dstDomNode) {
|
|
|
|
$tw.utils.setStyles(dstDomNode,$tw.utils.getComputedStyles(srcDomNode));
|
|
|
|
};
|
2012-12-13 21:31:41 +00:00
|
|
|
|
2012-06-20 21:16:41 +00:00
|
|
|
})();
|