2020-01-19 01:09:42 +03:00
|
|
|
define(['browser', 'dom', 'layoutManager', 'keyboardnavigation', 'css!./emby-slider', 'registerElement', 'emby-input'], function (browser, dom, layoutManager, keyboardnavigation) {
|
2019-01-10 15:39:37 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var EmbySliderPrototype = Object.create(HTMLInputElement.prototype);
|
|
|
|
|
|
|
|
var supportsNativeProgressStyle = browser.firefox;
|
|
|
|
var supportsValueSetOverride = false;
|
|
|
|
|
|
|
|
var enableWidthWithTransform;
|
|
|
|
|
|
|
|
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
|
|
|
|
|
|
|
|
var descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
|
|
|
|
// descriptor returning null in webos
|
|
|
|
if (descriptor && descriptor.configurable) {
|
|
|
|
supportsValueSetOverride = true;
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function updateValues() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-11-09 11:48:52 +03:00
|
|
|
// Do not update values when dragging with keyboard to keep current progress for reference
|
|
|
|
if (!!this.keyboardDragging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
var range = this;
|
|
|
|
var value = range.value;
|
|
|
|
|
|
|
|
// put this on a callback. Doing it within the event sometimes causes the slider to get hung up and not respond
|
|
|
|
requestAnimationFrame(function () {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var backgroundLower = range.backgroundLower;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (backgroundLower) {
|
|
|
|
var fraction = (value - range.min) / (range.max - range.min);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (enableWidthWithTransform) {
|
|
|
|
backgroundLower.style.transform = 'scaleX(' + (fraction) + ')';
|
|
|
|
} else {
|
|
|
|
fraction *= 100;
|
|
|
|
backgroundLower.style.width = fraction + '%';
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateBubble(range, value, bubble, bubbleText) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
requestAnimationFrame(function () {
|
|
|
|
|
|
|
|
bubble.style.left = value + '%';
|
|
|
|
|
|
|
|
if (range.getBubbleHtml) {
|
|
|
|
value = range.getBubbleHtml(value);
|
|
|
|
} else {
|
|
|
|
if (range.getBubbleText) {
|
|
|
|
value = range.getBubbleText(value);
|
|
|
|
} else {
|
|
|
|
value = Math.round(value);
|
|
|
|
}
|
|
|
|
value = '<h1 class="sliderBubbleText">' + value + '</h1>';
|
|
|
|
}
|
|
|
|
|
|
|
|
bubble.innerHTML = value;
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
EmbySliderPrototype.attachedCallback = function () {
|
|
|
|
|
|
|
|
if (this.getAttribute('data-embyslider') === 'true') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableWidthWithTransform == null) {
|
|
|
|
//enableWidthWithTransform = browser.supportsCssAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setAttribute('data-embyslider', 'true');
|
|
|
|
|
|
|
|
this.classList.add('mdl-slider');
|
|
|
|
this.classList.add('mdl-js-slider');
|
|
|
|
|
|
|
|
if (browser.noFlex) {
|
|
|
|
this.classList.add('slider-no-webkit-thumb');
|
|
|
|
}
|
|
|
|
if (!layoutManager.mobile) {
|
|
|
|
this.classList.add('mdl-slider-hoverthumb');
|
|
|
|
}
|
2019-11-09 11:48:52 +03:00
|
|
|
if (layoutManager.tv) {
|
|
|
|
this.classList.add('show-focus');
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
var containerElement = this.parentNode;
|
|
|
|
containerElement.classList.add('mdl-slider-container');
|
|
|
|
|
|
|
|
var htmlToInsert = '';
|
|
|
|
|
|
|
|
if (!supportsNativeProgressStyle) {
|
2019-01-22 15:08:02 +01:00
|
|
|
htmlToInsert += '<div class="mdl-slider-background-flex-container">';
|
2019-01-10 15:39:37 +03:00
|
|
|
htmlToInsert += '<div class="mdl-slider-background-flex">';
|
|
|
|
htmlToInsert += '<div class="mdl-slider-background-flex-inner">';
|
|
|
|
|
|
|
|
// the more of these, the more ranges we can display
|
|
|
|
htmlToInsert += '<div class="mdl-slider-background-upper"></div>';
|
|
|
|
|
|
|
|
if (enableWidthWithTransform) {
|
|
|
|
htmlToInsert += '<div class="mdl-slider-background-lower mdl-slider-background-lower-withtransform"></div>';
|
|
|
|
} else {
|
|
|
|
htmlToInsert += '<div class="mdl-slider-background-lower"></div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlToInsert += '</div>';
|
|
|
|
htmlToInsert += '</div>';
|
2019-01-22 15:08:02 +01:00
|
|
|
htmlToInsert += '</div>';
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
htmlToInsert += '<div class="sliderBubble hide"></div>';
|
|
|
|
|
|
|
|
containerElement.insertAdjacentHTML('beforeend', htmlToInsert);
|
|
|
|
|
|
|
|
this.backgroundLower = containerElement.querySelector('.mdl-slider-background-lower');
|
|
|
|
this.backgroundUpper = containerElement.querySelector('.mdl-slider-background-upper');
|
|
|
|
var sliderBubble = containerElement.querySelector('.sliderBubble');
|
|
|
|
|
|
|
|
var hasHideClass = sliderBubble.classList.contains('hide');
|
|
|
|
|
|
|
|
dom.addEventListener(this, 'input', function (e) {
|
|
|
|
this.dragging = true;
|
|
|
|
|
|
|
|
updateBubble(this, this.value, sliderBubble);
|
|
|
|
|
|
|
|
if (hasHideClass) {
|
|
|
|
sliderBubble.classList.remove('hide');
|
|
|
|
hasHideClass = false;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
|
|
|
|
dom.addEventListener(this, 'change', function () {
|
|
|
|
this.dragging = false;
|
|
|
|
updateValues.call(this);
|
|
|
|
|
|
|
|
sliderBubble.classList.add('hide');
|
|
|
|
hasHideClass = true;
|
|
|
|
|
|
|
|
}, {
|
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
|
2019-04-26 23:00:44 +00:00
|
|
|
dom.addEventListener(this, (window.PointerEvent ? 'pointermove' : 'mousemove'), function (e) {
|
|
|
|
|
|
|
|
if (!this.dragging) {
|
|
|
|
var rect = this.getBoundingClientRect();
|
|
|
|
var clientX = e.clientX;
|
|
|
|
var bubbleValue = (clientX - rect.left) / rect.width;
|
|
|
|
bubbleValue *= 100;
|
|
|
|
updateBubble(this, bubbleValue, sliderBubble);
|
|
|
|
|
|
|
|
if (hasHideClass) {
|
|
|
|
sliderBubble.classList.remove('hide');
|
|
|
|
hasHideClass = false;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2019-04-26 23:00:44 +00:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-04-26 23:00:44 +00:00
|
|
|
}, {
|
|
|
|
passive: true
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-04-26 23:00:44 +00:00
|
|
|
dom.addEventListener(this, (window.PointerEvent ? 'pointerleave' : 'mouseleave'), function () {
|
|
|
|
sliderBubble.classList.add('hide');
|
|
|
|
hasHideClass = true;
|
|
|
|
}, {
|
|
|
|
passive: true
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (!supportsNativeProgressStyle) {
|
|
|
|
|
|
|
|
if (supportsValueSetOverride) {
|
|
|
|
this.addEventListener('valueset', updateValues);
|
|
|
|
} else {
|
|
|
|
startInterval(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-09 11:48:52 +03:00
|
|
|
/**
|
|
|
|
* Keyboard dragging timeout.
|
|
|
|
* After this delay "change" event will be fired.
|
|
|
|
*/
|
|
|
|
var KeyboardDraggingTimeout = 1000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyboard dragging timer.
|
|
|
|
*/
|
|
|
|
var keyboardDraggingTimer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start keyboard dragging.
|
|
|
|
*
|
|
|
|
* @param {Object} elem slider itself
|
|
|
|
*/
|
|
|
|
function startKeyboardDragging(elem) {
|
|
|
|
elem.keyboardDragging = true;
|
|
|
|
|
|
|
|
clearTimeout(keyboardDraggingTimer);
|
|
|
|
keyboardDraggingTimer = setTimeout(function () {
|
|
|
|
finishKeyboardDragging(elem);
|
|
|
|
}, KeyboardDraggingTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish keyboard dragging.
|
|
|
|
*
|
|
|
|
* @param {Object} elem slider itself
|
|
|
|
*/
|
|
|
|
function finishKeyboardDragging(elem) {
|
|
|
|
clearTimeout(keyboardDraggingTimer);
|
|
|
|
keyboardDraggingTimer = undefined;
|
|
|
|
|
|
|
|
elem.keyboardDragging = false;
|
|
|
|
|
|
|
|
var event = new Event('change', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: false
|
|
|
|
});
|
|
|
|
elem.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do step by delta.
|
|
|
|
*
|
|
|
|
* @param {Object} elem slider itself
|
|
|
|
* @param {number} delta step amount
|
|
|
|
*/
|
|
|
|
function stepKeyboard(elem, delta) {
|
|
|
|
startKeyboardDragging(elem);
|
|
|
|
|
|
|
|
elem.value = Math.max(elem.min, Math.min(elem.max, parseFloat(elem.value) + delta));
|
|
|
|
|
|
|
|
var event = new Event('input', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: false
|
|
|
|
});
|
|
|
|
elem.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle KeyDown event
|
|
|
|
*/
|
|
|
|
function onKeyDown(e) {
|
2020-01-19 01:09:42 +03:00
|
|
|
switch (keyboardnavigation.getKeyName(e)) {
|
2019-11-09 11:48:52 +03:00
|
|
|
case 'ArrowLeft':
|
|
|
|
case 'Left':
|
|
|
|
stepKeyboard(this, -this.keyboardStepDown || -1);
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-13 00:19:00 +03:00
|
|
|
break;
|
2019-11-09 11:48:52 +03:00
|
|
|
case 'ArrowRight':
|
|
|
|
case 'Right':
|
|
|
|
stepKeyboard(this, this.keyboardStepUp || 1);
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-13 00:19:00 +03:00
|
|
|
break;
|
2019-11-09 11:48:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable keyboard dragging.
|
|
|
|
*/
|
|
|
|
EmbySliderPrototype.enableKeyboardDragging = function () {
|
|
|
|
if (!this.keyboardDraggingEnabled) {
|
|
|
|
this.addEventListener('keydown', onKeyDown);
|
|
|
|
this.keyboardDraggingEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set steps for keyboard input.
|
|
|
|
*
|
|
|
|
* @param {number} stepDown step to reduce
|
|
|
|
* @param {number} stepUp step to increase
|
|
|
|
*/
|
|
|
|
EmbySliderPrototype.setKeyboardSteps = function (stepDown, stepUp) {
|
|
|
|
this.keyboardStepDown = stepDown || stepUp || 1;
|
|
|
|
this.keyboardStepUp = stepUp || stepDown || 1;
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function setRange(elem, startPercent, endPercent) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var style = elem.style;
|
2019-01-10 15:39:37 +03:00
|
|
|
style.left = Math.max(startPercent, 0) + '%';
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var widthPercent = endPercent - startPercent;
|
2019-01-10 15:39:37 +03:00
|
|
|
style.width = Math.max(Math.min(widthPercent, 100), 0) + '%';
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapRangesFromRuntimeToPercent(ranges, runtime) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (!runtime) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ranges.map(function (r) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
return {
|
2019-01-10 15:39:37 +03:00
|
|
|
start: (r.start / runtime) * 100,
|
|
|
|
end: (r.end / runtime) * 100
|
|
|
|
};
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
EmbySliderPrototype.setBufferedRanges = function (ranges, runtime, position) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var elem = this.backgroundUpper;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!elem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runtime != null) {
|
|
|
|
ranges = mapRangesFromRuntimeToPercent(ranges, runtime);
|
|
|
|
|
|
|
|
position = (position / runtime) * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0, length = ranges.length; i < length; i++) {
|
|
|
|
|
|
|
|
var range = ranges[i];
|
|
|
|
|
|
|
|
if (position != null) {
|
|
|
|
if (position >= range.end) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
setRange(elem, range.start, range.end);
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
setRange(elem, 0, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbySliderPrototype.setIsClear = function (isClear) {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var backgroundLower = this.backgroundLower;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (backgroundLower) {
|
|
|
|
if (isClear) {
|
|
|
|
backgroundLower.classList.add('mdl-slider-background-lower-clear');
|
|
|
|
} else {
|
|
|
|
backgroundLower.classList.remove('mdl-slider-background-lower-clear');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function startInterval(range) {
|
|
|
|
var interval = range.interval;
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
range.interval = setInterval(updateValues.bind(range), 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmbySliderPrototype.detachedCallback = function () {
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
var interval = this.interval;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
this.interval = null;
|
|
|
|
this.backgroundUpper = null;
|
|
|
|
this.backgroundLower = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
document.registerElement('emby-slider', {
|
2018-10-23 01:05:09 +03:00
|
|
|
prototype: EmbySliderPrototype,
|
2019-01-10 15:39:37 +03:00
|
|
|
extends: 'input'
|
|
|
|
});
|
2019-04-26 23:00:44 +00:00
|
|
|
});
|