MouseEvent: How to fire click events?

543 views
Skip to first unread message

Alexander Orlov

unread,
Apr 23, 2012, 10:17:17 AM4/23/12
to mi...@dartlang.org
#import('dart:html');

testClickEvent() {
DivElement div = new Element.html('<div style="background-color: red; width: 10em; height: 10em;"></div>');
document.body.elements.add(div);
div.on.click.add((MouseEvent e) {
print(e.type);
print(e.view);
print(e.detail);
print(e.screenX);
print(e.screenY);
print(e.clientX);
print(e.clientY);
print(e.button);
});

}

fireClick() {
  MouseEvent e = new MouseEvent(
  "click", window, 1,
  2020, 293,
  100, 126,
  0
);

document.on.readyStateChange.dispatch(e);
document.on.load.dispatch(e);
}


testClickEvent() gave me parameter values which I've put into fireClick() to emulate a click event but nothing happens when I call fireClick().



John Evans

unread,
Apr 23, 2012, 10:57:08 AM4/23/12
to General Dart Discussion
If you are trying to fire events manually, this works:

Element e = new Element.tag('div');
e.on.click.add((ev){
print("You clicked me, didn't you?");

});

e.on.click.dispatch(new Event('click'));
> *testClickEvent() *gave me parameter values which I've put into *fireClick()
> *to emulate a click event but nothing happens when I call *fireClick().*

Alexander Orlov

unread,
Apr 23, 2012, 11:05:10 AM4/23/12
to John Evans, General Dart Discussion
On Mon, Apr 23, 2012 at 4:57 PM, John Evans <pru...@gmail.com> wrote:
If you are trying to fire events manually, this works:

 Element e = new Element.tag('div');
 e.on.click.add((ev){
   print("You clicked me, didn't you?");

 });

 e.on.click.dispatch(new Event('click'));

But what if I want just to fire a click without referencing a particular Element? I want to provide x/y coordinates as properties of the click event and fire it without a priori knowing whether there is an Element at this particular x/y coordinates that can respond to this click.

John Evans

unread,
Apr 23, 2012, 11:34:57 AM4/23/12
to General Dart Discussion
Here's one way: Just fire to all elements, and the ones that have
click registered will pick it up:

Element e = new Element.tag('div');
document.body.elements.add(e);
e.on.click.add((ev){
print("You clicked me, didn't you?");

});

document.body.queryAll('*').forEach((element){
element.on.click.dispatch(new MouseEvent('click', window, 0,
200, 300, 400, 500, 0));
});


On Apr 23, 10:05 am, Alexander Orlov <alexander.or...@loxal.net>
wrote:
> On Mon, Apr 23, 2012 at 4:57 PM, John Evans <pruj...@gmail.com> wrote:
> > If you are trying to fire events manually, this works:
>
> >  Element e = new Element.tag('div');
> >  e.on.click.add((ev){
> >    print("You clicked me, didn't you?");
>
> >  });
>
> >  e.on.click.dispatch(new Event('click'));
>
> But what if I want just to fire a click *without referencing a particular
> Element*? I want to provide x/y coordinates as properties of the click

Alexander Orlov

unread,
Apr 23, 2012, 11:38:43 AM4/23/12
to John Evans, General Dart Discussion
On Mon, Apr 23, 2012 at 5:34 PM, John Evans <pru...@gmail.com> wrote:

 document.body.queryAll('*').forEach((element){
   element.on.click.dispatch(new MouseEvent('click', window, 0,
     200, 300, 400, 500, 0));
 });

Nice! Thanks! 

John Evans

unread,
Apr 23, 2012, 11:39:34 AM4/23/12
to General Dart Discussion
Alternatively if you wanted to fire to only the leaf nodes in the tree
(those without children), then you could add a filter. This should
all the event to bubble up to any other listeners:

Element e = new Element.tag('div');
document.body.elements.add(e);
e.on.click.add((ev){
print("You clicked me, didn't you?");

});

document.body.queryAll('*')
.filter((element) => element.elements.isEmpty())
.forEach((element){
element.on.click.dispatch(new MouseEvent('click', window, 0,
200, 300, 400, 500, 0));
});

On Apr 23, 10:05 am, Alexander Orlov <alexander.or...@loxal.net>
wrote:
> On Mon, Apr 23, 2012 at 4:57 PM, John Evans <pruj...@gmail.com> wrote:
> > If you are trying to fire events manually, this works:
>
> >  Element e = new Element.tag('div');
> >  e.on.click.add((ev){
> >    print("You clicked me, didn't you?");
>
> >  });
>
> >  e.on.click.dispatch(new Event('click'));
>
> But what if I want just to fire a click *without referencing a particular
> Element*? I want to provide x/y coordinates as properties of the click
Reply all
Reply to author
Forward
0 new messages