Skip to content

Commit c12d9fe

Browse files
committed
Merge pull request #55 from sjmiles/master
latest event handling scheme
2 parents f1e11d2 + 8ebc90a commit c12d9fe

File tree

2 files changed

+77
-21
lines changed

2 files changed

+77
-21
lines changed

src/g-component.html

+76-20
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787

8888
function instanceReady() {
8989
// delegate host events
90-
bindHostEvents.call(this.$protected);
90+
//bindHostEvents.call(this.$protected);
91+
accumulateHostEvents.call(this.$protected, this.__events);
9192
// TODO(sjmiles): ideally delegated events are set up per root
9293
// not on the host node (see above)
9394
bindAccumulatedEvents(this, this.__events);
@@ -303,7 +304,7 @@
303304
propertySetChanged: function(inChange, inWatched) {
304305
if (inChange.mutation == 'add') {
305306
//log.watch && console.log('[%s] propertySetChanged: adding watch for [%s]', this.node.localName, inChange.propertyName);
306-
api.addWatch.call(this, inChange.propertyName, inWatched);
307+
api.addWatch.call(this, inChange.propertyName, null, inWatched);
307308
//this.propertyChanged(inChange.propertyName);
308309
}
309310
},
@@ -442,14 +443,14 @@
442443
}.bind(this), inTimeout || 0);
443444
},
444445
dispatch: function(inMethodName, inArguments) {
445-
// TODO(sjmiles): have 'controller' dispatch this method itself
446446
if (this[inMethodName]) {
447447
this[inMethodName].apply(this, inArguments);
448448
}
449449
},
450-
send: function(inType, inDetail) {
451-
log.events && console.log('[%s]: sending [%s]', this.node.localName, inType);
452-
this.node.dispatchEvent(
450+
send: function(inType, inDetail, inToNode) {
451+
var node = inToNode || this.node;
452+
log.events && console.log('[%s]: sending [%s]', node.localName, inType);
453+
node.dispatchEvent(
453454
new CustomEvent(inType, {bubbles: true, detail: inDetail})
454455
);
455456
},
@@ -736,12 +737,15 @@
736737

737738
var dispatch = function(inNode, inHandlerName, inArguments) {
738739
if (inNode && inNode.$protected) {
740+
log.events && console.group('[%s] dispatch [%s]', inNode.localName, inHandlerName);
739741
inNode.$protected.dispatch(inHandlerName, inArguments);
742+
log.events && console.groupEnd();
740743
}
741744
};
742745

743746
// automagic host-event binding
744747

748+
/*
745749
var bindHostEvents = function() {
746750
// TODO(sjmiles): must walk the prototype tree
747751
// to bind the superset of eventDelegate maps
@@ -764,7 +768,38 @@
764768
inNode.addEventListener(inEventName, fn);
765769
log.events && console.log('[%s] bindHostEvent: [%s] to [%s]', inNode.localName, inEventName, inHandler);
766770
};
771+
*/
767772

773+
var accumulateHostEvents = function(inEvents) {
774+
// TODO(sjmiles): must walk the prototype tree to operate on the union of
775+
// eventDelegates maps
776+
var p = this;
777+
while (p) {
778+
if (p.hasOwnProperty('eventDelegates')) {
779+
for (var n in p.eventDelegates) {
780+
inEvents[n] = 1;
781+
//bindHostEvent(this.node, n, p.eventDelegates[n]);
782+
}
783+
}
784+
p = p.__proto__;
785+
}
786+
};
787+
788+
var findHostHandler = function(inEventName) {
789+
// TODO(sjmiles): walking the tree again is inefficient; combine with code
790+
// in accumulateHostEvents into something more sane
791+
var p = this;
792+
while (p) {
793+
if (p.hasOwnProperty('eventDelegates')) {
794+
var h = p.eventDelegates[inEventName];
795+
if (h) {
796+
return h;
797+
}
798+
}
799+
p = p.__proto__;
800+
}
801+
};
802+
768803
//
769804
// new experimental late bound events
770805
//
@@ -807,29 +842,50 @@
807842
}
808843
};
809844

845+
// TODO(sjmiles): lots of work on the code here as we bash out a design
846+
// we like, cruftiness increasing in the process. Will be cleaned up when
847+
// design solidifies.
810848
function __(inEvent) {
811-
var on = prefix + inEvent.type;
812-
log.events && console.group("[%s]: __ [%s]", this.localName, on);
849+
inEvent.on = prefix + inEvent.type;
850+
//var on = prefix + inEvent.type;
851+
log.events && console.group("[%s]: __ [%s]", this.localName, inEvent.on);
813852
var t = inEvent.target;
814853
while (t && t != this) {
815854
t = deref(t);
816-
log.events && console.dir(t);
817-
if (t.attributes) {
818-
var h = t.getAttribute(on);
819-
if (h) {
820-
var c = findController(t);
821-
log.events && console.log('found handler [%s] for controller', h, c);
822-
if (c == this) {
823-
log.events && console.log('invoking [%s]', h);
824-
if (this.$protected[h]) {
825-
this.$protected[h](inEvent, inEvent.detail, t);
826-
}
827-
}
855+
var c = findController(t);
856+
if (c == this) {
857+
log.events && console.log('node [%s]', t.localName);
858+
handleHostEvent.call(this, t, inEvent);
859+
handleEvent.call(this, t, inEvent);
860+
if (inEvent.cancelBubble) {
861+
return;
828862
}
829863
}
830864
t = t.parentNode;
831865
}
866+
// if we are a top-level component, we have to fire our own host events
867+
if (t == this && !findController(t)) {
868+
handleHostEvent.call(this, t, inEvent);
869+
}
832870
log.events && console.groupEnd();
833871
};
872+
873+
function handleEvent(inNode, inEvent) {
874+
if (inNode.attributes) {
875+
var h = inNode.getAttribute(inEvent.on);
876+
if (h) {
877+
log.events && console.log('[%s] found handler name [%s]', this.localName, h);
878+
dispatch(this, h, [inEvent, inEvent.detail, inNode]);
879+
}
880+
}
881+
};
882+
883+
function handleHostEvent(inNode, inEvent) {
884+
var h = findHostHandler.call(inNode.$protected, inEvent.type);
885+
if (h) {
886+
log.events && console.log('[%s] found host handler name [%s]', inNode.localName, h);
887+
dispatch(inNode, h, [inEvent, inEvent.detail, inNode]);
888+
}
889+
};
834890
</script>
835891
</element>

src/g-selector.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* license that can be found in the LICENSE file.
66
*/
77
-->
8-
<element name="g-selector" attributes="selected multi" on-mousedown="activateHandler">
8+
<element name="g-selector" attributes="selected multi" on-mouseup="activateHandler">
99
<link rel="components" href="g-selection.html">
1010
<template>
1111
<g-selection id="selection" multi="{{multi}}" on-select="selectionSelect"></g-selection>

0 commit comments

Comments
 (0)