This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/select-kit/components/select-kit/select-kit-body.js
Robin Ward 07813c4a91
Convert select-kit from es6 to js (#9246)
* Convert select-kit from es6 to js

* Hide more git blames
2020-03-20 12:40:32 -04:00

60 lines
1.3 KiB
JavaScript

import Component from "@ember/component";
import { computed } from "@ember/object";
import { bind } from "@ember/runloop";
export default Component.extend({
layoutName: "select-kit/templates/components/select-kit/select-kit-body",
classNames: ["select-kit-body"],
attributeBindings: ["selectKitId:data-select-kit-id"],
selectKitId: computed("selectKit.uniqueID", function() {
return `${this.selectKit.uniqueID}-body`;
}),
rootEventType: "click",
init() {
this._super(...arguments);
this.handleRootMouseDownHandler = bind(this, this.handleRootMouseDown);
},
didInsertElement() {
this._super(...arguments);
document.addEventListener(
this.rootEventType,
this.handleRootMouseDownHandler,
true
);
},
willDestroyElement() {
this._super(...arguments);
document.removeEventListener(
this.rootEventType,
this.handleRootMouseDownHandler,
true
);
},
handleRootMouseDown(event) {
if (!this.selectKit.isExpanded) {
return;
}
const headerElement = document.querySelector(
`[data-select-kit-id=${this.selectKit.uniqueID}-header]`
);
if (headerElement && headerElement.contains(event.target)) {
return;
}
if (this.element.contains(event.target)) {
return;
}
this.selectKit.close(event);
}
});