2016-09-16 03:22:56 +00:00
|
|
|
/* global $, APP, config, AJS, interfaceConfig */
|
|
|
|
|
|
|
|
import KeyboardShortcut from '../../keyboardshortcut/keyboardshortcut';
|
2015-12-14 12:26:50 +00:00
|
|
|
|
2015-06-18 16:59:41 +00:00
|
|
|
/**
|
2015-01-07 14:54:03 +00:00
|
|
|
* Created by hristo on 12/22/14.
|
|
|
|
*/
|
2015-12-14 12:26:50 +00:00
|
|
|
var UIUtil = {
|
2015-12-25 16:55:45 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
/**
|
|
|
|
* Returns the available video width.
|
|
|
|
*/
|
2016-09-08 18:16:23 +00:00
|
|
|
getAvailableVideoWidth: function () {
|
2015-12-25 16:55:45 +00:00
|
|
|
let rightPanelWidth = 0;
|
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
return window.innerWidth - rightPanelWidth;
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
2016-02-24 21:05:24 +00:00
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
/**
|
|
|
|
* Changes the style class of the element given by id.
|
|
|
|
*/
|
|
|
|
buttonClick: function(id, classname) {
|
2016-09-11 21:54:32 +00:00
|
|
|
// add the class to the clicked element
|
|
|
|
$("#" + id).toggleClass(classname);
|
2015-01-23 12:01:44 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Returns the text width for the given element.
|
|
|
|
*
|
|
|
|
* @param el the element
|
|
|
|
*/
|
|
|
|
getTextWidth: function (el) {
|
|
|
|
return (el.clientWidth + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the text height for the given element.
|
|
|
|
*
|
|
|
|
* @param el the element
|
|
|
|
*/
|
|
|
|
getTextHeight: function (el) {
|
|
|
|
return (el.clientHeight + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays the sound given by id.
|
|
|
|
*
|
|
|
|
* @param id the identifier of the audio element.
|
|
|
|
*/
|
|
|
|
playSoundNotification: function (id) {
|
|
|
|
document.getElementById(id).play();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes the given text.
|
|
|
|
*/
|
|
|
|
escapeHtml: function (unsafeText) {
|
|
|
|
return $('<div/>').text(unsafeText).html();
|
|
|
|
},
|
|
|
|
|
2016-02-12 12:48:57 +00:00
|
|
|
/**
|
|
|
|
* Unescapes the given text.
|
|
|
|
*
|
|
|
|
* @param {string} safe string which contains escaped html
|
|
|
|
* @returns {string} unescaped html string.
|
|
|
|
*/
|
|
|
|
unescapeHtml: function (safe) {
|
|
|
|
return $('<div />').html(safe).text();
|
|
|
|
},
|
|
|
|
|
2015-01-23 12:01:44 +00:00
|
|
|
imageToGrayScale: function (canvas) {
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
|
|
var pixels = imgData.data;
|
|
|
|
|
|
|
|
for (var i = 0, n = pixels.length; i < n; i += 4) {
|
|
|
|
var grayscale
|
2015-07-28 21:52:32 +00:00
|
|
|
= pixels[i] * 0.3 + pixels[i+1] * 0.59 + pixels[i+2] * 0.11;
|
2015-01-23 12:01:44 +00:00
|
|
|
pixels[i ] = grayscale; // red
|
|
|
|
pixels[i+1] = grayscale; // green
|
|
|
|
pixels[i+2] = grayscale; // blue
|
|
|
|
// pixels[i+3] is alpha
|
|
|
|
}
|
|
|
|
// redraw the image in black & white
|
|
|
|
context.putImageData(imgData, 0, 0);
|
|
|
|
},
|
|
|
|
|
2016-09-16 03:22:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the tooltip to the given element.
|
|
|
|
*
|
|
|
|
* @param element the element to set the tooltip to
|
|
|
|
* @param key the tooltip data-i18n key
|
|
|
|
* @param position the position of the tooltip in relation to the element
|
|
|
|
*/
|
2015-02-06 15:46:50 +00:00
|
|
|
setTooltip: function (element, key, position) {
|
2016-09-16 03:22:56 +00:00
|
|
|
let positions = {
|
|
|
|
'top': 's',
|
|
|
|
'top-left': 'se',
|
|
|
|
'left': 'e',
|
|
|
|
'bottom-left': 'ne',
|
|
|
|
'bottom': 'n',
|
|
|
|
'bottom-right': 'nw',
|
|
|
|
'right': 'w',
|
|
|
|
'top-right': 'sw'
|
|
|
|
};
|
|
|
|
|
|
|
|
element.setAttribute("data-i18n", "[content]" + key);
|
|
|
|
APP.translation.translateElement($(element));
|
|
|
|
|
|
|
|
AJS.$(element).tooltip({
|
|
|
|
gravity: positions[position],
|
|
|
|
title: this._getTooltipText.bind(this, element),
|
|
|
|
html: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal util function for generating tooltip title.
|
|
|
|
*
|
|
|
|
* @param element
|
|
|
|
* @returns {string|*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getTooltipText: function (element) {
|
|
|
|
let title = element.getAttribute('content');
|
|
|
|
let shortcut = element.getAttribute('shortcut');
|
|
|
|
if(shortcut) {
|
|
|
|
let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
|
|
|
|
title += ` ${shortcutString}`;
|
|
|
|
}
|
|
|
|
return title;
|
2015-08-12 10:13:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts given child element as the first one into the container.
|
|
|
|
* @param container the container to which new child element will be added
|
|
|
|
* @param newChild the new element that will be inserted into the container
|
|
|
|
*/
|
|
|
|
prependChild: function (container, newChild) {
|
|
|
|
var firstChild = container.childNodes[0];
|
|
|
|
if (firstChild) {
|
|
|
|
container.insertBefore(newChild, firstChild);
|
|
|
|
} else {
|
|
|
|
container.appendChild(newChild);
|
|
|
|
}
|
2015-08-28 21:13:40 +00:00
|
|
|
},
|
|
|
|
|
2016-09-13 22:37:09 +00:00
|
|
|
/**
|
|
|
|
* Indicates if a toolbar button is enabled.
|
|
|
|
* @param name the name of the setting section as defined in
|
|
|
|
* interface_config.js and Toolbar.js
|
|
|
|
* @returns {boolean} {true} to indicate that the given toolbar button
|
|
|
|
* is enabled, {false} - otherwise
|
|
|
|
*/
|
2015-08-28 21:13:40 +00:00
|
|
|
isButtonEnabled: function (name) {
|
2016-09-14 03:10:13 +00:00
|
|
|
return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
|
|
|
|
|| interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
|
2015-08-28 21:13:40 +00:00
|
|
|
},
|
2016-09-13 22:37:09 +00:00
|
|
|
/**
|
|
|
|
* Indicates if the setting section is enabled.
|
|
|
|
*
|
|
|
|
* @param name the name of the setting section as defined in
|
|
|
|
* interface_config.js and SettingsMenu.js
|
|
|
|
* @returns {boolean} {true} to indicate that the given setting section
|
|
|
|
* is enabled, {false} - otherwise
|
|
|
|
*/
|
|
|
|
isSettingEnabled: function (name) {
|
|
|
|
return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the element given by id.
|
|
|
|
*
|
2016-09-14 00:44:47 +00:00
|
|
|
* @param {String} the identifier of the element to show
|
2016-09-13 22:37:09 +00:00
|
|
|
*/
|
2016-09-14 00:44:47 +00:00
|
|
|
showElement(id) {
|
|
|
|
if ($("#"+id).hasClass("hide"))
|
|
|
|
$("#"+id).removeClass("hide");
|
|
|
|
|
|
|
|
$("#"+id).addClass("show");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides the element given by id.
|
|
|
|
*
|
|
|
|
* @param {String} the identifier of the element to hide
|
|
|
|
*/
|
|
|
|
hideElement(id) {
|
|
|
|
if ($("#"+id).hasClass("show"))
|
|
|
|
$("#"+id).removeClass("show");
|
|
|
|
|
|
|
|
$("#"+id).addClass("hide");
|
2016-09-13 22:37:09 +00:00
|
|
|
},
|
2015-08-28 21:13:40 +00:00
|
|
|
|
|
|
|
hideDisabledButtons: function (mappings) {
|
|
|
|
var selector = Object.keys(mappings)
|
2015-09-14 14:25:43 +00:00
|
|
|
.map(function (buttonName) {
|
|
|
|
return UIUtil.isButtonEnabled(buttonName)
|
2016-09-13 04:10:18 +00:00
|
|
|
? null : "#" + mappings[buttonName].id; })
|
2015-08-28 21:13:40 +00:00
|
|
|
.filter(function (item) { return item; })
|
|
|
|
.join(',');
|
|
|
|
$(selector).hide();
|
2015-12-17 15:31:11 +00:00
|
|
|
},
|
|
|
|
|
2015-12-29 22:30:50 +00:00
|
|
|
redirect (url) {
|
2015-12-17 15:31:11 +00:00
|
|
|
window.location.href = url;
|
2016-09-13 04:10:18 +00:00
|
|
|
},
|
2015-12-25 16:55:45 +00:00
|
|
|
|
|
|
|
isFullScreen () {
|
|
|
|
return document.fullScreen
|
|
|
|
|| document.mozFullScreen
|
|
|
|
|| document.webkitIsFullScreen;
|
2016-02-09 10:19:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create html attributes string out of object properties.
|
|
|
|
* @param {Object} attrs object with properties
|
|
|
|
* @returns {String} string of html element attributes
|
|
|
|
*/
|
|
|
|
attrsToString: function (attrs) {
|
|
|
|
return Object.keys(attrs).map(
|
|
|
|
key => ` ${key}="${attrs[key]}"`
|
|
|
|
).join(' ');
|
2016-04-08 15:55:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given DOM element is currently visible. The offsetParent
|
|
|
|
* will be null if the "display" property of the element or any of its
|
|
|
|
* parent containers is set to "none". This method will NOT check the
|
|
|
|
* visibility property though.
|
|
|
|
* @param {el} The DOM element we'd like to check for visibility
|
|
|
|
*/
|
|
|
|
isVisible(el) {
|
|
|
|
return (el.offsetParent !== null);
|
2016-04-19 18:07:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows / hides the element given by {selector} and sets a timeout if the
|
|
|
|
* {hideDelay} is set to a value > 0.
|
|
|
|
* @param selector the jquery selector of the element to show/hide.
|
|
|
|
* @param show a {boolean} that indicates if the element should be shown or
|
|
|
|
* hidden
|
|
|
|
* @param hideDelay the value in milliseconds to wait before hiding the
|
|
|
|
* element
|
|
|
|
*/
|
|
|
|
animateShowElement(selector, show, hideDelay) {
|
|
|
|
if(show) {
|
|
|
|
if (!selector.is(":visible"))
|
|
|
|
selector.css("display", "inline-block");
|
|
|
|
|
|
|
|
selector.fadeIn(300,
|
|
|
|
() => {selector.css({opacity: 1});}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hideDelay && hideDelay > 0)
|
|
|
|
setTimeout(
|
|
|
|
function () {
|
|
|
|
selector.fadeOut(300,
|
|
|
|
() => {selector.css({opacity: 0});}
|
|
|
|
);
|
|
|
|
}, hideDelay);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selector.fadeOut(300,
|
|
|
|
() => {selector.css({opacity: 0});}
|
|
|
|
);
|
|
|
|
}
|
2016-05-01 18:35:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the given cssValue as an Integer. If the value is not a number
|
|
|
|
* we return 0 instead of NaN.
|
|
|
|
* @param cssValue the string value we obtain when querying css properties
|
|
|
|
*/
|
|
|
|
parseCssInt(cssValue) {
|
|
|
|
return parseInt(cssValue) || 0;
|
2016-09-16 19:05:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds href value to 'a' link jquery object. If link value is null,
|
|
|
|
* undefined or empty string, disables the link.
|
|
|
|
* @param {object} aLinkElement the jquery object
|
|
|
|
* @param {string} link the link value
|
|
|
|
*/
|
|
|
|
setLinkHref(aLinkElement, link) {
|
|
|
|
if (link) {
|
|
|
|
aLinkElement.attr('href', link);
|
|
|
|
} else {
|
|
|
|
aLinkElement.css({
|
|
|
|
"pointer-events": "none",
|
|
|
|
"cursor": "default"
|
|
|
|
});
|
|
|
|
}
|
2016-04-08 15:55:19 +00:00
|
|
|
}
|
2015-12-11 14:48:16 +00:00
|
|
|
};
|
2015-12-14 12:26:50 +00:00
|
|
|
|
|
|
|
export default UIUtil;
|