1 /**
  2  * @fileOverview delegate events for children
  3  * @author yiminghe@gmail.com
  4  */
  5 KISSY.add("component/delegateChildren", function (S) {
  6 
  7     function DelegateChildren() {
  8 
  9     }
 10 
 11     function handleChildMouseEvents(e) {
 12         var control = this.getOwnerControl(e.target);
 13         if (control) {
 14             // Child control identified; forward the event.
 15             switch (e.type) {
 16                 case mousedown:
 17                     control.handleMouseDown(e);
 18                     break;
 19                 case mouseup:
 20                     control.handleMouseUp(e);
 21                     break;
 22                 case mouseover:
 23                     control.handleMouseOver(e);
 24                     break;
 25                 case mouseout:
 26                     control.handleMouseOut(e);
 27                     break;
 28                 case dblclick:
 29                     control.handleDblClick(e);
 30                     break;
 31                 default:
 32                     S.error(e.type + " unhandled!");
 33             }
 34         }
 35     }
 36 
 37     S.augment(DelegateChildren, {
 38 
 39         __bindUI:function () {
 40             var self = this;
 41             self.get("el").on("mousedown mouseup mouseover mouseout dblclick",
 42                 handleChildMouseEvents, self);
 43         },
 44 
 45         getOwnerControl:function (target) {
 46             var self = this,
 47                 children = self.get("children"),
 48                 len = children.length,
 49                 elem = self.get("el")[0];
 50             while (target && target !== elem) {
 51                 for (var i = 0; i < len; i++) {
 52                     if (children[i].get("el")[0] === target) {
 53                         return children[i];
 54                     }
 55                 }
 56                 target = target.parentNode;
 57             }
 58             return null;
 59         }
 60     });
 61 
 62     return DelegateChildren;
 63 });