Skip to content

Commit

Permalink
Update on-oustside modifier for new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wenincode authored and chris-hut committed Oct 16, 2023
1 parent 964ef50 commit 0fd98e7
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions ui/packages/consul-ui/app/modifiers/on-outside.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@
import Modifier from 'ember-modifier';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { registerDestructor } from '@ember/destroyable';

function cleanup(instance) {
if (instance) {
instance.doc?.removeEventListener('click', instance.listen);
}
}

export default class OnOutsideModifier extends Modifier {
@service('dom') dom;

constructor() {
super(...arguments);
constructor(owner, args) {
super(owner, args);
this.doc = this.dom.document();

registerDestructor(this, cleanup);
}
async connect(params, options) {

async modify(element, positional, named) {
cleanup.call(this);

this.params = positional;
this.options = named;
this.element = element;

await new Promise((resolve) => setTimeout(resolve, 0));
try {
this.doc.addEventListener(params[0], this.listen);
this.doc.addEventListener(positional[0], this.listen);
} catch (e) {
// continue
}
}

@action
listen(e) {
if (this.dom.isOutside(this.element, e.target)) {
debugger;
if (this.element && this.dom.isOutside(this.element, e.target)) {
const dispatch = typeof this.params[1] === 'function' ? this.params[1] : (_) => {};
dispatch.apply(this.element, [e]);
}
}

disconnect() {
this.doc.removeEventListener('click', this.listen);
}

didReceiveArguments() {
this.params = this.args.positional;
this.options = this.args.named;
}

didInstall() {
this.connect(this.args.positional, this.args.named);
}

willRemove() {
this.disconnect();
}
}

0 comments on commit 0fd98e7

Please sign in to comment.