2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-17 18:47:37 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
dom: service('dom'),
|
|
|
|
tagName: '',
|
2020-01-22 12:08:29 +00:00
|
|
|
checked: false,
|
2022-09-15 08:43:17 +00:00
|
|
|
onchange: function () {},
|
2020-03-24 10:55:45 +00:00
|
|
|
// TODO: reserved for the moment but we don't need it yet
|
2022-09-15 08:43:17 +00:00
|
|
|
onblur: function () {},
|
|
|
|
init: function () {
|
2019-12-17 18:47:37 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
this.guid = this.dom.guid(this);
|
|
|
|
this._listeners = this.dom.listeners();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
willDestroyElement: function () {
|
2019-12-17 18:47:37 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners.remove();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
didReceiveAttrs: function () {
|
2020-03-24 10:55:45 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
if (this.checked) {
|
|
|
|
this.addClickOutsideListener();
|
|
|
|
} else {
|
|
|
|
this._listeners.remove();
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
addClickOutsideListener: function () {
|
2020-03-24 10:55:45 +00:00
|
|
|
// default onblur event
|
|
|
|
this._listeners.remove();
|
2022-09-15 08:43:17 +00:00
|
|
|
this._listeners.add(this.dom.document(), 'click', (e) => {
|
2020-03-24 10:55:45 +00:00
|
|
|
if (this.dom.isOutside(this.label, e.target)) {
|
|
|
|
if (this.dom.isOutside(this.label.nextElementSibling, e.target)) {
|
|
|
|
if (this.input.checked) {
|
|
|
|
this.input.checked = false;
|
|
|
|
// TODO: This should be an event
|
|
|
|
this.onchange({ target: this.input });
|
|
|
|
}
|
|
|
|
this._listeners.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2019-12-17 18:47:37 +00:00
|
|
|
actions: {
|
2022-09-15 08:43:17 +00:00
|
|
|
click: function (e) {
|
2020-05-18 16:21:10 +00:00
|
|
|
// only preventDefault if the target isn't an external link
|
|
|
|
// TODO: this should be changed for an explicit close
|
|
|
|
if ((e.target.rel || '').indexOf('noopener') === -1) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2019-12-17 18:47:37 +00:00
|
|
|
this.input.checked = !this.input.checked;
|
2020-01-22 12:08:29 +00:00
|
|
|
// manually dispatched mouse events have a detail = 0
|
|
|
|
// real mouse events have the number of click counts
|
|
|
|
if (e.detail !== 0) {
|
|
|
|
e.target.blur();
|
|
|
|
}
|
2019-12-17 18:47:37 +00:00
|
|
|
this.actions.change.apply(this, [e]);
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
change: function (e) {
|
2019-12-17 18:47:37 +00:00
|
|
|
if (this.input.checked) {
|
2020-03-24 10:55:45 +00:00
|
|
|
this.addClickOutsideListener();
|
2019-12-17 18:47:37 +00:00
|
|
|
}
|
|
|
|
// TODO: This should be an event
|
2020-01-22 12:08:29 +00:00
|
|
|
this.onchange({ target: this.input });
|
2019-12-17 18:47:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|