2019-10-30 09:48:24 -04:00
|
|
|
import { next } from "@ember/runloop";
|
|
|
|
import { schedule } from "@ember/runloop";
|
2019-11-07 16:38:28 -05:00
|
|
|
import { on } from "discourse-common/utils/decorators";
|
2019-10-30 16:13:48 -04:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2019-10-30 15:03:08 -04:00
|
|
|
export default Mixin.create({
|
2017-10-19 15:51:08 -04:00
|
|
|
init() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2017-10-19 15:51:08 -04:00
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
this._previousScrollParentOverflow = null;
|
|
|
|
this._previousCSSContext = null;
|
2018-03-22 06:29:55 -04:00
|
|
|
this.selectionSelector = ".choice";
|
2017-11-21 05:53:09 -05:00
|
|
|
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";
|
2017-12-28 10:12:45 -05:00
|
|
|
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) {
|
2019-07-16 06:45:15 -04:00
|
|
|
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() {
|
2019-07-16 06:45:15 -04:00
|
|
|
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() {
|
2019-07-16 06:45:15 -04:00
|
|
|
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() {
|
2019-07-16 06:45:15 -04:00
|
|
|
return $(this.element && this.element.querySelector(this.wrapperSelector));
|
2018-06-15 11:03:24 -04:00
|
|
|
},
|
2017-12-28 10:12:45 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
$collection() {
|
2019-07-16 06:45:15 -04:00
|
|
|
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);
|
|
|
|
},
|
2017-12-28 10:12:45 -05:00
|
|
|
|
|
|
|
$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() {
|
2019-07-16 06:45:15 -04:00
|
|
|
return $(
|
|
|
|
this.element && this.element.querySelector(this.filterInputSelector)
|
|
|
|
);
|
2018-06-15 11:03:24 -04:00
|
|
|
},
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
_adjustPosition() {
|
2018-12-17 06:29:51 -05:00
|
|
|
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() {
|
2017-12-28 10:12:45 -05:00
|
|
|
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
|
2017-11-21 05:53:09 -05:00
|
|
|
close(event) {
|
2017-11-09 13:57:53 -05:00
|
|
|
this.setProperties({ isFocused: false });
|
2018-08-03 16:41:37 -04:00
|
|
|
this.collapse(event);
|
2017-10-19 15:51:08 -04:00
|
|
|
},
|
|
|
|
|
2018-03-05 04:55:20 -05:00
|
|
|
focus() {
|
2018-07-23 12:19:40 -04:00
|
|
|
this.focusFilterOrHeader();
|
2018-03-22 06:29:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// try to focus filter and fallback to header if not present
|
|
|
|
focusFilterOrHeader() {
|
2018-04-02 16:50:20 -04:00
|
|
|
const context = this;
|
2018-03-22 06:29:55 -04:00
|
|
|
// next so we are sure it finised expand/collapse
|
2019-10-30 09:48:24 -04:00
|
|
|
next(() => {
|
|
|
|
schedule("afterRender", () => {
|
2018-08-09 04:45:53 -04:00
|
|
|
if (
|
|
|
|
!context.$filterInput() ||
|
|
|
|
!context.$filterInput().is(":visible") ||
|
|
|
|
context
|
|
|
|
.$filterInput()
|
|
|
|
.parent()
|
|
|
|
.hasClass("is-hidden")
|
|
|
|
) {
|
2018-07-23 12:19:40 -04:00
|
|
|
if (context.$header()) {
|
|
|
|
context.$header().focus();
|
|
|
|
} else {
|
|
|
|
$(context.element).focus();
|
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
} else {
|
2018-08-09 08:23:09 -04:00
|
|
|
if (this.site && this.site.isMobileDevice) {
|
|
|
|
this.expand();
|
|
|
|
} else {
|
|
|
|
context.$filterInput().focus();
|
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
}
|
|
|
|
});
|
2017-11-21 12:07:10 -05:00
|
|
|
});
|
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() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.isExpanded) return;
|
2019-03-04 04:30:22 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.setProperties({
|
|
|
|
isExpanded: true,
|
|
|
|
renderedBodyOnce: true,
|
|
|
|
isFocused: true
|
|
|
|
});
|
2018-03-22 06:29:55 -04:00
|
|
|
this.focusFilterOrHeader();
|
2018-03-05 04:55:20 -05:00
|
|
|
this.autoHighlight();
|
2018-12-27 17:38:01 -05:00
|
|
|
|
2019-10-30 09:48:24 -04:00
|
|
|
next(() => {
|
2018-12-27 17:38:01 -05:00
|
|
|
this._boundaryActionHandler("onExpand", this);
|
2019-10-30 09:48:24 -04:00
|
|
|
schedule("afterRender", () => {
|
2018-12-27 17:38:01 -05:00
|
|
|
if (!this.isDestroying && !this.isDestroyed) {
|
|
|
|
this._adjustPosition();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-10-19 15:51:08 -04:00
|
|
|
},
|
|
|
|
|
2017-11-09 13:57:53 -05:00
|
|
|
collapse() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (!this.isExpanded) return;
|
2019-03-04 04:30:22 -05:00
|
|
|
|
2017-11-09 13:57:53 -05:00
|
|
|
this.set("isExpanded", false);
|
2018-08-03 16:41:37 -04:00
|
|
|
|
2019-10-30 09:48:24 -04:00
|
|
|
next(() => {
|
2018-08-03 16:41:37 -04:00
|
|
|
this._boundaryActionHandler("onCollapse", this);
|
2019-10-30 09:48:24 -04:00
|
|
|
schedule("afterRender", () => {
|
2018-12-27 17:38:01 -05:00
|
|
|
if (!this.isDestroying && !this.isDestroyed) {
|
|
|
|
this._removeFixedPosition();
|
|
|
|
}
|
|
|
|
});
|
2018-08-03 16:41:37 -04:00
|
|
|
});
|
2017-10-19 15:51:08 -04:00
|
|
|
},
|
|
|
|
|
2017-11-09 13:57:53 -05:00
|
|
|
// lose focus of the component in two steps
|
2017-11-21 05:53:09 -05:00
|
|
|
// first collapse and keep focus and then remove focus
|
|
|
|
unfocus(event) {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.isExpanded) {
|
2017-11-21 05:53:09 -05:00
|
|
|
this.collapse(event);
|
|
|
|
this.focus(event);
|
2017-11-09 13:57:53 -05:00
|
|
|
} else {
|
2017-11-21 05:53:09 -05:00
|
|
|
this.close(event);
|
2017-11-09 13:57:53 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -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" };
|
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
const discourseHeader = $(".d-header")[0];
|
2018-06-15 11:03:24 -04:00
|
|
|
const discourseHeaderHeight = discourseHeader
|
|
|
|
? discourseHeader.getBoundingClientRect().top +
|
|
|
|
this._computedStyle(discourseHeader, "height")
|
|
|
|
: 0;
|
2017-12-28 10:12:45 -05:00
|
|
|
const bodyHeight = this._computedStyle(this.$body()[0], "height");
|
2019-05-27 04:15:39 -04:00
|
|
|
const componentHeight = this._computedStyle(this.element, "height");
|
|
|
|
const offsetTop = this.element.getBoundingClientRect().top;
|
|
|
|
const offsetBottom = this.element.getBoundingClientRect().bottom;
|
2018-03-01 05:53:14 -05:00
|
|
|
const windowWidth = $(window).width();
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2019-05-27 04:42:53 -04:00
|
|
|
if (this.fullWidthOnMobile && (this.site && this.site.isMobileDevice)) {
|
2017-11-09 13:57:53 -05:00
|
|
|
const margin = 10;
|
2019-07-16 06:45:15 -04:00
|
|
|
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;
|
2017-12-28 10:12:45 -05:00
|
|
|
const bodyWidth = this._computedStyle(this.$body()[0], "width");
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2018-12-28 13:46:31 -05:00
|
|
|
let spaceToLeftEdge;
|
2018-03-01 05:53:14 -05:00
|
|
|
if (this.$scrollableParent().length) {
|
2018-12-28 13:46:31 -05:00
|
|
|
spaceToLeftEdge =
|
2019-07-16 06:45:15 -04:00
|
|
|
$(this.element).offset().left -
|
|
|
|
this.$scrollableParent().offset().left;
|
2018-03-01 05:53:14 -05:00
|
|
|
} else {
|
2019-05-27 04:15:39 -04:00
|
|
|
spaceToLeftEdge = this.element.getBoundingClientRect().left;
|
2018-03-01 05:53:14 -05:00
|
|
|
}
|
|
|
|
|
2018-12-28 13:46:31 -05:00
|
|
|
let isLeftAligned = true;
|
|
|
|
const spaceToRightEdge = parentWidth - spaceToLeftEdge;
|
2019-05-27 04:15:39 -04:00
|
|
|
const elementWidth = this.element.getBoundingClientRect().width;
|
2018-12-28 13:46:31 -05:00
|
|
|
if (spaceToRightEdge > spaceToLeftEdge + elementWidth) {
|
|
|
|
isLeftAligned = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLeftAligned) {
|
2019-07-16 06:45:15 -04:00
|
|
|
this.element.classList.add("is-left-aligned");
|
|
|
|
this.element.classList.remove("is-right-aligned");
|
2018-12-28 13:46:31 -05:00
|
|
|
|
|
|
|
if (this._isRTL()) {
|
2019-05-27 04:15:39 -04:00
|
|
|
options.right = this.horizontalOffset;
|
2018-12-28 13:46:31 -05:00
|
|
|
} else {
|
2019-05-27 04:42:53 -04:00
|
|
|
options.left = -bodyWidth + elementWidth - this.horizontalOffset;
|
2018-12-28 13:46:31 -05:00
|
|
|
}
|
2017-11-09 13:57:53 -05:00
|
|
|
} else {
|
2019-07-16 06:45:15 -04:00
|
|
|
this.element.classList.add("is-right-aligned");
|
|
|
|
this.element.classList.remove("is-left-aligned");
|
2018-12-28 13:46:31 -05:00
|
|
|
|
|
|
|
if (this._isRTL()) {
|
2019-05-27 04:42:53 -04:00
|
|
|
options.right = -bodyWidth + elementWidth - this.horizontalOffset;
|
2018-12-28 13:46:31 -05:00
|
|
|
} else {
|
2019-05-27 04:15:39 -04:00
|
|
|
options.left = this.horizontalOffset;
|
2018-12-28 13:46:31 -05:00
|
|
|
}
|
2017-11-09 13:57:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:42:53 -04:00
|
|
|
const fullHeight = this.verticalOffset + bodyHeight + componentHeight;
|
2018-12-15 10:23:23 -05:00
|
|
|
const hasBelowSpace = $(window).height() - offsetBottom - fullHeight >= -1;
|
|
|
|
const hasAboveSpace = offsetTop - fullHeight - discourseHeaderHeight >= -1;
|
2017-12-28 10:12:45 -05:00
|
|
|
const headerHeight = this._computedStyle(this.$header()[0], "height");
|
2018-12-15 10:23:23 -05:00
|
|
|
|
2017-11-09 13:57:53 -05:00
|
|
|
if (hasBelowSpace || (!hasBelowSpace && !hasAboveSpace)) {
|
2019-07-16 06:45:15 -04:00
|
|
|
this.element.classList.add("is-below");
|
|
|
|
this.element.classList.remove("is-above");
|
2019-05-27 04:15:39 -04:00
|
|
|
options.top = headerHeight + this.verticalOffset;
|
2017-11-09 13:57:53 -05:00
|
|
|
} else {
|
2019-07-16 06:45:15 -04:00
|
|
|
this.element.classList.add("is-above");
|
|
|
|
this.element.classList.remove("is-below");
|
2019-05-27 04:15:39 -04:00
|
|
|
options.bottom = headerHeight + this.verticalOffset;
|
2017-11-09 13:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$body().css(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
_applyFixedPosition() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.isExpanded !== true) return;
|
2018-03-01 05:53:14 -05:00
|
|
|
if (this.$fixedPlaceholder().length) return;
|
|
|
|
if (!this.$scrollableParent().length) return;
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2019-05-27 04:15:39 -04: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
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
this._previousCSSContext = this._previousCSSContext || {
|
|
|
|
width,
|
2019-07-16 06:45:15 -04:00
|
|
|
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 = {
|
2019-05-27 04:15:39 -04:00
|
|
|
top: this.element.getBoundingClientRect().top,
|
2017-11-09 13:57:53 -05:00
|
|
|
width,
|
2019-05-27 04:15:39 -04:00
|
|
|
left: this.element.getBoundingClientRect().left,
|
2017-12-28 10:12:45 -05:00
|
|
|
marginLeft: 0,
|
|
|
|
marginRight: 0,
|
2017-11-09 13:57:53 -05:00
|
|
|
minWidth: "unset",
|
2017-12-28 10:12:45 -05:00
|
|
|
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>`
|
|
|
|
);
|
2017-12-28 10:12:45 -05:00
|
|
|
$placeholderTemplate.css({
|
|
|
|
display: "inline-block",
|
|
|
|
width,
|
|
|
|
height,
|
2019-07-16 06:45:15 -04:00
|
|
|
"margin-bottom": this.element.style.marginBottom,
|
2017-12-28 10:12:45 -05:00
|
|
|
"vertical-align": "middle"
|
|
|
|
});
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2019-07-16 06:45:15 -04:00
|
|
|
$(this.element)
|
2018-06-15 11:03:24 -04:00
|
|
|
.before($placeholderTemplate)
|
|
|
|
.css(componentStyles);
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
this.$scrollableParent().css({ overflow: "hidden" });
|
2017-11-09 13:57:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_removeFixedPosition() {
|
2017-12-28 10:12:45 -05:00
|
|
|
this.$fixedPlaceholder().remove();
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) return;
|
|
|
|
if (this.$scrollableParent().length === 0) return;
|
|
|
|
|
2019-07-16 06:45:15 -04:00
|
|
|
$(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() {
|
2019-05-27 04:15:39 -04:00
|
|
|
const elementWidth = this._computedStyle(this.element, "width");
|
2017-12-28 10:12:45 -05:00
|
|
|
const headerHeight = this._computedStyle(this.$header()[0], "height");
|
|
|
|
const bodyHeight = this._computedStyle(this.$body()[0], "height");
|
2017-11-09 13:57:53 -05:00
|
|
|
|
2017-12-28 10:12:45 -05:00
|
|
|
this.$wrapper().css({
|
|
|
|
width: elementWidth,
|
|
|
|
height: headerHeight + bodyHeight
|
2017-11-09 13:57:53 -05:00
|
|
|
});
|
|
|
|
},
|
2017-12-28 10:12:45 -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
|
|
|
});
|