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

107 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-08-07 12:43:52 -07:00
define(['layoutManager', 'browser', 'dom', 'css!./emby-input', 'registerElement'], function (layoutManager, browser, dom) {
2016-05-28 11:03:38 -07:00
var EmbyInputPrototype = Object.create(HTMLInputElement.prototype);
2016-06-03 12:32:10 -07:00
var inputId = 0;
2016-06-06 14:12:44 -07:00
var supportsFloatingLabel = false;
2016-06-03 12:32:10 -07:00
2016-06-04 17:17:35 -07:00
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
var descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
2016-06-13 12:02:48 -07:00
// descriptor returning null in webos
if (descriptor && descriptor.configurable) {
2016-06-04 21:49:29 -07:00
var baseSetMethod = descriptor.set;
descriptor.set = function (value) {
baseSetMethod.call(this, value);
2016-06-13 12:02:48 -07:00
2016-06-04 21:49:29 -07:00
this.dispatchEvent(new CustomEvent('valueset', {
bubbles: false,
cancelable: false
}));
}
Object.defineProperty(HTMLInputElement.prototype, 'value', descriptor);
2016-06-06 14:12:44 -07:00
supportsFloatingLabel = true;
2016-06-04 17:17:35 -07:00
}
}
2016-05-28 11:03:38 -07:00
EmbyInputPrototype.createdCallback = function () {
if (!this.id) {
2016-06-03 12:32:10 -07:00
this.id = 'embyinput' + inputId;
inputId++;
2016-05-28 11:03:38 -07:00
}
};
EmbyInputPrototype.attachedCallback = function () {
2016-08-02 21:30:22 -07:00
if (this.classList.contains('emby-input')) {
2016-05-29 11:42:03 -07:00
return;
}
2016-05-28 11:03:38 -07:00
2016-08-02 21:30:22 -07:00
this.classList.add('emby-input');
2016-05-29 11:42:03 -07:00
var parentNode = this.parentNode;
var label = this.ownerDocument.createElement('label');
label.innerHTML = this.getAttribute('label') || '';
label.classList.add('inputLabel');
2016-05-28 11:03:38 -07:00
2016-08-02 21:30:22 -07:00
var instanceSupportsFloat = supportsFloatingLabel && this.type != 'date';
2016-06-06 14:12:44 -07:00
2016-05-29 11:42:03 -07:00
label.htmlFor = this.id;
parentNode.insertBefore(label, this);
var div = document.createElement('div');
div.classList.add('emby-input-selectionbar');
parentNode.insertBefore(div, this.nextSibling);
function onChange() {
if (this.value) {
2016-08-02 21:30:22 -07:00
label.classList.remove('inputLabel-float');
2016-05-29 11:42:03 -07:00
} else {
2016-08-02 21:30:22 -07:00
if (instanceSupportsFloat) {
label.classList.add('inputLabel-float');
}
2016-05-29 11:42:03 -07:00
}
2016-05-28 11:03:38 -07:00
}
2016-05-29 11:42:03 -07:00
2016-08-07 12:43:52 -07:00
dom.addEventListener(this, 'focus', function () {
2016-05-29 11:42:03 -07:00
onChange.call(this);
2016-08-02 21:30:22 -07:00
label.classList.add('inputLabelFocused');
label.classList.remove('inputLabelUnfocused');
2016-08-07 12:43:52 -07:00
}, {
passive: true
2016-05-29 11:42:03 -07:00
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(this, 'blur', function () {
2016-06-04 17:17:35 -07:00
onChange.call(this);
2016-08-02 21:30:22 -07:00
label.classList.remove('inputLabelFocused');
label.classList.add('inputLabelUnfocused');
2016-08-07 12:43:52 -07:00
}, {
passive: true
2016-05-29 11:42:03 -07:00
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(this, 'change', onChange, {
passive: true
});
dom.addEventListener(this, 'input', onChange, {
passive: true
});
dom.addEventListener(this, 'valueset', onChange, {
passive: true
});
2016-05-29 11:42:03 -07:00
onChange.call(this);
2016-06-11 08:55:39 -07:00
this.label = function (text) {
label.innerHTML = text;
};
2016-05-29 12:41:39 -07:00
};
2016-05-28 11:03:38 -07:00
document.registerElement('emby-input', {
prototype: EmbyInputPrototype,
extends: 'input'
});
});