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

100 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-07-02 11:05:40 -07:00
define(['browser', 'css!./emby-collapse', 'registerElement'], function (browser) {
2016-10-05 21:28:10 -07:00
'use strict';
2016-07-02 11:05:40 -07:00
var EmbyButtonPrototype = Object.create(HTMLDivElement.prototype);
function slideDownToShow(button, elem) {
elem.classList.remove('hide');
elem.classList.add('expanded');
elem.style.height = 'auto';
var height = elem.offsetHeight + 'px';
elem.style.height = '0';
2016-10-05 21:28:10 -07:00
// trigger reflow
var newHeight = elem.offsetHeight;
2016-07-02 11:05:40 -07:00
elem.style.height = height;
setTimeout(function () {
if (elem.classList.contains('expanded')) {
elem.classList.remove('hide');
} else {
elem.classList.add('hide');
}
2016-07-04 15:53:15 -07:00
elem.style.height = 'auto';
2016-07-02 11:05:40 -07:00
}, 300);
var icon = button.querySelector('i');
//icon.innerHTML = 'expand_less';
2016-08-01 22:55:52 -07:00
icon.classList.add('emby-collapse-expandIconExpanded');
2016-07-02 11:05:40 -07:00
}
function slideUpToHide(button, elem) {
2016-07-04 15:53:15 -07:00
elem.style.height = elem.offsetHeight + 'px';
2016-10-05 21:28:10 -07:00
// trigger reflow
var newHeight = elem.offsetHeight;
2016-07-04 15:53:15 -07:00
2016-07-02 11:05:40 -07:00
elem.classList.remove('expanded');
elem.style.height = '0';
setTimeout(function () {
if (elem.classList.contains('expanded')) {
elem.classList.remove('hide');
} else {
elem.classList.add('hide');
}
}, 300);
var icon = button.querySelector('i');
//icon.innerHTML = 'expand_more';
2016-08-01 22:55:52 -07:00
icon.classList.remove('emby-collapse-expandIconExpanded');
2016-07-02 11:05:40 -07:00
}
function onButtonClick(e) {
2016-10-05 21:28:10 -07:00
var button = this;
var collapseContent = button.parentNode.querySelector('.collapseContent');
2016-07-02 11:05:40 -07:00
2016-07-02 19:47:39 -07:00
if (collapseContent.expanded) {
collapseContent.expanded = false;
2016-10-05 21:28:10 -07:00
slideUpToHide(button, collapseContent);
2016-07-02 11:05:40 -07:00
} else {
2016-07-02 19:47:39 -07:00
collapseContent.expanded = true;
2016-10-05 21:28:10 -07:00
slideDownToShow(button, collapseContent);
2016-07-02 11:05:40 -07:00
}
}
EmbyButtonPrototype.attachedCallback = function () {
2016-08-02 18:32:16 -07:00
if (this.classList.contains('emby-collapse')) {
2016-07-02 11:05:40 -07:00
return;
}
2016-08-02 18:32:16 -07:00
this.classList.add('emby-collapse');
2016-07-02 11:05:40 -07:00
var collapseContent = this.querySelector('.collapseContent');
if (collapseContent) {
collapseContent.classList.add('hide');
}
var title = this.getAttribute('title');
2016-10-05 21:28:10 -07:00
var html = '<button is="emby-button" type="button" on-click="toggleExpand" id="expandButton" class="emby-collapsible-button iconRight"><h3 class="emby-collapsible-title" title="' + title + '">' + title + '</h3><i class="md-icon emby-collapse-expandIcon">expand_more</i></button>';
2016-07-02 11:05:40 -07:00
this.insertAdjacentHTML('afterbegin', html);
2016-07-11 09:56:16 -07:00
var button = this.querySelector('.emby-collapsible-button');
button.addEventListener('click', onButtonClick);
2016-10-05 21:28:10 -07:00
if (this.getAttribute('data-expanded') === 'true') {
2016-07-11 09:56:16 -07:00
onButtonClick.call(button);
}
2016-07-02 11:05:40 -07:00
};
document.registerElement('emby-collapse', {
prototype: EmbyButtonPrototype,
extends: 'div'
});
});