2017-11-10 02:57:53 +08:00
|
|
|
import { on } from "ember-addons/ember-computed-decorators";
|
|
|
|
|
2017-10-20 03:51:08 +08:00
|
|
|
export default Ember.Mixin.create({
|
|
|
|
init() {
|
|
|
|
this._super();
|
|
|
|
|
2017-11-21 18:53:09 +08: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-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
$findRowByValue(value) { return this.$(`${this.rowSelector}[data-value='${value}']`); },
|
|
|
|
|
|
|
|
$header() { return this.$(this.headerSelector); },
|
|
|
|
|
|
|
|
$body() { return this.$(this.bodySelector); },
|
|
|
|
|
|
|
|
$collection() { return this.$(this.collectionSelector); },
|
|
|
|
|
|
|
|
$rows(withHidden) {
|
|
|
|
|
|
|
|
if (withHidden === true) {
|
|
|
|
return this.$(`${this.rowSelector}:not(.no-content)`);
|
|
|
|
} else {
|
|
|
|
return this.$(`${this.rowSelector}:not(.no-content):not(.is-hidden)`);
|
|
|
|
}
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
$highlightedRow() { return this.$rows().filter(".is-highlighted"); },
|
|
|
|
|
|
|
|
$selectedRow() { return this.$rows().filter(".is-selected"); },
|
|
|
|
|
|
|
|
$filterInput() { return this.$(this.filterInputSelector); },
|
|
|
|
|
|
|
|
@on("didRender")
|
|
|
|
_ajustPosition() {
|
2017-11-21 18:53:09 +08:00
|
|
|
$(`.select-kit-fixed-placeholder-${this.elementId}`).remove();
|
2017-11-10 02:57:53 +08:00
|
|
|
this.$collection().css("max-height", this.get("collectionHeight"));
|
|
|
|
this._applyFixedPosition();
|
|
|
|
this._applyDirection();
|
|
|
|
this._positionWrapper();
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
@on("didInsertElement")
|
|
|
|
_setupResizeListener() {
|
|
|
|
$(window).on("resize.select-kit", () => this.collapse() );
|
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
@on("willDestroyElement")
|
|
|
|
_clearState() {
|
2017-11-21 18:53:09 +08:00
|
|
|
$(window).off("resize.select-kit");
|
|
|
|
$(`.select-kit-fixed-placeholder-${this.elementId}`).remove();
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
// use to collapse and remove focus
|
2017-11-21 18:53:09 +08:00
|
|
|
close(event) {
|
|
|
|
this.collapse(event);
|
2017-11-10 02:57:53 +08:00
|
|
|
this.setProperties({ isFocused: false });
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
// force the component in a known default state
|
|
|
|
focus() {
|
2017-11-22 01:23:49 +08:00
|
|
|
Ember.run.schedule("afterRender", () => this.$header().focus());
|
|
|
|
},
|
|
|
|
|
2017-11-22 18:29:30 +08:00
|
|
|
// try to focus filter and fallback to header if not present
|
|
|
|
focusFilterOrHeader() {
|
2017-11-22 01:07:10 +08:00
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
if (this.$filterInput().is(":visible")) {
|
|
|
|
this.$filterInput().focus();
|
|
|
|
} else {
|
|
|
|
this.$header().focus();
|
|
|
|
}
|
|
|
|
});
|
2017-11-22 18:29:30 +08:00
|
|
|
},
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-11-22 18:29:30 +08:00
|
|
|
expand() {
|
|
|
|
if (this.get("isExpanded") === true) return;
|
|
|
|
this.setProperties({ isExpanded: true, renderedBodyOnce: true, isFocused: true });
|
|
|
|
this.focusFilterOrHeader();
|
2017-11-21 18:53:09 +08:00
|
|
|
this.autoHighlight();
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
collapse() {
|
|
|
|
this.set("isExpanded", false);
|
|
|
|
Ember.run.schedule("afterRender", () => this._removeFixedPosition() );
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
// lose focus of the component in two steps
|
2017-11-21 18:53:09 +08:00
|
|
|
// first collapse and keep focus and then remove focus
|
|
|
|
unfocus(event) {
|
2017-11-10 02:57:53 +08:00
|
|
|
if (this.get("isExpanded") === true) {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.collapse(event);
|
|
|
|
this.focus(event);
|
2017-11-10 02:57:53 +08:00
|
|
|
} else {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.close(event);
|
2017-11-10 02:57:53 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
_destroyEvent(event) {
|
2017-10-20 03:51:08 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2017-11-10 02:57:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_applyDirection() {
|
|
|
|
let options = { left: "auto", bottom: "auto", top: "auto" };
|
|
|
|
|
|
|
|
const dHeader = $(".d-header")[0];
|
|
|
|
const dHeaderBounds = dHeader ? dHeader.getBoundingClientRect() : {top: 0, height: 0};
|
|
|
|
const dHeaderHeight = dHeaderBounds.top + dHeaderBounds.height;
|
2017-11-21 18:53:09 +08:00
|
|
|
const componentHeight = this.$().outerHeight(false);
|
|
|
|
const componentWidth = this.$().outerWidth(false);
|
2017-11-10 02:57:53 +08:00
|
|
|
const bodyHeight = this.$body().outerHeight(false);
|
|
|
|
const windowWidth = $(window).width();
|
|
|
|
const windowHeight = $(window).height();
|
|
|
|
const boundingRect = this.get("element").getBoundingClientRect();
|
|
|
|
const offsetTop = boundingRect.top;
|
|
|
|
const offsetBottom = boundingRect.bottom;
|
|
|
|
|
|
|
|
if (this.get("fullWidthOnMobile") && windowWidth <= 420) {
|
|
|
|
const margin = 10;
|
|
|
|
const relativeLeft = this.$().offset().left - $(window).scrollLeft();
|
|
|
|
options.left = margin - relativeLeft;
|
|
|
|
options.width = windowWidth - margin * 2;
|
|
|
|
options.maxWidth = options.minWidth = "unset";
|
|
|
|
} else {
|
|
|
|
const bodyWidth = this.$body().outerWidth(false);
|
|
|
|
|
|
|
|
if ($("html").css("direction") === "rtl") {
|
|
|
|
const horizontalSpacing = boundingRect.right;
|
|
|
|
const hasHorizontalSpace = horizontalSpacing - (this.get("horizontalOffset") + bodyWidth) > 0;
|
|
|
|
if (hasHorizontalSpace) {
|
|
|
|
this.setProperties({ isLeftAligned: true, isRightAligned: false });
|
|
|
|
options.left = bodyWidth + this.get("horizontalOffset");
|
|
|
|
} else {
|
|
|
|
this.setProperties({ isLeftAligned: false, isRightAligned: true });
|
2017-11-21 18:53:09 +08:00
|
|
|
options.right = - (bodyWidth - componentWidth + this.get("horizontalOffset"));
|
2017-11-10 02:57:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const horizontalSpacing = boundingRect.left;
|
|
|
|
const hasHorizontalSpace = (windowWidth - (this.get("horizontalOffset") + horizontalSpacing + bodyWidth) > 0);
|
|
|
|
if (hasHorizontalSpace) {
|
|
|
|
this.setProperties({ isLeftAligned: true, isRightAligned: false });
|
|
|
|
options.left = this.get("horizontalOffset");
|
|
|
|
} else {
|
|
|
|
this.setProperties({ isLeftAligned: false, isRightAligned: true });
|
|
|
|
options.right = this.get("horizontalOffset");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
const fullHeight = this.get("verticalOffset") + bodyHeight + componentHeight;
|
|
|
|
const hasBelowSpace = windowHeight - offsetBottom - fullHeight > 0;
|
|
|
|
const hasAboveSpace = offsetTop - fullHeight - dHeaderHeight > 0;
|
2017-11-10 02:57:53 +08:00
|
|
|
if (hasBelowSpace || (!hasBelowSpace && !hasAboveSpace)) {
|
|
|
|
this.setProperties({ isBelow: true, isAbove: false });
|
2017-11-21 18:53:09 +08:00
|
|
|
options.top = componentHeight + this.get("verticalOffset") - 2;
|
2017-11-10 02:57:53 +08:00
|
|
|
} else {
|
|
|
|
this.setProperties({ isBelow: false, isAbove: true });
|
2017-11-21 18:53:09 +08:00
|
|
|
options.bottom = componentHeight + this.get("verticalOffset") - 1;
|
2017-11-10 02:57:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$body().css(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
_applyFixedPosition() {
|
2017-11-12 02:33:37 +08:00
|
|
|
if (this.get("isExpanded") !== true) { return; }
|
2017-11-12 03:46:26 +08:00
|
|
|
|
|
|
|
const scrollableParent = this.$().parents(this.get("scrollableParentSelector"));
|
|
|
|
if (scrollableParent.length === 0) { return; }
|
2017-11-10 02:57:53 +08:00
|
|
|
|
|
|
|
const width = this.$().outerWidth(false);
|
|
|
|
const height = this.$().outerHeight(false);
|
2017-11-21 18:53:09 +08:00
|
|
|
const $placeholder = $(`<div class='select-kit-fixed-placeholder-${this.elementId}'></div>`);
|
2017-11-10 02:57:53 +08:00
|
|
|
|
2017-11-12 03:46:26 +08:00
|
|
|
this._previousScrollParentOverflow = this._previousScrollParentOverflow || scrollableParent.css("overflow");
|
|
|
|
scrollableParent.css({ overflow: "hidden" });
|
2017-11-10 02:57:53 +08:00
|
|
|
|
|
|
|
this._previousCSSContext = {
|
|
|
|
minWidth: this.$().css("min-width"),
|
|
|
|
maxWidth: this.$().css("max-width")
|
|
|
|
};
|
|
|
|
|
|
|
|
const componentStyles = {
|
|
|
|
position: "fixed",
|
2017-11-12 03:46:26 +08:00
|
|
|
"margin-top": -scrollableParent.scrollTop(),
|
2017-11-10 02:57:53 +08:00
|
|
|
width,
|
|
|
|
minWidth: "unset",
|
|
|
|
maxWidth: "unset"
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($("html").css("direction") === "rtl") {
|
|
|
|
componentStyles.marginRight = -width;
|
|
|
|
} else {
|
|
|
|
componentStyles.marginLeft = -width;
|
|
|
|
}
|
|
|
|
|
|
|
|
$placeholder.css({ display: "inline-block", width, height, "vertical-align": "middle" });
|
|
|
|
|
|
|
|
this.$().before($placeholder).css(componentStyles);
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeFixedPosition() {
|
2017-11-21 18:53:09 +08:00
|
|
|
$(`.select-kit-fixed-placeholder-${this.elementId}`).remove();
|
2017-11-10 02:57:53 +08:00
|
|
|
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) { return; }
|
|
|
|
|
2017-11-12 03:46:26 +08:00
|
|
|
const scrollableParent = this.$().parents(this.get("scrollableParentSelector"));
|
|
|
|
if (scrollableParent.length === 0) { return; }
|
|
|
|
|
2017-11-10 02:57:53 +08:00
|
|
|
const css = jQuery.extend(
|
|
|
|
this._previousCSSContext,
|
|
|
|
{
|
|
|
|
top: "auto",
|
|
|
|
left: "auto",
|
|
|
|
"margin-left": "auto",
|
|
|
|
"margin-right": "auto",
|
|
|
|
"margin-top": "auto",
|
|
|
|
position: "relative"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.$().css(css);
|
|
|
|
|
2017-11-12 03:46:26 +08:00
|
|
|
scrollableParent.css("overflow", this._previousScrollParentOverflow);
|
2017-11-10 02:57:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_positionWrapper() {
|
2017-11-21 18:53:09 +08:00
|
|
|
const componentHeight = this.$().outerHeight(false);
|
2017-11-10 02:57:53 +08:00
|
|
|
|
|
|
|
this.$(this.wrapperSelector).css({
|
2017-11-21 18:53:09 +08:00
|
|
|
width: this.$().outerWidth(false) - 2,
|
|
|
|
height: componentHeight + this.$body().outerHeight(false)
|
2017-11-10 02:57:53 +08:00
|
|
|
});
|
|
|
|
},
|
2017-10-20 03:51:08 +08:00
|
|
|
});
|