jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/emby-slider/emby-slider.js

175 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-10-20 21:28:32 -07:00
define(['browser', 'dom', 'css!./emby-slider', 'registerElement', 'emby-input'], function (browser, dom) {
2016-10-05 21:28:10 -07:00
'use strict';
2016-06-13 12:02:48 -07:00
var EmbySliderPrototype = Object.create(HTMLInputElement.prototype);
var supportsNativeProgressStyle = browser.firefox || browser.edge || browser.msie;
var supportsValueSetOverride = false;
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
var descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
// descriptor returning null in webos
if (descriptor && descriptor.configurable) {
supportsValueSetOverride = true;
}
}
function updateValues(range, backgroundLower, backgroundUpper) {
2016-07-06 10:44:44 -07:00
var value = range.value;
requestAnimationFrame(function () {
2016-06-13 12:02:48 -07:00
2016-07-06 10:44:44 -07:00
if (backgroundLower) {
var fraction = (value - range.min) / (range.max - range.min);
2016-11-26 22:00:20 -07:00
if (browser.noFlex) {
backgroundLower.style['-webkit-flex'] = fraction;
backgroundUpper.style['-webkit-flex'] = 1 - fraction;
backgroundLower.style['-webkit-box-flex'] = fraction;
backgroundUpper.style['-webkit-box-flex'] = 1 - fraction;
}
2016-07-06 10:44:44 -07:00
backgroundLower.style.flex = fraction;
backgroundUpper.style.flex = 1 - fraction;
}
});
2016-06-13 12:02:48 -07:00
}
2016-11-05 10:39:12 -07:00
function updateBubble(range, value, bubble, bubbleText) {
2016-07-06 10:44:44 -07:00
2016-06-13 23:40:21 -07:00
bubble.style.left = (value - 1) + '%';
if (range.getBubbleText) {
value = range.getBubbleText(value);
2017-01-04 22:22:32 -07:00
} else {
value = Math.round(value);
2016-06-13 23:40:21 -07:00
}
2017-01-04 22:22:32 -07:00
2016-11-05 10:39:12 -07:00
bubbleText.innerHTML = value;
2016-06-13 23:40:21 -07:00
}
2016-06-13 12:02:48 -07:00
EmbySliderPrototype.attachedCallback = function () {
2017-01-07 13:11:41 -07:00
if (this.getAttribute('data-embyslider') === 'true') {
2016-06-13 12:02:48 -07:00
return;
}
2017-01-07 13:11:41 -07:00
this.setAttribute('data-embyslider', 'true');
2016-06-13 12:02:48 -07:00
this.classList.add('mdl-slider');
this.classList.add('mdl-js-slider');
2016-11-27 12:36:56 -07:00
if (browser.noFlex) {
2016-11-30 12:50:39 -07:00
this.classList.add('slider-no-webkit-thumb');
2016-11-27 12:36:56 -07:00
}
2016-06-13 12:02:48 -07:00
var containerElement = this.parentNode;
containerElement.classList.add('mdl-slider__container');
2016-06-13 23:40:21 -07:00
var htmlToInsert = '';
2016-06-13 12:02:48 -07:00
if (!supportsNativeProgressStyle) {
2016-06-13 23:40:21 -07:00
htmlToInsert += '<div class="mdl-slider__background-flex"><div class="mdl-slider__background-lower"></div><div class="mdl-slider__background-upper"></div></div>';
2016-06-13 12:02:48 -07:00
}
2016-11-05 10:39:12 -07:00
htmlToInsert += '<div class="sliderBubble hide"><h1 class="sliderBubbleText"></h1></div>';
2016-06-13 23:40:21 -07:00
containerElement.insertAdjacentHTML('beforeend', htmlToInsert);
2016-06-13 12:02:48 -07:00
var backgroundLower = containerElement.querySelector('.mdl-slider__background-lower');
var backgroundUpper = containerElement.querySelector('.mdl-slider__background-upper');
2016-06-13 23:40:21 -07:00
var sliderBubble = containerElement.querySelector('.sliderBubble');
2016-11-05 10:39:12 -07:00
var sliderBubbleText = containerElement.querySelector('.sliderBubbleText');
2016-06-13 12:02:48 -07:00
2016-07-06 10:44:44 -07:00
var hasHideClass = sliderBubble.classList.contains('hide');
2016-10-20 21:28:32 -07:00
dom.addEventListener(this, 'input', function (e) {
2016-06-13 12:02:48 -07:00
this.dragging = true;
2016-10-27 10:29:40 -07:00
2016-11-05 10:39:12 -07:00
updateBubble(this, this.value, sliderBubble, sliderBubbleText);
2016-10-27 10:29:40 -07:00
if (hasHideClass) {
sliderBubble.classList.remove('hide');
hasHideClass = false;
}
2016-10-20 21:28:32 -07:00
}, {
passive: true
2016-10-08 21:58:57 -07:00
});
2016-10-20 21:28:32 -07:00
dom.addEventListener(this, 'change', function () {
2016-10-08 21:58:57 -07:00
this.dragging = false;
updateValues(this, backgroundLower, backgroundUpper);
2016-10-27 10:29:40 -07:00
sliderBubble.classList.add('hide');
hasHideClass = true;
2016-10-20 21:28:32 -07:00
}, {
passive: true
2016-10-08 21:58:57 -07:00
});
2016-10-20 21:28:32 -07:00
// In firefox this feature disrupts the ability to move the slider
if (!browser.firefox) {
dom.addEventListener(this, 'mousemove', function (e) {
2016-10-27 10:29:40 -07:00
if (!this.dragging) {
var rect = this.getBoundingClientRect();
var clientX = e.clientX;
var bubbleValue = (clientX - rect.left) / rect.width;
bubbleValue *= 100;
2017-01-04 22:22:32 -07:00
updateBubble(this, bubbleValue, sliderBubble, sliderBubbleText);
2016-10-27 10:29:40 -07:00
if (hasHideClass) {
sliderBubble.classList.remove('hide');
hasHideClass = false;
}
2016-10-20 21:28:32 -07:00
}
2016-10-27 10:29:40 -07:00
2016-10-20 21:28:32 -07:00
}, {
passive: true
});
dom.addEventListener(this, 'mouseleave', function () {
sliderBubble.classList.add('hide');
hasHideClass = true;
}, {
passive: true
});
}
2016-06-13 12:02:48 -07:00
if (!supportsNativeProgressStyle) {
if (supportsValueSetOverride) {
this.addEventListener('valueset', function () {
updateValues(this, backgroundLower, backgroundUpper);
});
} else {
startInterval(this, backgroundLower, backgroundUpper);
}
}
};
function startInterval(range, backgroundLower, backgroundUpper) {
var interval = range.interval;
if (interval) {
clearInterval(interval);
}
range.interval = setInterval(function () {
updateValues(range, backgroundLower, backgroundUpper);
}, 100);
}
EmbySliderPrototype.detachedCallback = function () {
var interval = this.interval;
if (interval) {
clearInterval(interval);
}
this.interval = null;
};
document.registerElement('emby-slider', {
prototype: EmbySliderPrototype,
extends: 'input'
});
});