B2G/gecko/browser/components/tabview/items.js
// Enables dragging on this item. Note: not to be called multiple times on the same item!
Enables dragging on this item. Note: not to be called multiple times on the same item!
Enables dragging on this item . Note : not to be called multiple times on the same item!
function Item_draggable() {
    try {
      Utils.assert(this.dragOptions, 'dragOptions');
      var cancelClasses = [];
      if (typeof this.dragOptions.cancelClass == 'string')
        cancelClasses = this.dragOptions.cancelClass.split(' ');
      var self = this;
      var $container = iQ(this.container);
      var startMouse;
      var startPos;
      var startSent;
      var startEvent;
      var droppables;
      var dropTarget;
      let determineBestDropTarget = function (e, box) {
        var best = {
          dropTarget: null,
          score: 0
        };
        droppables.forEach(function(droppable) {
          var intersection = box.intersection(droppable.bounds);
          if (intersection && intersection.area() > best.score) {
            var possibleDropTarget = droppable.item;
            var accept = true;
            if (possibleDropTarget != dropTarget) {
              var dropOptions = possibleDropTarget.dropOptions;
              if (dropOptions && typeof dropOptions.accept == "function")
                accept = dropOptions.accept.apply(possibleDropTarget, [self]);
            }
            if (accept) {
              best.dropTarget = possibleDropTarget;
              best.score = intersection.area();
            }
          }
        });
        return best.dropTarget;
      }
      var handleMouseMove = function(e) {
        drag.lastMoveTime = Date.now();
        var mouse = new Point(e.pageX, e.pageY);
        if (!startSent) {
          if(Math.abs(mouse.x - startMouse.x) > self.dragOptions.minDragDistance ||
             Math.abs(mouse.y - startMouse.y) > self.dragOptions.minDragDistance) {
            if (typeof self.dragOptions.start == "function")
              self.dragOptions.start.apply(self,
                  [startEvent, {position: {left: startPos.x, top: startPos.y}}]);
            startSent = true;
          }
        }
        if (startSent) {
          var box = self.getBounds();
          box.left = startPos.x + (mouse.x - startMouse.x);
          box.top = startPos.y + (mouse.y - startMouse.y);
          self.setBounds(box, true);
          if (typeof self.dragOptions.drag == "function")
            self.dragOptions.drag.apply(self, [e]);
          let bestDropTarget = determineBestDropTarget(e, box);
          if (bestDropTarget != dropTarget) {
            var dropOptions;
            if (dropTarget) {
              dropOptions = dropTarget.dropOptions;
              if (dropOptions && typeof dropOptions.out == "function")
                dropOptions.out.apply(dropTarget, [e]);
            }
            dropTarget = bestDropTarget;
            if (dropTarget) {
              dropOptions = dropTarget.dropOptions;
              if (dropOptions && typeof dropOptions.over == "function")
                dropOptions.over.apply(dropTarget, [e]);
            }
          }
          if (dropTarget) {
            dropOptions = dropTarget.dropOptions;
            if (dropOptions && typeof dropOptions.move == "function")
              dropOptions.move.apply(dropTarget, [e]);
          }
        }
        e.preventDefault();
      };
      var handleMouseUp = function(e) {
        iQ(gWindow)
          .unbind('mousemove', handleMouseMove)
          .unbind('mouseup', handleMouseUp);
        if (startSent && dropTarget) {
          var dropOptions = dropTarget.dropOptions;
          if (dropOptions && typeof dropOptions.drop == "function")
            dropOptions.drop.apply(dropTarget, [e]);
        }
        if (startSent && typeof self.dragOptions.stop == "function")
          self.dragOptions.stop.apply(self, [e]);
        e.preventDefault();
      };
      $container.mousedown(function(e) {
        if (!Utils.isLeftClick(e))
          return;
        var cancel = false;
        var $target = iQ(e.target);
        cancelClasses.forEach(function(className) {
          if ($target.hasClass(className))
            cancel = true;
        });
        if (cancel) {
          e.preventDefault();
          return;
        }
        startMouse = new Point(e.pageX, e.pageY);
        let bounds = self.getBounds();
        startPos = bounds.position();
        startEvent = e;
        startSent = false;
        droppables = [];
        iQ('.iq-droppable').each(function(elem) {
          if (elem != self.container) {
            var item = Items.item(elem);
            droppables.push({
              item: item,
              bounds: item.getBounds()
            });
          }
        });
        dropTarget = determineBestDropTarget(e, bounds);
        iQ(gWindow)
          .mousemove(handleMouseMove)
          .mouseup(handleMouseUp);
        e.preventDefault();
      });
    } catch(e) {
      Utils.log(e);
    }
  }
