2012-07-15 16:36:25 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/dom/browser.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Browser feature detection
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-25 13:58:32 +00:00
|
|
|
/*
|
|
|
|
Set style properties of an element
|
|
|
|
element: dom node
|
|
|
|
styles: ordered array of {name: value} pairs
|
|
|
|
*/
|
|
|
|
exports.setStyle = function(element,styles) {
|
2014-01-04 15:15:12 +00:00
|
|
|
if(element.nodeType === 1) { // Element.ELEMENT_NODE
|
2014-01-02 19:00:46 +00:00
|
|
|
for(var t=0; t<styles.length; t++) {
|
|
|
|
for(var styleName in styles[t]) {
|
|
|
|
element.style[$tw.utils.convertStyleNameToPropertyName(styleName)] = styles[t][styleName];
|
|
|
|
}
|
2012-10-25 21:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-25 13:58:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Converts a standard CSS property name into the local browser-specific equivalent. For example:
|
|
|
|
"background-color" --> "backgroundColor"
|
|
|
|
"transition" --> "webkitTransition"
|
|
|
|
*/
|
|
|
|
|
|
|
|
var styleNameCache = {}; // We'll cache the style name conversions
|
|
|
|
|
2012-10-25 21:20:27 +00:00
|
|
|
exports.convertStyleNameToPropertyName = function(styleName) {
|
2012-10-25 13:58:32 +00:00
|
|
|
// Return from the cache if we can
|
|
|
|
if(styleNameCache[styleName]) {
|
|
|
|
return styleNameCache[styleName];
|
|
|
|
}
|
|
|
|
// Convert it by first removing any hyphens
|
2012-10-25 21:20:27 +00:00
|
|
|
var propertyName = $tw.utils.unHyphenateCss(styleName);
|
2012-10-25 13:58:32 +00:00
|
|
|
// Then check if it needs a prefix
|
2015-08-29 15:33:04 +00:00
|
|
|
if($tw.browser && document.body.style[propertyName] === undefined) {
|
2012-10-25 13:58:32 +00:00
|
|
|
var prefixes = ["O","MS","Moz","webkit"];
|
|
|
|
for(var t=0; t<prefixes.length; t++) {
|
2012-10-25 21:20:27 +00:00
|
|
|
var prefixedName = prefixes[t] + propertyName.substr(0,1).toUpperCase() + propertyName.substr(1);
|
2012-10-25 13:58:32 +00:00
|
|
|
if(document.body.style[prefixedName] !== undefined) {
|
2012-10-25 21:20:27 +00:00
|
|
|
propertyName = prefixedName;
|
2012-10-25 13:58:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Put it in the cache too
|
2012-11-06 17:21:56 +00:00
|
|
|
styleNameCache[styleName] = propertyName;
|
2012-10-25 21:20:27 +00:00
|
|
|
return propertyName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Converts a JS format CSS property name back into the dashed form used in CSS declarations. For example:
|
|
|
|
"backgroundColor" --> "background-color"
|
|
|
|
"webkitTransform" --> "-webkit-transform"
|
|
|
|
*/
|
|
|
|
exports.convertPropertyNameToStyleName = function(propertyName) {
|
|
|
|
// Rehyphenate the name
|
|
|
|
var styleName = $tw.utils.hyphenateCss(propertyName);
|
|
|
|
// If there's a webkit prefix, add a dash (other browsers have uppercase prefixes, and so get the dash automatically)
|
|
|
|
if(styleName.indexOf("webkit") === 0) {
|
|
|
|
styleName = "-" + styleName;
|
|
|
|
} else if(styleName.indexOf("-m-s") === 0) {
|
|
|
|
styleName = "-ms" + styleName.substr(4);
|
|
|
|
}
|
|
|
|
return styleName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Round trip a stylename to a property name and back again. For example:
|
|
|
|
"transform" --> "webkitTransform" --> "-webkit-transform"
|
|
|
|
*/
|
|
|
|
exports.roundTripPropertyName = function(propertyName) {
|
|
|
|
return $tw.utils.convertPropertyNameToStyleName($tw.utils.convertStyleNameToPropertyName(propertyName));
|
|
|
|
};
|
2012-10-25 13:58:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Converts a standard event name into the local browser specific equivalent. For example:
|
|
|
|
"animationEnd" --> "webkitAnimationEnd"
|
|
|
|
*/
|
|
|
|
|
|
|
|
var eventNameCache = {}; // We'll cache the conversions
|
|
|
|
|
|
|
|
var eventNameMappings = {
|
|
|
|
"transitionEnd": {
|
|
|
|
correspondingCssProperty: "transition",
|
|
|
|
mappings: {
|
2012-10-26 21:12:40 +00:00
|
|
|
transition: "transitionend",
|
2012-10-25 13:58:32 +00:00
|
|
|
OTransition: "oTransitionEnd",
|
|
|
|
MSTransition: "msTransitionEnd",
|
2012-10-26 21:12:40 +00:00
|
|
|
MozTransition: "transitionend",
|
2012-10-25 13:58:32 +00:00
|
|
|
webkitTransition: "webkitTransitionEnd"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"animationEnd": {
|
|
|
|
correspondingCssProperty: "animation",
|
|
|
|
mappings: {
|
2012-10-26 21:12:40 +00:00
|
|
|
animation: "animationend",
|
2012-10-25 13:58:32 +00:00
|
|
|
OAnimation: "oAnimationEnd",
|
|
|
|
MSAnimation: "msAnimationEnd",
|
2012-10-26 21:12:40 +00:00
|
|
|
MozAnimation: "animationend",
|
2012-10-25 13:58:32 +00:00
|
|
|
webkitAnimation: "webkitAnimationEnd"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.convertEventName = function(eventName) {
|
|
|
|
if(eventNameCache[eventName]) {
|
|
|
|
return eventNameCache[eventName];
|
|
|
|
}
|
|
|
|
var newEventName = eventName,
|
|
|
|
mappings = eventNameMappings[eventName];
|
|
|
|
if(mappings) {
|
2012-10-26 09:28:32 +00:00
|
|
|
var convertedProperty = $tw.utils.convertStyleNameToPropertyName(mappings.correspondingCssProperty);
|
2012-10-25 13:58:32 +00:00
|
|
|
if(mappings.mappings[convertedProperty]) {
|
|
|
|
newEventName = mappings.mappings[convertedProperty];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Put it in the cache too
|
2012-11-06 17:21:56 +00:00
|
|
|
eventNameCache[eventName] = newEventName;
|
2012-10-25 13:58:32 +00:00
|
|
|
return newEventName;
|
|
|
|
};
|
|
|
|
|
2014-08-01 15:52:12 +00:00
|
|
|
/*
|
|
|
|
Return the names of the fullscreen APIs
|
|
|
|
*/
|
|
|
|
exports.getFullScreenApis = function() {
|
|
|
|
var d = document,
|
2014-08-03 11:35:53 +00:00
|
|
|
db = d.body,
|
|
|
|
result = {
|
2014-08-01 15:52:12 +00:00
|
|
|
"_requestFullscreen": db.webkitRequestFullscreen !== undefined ? "webkitRequestFullscreen" :
|
|
|
|
db.mozRequestFullScreen !== undefined ? "mozRequestFullScreen" :
|
|
|
|
db.msRequestFullscreen !== undefined ? "msRequestFullscreen" :
|
|
|
|
db.requestFullscreen !== undefined ? "requestFullscreen" : "",
|
|
|
|
"_exitFullscreen": d.webkitExitFullscreen !== undefined ? "webkitExitFullscreen" :
|
|
|
|
d.mozCancelFullScreen !== undefined ? "mozCancelFullScreen" :
|
|
|
|
d.msExitFullscreen !== undefined ? "msExitFullscreen" :
|
|
|
|
d.exitFullscreen !== undefined ? "exitFullscreen" : "",
|
|
|
|
"_fullscreenElement": d.webkitFullscreenElement !== undefined ? "webkitFullscreenElement" :
|
|
|
|
d.mozFullScreenElement !== undefined ? "mozFullScreenElement" :
|
|
|
|
d.msFullscreenElement !== undefined ? "msFullscreenElement" :
|
2015-01-08 19:18:41 +00:00
|
|
|
d.fullscreenElement !== undefined ? "fullscreenElement" : "",
|
|
|
|
"_fullscreenChange": d.webkitFullscreenElement !== undefined ? "webkitfullscreenchange" :
|
|
|
|
d.mozFullScreenElement !== undefined ? "mozfullscreenchange" :
|
|
|
|
d.msFullscreenElement !== undefined ? "MSFullscreenChange" :
|
|
|
|
d.fullscreenElement !== undefined ? "fullscreenchange" : ""
|
2014-08-01 15:52:12 +00:00
|
|
|
};
|
2015-01-08 19:18:41 +00:00
|
|
|
if(!result._requestFullscreen || !result._exitFullscreen || !result._fullscreenElement || !result._fullscreenChange) {
|
2014-08-03 11:35:53 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-01 15:52:12 +00:00
|
|
|
};
|
|
|
|
|
2012-07-15 16:36:25 +00:00
|
|
|
})();
|