87
87
88
88
function instanceReady ( ) {
89
89
// delegate host events
90
- bindHostEvents . call ( this . $protected ) ;
90
+ //bindHostEvents.call(this.$protected);
91
+ accumulateHostEvents . call ( this . $protected , this . __events ) ;
91
92
// TODO(sjmiles): ideally delegated events are set up per root
92
93
// not on the host node (see above)
93
94
bindAccumulatedEvents ( this , this . __events ) ;
303
304
propertySetChanged : function ( inChange , inWatched ) {
304
305
if ( inChange . mutation == 'add' ) {
305
306
//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 ) ;
307
308
//this.propertyChanged(inChange.propertyName);
308
309
}
309
310
} ,
442
443
} . bind ( this ) , inTimeout || 0 ) ;
443
444
} ,
444
445
dispatch : function ( inMethodName , inArguments ) {
445
- // TODO(sjmiles): have 'controller' dispatch this method itself
446
446
if ( this [ inMethodName ] ) {
447
447
this [ inMethodName ] . apply ( this , inArguments ) ;
448
448
}
449
449
} ,
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 (
453
454
new CustomEvent ( inType , { bubbles : true , detail : inDetail } )
454
455
) ;
455
456
} ,
736
737
737
738
var dispatch = function ( inNode , inHandlerName , inArguments ) {
738
739
if ( inNode && inNode . $protected ) {
740
+ log . events && console . group ( '[%s] dispatch [%s]' , inNode . localName , inHandlerName ) ;
739
741
inNode . $protected . dispatch ( inHandlerName , inArguments ) ;
742
+ log . events && console . groupEnd ( ) ;
740
743
}
741
744
} ;
742
745
743
746
// automagic host-event binding
744
747
748
+ /*
745
749
var bindHostEvents = function() {
746
750
// TODO(sjmiles): must walk the prototype tree
747
751
// to bind the superset of eventDelegate maps
764
768
inNode.addEventListener(inEventName, fn);
765
769
log.events && console.log('[%s] bindHostEvent: [%s] to [%s]', inNode.localName, inEventName, inHandler);
766
770
};
771
+ */
767
772
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
+
768
803
//
769
804
// new experimental late bound events
770
805
//
807
842
}
808
843
} ;
809
844
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.
810
848
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 ) ;
813
852
var t = inEvent . target ;
814
853
while ( t && t != this ) {
815
854
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 ;
828
862
}
829
863
}
830
864
t = t . parentNode ;
831
865
}
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
+ }
832
870
log . events && console . groupEnd ( ) ;
833
871
} ;
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
+ } ;
834
890
</ script >
835
891
</ element >
0 commit comments