discourse/app/assets/javascripts/select-kit/mixins/dom-helpers.js.es6

357 lines
9.7 KiB
Plaintext
Raw Normal View History

import { next } from "@ember/runloop";
import { schedule } from "@ember/runloop";
import { on } from "discourse-common/utils/decorators";
import Mixin from "@ember/object/mixin";
2017-11-09 13:57:53 -05:00
export default Mixin.create({
2017-10-19 15:51:08 -04:00
init() {
this._super(...arguments);
2017-10-19 15:51:08 -04:00
this._previousScrollParentOverflow = null;
this._previousCSSContext = null;
this.selectionSelector = ".choice";
this.filterInputSelector = ".filter-input";
this.rowSelector = ".select-kit-row";
this.collectionSelector = ".select-kit-collection";
this.headerSelector = ".select-kit-header";
this.bodySelector = ".select-kit-body";
this.wrapperSelector = ".select-kit-wrapper";
this.scrollableParentSelector = ".modal-body";
2019-06-26 11:09:52 -04:00
this.fixedPlaceholderSelector = `.select-kit-fixed-placeholder-${this.elementId}`;
2017-10-19 15:51:08 -04:00
},
2018-06-15 11:03:24 -04:00
$findRowByValue(value) {
return $(
this.element.querySelector(`${this.rowSelector}[data-value='${value}']`)
);
2018-06-15 11:03:24 -04:00
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$header() {
return $(this.element && this.element.querySelector(this.headerSelector));
2018-06-15 11:03:24 -04:00
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$body() {
return $(this.element && this.element.querySelector(this.bodySelector));
2018-06-15 11:03:24 -04:00
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$wrapper() {
return $(this.element && this.element.querySelector(this.wrapperSelector));
2018-06-15 11:03:24 -04:00
},
2018-06-15 11:03:24 -04:00
$collection() {
return $(
this.element && this.element.querySelector(this.collectionSelector)
);
2018-06-15 11:03:24 -04:00
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$scrollableParent() {
return $(this.scrollableParentSelector);
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$fixedPlaceholder() {
return $(this.fixedPlaceholderSelector);
},
$rows() {
return this.$(`${this.rowSelector}:not(.no-content):not(.is-hidden)`);
2017-10-19 15:51:08 -04:00
},
2018-06-15 11:03:24 -04:00
$highlightedRow() {
return this.$rows().filter(".is-highlighted");
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$selectedRow() {
return this.$rows().filter(".is-selected");
},
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
$filterInput() {
return $(
this.element && this.element.querySelector(this.filterInputSelector)
);
2018-06-15 11:03:24 -04:00
},
2017-11-09 13:57:53 -05:00
_adjustPosition() {
this._applyDirection();
2017-11-09 13:57:53 -05:00
this._applyFixedPosition();
this._positionWrapper();
2017-10-19 15:51:08 -04:00
},
2017-11-09 13:57:53 -05:00
@on("willDestroyElement")
_clearState() {
this.$fixedPlaceholder().remove();
2017-10-19 15:51:08 -04:00
},
2017-11-09 13:57:53 -05:00
// use to collapse and remove focus
close(event) {
2017-11-09 13:57:53 -05:00
this.setProperties({ isFocused: false });
this.collapse(event);
2017-10-19 15:51:08 -04:00
},
focus() {
this.focusFilterOrHeader();
},
// try to focus filter and fallback to header if not present
focusFilterOrHeader() {
const context = this;
// next so we are sure it finised expand/collapse
next(() => {
schedule("afterRender", () => {
if (
!context.$filterInput() ||
!context.$filterInput().is(":visible") ||
context
.$filterInput()
.parent()
.hasClass("is-hidden")
) {
if (context.$header()) {
context.$header().focus();
} else {
$(context.element).focus();
}
} else {
if (this.site && this.site.isMobileDevice) {
this.expand();
} else {
context.$filterInput().focus();
}
}
});
});
2017-11-22 05:29:30 -05:00
},
2017-10-19 15:51:08 -04:00
2017-11-22 05:29:30 -05:00
expand() {
if (this.isExpanded) return;
2018-06-15 11:03:24 -04:00
this.setProperties({
isExpanded: true,
renderedBodyOnce: true,
isFocused: true
});
this.focusFilterOrHeader();
this.autoHighlight();
next(() => {
this._boundaryActionHandler("onExpand", this);
schedule("afterRender", () => {
if (!this.isDestroying && !this.isDestroyed) {
this._adjustPosition();
}
});
});
2017-10-19 15:51:08 -04:00
},
2017-11-09 13:57:53 -05:00
collapse() {
if (!this.isExpanded) return;
2017-11-09 13:57:53 -05:00
this.set("isExpanded", false);
next(() => {
this._boundaryActionHandler("onCollapse", this);
schedule("afterRender", () => {
if (!this.isDestroying && !this.isDestroyed) {
this._removeFixedPosition();
}
});
});
2017-10-19 15:51:08 -04:00
},
2017-11-09 13:57:53 -05:00
// lose focus of the component in two steps
// first collapse and keep focus and then remove focus
unfocus(event) {
if (this.isExpanded) {
this.collapse(event);
this.focus(event);
2017-11-09 13:57:53 -05:00
} else {
this.close(event);
2017-11-09 13:57:53 -05:00
}
},
_destroyEvent(event) {
2017-10-19 15:51:08 -04:00
event.preventDefault();
event.stopPropagation();
2017-11-09 13:57:53 -05:00
},
_applyDirection() {
let options = { left: "auto", bottom: "auto", top: "auto" };
const discourseHeader = $(".d-header")[0];
2018-06-15 11:03:24 -04:00
const discourseHeaderHeight = discourseHeader
? discourseHeader.getBoundingClientRect().top +
this._computedStyle(discourseHeader, "height")
: 0;
const bodyHeight = this._computedStyle(this.$body()[0], "height");
const componentHeight = this._computedStyle(this.element, "height");
const offsetTop = this.element.getBoundingClientRect().top;
const offsetBottom = this.element.getBoundingClientRect().bottom;
const windowWidth = $(window).width();
2017-11-09 13:57:53 -05:00
if (this.fullWidthOnMobile && (this.site && this.site.isMobileDevice)) {
2017-11-09 13:57:53 -05:00
const margin = 10;
const relativeLeft =
$(this.element).offset().left - $(window).scrollLeft();
2017-11-09 13:57:53 -05:00
options.left = margin - relativeLeft;
options.width = windowWidth - margin * 2;
options.maxWidth = options.minWidth = "unset";
} else {
2018-06-15 11:03:24 -04:00
const parentWidth = this.$scrollableParent().length
? this.$scrollableParent().width()
: windowWidth;
const bodyWidth = this._computedStyle(this.$body()[0], "width");
2017-11-09 13:57:53 -05:00
let spaceToLeftEdge;
if (this.$scrollableParent().length) {
spaceToLeftEdge =
$(this.element).offset().left -
this.$scrollableParent().offset().left;
} else {
spaceToLeftEdge = this.element.getBoundingClientRect().left;
}
let isLeftAligned = true;
const spaceToRightEdge = parentWidth - spaceToLeftEdge;
const elementWidth = this.element.getBoundingClientRect().width;
if (spaceToRightEdge > spaceToLeftEdge + elementWidth) {
isLeftAligned = false;
}
if (isLeftAligned) {
this.element.classList.add("is-left-aligned");
this.element.classList.remove("is-right-aligned");
if (this._isRTL()) {
options.right = this.horizontalOffset;
} else {
options.left = -bodyWidth + elementWidth - this.horizontalOffset;
}
2017-11-09 13:57:53 -05:00
} else {
this.element.classList.add("is-right-aligned");
this.element.classList.remove("is-left-aligned");
if (this._isRTL()) {
options.right = -bodyWidth + elementWidth - this.horizontalOffset;
} else {
options.left = this.horizontalOffset;
}
2017-11-09 13:57:53 -05:00
}
}
const fullHeight = this.verticalOffset + bodyHeight + componentHeight;
const hasBelowSpace = $(window).height() - offsetBottom - fullHeight >= -1;
const hasAboveSpace = offsetTop - fullHeight - discourseHeaderHeight >= -1;
const headerHeight = this._computedStyle(this.$header()[0], "height");
2017-11-09 13:57:53 -05:00
if (hasBelowSpace || (!hasBelowSpace && !hasAboveSpace)) {
this.element.classList.add("is-below");
this.element.classList.remove("is-above");
options.top = headerHeight + this.verticalOffset;
2017-11-09 13:57:53 -05:00
} else {
this.element.classList.add("is-above");
this.element.classList.remove("is-below");
options.bottom = headerHeight + this.verticalOffset;
2017-11-09 13:57:53 -05:00
}
this.$body().css(options);
},
_applyFixedPosition() {
if (this.isExpanded !== true) return;
if (this.$fixedPlaceholder().length) return;
if (!this.$scrollableParent().length) return;
2017-11-09 13:57:53 -05:00
const width = this._computedStyle(this.element, "width");
const height = this._computedStyle(this.element, "height");
2017-11-09 13:57:53 -05:00
2018-06-15 11:03:24 -04:00
this._previousScrollParentOverflow =
this._previousScrollParentOverflow ||
this.$scrollableParent().css("overflow");
2017-11-09 13:57:53 -05:00
this._previousCSSContext = this._previousCSSContext || {
width,
minWidth: this.element.style.minWidth,
maxWidth: this.element.style.maxWidth,
top: this.element.style.top,
left: this.element.style.left,
marginLeft: this.element.style.marginLeft,
marginRight: this.element.style.marginRight,
position: this.element.style.position
2017-11-09 13:57:53 -05:00
};
const componentStyles = {
top: this.element.getBoundingClientRect().top,
2017-11-09 13:57:53 -05:00
width,
left: this.element.getBoundingClientRect().left,
marginLeft: 0,
marginRight: 0,
2017-11-09 13:57:53 -05:00
minWidth: "unset",
maxWidth: "unset",
position: "fixed"
2017-11-09 13:57:53 -05:00
};
2018-06-15 11:03:24 -04:00
const $placeholderTemplate = $(
`<div class='select-kit-fixed-placeholder-${this.elementId}'></div>`
);
$placeholderTemplate.css({
display: "inline-block",
width,
height,
"margin-bottom": this.element.style.marginBottom,
"vertical-align": "middle"
});
2017-11-09 13:57:53 -05:00
$(this.element)
2018-06-15 11:03:24 -04:00
.before($placeholderTemplate)
.css(componentStyles);
2017-11-09 13:57:53 -05:00
this.$scrollableParent().css({ overflow: "hidden" });
2017-11-09 13:57:53 -05:00
},
_removeFixedPosition() {
this.$fixedPlaceholder().remove();
2017-11-09 13:57:53 -05:00
if (!this.element || this.isDestroying || this.isDestroyed) return;
if (this.$scrollableParent().length === 0) return;
$(this.element).css(this._previousCSSContext || {});
2018-06-15 11:03:24 -04:00
this.$scrollableParent().css(
"overflow",
this._previousScrollParentOverflow || {}
);
2017-11-09 13:57:53 -05:00
},
_positionWrapper() {
const elementWidth = this._computedStyle(this.element, "width");
const headerHeight = this._computedStyle(this.$header()[0], "height");
const bodyHeight = this._computedStyle(this.$body()[0], "height");
2017-11-09 13:57:53 -05:00
this.$wrapper().css({
width: elementWidth,
height: headerHeight + bodyHeight
2017-11-09 13:57:53 -05:00
});
},
_isRTL() {
return $("html").css("direction") === "rtl";
},
_computedStyle(element, style) {
if (!element) return 0;
let value;
if (window.getComputedStyle) {
value = window.getComputedStyle(element, null)[style];
} else {
value = $(element).css(style);
}
return this._getFloat(value);
},
_getFloat(value) {
value = parseFloat(value);
return $.isNumeric(value) ? value : 0;
}
2017-10-19 15:51:08 -04:00
});