function Item_draggable ( ) { try { Utils . assert ( this . dragOptions , 'dragOptions' ) ; var cancelClasses = [ ] ; if ( typeof this . dragOptions . cancelClass == 'string' ) cancelClasses = this . dragOptions . cancelClass . split ( ' ' ) ; var self = this ; var $container = iQ ( this . container ) ; var startMouse ; var startPos ; var startSent ; var startEvent ; var droppables ; var dropTarget ; let determineBestDropTarget = function ( e , box ) { var best = { dropTarget : null , score : 0 } ; droppables . forEach ( function ( droppable ) { var intersection = box . intersection ( droppable . bounds ) ; if ( intersection && intersection . area ( ) > best . score ) { var possibleDropTarget = droppable . item ; var accept = true ; if ( possibleDropTarget != dropTarget ) { var dropOptions = possibleDropTarget . dropOptions ; if ( dropOptions && typeof dropOptions . accept == "function" ) accept = dropOptions . accept . apply ( possibleDropTarget , [ self ] ) ; } if ( accept ) { best . dropTarget = possibleDropTarget ; best . score = intersection . area ( ) ; } } } ) ; return best . dropTarget ; } var handleMouseMove = function ( e ) { drag . lastMoveTime = Date . now ( ) ; var mouse = new Point ( e . pageX , e . pageY ) ; if ( ! startSent ) { if ( Math . abs ( mouse . x - startMouse . x ) > self . dragOptions . minDragDistance || Math . abs ( mouse . y - startMouse . y ) > self . dragOptions . minDragDistance ) { if ( typeof self . dragOptions . start == "function" ) self . dragOptions . start . apply ( self , [ startEvent , { position : { left : startPos . x , top : startPos . y } } ] ) ; startSent = true ; } } if ( startSent ) { var box = self . getBounds ( ) ; box . left = startPos . x + ( mouse . x - startMouse . x ) ; box . top = startPos . y + ( mouse . y - startMouse . y ) ; self . setBounds ( box , true ) ; if ( typeof self . dragOptions . drag == "function" ) self . dragOptions . drag . apply ( self , [ e ] ) ; let bestDropTarget = determineBestDropTarget ( e , box ) ; if ( bestDropTarget != dropTarget ) { var dropOptions ; if ( dropTarget ) { dropOptions = dropTarget . dropOptions ; if ( dropOptions && typeof dropOptions . out == "function" ) dropOptions . out . apply ( dropTarget , [ e ] ) ; } dropTarget = bestDropTarget ; if ( dropTarget ) { dropOptions = dropTarget . dropOptions ; if ( dropOptions && typeof dropOptions . over == "function" ) dropOptions . over . apply ( dropTarget , [ e ] ) ; } } if ( dropTarget ) { dropOptions = dropTarget . dropOptions ; if ( dropOptions && typeof dropOptions . move == "function" ) dropOptions . move . apply ( dropTarget , [ e ] ) ; } } e . preventDefault ( ) ; } ; var handleMouseUp = function ( e ) { iQ ( gWindow ) . unbind ( 'mousemove' , handleMouseMove ) . unbind ( 'mouseup' , handleMouseUp ) ; if ( startSent && dropTarget ) { var dropOptions = dropTarget . dropOptions ; if ( dropOptions && typeof dropOptions . drop == "function" ) dropOptions . drop . apply ( dropTarget , [ e ] ) ; } if ( startSent && typeof self . dragOptions . stop == "function" ) self . dragOptions . stop . apply ( self , [ e ] ) ; e . preventDefault ( ) ; } ; $container . mousedown ( function ( e ) { if ( ! Utils . isLeftClick ( e ) ) return ; var cancel = false ; var $target = iQ ( e . target ) ; cancelClasses . forEach ( function ( className ) { if ( $target . hasClass ( className ) ) cancel = true ; } ) ; if ( cancel ) { e . preventDefault ( ) ; return ; } startMouse = new Point ( e . pageX , e . pageY ) ; let bounds = self . getBounds ( ) ; startPos = bounds . position ( ) ; startEvent = e ; startSent = false ; droppables = [ ] ; iQ ( '.iq-droppable' ) . each ( function ( elem ) { if ( elem != self . container ) { var item = Items . item ( elem ) ; droppables . push ( { item : item , bounds : item . getBounds ( ) } ) ; } } ) ; dropTarget = determineBestDropTarget ( e , bounds ) ; iQ ( gWindow ) . mousemove ( handleMouseMove ) . mouseup ( handleMouseUp ) ; e . preventDefault ( ) ; } ) ; } catch ( e ) { Utils . log ( e ) ; } }
Enables dragging on this item.
Enables dragging on this item .
// determine the best drop target based on the current mouse coordinates // drop events // ___ mousemove // global drag tracking // positioning // drag events // ___ mouseup // ___ mousedown