From 109e4bb3521d1297c1adc0945d05af272d04ad21 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 7 Sep 2016 17:42:09 -0700 Subject: [PATCH] Move code to touch.js --- blockly_compressed.js | 77 +++++------ blockly_uncompressed.js | 6 +- core/block_svg.js | 4 +- core/blockly.js | 171 +------------------------ core/bubble.js | 2 +- core/flyout.js | 16 ++- core/scrollbar.js | 4 +- core/toolbox.js | 4 +- core/touch.js | 274 ++++++++++++++++++++++++++++++++++++++++ core/utils.js | 75 +---------- core/workspace_svg.js | 11 +- core/zoom_controls.js | 6 +- 12 files changed, 353 insertions(+), 297 deletions(-) create mode 100644 core/touch.js diff --git a/blockly_compressed.js b/blockly_compressed.js index fc27724d0..ff8412ef4 100644 --- a/blockly_compressed.js +++ b/blockly_compressed.js @@ -889,14 +889,14 @@ Blockly.Workspace.prototype.addChangeListener=function(a){this.listeners_.push(a Blockly.Workspace.prototype.getBlockById=function(a){return this.blockDB_[a]||null};Blockly.Workspace.WorkspaceDB_=Object.create(null);Blockly.Workspace.getById=function(a){return Blockly.Workspace.WorkspaceDB_[a]||null};Blockly.Workspace.prototype.clear=Blockly.Workspace.prototype.clear;Blockly.Workspace.prototype.clearUndo=Blockly.Workspace.prototype.clearUndo;Blockly.Workspace.prototype.addChangeListener=Blockly.Workspace.prototype.addChangeListener; Blockly.Workspace.prototype.removeChangeListener=Blockly.Workspace.prototype.removeChangeListener;Blockly.Bubble=function(a,b,c,d,e,f){this.workspace_=a;this.content_=b;this.shape_=c;c=Blockly.Bubble.ARROW_ANGLE;this.workspace_.RTL&&(c=-c);this.arrow_radians_=goog.math.toRadians(c);a.getBubbleCanvas().appendChild(this.createDom_(b,!(!e||!f)));this.setAnchorLocation(d);e&&f||(b=this.content_.getBBox(),e=b.width+2*Blockly.Bubble.BORDER_WIDTH,f=b.height+2*Blockly.Bubble.BORDER_WIDTH);this.setBubbleSize(e,f);this.positionBubble_();this.renderArrow_();this.rendered_=!0;a.options.readOnly||(Blockly.bindEvent_(this.bubbleBack_, "mousedown",this,this.bubbleMouseDown_),this.resizeGroup_&&Blockly.bindEvent_(this.resizeGroup_,"mousedown",this,this.resizeMouseDown_))};Blockly.Bubble.BORDER_WIDTH=6;Blockly.Bubble.ARROW_THICKNESS=10;Blockly.Bubble.ARROW_ANGLE=20;Blockly.Bubble.ARROW_BEND=4;Blockly.Bubble.ANCHOR_RADIUS=8;Blockly.Bubble.onMouseUpWrapper_=null;Blockly.Bubble.onMouseMoveWrapper_=null;Blockly.Bubble.prototype.resizeCallback_=null; -Blockly.Bubble.unbindDragEvents_=function(){Blockly.Bubble.onMouseUpWrapper_&&(Blockly.unbindEvent_(Blockly.Bubble.onMouseUpWrapper_),Blockly.Bubble.onMouseUpWrapper_=null);Blockly.Bubble.onMouseMoveWrapper_&&(Blockly.unbindEvent_(Blockly.Bubble.onMouseMoveWrapper_),Blockly.Bubble.onMouseMoveWrapper_=null)};Blockly.Bubble.bubbleMouseUp_=function(a){Blockly.clearTouchIdentifier();Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);Blockly.Bubble.unbindDragEvents_()};Blockly.Bubble.prototype.rendered_=!1; -Blockly.Bubble.prototype.anchorXY_=null;Blockly.Bubble.prototype.relativeLeft_=0;Blockly.Bubble.prototype.relativeTop_=0;Blockly.Bubble.prototype.width_=0;Blockly.Bubble.prototype.height_=0;Blockly.Bubble.prototype.autoLayout_=!0; +Blockly.Bubble.unbindDragEvents_=function(){Blockly.Bubble.onMouseUpWrapper_&&(Blockly.unbindEvent_(Blockly.Bubble.onMouseUpWrapper_),Blockly.Bubble.onMouseUpWrapper_=null);Blockly.Bubble.onMouseMoveWrapper_&&(Blockly.unbindEvent_(Blockly.Bubble.onMouseMoveWrapper_),Blockly.Bubble.onMouseMoveWrapper_=null)};Blockly.Bubble.bubbleMouseUp_=function(){Blockly.Touch.clearTouchIdentifier();Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);Blockly.Bubble.unbindDragEvents_()}; +Blockly.Bubble.prototype.rendered_=!1;Blockly.Bubble.prototype.anchorXY_=null;Blockly.Bubble.prototype.relativeLeft_=0;Blockly.Bubble.prototype.relativeTop_=0;Blockly.Bubble.prototype.width_=0;Blockly.Bubble.prototype.height_=0;Blockly.Bubble.prototype.autoLayout_=!0; Blockly.Bubble.prototype.createDom_=function(a,b){this.bubbleGroup_=Blockly.createSvgElement("g",{},null);var c={filter:"url(#"+this.workspace_.options.embossFilterId+")"};-1!=goog.userAgent.getUserAgentString().indexOf("JavaFX")&&(c={});c=Blockly.createSvgElement("g",c,this.bubbleGroup_);this.bubbleArrow_=Blockly.createSvgElement("path",{},c);this.bubbleBack_=Blockly.createSvgElement("rect",{"class":"blocklyDraggable",x:0,y:0,rx:Blockly.Bubble.BORDER_WIDTH,ry:Blockly.Bubble.BORDER_WIDTH},c);b?(this.resizeGroup_= Blockly.createSvgElement("g",{"class":this.workspace_.RTL?"blocklyResizeSW":"blocklyResizeSE"},this.bubbleGroup_),c=2*Blockly.Bubble.BORDER_WIDTH,Blockly.createSvgElement("polygon",{points:"0,x x,x x,0".replace(/x/g,c.toString())},this.resizeGroup_),Blockly.createSvgElement("line",{"class":"blocklyResizeLine",x1:c/3,y1:c-1,x2:c-1,y2:c/3},this.resizeGroup_),Blockly.createSvgElement("line",{"class":"blocklyResizeLine",x1:2*c/3,y1:c-1,x2:c-1,y2:2*c/3},this.resizeGroup_)):this.resizeGroup_=null;this.bubbleGroup_.appendChild(a); return this.bubbleGroup_}; Blockly.Bubble.prototype.bubbleMouseDown_=function(a){this.promote_();Blockly.Bubble.unbindDragEvents_();Blockly.isRightButton(a)?a.stopPropagation():Blockly.isTargetInput_(a)||(Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED),this.workspace_.startDrag(a,new goog.math.Coordinate(this.workspace_.RTL?-this.relativeLeft_:this.relativeLeft_,this.relativeTop_)),Blockly.Bubble.onMouseUpWrapper_=Blockly.bindEvent_(document,"mouseup",this,Blockly.Bubble.bubbleMouseUp_),Blockly.Bubble.onMouseMoveWrapper_=Blockly.bindEvent_(document, "mousemove",this,this.bubbleMouseMove_),Blockly.hideChaff(),a.stopPropagation())};Blockly.Bubble.prototype.bubbleMouseMove_=function(a){this.autoLayout_=!1;a=this.workspace_.moveDrag(a);this.relativeLeft_=this.workspace_.RTL?-a.x:a.x;this.relativeTop_=a.y;this.positionBubble_();this.renderArrow_()}; -Blockly.Bubble.prototype.resizeMouseDown_=function(a){this.promote_();Blockly.Bubble.unbindDragEvents_();Blockly.isRightButton(a)||(Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED),this.workspace_.startDrag(a,new goog.math.Coordinate(this.workspace_.RTL?-this.width_:this.width_,this.height_)),Blockly.Bubble.onMouseUpWrapper_=Blockly.bindEvent_(document,"mouseup",this,Blockly.Bubble.unbindDragEvents_),Blockly.Bubble.onMouseMoveWrapper_=Blockly.bindEvent_(document,"mousemove",this,this.resizeMouseMove_), +Blockly.Bubble.prototype.resizeMouseDown_=function(a){this.promote_();Blockly.Bubble.unbindDragEvents_();Blockly.isRightButton(a)||(Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED),this.workspace_.startDrag(a,new goog.math.Coordinate(this.workspace_.RTL?-this.width_:this.width_,this.height_)),Blockly.Bubble.onMouseUpWrapper_=Blockly.bindEvent_(document,"mouseup",this,Blockly.Bubble.bubbleMouseUp_),Blockly.Bubble.onMouseMoveWrapper_=Blockly.bindEvent_(document,"mousemove",this,this.resizeMouseMove_), Blockly.hideChaff());a.stopPropagation()};Blockly.Bubble.prototype.resizeMouseMove_=function(a){this.autoLayout_=!1;a=this.workspace_.moveDrag(a);this.setBubbleSize(this.workspace_.RTL?-a.x:a.x,a.y);this.workspace_.RTL&&this.positionBubble_()};Blockly.Bubble.prototype.registerResizeEvent=function(a){this.resizeCallback_=a};Blockly.Bubble.prototype.promote_=function(){this.bubbleGroup_.parentNode.appendChild(this.bubbleGroup_)}; Blockly.Bubble.prototype.setAnchorLocation=function(a){this.anchorXY_=a;this.rendered_&&this.positionBubble_()}; Blockly.Bubble.prototype.layoutBubble_=function(){var a=-this.width_/4,b=-this.height_-Blockly.BlockSvg.MIN_BLOCK_Y,c=this.workspace_.getMetrics();c.viewWidth/=this.workspace_.scale;c.viewLeft/=this.workspace_.scale;var d=this.anchorXY_.x;this.workspace_.RTL?d-c.viewLeft-a-this.width_c.viewWidth&&(a=d-c.viewLeft-c.viewWidth):d+athis.maxDisplayLength&&(a=a.substring(0,this.maxDisplayLength-2)+"\u2026");goog.dom.removeChildren(this.textElement_);a=a.replace(/\s/g,Blockly.Field.NBSP);this.sourceBlock_.RTL&&a&&(a+="\u200f");a||(a=Blockly.Field.NBSP);a=document.createTextNode(a);this.textElement_.appendChild(a);this.size_.width=0}};Blockly.Field.prototype.getValue=function(){return this.getText()}; Blockly.Field.prototype.setValue=function(a){if(null!==a){var b=this.getValue();b!=a&&(this.sourceBlock_&&Blockly.Events.isEnabled()&&Blockly.Events.fire(new Blockly.Events.Change(this.sourceBlock_,"field",this.name,b,a)),this.setText(a))}}; Blockly.Field.prototype.onMouseUp_=function(a){if(!goog.userAgent.IPHONE&&!goog.userAgent.IPAD||goog.userAgent.isVersionOrHigher("537.51.2")||0===a.layerX||0===a.layerY)Blockly.isRightButton(a)||this.sourceBlock_.workspace.isDragging()||this.sourceBlock_.isEditable()&&this.showEditor_()};Blockly.Field.prototype.setTooltip=function(a){};Blockly.Field.prototype.getAbsoluteXY_=function(){return goog.style.getPageOffset(this.borderRect_)};Blockly.Tooltip={};Blockly.Tooltip.visible=!1;Blockly.Tooltip.LIMIT=50;Blockly.Tooltip.mouseOutPid_=0;Blockly.Tooltip.showPid_=0;Blockly.Tooltip.lastX_=0;Blockly.Tooltip.lastY_=0;Blockly.Tooltip.element_=null;Blockly.Tooltip.poisonedElement_=null;Blockly.Tooltip.OFFSET_X=0;Blockly.Tooltip.OFFSET_Y=10;Blockly.Tooltip.RADIUS_OK=10;Blockly.Tooltip.HOVER_MS=750;Blockly.Tooltip.MARGINS=5;Blockly.Tooltip.DIV=null; -Blockly.Tooltip.createDom=function(){Blockly.Tooltip.DIV||(Blockly.Tooltip.DIV=goog.dom.createDom("DIV","blocklyTooltipDiv"),document.body.appendChild(Blockly.Tooltip.DIV))};Blockly.Tooltip.bindMouseEvents=function(a){Blockly.bindEvent_(a,"mouseover",null,Blockly.Tooltip.onMouseOver_);Blockly.bindEvent_(a,"mouseout",null,Blockly.Tooltip.onMouseOut_);Blockly.bindEvent_(a,"mousemove",null,Blockly.Tooltip.onMouseMove_)}; +Blockly.Tooltip.createDom=function(){Blockly.Tooltip.DIV||(Blockly.Tooltip.DIV=goog.dom.createDom("DIV","blocklyTooltipDiv"),document.body.appendChild(Blockly.Tooltip.DIV))};Blockly.Tooltip.bindMouseEvents=function(a){Blockly.bindEvent_(a,"mouseover",null,Blockly.Tooltip.onMouseOver_);Blockly.bindEvent_(a,"mouseout",null,Blockly.Tooltip.onMouseOut_);a.addEventListener("mousemove",Blockly.Tooltip.onMouseMove_,!1)}; Blockly.Tooltip.onMouseOver_=function(a){for(a=a.target;!goog.isString(a.tooltip)&&!goog.isFunction(a.tooltip);)a=a.tooltip;Blockly.Tooltip.element_!=a&&(Blockly.Tooltip.hide(),Blockly.Tooltip.poisonedElement_=null,Blockly.Tooltip.element_=a);clearTimeout(Blockly.Tooltip.mouseOutPid_)};Blockly.Tooltip.onMouseOut_=function(a){Blockly.Tooltip.mouseOutPid_=setTimeout(function(){Blockly.Tooltip.element_=null;Blockly.Tooltip.poisonedElement_=null;Blockly.Tooltip.hide()},1);clearTimeout(Blockly.Tooltip.showPid_)}; Blockly.Tooltip.onMouseMove_=function(a){if(Blockly.Tooltip.element_&&Blockly.Tooltip.element_.tooltip&&Blockly.dragMode_==Blockly.DRAG_NONE&&!Blockly.WidgetDiv.isVisible())if(Blockly.Tooltip.visible){var b=Blockly.Tooltip.lastX_-a.pageX;a=Blockly.Tooltip.lastY_-a.pageY;Math.sqrt(b*b+a*a)>Blockly.Tooltip.RADIUS_OK&&Blockly.Tooltip.hide()}else Blockly.Tooltip.poisonedElement_!=Blockly.Tooltip.element_&&(clearTimeout(Blockly.Tooltip.showPid_),Blockly.Tooltip.lastX_=a.pageX,Blockly.Tooltip.lastY_=a.pageY, Blockly.Tooltip.showPid_=setTimeout(Blockly.Tooltip.show_,Blockly.Tooltip.HOVER_MS))};Blockly.Tooltip.hide=function(){Blockly.Tooltip.visible&&(Blockly.Tooltip.visible=!1,Blockly.Tooltip.DIV&&(Blockly.Tooltip.DIV.style.display="none"));clearTimeout(Blockly.Tooltip.showPid_)}; @@ -994,9 +994,10 @@ Blockly.Scrollbar.prototype.resizeViewVertical=function(a){var b=a.viewHeight-1; Blockly.Scrollbar.prototype.resizeContentVertical=function(a){this.pair_||this.setVisible(this.scrollViewSize_=c+this.handleLength_&&(d+=e);this.setHandlePosition(this.constrainHandle_(d));this.onScroll_();a.stopPropagation(); -a.preventDefault()}};Blockly.Scrollbar.prototype.onMouseDownHandle_=function(a){this.cleanUp_();Blockly.isRightButton(a)?a.stopPropagation():(this.startDragHandle=this.handlePosition_,this.startDragMouse=this.horizontal_?a.clientX:a.clientY,Blockly.Scrollbar.onMouseUpWrapper_=Blockly.bindEvent_(document,"mouseup",this,this.onMouseUpHandle_),Blockly.Scrollbar.onMouseMoveWrapper_=Blockly.bindEvent_(document,"mousemove",this,this.onMouseMoveHandle_),a.stopPropagation(),a.preventDefault())}; -Blockly.Scrollbar.prototype.onMouseMoveHandle_=function(a){this.setHandlePosition(this.constrainHandle_(this.startDragHandle+((this.horizontal_?a.clientX:a.clientY)-this.startDragMouse)));this.onScroll_()};Blockly.Scrollbar.prototype.onMouseUpHandle_=function(){Blockly.clearTouchIdentifier();this.cleanUp_()}; +Blockly.Scrollbar.prototype.onMouseDownBar_=function(a){Blockly.Touch.clearTouchIdentifier();this.cleanUp_();if(Blockly.isRightButton(a))a.stopPropagation();else{var b=Blockly.mouseToSvg(a,this.workspace_.getParentSvg(),this.workspace_.getInverseScreenCTM()),b=this.horizontal_?b.x:b.y,c=Blockly.getSvgXY_(this.svgHandle_,this.workspace_),c=this.horizontal_?c.x:c.y,d=this.handlePosition_,e=.95*this.handleLength_;b<=c?d-=e:b>=c+this.handleLength_&&(d+=e);this.setHandlePosition(this.constrainHandle_(d)); +this.onScroll_();a.stopPropagation();a.preventDefault()}}; +Blockly.Scrollbar.prototype.onMouseDownHandle_=function(a){this.cleanUp_();Blockly.isRightButton(a)?a.stopPropagation():(this.startDragHandle=this.handlePosition_,this.startDragMouse=this.horizontal_?a.clientX:a.clientY,Blockly.Scrollbar.onMouseUpWrapper_=Blockly.bindEvent_(document,"mouseup",this,this.onMouseUpHandle_),Blockly.Scrollbar.onMouseMoveWrapper_=Blockly.bindEvent_(document,"mousemove",this,this.onMouseMoveHandle_),a.stopPropagation(),a.preventDefault())}; +Blockly.Scrollbar.prototype.onMouseMoveHandle_=function(a){this.setHandlePosition(this.constrainHandle_(this.startDragHandle+((this.horizontal_?a.clientX:a.clientY)-this.startDragMouse)));this.onScroll_()};Blockly.Scrollbar.prototype.onMouseUpHandle_=function(){Blockly.Touch.clearTouchIdentifier();this.cleanUp_()}; Blockly.Scrollbar.prototype.cleanUp_=function(){Blockly.hideChaff(!0);Blockly.Scrollbar.onMouseUpWrapper_&&(Blockly.unbindEvent_(Blockly.Scrollbar.onMouseUpWrapper_),Blockly.Scrollbar.onMouseUpWrapper_=null);Blockly.Scrollbar.onMouseMoveWrapper_&&(Blockly.unbindEvent_(Blockly.Scrollbar.onMouseMoveWrapper_),Blockly.Scrollbar.onMouseMoveWrapper_=null)}; Blockly.Scrollbar.prototype.constrainHandle_=function(a){return a=0>=a||isNaN(a)||this.scrollViewSize_=this.remainingCapacity())){Blockly.terminateDrag_();Blockly.Events.disable();try{var b=Blockly.Xml.domToBlock(a,this),c=parseInt(a.getAttribute("x"),10),d=parseInt(a.getAttribute("y"),10);if(!isNaN(c)&&!isNaN(d)){this.RTL&&(c=-c);do{a=!1;for(var e=this.getAllBlocks(),f=0,g;g=e[f];f++){var h=g.getRelativeToSurfaceXY();if(1>=Math.abs(c-h.x)&&1>=Math.abs(d-h.y)){a=!0;break}}if(!a)for(var k=b.getConnections_(!1), f=0,m;m=k[f];f++)if(m.closest(Blockly.SNAP_RADIUS,new goog.math.Coordinate(c,d)).connection){a=!0;break}a&&(c=this.RTL?c-Blockly.SNAP_RADIUS:c+Blockly.SNAP_RADIUS,d+=2*Blockly.SNAP_RADIUS)}while(a);b.moveBy(c,d)}}finally{Blockly.Events.enable()}Blockly.Events.isEnabled()&&!b.isShadow()&&Blockly.Events.fire(new Blockly.Events.Create(b));b.select()}}; -Blockly.WorkspaceSvg.prototype.createVariable=function(a){Blockly.WorkspaceSvg.superClass_.createVariable.call(this,a);this.toolbox_&&this.toolbox_.flyout_&&this.toolbox_.refreshSelection()};Blockly.WorkspaceSvg.prototype.recordDeleteAreas=function(){this.deleteAreaTrash_=this.trashcan?this.trashcan.getClientRect():null;this.deleteAreaToolbox_=this.flyout_?this.flyout_.getClientRect():this.toolbox_?this.toolbox_.getClientRect():null}; +Blockly.WorkspaceSvg.prototype.createVariable=function(a){Blockly.WorkspaceSvg.superClass_.createVariable.call(this,a);this.toolbox_&&this.toolbox_.flyout_&&!Blockly.Flyout.startFlyout_&&this.toolbox_.refreshSelection()};Blockly.WorkspaceSvg.prototype.recordDeleteAreas=function(){this.deleteAreaTrash_=this.trashcan?this.trashcan.getClientRect():null;this.deleteAreaToolbox_=this.flyout_?this.flyout_.getClientRect():this.toolbox_?this.toolbox_.getClientRect():null}; Blockly.WorkspaceSvg.prototype.isDeleteArea=function(a){a=new goog.math.Coordinate(a.clientX,a.clientY);if(this.deleteAreaTrash_){if(this.deleteAreaTrash_.contains(a))return this.trashcan.setOpen_(!0),Blockly.Css.setCursor(Blockly.Css.Cursor.DELETE),!0;this.trashcan.setOpen_(!1)}if(this.deleteAreaToolbox_&&this.deleteAreaToolbox_.contains(a))return Blockly.Css.setCursor(Blockly.Css.Cursor.DELETE),!0;Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED);return!1}; -Blockly.WorkspaceSvg.prototype.onMouseDown_=function(a){this.markFocused();Blockly.isTargetInput_(a)||(Blockly.terminateDrag_(),Blockly.hideChaff(),a.target&&a.target.nodeName&&("svg"==a.target.nodeName.toLowerCase()||a.target==this.svgBackground_)&&Blockly.selected&&!this.options.readOnly&&Blockly.selected.unselect(),Blockly.isRightButton(a)?this.showContextMenu_(a):this.scrollbar&&(this.dragMode_=Blockly.DRAG_BEGIN,this.startDragMouseX=a.clientX,this.startDragMouseY=a.clientY,this.startDragMetrics= -this.getMetrics(),this.startScrollX=this.scrollX,this.startScrollY=this.scrollY,"mouseup"in Blockly.bindEvent_.TOUCH_MAP&&(Blockly.onTouchUpWrapper_=Blockly.onTouchUpWrapper_||[],Blockly.onTouchUpWrapper_=Blockly.onTouchUpWrapper_.concat(Blockly.bindEvent_(document,"mouseup",null,Blockly.onMouseUp_))),Blockly.onMouseMoveWrapper_=Blockly.onMouseMoveWrapper_||[],Blockly.onMouseMoveWrapper_=Blockly.onMouseMoveWrapper_.concat(Blockly.bindEvent_(document,"mousemove",null,Blockly.onMouseMove_))),a.stopPropagation(), -a.preventDefault())};Blockly.WorkspaceSvg.prototype.startDrag=function(a,b){var c=Blockly.mouseToSvg(a,this.getParentSvg(),this.getInverseScreenCTM());c.x/=this.scale;c.y/=this.scale;this.dragDeltaXY_=goog.math.Coordinate.difference(b,c)};Blockly.WorkspaceSvg.prototype.moveDrag=function(a){a=Blockly.mouseToSvg(a,this.getParentSvg(),this.getInverseScreenCTM());a.x/=this.scale;a.y/=this.scale;return goog.math.Coordinate.sum(this.dragDeltaXY_,a)}; -Blockly.WorkspaceSvg.prototype.isDragging=function(){return Blockly.dragMode_==Blockly.DRAG_FREE||Blockly.Flyout.startFlyout_&&Blockly.Flyout.startFlyout_.dragMode_==Blockly.DRAG_FREE||this.dragMode_==Blockly.DRAG_FREE};Blockly.WorkspaceSvg.prototype.onMouseWheel_=function(a){Blockly.terminateDrag_();var b=0b.bottomRight.x&&(b.bottomRight.x=d.bottomRight.x);d.topLeft.yb.bottomRight.y&&(b.bottomRight.y=d.bottomRight.y)}return{x:b.topLeft.x,y:b.topLeft.y,width:b.bottomRight.x- b.topLeft.x,height:b.bottomRight.y-b.topLeft.y}};Blockly.WorkspaceSvg.prototype.cleanUp=function(){Blockly.Events.setGroup(!0);for(var a=this.getTopBlocks(!0),b=0,c=0,d;d=a[c];c++){var e=d.getRelativeToSurfaceXY();d.moveBy(-e.x,b-e.y);d.snapToGrid();b=d.getRelativeToSurfaceXY().y+d.getHeightWidth().height+Blockly.BlockSvg.MIN_BLOCK_Y}Blockly.Events.setGroup(!1);this.resizeContents()}; Blockly.WorkspaceSvg.prototype.showContextMenu_=function(a){function b(a){if(a.isDeletable())l=l.concat(a.getDescendants());else{a=a.getChildren();for(var c=0;cthis.workspace.remainingCapacity()&&(d.enabled=!1);c.push(d);this.isEditable()&&!this.collapsed_&&this.workspace.options.comments&&(d={enabled:!goog.userAgent.IE},this.comment?(d.text=Blockly.Msg.REMOVE_COMMENT, d.callback=function(){b.setCommentText(null)}):(d.text=Blockly.Msg.ADD_COMMENT,d.callback=function(){b.setCommentText("")}),c.push(d));if(!this.collapsed_)for(d=1;db.contentWidth-b.viewWidth)){var c=a.clientX-this.startDragMouseX_;this.startDragMouseX_=a.clientX;a=b.viewLeft-c;a=goog.math.clamp(a,0,b.contentWidth-b.viewWidth);this.scrollbar_.set(a)}}else 0>b.contentHeight-b.viewHeight||(c=a.clientY-this.startDragMouseY_,this.startDragMouseY_=a.clientY,a=b.viewTop-c,a=goog.math.clamp(a,0,b.contentHeight-b.viewHeight),this.scrollbar_.set(a))}; -Blockly.Flyout.prototype.onMouseMoveBlock_=function(a){if(!("mousemove"==a.type&&1>=a.clientX&&0==a.clientY&&0==a.button))if(this.determineDragIntention_(a.clientX-Blockly.Flyout.startDownEvent_.clientX,a.clientY-Blockly.Flyout.startDownEvent_.clientY))this.createBlockFunc_(Blockly.Flyout.startBlock_)(Blockly.Flyout.startDownEvent_);else if(this.dragMode_==Blockly.DRAG_FREE)this.onMouseMove_(a);a.stopPropagation()}; +Blockly.Flyout.prototype.onMouseMoveBlock_=function(a){"mousemove"==a.type&&1>=a.clientX&&0==a.clientY&&0==a.button||(this.determineDragIntention_(a.clientX-Blockly.Flyout.startDownEvent_.clientX,a.clientY-Blockly.Flyout.startDownEvent_.clientY)?(Blockly.longStop_(),this.createBlockFunc_(Blockly.Flyout.startBlock_)(Blockly.Flyout.startDownEvent_)):this.dragMode_==Blockly.DRAG_FREE&&(Blockly.longStop_(),this.onMouseMove_(a)));a.stopPropagation()}; Blockly.Flyout.prototype.determineDragIntention_=function(a,b){if(this.dragMode_==Blockly.DRAG_FREE)return!1;if(Math.sqrt(a*a+b*b)90-e&&(d=!0):c>-90-e&&c<-90+e&&(d=!0);else if(this.toolboxPosition_==Blockly.TOOLBOX_AT_LEFT)c-e&&(d=!0);else if(c<-180+e||c>180-e)d=!0;return d}; Blockly.Flyout.prototype.createBlockFunc_=function(a){var b=this;return function(c){if(!Blockly.isRightButton(c)&&!a.disabled){Blockly.Events.disable();try{var d=b.placeNewBlock_(a)}finally{Blockly.Events.enable()}Blockly.Events.isEnabled()&&(Blockly.Events.setGroup(!0),Blockly.Events.fire(new Blockly.Events.Create(d)));b.autoClose?b.hide():b.filterForCapacity_();d.onMouseDown_(c);Blockly.dragMode_=Blockly.DRAG_FREE;d.setDragging_(!0)}}}; @@ -1371,7 +1374,7 @@ Blockly.Flyout.prototype.placeNewBlock_=function(a){var b=this.targetWorkspace_, c.y+=d/e-d);a=Blockly.Xml.blockToDom(a);a=Blockly.Xml.domToBlock(a,b);e=a.getSvgRoot();if(!e)throw"block is not rendered.";e=Blockly.getSvgXY_(e,b);e.x+=b.scrollX/b.scale-b.scrollX;e.y+=b.scrollY/b.scale-b.scrollY;b.toolbox_&&!b.scrollbar&&(e.x+=b.toolbox_.getWidth()/b.scale,e.y+=b.toolbox_.getHeight()/b.scale);a.moveBy(c.x-e.x,c.y-e.y);return a}; Blockly.Flyout.prototype.filterForCapacity_=function(){for(var a=this.targetWorkspace_.remainingCapacity(),b=this.workspace_.getTopBlocks(!1),c=0,d;d=b[c];c++)if(-1==this.permanentlyDisabled_.indexOf(d)){var e=d.getDescendants();d.setDisabled(e.length>a)}}; Blockly.Flyout.prototype.getClientRect=function(){if(!this.svgGroup_)return null;var a=this.svgGroup_.getBoundingClientRect(),b=a.left,c=a.top,d=a.width,a=a.height;return this.toolboxPosition_==Blockly.TOOLBOX_AT_TOP?new goog.math.Rect(-1E9,c-1E9,2E9,1E9+a):this.toolboxPosition_==Blockly.TOOLBOX_AT_BOTTOM?new goog.math.Rect(-1E9,c,2E9,1E9+a):this.toolboxPosition_==Blockly.TOOLBOX_AT_LEFT?new goog.math.Rect(b-1E9,-1E9,1E9+d,2E9):new goog.math.Rect(b,-1E9,1E9+d,2E9)}; -Blockly.Flyout.terminateDrag_=function(){Blockly.Flyout.startFlyout_&&(Blockly.Flyout.startFlyout_.dragMode_==Blockly.DRAG_FREE&&Blockly.clearTouchIdentifier(),Blockly.Flyout.startFlyout_.dragMode_=Blockly.DRAG_NONE,Blockly.Flyout.startFlyout_=null);Blockly.Flyout.onMouseUpWrapper_&&(Blockly.unbindEvent_(Blockly.Flyout.onMouseUpWrapper_),Blockly.Flyout.onMouseUpWrapper_=null);Blockly.Flyout.onMouseMoveBlockWrapper_&&(Blockly.unbindEvent_(Blockly.Flyout.onMouseMoveBlockWrapper_),Blockly.Flyout.onMouseMoveBlockWrapper_= +Blockly.Flyout.terminateDrag_=function(){Blockly.Flyout.startFlyout_&&(Blockly.Flyout.startFlyout_.dragMode_==Blockly.DRAG_FREE&&Blockly.Touch.clearTouchIdentifier(),Blockly.Flyout.startFlyout_.dragMode_=Blockly.DRAG_NONE,Blockly.Flyout.startFlyout_=null);Blockly.Flyout.onMouseUpWrapper_&&(Blockly.unbindEvent_(Blockly.Flyout.onMouseUpWrapper_),Blockly.Flyout.onMouseUpWrapper_=null);Blockly.Flyout.onMouseMoveBlockWrapper_&&(Blockly.unbindEvent_(Blockly.Flyout.onMouseMoveBlockWrapper_),Blockly.Flyout.onMouseMoveBlockWrapper_= null);Blockly.Flyout.onMouseMoveWrapper_&&(Blockly.unbindEvent_(Blockly.Flyout.onMouseMoveWrapper_),Blockly.Flyout.onMouseMoveWrapper_=null);Blockly.Flyout.startDownEvent_=null;Blockly.Flyout.startBlock_=null}; Blockly.Flyout.prototype.reflowHorizontal=function(a){this.workspace_.scale=this.targetWorkspace_.scale;for(var b=0,c=0,d;d=a[c];c++)b=Math.max(b,d.getHeightWidth().height);b+=1.5*this.MARGIN;b*=this.workspace_.scale;b+=Blockly.Scrollbar.scrollbarThickness;if(this.height_!=b){for(c=0;d=a[c];c++){var e=d.getHeightWidth();if(d.flyoutRect_){d.flyoutRect_.setAttribute("width",e.width);d.flyoutRect_.setAttribute("height",e.height);var f=d.outputConnection?Blockly.BlockSvg.TAB_WIDTH:0,g=d.getRelativeToSurfaceXY(); d.flyoutRect_.setAttribute("y",g.y);d.flyoutRect_.setAttribute("x",this.RTL?g.x-e.width+f:g.x-f);(e=d.startHat_?Blockly.BlockSvg.START_HAT_HEIGHT:0)&&d.moveBy(0,e);d.flyoutRect_.setAttribute("y",g.y)}}this.height_=b;this.targetWorkspace_.resize()}}; @@ -1379,7 +1382,7 @@ Blockly.Flyout.prototype.reflowVertical=function(a){this.workspace_.scale=this.t g=b/this.workspace_.scale-this.MARGIN,g=g-Blockly.BlockSvg.TAB_WIDTH;d.moveBy(g-f,0)}d.flyoutRect_&&(d.flyoutRect_.setAttribute("width",e.width),d.flyoutRect_.setAttribute("height",e.height),g=d.outputConnection?Blockly.BlockSvg.TAB_WIDTH:0,f=d.getRelativeToSurfaceXY(),d.flyoutRect_.setAttribute("x",this.RTL?f.x-e.width+g:f.x-g),(e=d.startHat_?Blockly.BlockSvg.START_HAT_HEIGHT:0)&&d.moveBy(0,e),d.flyoutRect_.setAttribute("y",f.y))}this.width_=b;this.targetWorkspace_.resize()}}; Blockly.Flyout.prototype.reflow=function(){this.reflowWrapper_&&this.workspace_.removeChangeListener(this.reflowWrapper_);var a=this.workspace_.getTopBlocks(!1);this.horizontalLayout_?this.reflowHorizontal(a):this.reflowVertical(a);this.reflowWrapper_&&this.workspace_.addChangeListener(this.reflowWrapper_)};Blockly.Toolbox=function(a){this.workspace_=a;this.RTL=a.options.RTL;this.horizontalLayout_=a.options.horizontalLayout;this.toolboxPosition=a.options.toolboxPosition;this.config_={indentWidth:19,cssRoot:"blocklyTreeRoot",cssHideRoot:"blocklyHidden",cssItem:"",cssTreeRow:"blocklyTreeRow",cssItemLabel:"blocklyTreeLabel",cssTreeIcon:"blocklyTreeIcon",cssExpandedFolderIcon:"blocklyTreeIconOpen",cssFileIcon:"blocklyTreeIconNone",cssSelectedRow:"blocklyTreeSelected"};this.treeSeparatorConfig_={cssTreeRow:"blocklyTreeSeparator"}; this.horizontalLayout_&&(this.config_.cssTreeRow+=a.RTL?" blocklyHorizontalTreeRtl":" blocklyHorizontalTree",this.treeSeparatorConfig_.cssTreeRow="blocklyTreeSeparatorHorizontal "+(a.RTL?"blocklyHorizontalTreeRtl":"blocklyHorizontalTree"),this.config_.cssTreeIcon="")};Blockly.Toolbox.prototype.width=0;Blockly.Toolbox.prototype.height=0;Blockly.Toolbox.prototype.selectedOption_=null;Blockly.Toolbox.prototype.lastCategory_=null; -Blockly.Toolbox.prototype.init=function(){var a=this.workspace_,b=this.workspace_.getParentSvg();this.HtmlDiv=goog.dom.createDom("DIV","blocklyToolboxDiv");this.HtmlDiv.setAttribute("dir",a.RTL?"RTL":"LTR");b.parentNode.insertBefore(this.HtmlDiv,b);Blockly.bindEvent_(this.HtmlDiv,"mousedown",this,function(a){Blockly.isRightButton(a)||a.target==this.HtmlDiv?Blockly.hideChaff(!1):Blockly.hideChaff(!0);Blockly.clearTouchIdentifier()});this.flyout_=new Blockly.Flyout({disabledPatternId:a.options.disabledPatternId, +Blockly.Toolbox.prototype.init=function(){var a=this.workspace_,b=this.workspace_.getParentSvg();this.HtmlDiv=goog.dom.createDom("DIV","blocklyToolboxDiv");this.HtmlDiv.setAttribute("dir",a.RTL?"RTL":"LTR");b.parentNode.insertBefore(this.HtmlDiv,b);Blockly.bindEvent_(this.HtmlDiv,"mousedown",this,function(a){Blockly.isRightButton(a)||a.target==this.HtmlDiv?Blockly.hideChaff(!1):Blockly.hideChaff(!0);Blockly.Touch.clearTouchIdentifier()});this.flyout_=new Blockly.Flyout({disabledPatternId:a.options.disabledPatternId, parentWorkspace:a,RTL:a.RTL,horizontalLayout:a.horizontalLayout,toolboxPosition:a.options.toolboxPosition});goog.dom.insertSiblingAfter(this.flyout_.createDom(),a.svgGroup_);this.flyout_.init(a);this.config_.cleardotPath=a.options.pathToMedia+"1x1.gif";this.config_.cssCollapsedFolderIcon="blocklyTreeIconClosed"+(a.RTL?"Rtl":"Ltr");this.tree_=b=new Blockly.Toolbox.TreeControl(this,this.config_);b.setShowRootNode(!1);b.setShowLines(!1);b.setShowExpandIcons(!1);b.setSelectedItem(null);a=this.populate_(a.options.languageTree); b.render(this.HtmlDiv);a&&b.setSelectedItem(a);this.addColour_();this.position()};Blockly.Toolbox.prototype.dispose=function(){this.flyout_.dispose();this.tree_.dispose();goog.dom.removeNode(this.HtmlDiv);this.lastCategory_=this.workspace_=null};Blockly.Toolbox.prototype.getWidth=function(){return this.width};Blockly.Toolbox.prototype.getHeight=function(){return this.height}; Blockly.Toolbox.prototype.position=function(){var a=this.HtmlDiv;if(a){var b=this.workspace_.getParentSvg();goog.style.getPageOffset(b);b=Blockly.svgSize(b);this.horizontalLayout_?(a.style.left="0",a.style.height="auto",a.style.width=b.width+"px",this.height=a.offsetHeight,this.toolboxPosition==Blockly.TOOLBOX_AT_TOP?a.style.top="0":a.style.bottom="0"):(this.toolboxPosition==Blockly.TOOLBOX_AT_RIGHT?a.style.right="0":a.style.left="0",a.style.height=b.height+"px",this.width=a.offsetWidth);this.flyout_.position()}}; @@ -1389,13 +1392,17 @@ Blockly.Toolbox.prototype.syncTrees_=function(a,b,c){for(var d=null,e=null,f=0,g Blockly.Toolbox.prototype.addColour_=function(a){a=(a||this.tree_).getChildren();for(var b=0,c;c=a[b];b++){var d=c.getRowElement();if(d){var e=this.hasColours_?"8px solid "+(c.hexColour||"#ddd"):"none";this.workspace_.RTL?d.style.borderRight=e:d.style.borderLeft=e}this.addColour_(c)}};Blockly.Toolbox.prototype.clearSelection=function(){this.tree_.setSelectedItem(null)}; Blockly.Toolbox.prototype.getClientRect=function(){if(!this.HtmlDiv)return null;var a=this.HtmlDiv.getBoundingClientRect(),b=a.left,c=a.top,d=a.width,a=a.height;return this.toolboxPosition==Blockly.TOOLBOX_AT_LEFT?new goog.math.Rect(-1E7,-1E7,1E7+b+d,2E7):this.toolboxPosition==Blockly.TOOLBOX_AT_RIGHT?new goog.math.Rect(b,-1E7,1E7+d,2E7):this.toolboxPosition==Blockly.TOOLBOX_AT_TOP?new goog.math.Rect(-1E7,-1E7,2E7,1E7+c+a):new goog.math.Rect(0,c,2E7,1E7+d)}; Blockly.Toolbox.prototype.refreshSelection=function(){var a=this.tree_.getSelectedItem();a&&a.blocks&&this.flyout_.show(a.blocks)};Blockly.Toolbox.TreeControl=function(a,b){this.toolbox_=a;goog.ui.tree.TreeControl.call(this,goog.html.SafeHtml.EMPTY,b)};goog.inherits(Blockly.Toolbox.TreeControl,goog.ui.tree.TreeControl); -Blockly.Toolbox.TreeControl.prototype.enterDocument=function(){Blockly.Toolbox.TreeControl.superClass_.enterDocument.call(this);var a=this.getElement();goog.events.BrowserFeature.TOUCH_ENABLED&&Blockly.bindEvent_(a,goog.events.EventType.TOUCHSTART,this,this.handleTouchEvent_);Blockly.bindEvent_(a,"mouseup",this,Blockly.clearTouchIdentifier)}; +Blockly.Toolbox.TreeControl.prototype.enterDocument=function(){Blockly.Toolbox.TreeControl.superClass_.enterDocument.call(this);var a=this.getElement();goog.events.BrowserFeature.TOUCH_ENABLED&&Blockly.bindEvent_(a,goog.events.EventType.TOUCHSTART,this,this.handleTouchEvent_);Blockly.bindEvent_(a,"mouseup",this,Blockly.Touch.clearTouchIdentifier)}; Blockly.Toolbox.TreeControl.prototype.handleTouchEvent_=function(a){a.preventDefault();var b=this.getNodeFromEvent_(a);b&&a.type===goog.events.EventType.TOUCHSTART&&setTimeout(function(){b.onMouseDown(a)},1)};Blockly.Toolbox.TreeControl.prototype.createNode=function(a){return new Blockly.Toolbox.TreeNode(this.toolbox_,a?goog.html.SafeHtml.htmlEscape(a):goog.html.SafeHtml.EMPTY,this.getConfig(),this.getDomHelper())}; Blockly.Toolbox.TreeControl.prototype.setSelectedItem=function(a){var b=this.toolbox_;if(a!=this.selectedItem_&&a!=b.tree_){b.lastCategory_&&(b.lastCategory_.getRowElement().style.backgroundColor="");if(a){var c=a.hexColour||"#57e";a.getRowElement().style.backgroundColor=c;b.addColour_(a)}c=this.getSelectedItem();goog.ui.tree.TreeControl.prototype.setSelectedItem.call(this,a);a&&a.blocks&&a.blocks.length?(b.flyout_.show(a.blocks),b.lastCategory_!=a&&b.flyout_.scrollToStart()):b.flyout_.hide();c!= a&&c!=this&&(c=new Blockly.Events.Ui(null,"category",c&&c.getHtml(),a&&a.getHtml()),c.workspaceId=b.workspace_.id,Blockly.Events.fire(c));a&&(b.lastCategory_=a)}};Blockly.Toolbox.TreeNode=function(a,b,c,d){goog.ui.tree.TreeNode.call(this,b,c,d);a&&(this.horizontalLayout_=a.horizontalLayout_,b=function(){Blockly.svgResize(a.workspace_)},goog.events.listen(a.tree_,goog.ui.tree.BaseNode.EventType.EXPAND,b),goog.events.listen(a.tree_,goog.ui.tree.BaseNode.EventType.COLLAPSE,b))}; goog.inherits(Blockly.Toolbox.TreeNode,goog.ui.tree.TreeNode);Blockly.Toolbox.TreeNode.prototype.getExpandIconSafeHtml=function(){return goog.html.SafeHtml.create("span")};Blockly.Toolbox.TreeNode.prototype.onMouseDown=function(a){this.hasChildren()&&this.isUserCollapsible_?(this.toggle(),this.select()):this.isSelected()?this.getTree().setSelectedItem(null):this.select();this.updateRow()};Blockly.Toolbox.TreeNode.prototype.onDoubleClick_=function(a){}; Blockly.Toolbox.TreeNode.prototype.onKeyDown=function(a){if(this.horizontalLayout_){var b={};b[goog.events.KeyCodes.RIGHT]=goog.events.KeyCodes.DOWN;b[goog.events.KeyCodes.LEFT]=goog.events.KeyCodes.UP;b[goog.events.KeyCodes.UP]=goog.events.KeyCodes.LEFT;b[goog.events.KeyCodes.DOWN]=goog.events.KeyCodes.RIGHT;a.keyCode=b[a.keyCode]||a.keyCode}return Blockly.Toolbox.TreeNode.superClass_.onKeyDown.call(this,a)};Blockly.Toolbox.TreeSeparator=function(a){Blockly.Toolbox.TreeNode.call(this,null,"",a)}; -goog.inherits(Blockly.Toolbox.TreeSeparator,Blockly.Toolbox.TreeNode);Blockly.Css={};Blockly.Css.Cursor={OPEN:"handopen",CLOSED:"handclosed",DELETE:"handdelete"};Blockly.Css.currentCursor_="";Blockly.Css.styleSheet_=null;Blockly.Css.mediaPath_=""; +goog.inherits(Blockly.Toolbox.TreeSeparator,Blockly.Toolbox.TreeNode);Blockly.Touch={};Blockly.touchIdentifier_=null;Blockly.Touch.TOUCH_MAP={};goog.events.BrowserFeature.TOUCH_ENABLED&&(Blockly.Touch.TOUCH_MAP={mousedown:["touchstart"],mousemove:["touchmove"],mouseup:["touchend","touchcancel"]});Blockly.Touch.clearTouchIdentifier=function(){console.trace("\tclearing touch identifier");null==Blockly.touchIdentifier_&&console.log("\t\ttouch identifier was already null");Blockly.touchIdentifier_=null}; +Blockly.Touch.shouldHandleEvent=function(a){return!Blockly.Touch.isMouseOrTouchEvent(a)||Blockly.Touch.checkTouchIdentifier(a)}; +Blockly.Touch.checkTouchIdentifier=function(a){var b;try{b=a.changedTouches&&a.changedTouches.item(0)&&void 0!=a.changedTouches.item(0).identifier&&null!=a.changedTouches.item(0).identifier?a.changedTouches.item(0).identifier:"mouse"}catch(c){b=a.changedTouches&&a.changedTouches[0]&&void 0!=a.changedTouches[0].identifier&&null!=a.changedTouches[0].identifier?a.changedTouches[0].identifier:"mouse"}return void 0!=Blockly.touchIdentifier_&&null!=Blockly.touchIdentifier_?Blockly.touchIdentifier_==b:"mousedown"== +a.type||"touchstart"==a.type?(console.trace("setting touch identfier"),Blockly.touchIdentifier_=b,!0):!1};Blockly.Touch.setClientFromTouch=function(a){if(0==a.type.indexOf("touch")){var b=a.changedTouches[0];a.clientX=b.clientX;a.clientY=b.clientY}};Blockly.Touch.isMouseOrTouchEvent=function(a){return 0==a.type.indexOf("touch")||0==a.type.indexOf("mouse")}; +Blockly.Touch.splitEventByTouches=function(a){var b=[];if(a.changedTouches&&1>>/g,Blockly.Css.mediaPath_),d=document.createElement("style");document.head.appendChild(d);c=document.createTextNode(c);d.appendChild(c);Blockly.Css.styleSheet_=d.sheet;Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN)}}; Blockly.Css.setCursor=function(a){if(Blockly.Css.currentCursor_!=a){Blockly.Css.currentCursor_=a;var b="url("+Blockly.Css.mediaPath_+"/"+a+".cur), auto",c=".blocklyDraggable {\n cursor: "+b+";\n}\n";Blockly.Css.styleSheet_.deleteRule(0);Blockly.Css.styleSheet_.insertRule(c,0);for(var c=document.getElementsByClassName("blocklyToolboxDiv"),d=0,e;e=c[d];d++)e.style.cursor=a==Blockly.Css.Cursor.DELETE?b:"";document.body.parentNode.style.cursor=a==Blockly.Css.Cursor.OPEN?"":b}}; Blockly.Css.CONTENT=[".blocklySvg {","background-color: #fff;","outline: none;","overflow: hidden;","display: block;","}",".blocklyWidgetDiv {","display: none;","position: absolute;","z-index: 99999;","}",".injectionDiv {","height: 100%;","position: relative;","}",".blocklyNonSelectable {","user-select: none;","-moz-user-select: none;","-webkit-user-select: none;","-ms-user-select: none;","}",".blocklyTooltipDiv {","background-color: #ffffc7;","border: 1px solid #ddc;","box-shadow: 4px 4px 20px 1px rgba(0,0,0,.15);", @@ -1429,13 +1436,11 @@ a.contentWidth>(b.RTL?a.viewWidth:a.viewWidth+e))for(var g=c.getTopBlocks(!1),h= Blockly.init_=function(a){var b=a.options,c=a.getParentSvg();Blockly.bindEvent_(c,"contextmenu",null,function(a){Blockly.isTargetInput_(a)||a.preventDefault()});c=Blockly.bindEvent_(window,"resize",null,function(){Blockly.hideChaff(!0);Blockly.svgResize(a)});a.setResizeHandlerWrapper(c);Blockly.inject.bindDocumentEvents_();b.languageTree&&(a.toolbox_?a.toolbox_.init(a):a.flyout_&&(a.flyout_.init(a),a.flyout_.show(b.languageTree.childNodes),a.flyout_.scrollToStart(),a.scrollX=a.flyout_.width_,b.toolboxPosition== Blockly.TOOLBOX_AT_RIGHT&&(a.scrollX*=-1),a.translate(a.scrollX,0)));b.hasScrollbars&&(a.scrollbar=new Blockly.ScrollbarPair(a),a.scrollbar.resize());b.hasSounds&&Blockly.inject.loadSounds_(b.pathToMedia,a)}; Blockly.inject.bindDocumentEvents_=function(){Blockly.documentEventsBound_||(Blockly.bindEvent_(document,"keydown",null,Blockly.onKeyDown_),Blockly.bindEvent_(document,"touchend",null,Blockly.longStop_),Blockly.bindEvent_(document,"touchcancel",null,Blockly.longStop_),document.addEventListener("mouseup",Blockly.onMouseUp_,!1),goog.userAgent.IPAD&&Blockly.bindEvent_(window,"orientationchange",document,function(){Blockly.svgResize(Blockly.getMainWorkspace())}));Blockly.documentEventsBound_=!0}; -Blockly.inject.loadSounds_=function(a,b){b.loadAudio_([a+"click.mp3",a+"click.wav",a+"click.ogg"],"click");b.loadAudio_([a+"disconnect.wav",a+"disconnect.mp3",a+"disconnect.ogg"],"disconnect");b.loadAudio_([a+"delete.mp3",a+"delete.ogg",a+"delete.wav"],"delete");var c=[],d=function(){for(;c.length;)Blockly.unbindEvent_(c.pop()),Blockly.clearTouchIdentifier(),console.log("\t\t\tunbinding sounds");b.preloadAudio_()};c.push(Blockly.bindEvent_(document,"mousemove",null,d));c.push(Blockly.bindEvent_(document, -"touchstart",null,d))};Blockly.updateToolbox=function(a){console.warn("Deprecated call to Blockly.updateToolbox, use workspace.updateToolbox instead.");Blockly.getMainWorkspace().updateToolbox(a)};Blockly.utils={};Blockly.addClass_=function(a,b){var c=a.getAttribute("class")||"";-1==(" "+c+" ").indexOf(" "+b+" ")&&(c&&(c+=" "),a.setAttribute("class",c+b))};Blockly.removeClass_=function(a,b){var c=a.getAttribute("class");if(-1!=(" "+c+" ").indexOf(" "+b+" ")){for(var c=c.split(/\s+/),d=0;dc;c++)b[c]=Blockly.genUid.soup_.charAt(Math.random()*a);return b.join("")};Blockly.genUid.soup_="!#%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";Blockly.utils.wrap=function(a,b){for(var c=a.split("\n"),d=0;db&&(b=c[d].length);var e,d=-Infinity,f,g=1;do{e=d;f=a;for(var h=[],k=c.length/g,m=1,d=0;de);return f}; Blockly.utils.wrapScore_=function(a,b,c){for(var d=[0],e=[],f=0;fd&&(d=h,e=g)}return e?Blockly.utils.wrapMutate_(a,e,c):b};Blockly.utils.wrapToText_=function(a,b){for(var c=[],d=0;dd&&(d=h,e=g)}return e?Blockly.utils.wrapMutate_(a,e,c):b};Blockly.utils.wrapToText_=function(a,b){for(var c=[],d=0;dBlockly.DRAG_RADIUS&&(Blockly.longStop_(), b.dragMode_=Blockly.DRAG_FREE);a.stopPropagation();a.preventDefault()}}; Blockly.onKeyDown_=function(a){if(!Blockly.mainWorkspace.options.readOnly&&!Blockly.isTargetInput_(a)){var b=!1;if(27==a.keyCode)Blockly.hideChaff();else if(8==a.keyCode||46==a.keyCode)a.preventDefault(),Blockly.selected&&Blockly.selected.isDeletable()&&(b=!0);else if(a.altKey||a.ctrlKey||a.metaKey)Blockly.selected&&Blockly.selected.isDeletable()&&Blockly.selected.isMovable()&&(67==a.keyCode?(Blockly.hideChaff(),Blockly.copy_(Blockly.selected)):88==a.keyCode&&(Blockly.copy_(Blockly.selected),b=!0)), diff --git a/blockly_uncompressed.js b/blockly_uncompressed.js index 51f44da17..73d2f4597 100644 --- a/blockly_uncompressed.js +++ b/blockly_uncompressed.js @@ -46,10 +46,10 @@ goog.addDependency("../../../" + dir + "/core/events.js", ['Blockly.Events'], [' goog.addDependency("../../../" + dir + "/core/trashcan.js", ['Blockly.Trashcan'], ['goog.Timer', 'goog.dom', 'goog.math', 'goog.math.Rect']); goog.addDependency("../../../" + dir + "/core/connection.js", ['Blockly.Connection'], ['goog.asserts', 'goog.dom']); goog.addDependency("../../../" + dir + "/core/widgetdiv.js", ['Blockly.WidgetDiv'], ['Blockly.Css', 'goog.dom', 'goog.dom.TagName', 'goog.style']); -goog.addDependency("../../../" + dir + "/core/blockly.js", ['Blockly'], ['Blockly.BlockSvg.render', 'Blockly.Events', 'Blockly.FieldAngle', 'Blockly.FieldCheckbox', 'Blockly.FieldColour', 'Blockly.FieldDropdown', 'Blockly.FieldImage', 'Blockly.FieldTextInput', 'Blockly.FieldNumber', 'Blockly.FieldVariable', 'Blockly.Generator', 'Blockly.Msg', 'Blockly.Procedures', 'Blockly.Toolbox', 'Blockly.WidgetDiv', 'Blockly.WorkspaceSvg', 'Blockly.constants', 'Blockly.inject', 'Blockly.utils', 'goog.color', 'goog.userAgent']); +goog.addDependency("../../../" + dir + "/core/blockly.js", ['Blockly'], ['Blockly.BlockSvg.render', 'Blockly.Events', 'Blockly.FieldAngle', 'Blockly.FieldCheckbox', 'Blockly.FieldColour', 'Blockly.FieldDropdown', 'Blockly.FieldImage', 'Blockly.FieldTextInput', 'Blockly.FieldNumber', 'Blockly.FieldVariable', 'Blockly.Generator', 'Blockly.Msg', 'Blockly.Procedures', 'Blockly.Toolbox', 'Blockly.Touch', 'Blockly.WidgetDiv', 'Blockly.WorkspaceSvg', 'Blockly.constants', 'Blockly.inject', 'Blockly.utils', 'goog.color', 'goog.userAgent']); goog.addDependency("../../../" + dir + "/core/flyout_button.js", ['Blockly.FlyoutButton'], ['goog.dom', 'goog.math.Coordinate']); goog.addDependency("../../../" + dir + "/core/block_render_svg.js", ['Blockly.BlockSvg.render'], ['Blockly.BlockSvg']); -goog.addDependency("../../../" + dir + "/core/utils.js", ['Blockly.utils'], ['goog.dom', 'goog.events.BrowserFeature', 'goog.math.Coordinate', 'goog.userAgent']); +goog.addDependency("../../../" + dir + "/core/utils.js", ['Blockly.utils'], ['Blockly.Touch', 'goog.dom', 'goog.events.BrowserFeature', 'goog.math.Coordinate', 'goog.userAgent']); goog.addDependency("../../../" + dir + "/core/msg.js", ['Blockly.Msg'], []); goog.addDependency("../../../" + dir + "/core/contextmenu.js", ['Blockly.ContextMenu'], ['goog.dom', 'goog.events', 'goog.style', 'goog.ui.Menu', 'goog.ui.MenuItem']); goog.addDependency("../../../" + dir + "/core/icon.js", ['Blockly.Icon'], ['goog.dom', 'goog.math.Coordinate']); @@ -83,6 +83,7 @@ goog.addDependency("../../../" + dir + "/core/field_angle.js", ['Blockly.FieldAn goog.addDependency("../../../" + dir + "/core/zoom_controls.js", ['Blockly.ZoomControls'], ['goog.dom']); goog.addDependency("../../../" + dir + "/core/workspace.js", ['Blockly.Workspace'], ['goog.math']); goog.addDependency("../../../" + dir + "/core/field_date.js", ['Blockly.FieldDate'], ['Blockly.Field', 'goog.date', 'goog.dom', 'goog.events', 'goog.i18n.DateTimeSymbols', 'goog.i18n.DateTimeSymbols_he', 'goog.style', 'goog.ui.DatePicker']); +goog.addDependency("../../../" + dir + "/core/touch.js", ['Blockly.Touch'], []); goog.addDependency("../../../" + dir + "/core/generator.js", ['Blockly.Generator'], ['Blockly.Block', 'goog.asserts']); goog.addDependency("../../../" + dir + "/core/inject.js", ['Blockly.inject'], ['Blockly.Css', 'Blockly.Options', 'Blockly.WorkspaceSvg', 'goog.dom', 'goog.ui.Component', 'goog.userAgent']); goog.addDependency("../../../" + dir + "/core/connection_db.js", ['Blockly.ConnectionDB'], ['Blockly.Connection']); @@ -1645,6 +1646,7 @@ goog.require('Blockly.Scrollbar'); goog.require('Blockly.ScrollbarPair'); goog.require('Blockly.Toolbox'); goog.require('Blockly.Tooltip'); +goog.require('Blockly.Touch'); goog.require('Blockly.Trashcan'); goog.require('Blockly.Variables'); goog.require('Blockly.Warning'); diff --git a/core/block_svg.js b/core/block_svg.js index 42520184a..7553ca899 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -558,7 +558,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { // Right-click. this.showContextMenu_(e); // Click, not drag, so stop waiting for other touches from this identifier. - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); } else if (!this.isMovable()) { // Allow immovable blocks to be selected and context menued, but not // dragged. Let this event bubble up to document, so the workspace may be @@ -604,7 +604,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { * @private */ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); if (Blockly.dragMode_ != Blockly.DRAG_FREE && !Blockly.WidgetDiv.isVisible()) { Blockly.Events.fire( diff --git a/core/blockly.js b/core/blockly.js index 68d372959..106957f52 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -44,6 +44,7 @@ goog.require('Blockly.Generator'); goog.require('Blockly.Msg'); goog.require('Blockly.Procedures'); goog.require('Blockly.Toolbox'); +goog.require('Blockly.Touch'); goog.require('Blockly.WidgetDiv'); goog.require('Blockly.WorkspaceSvg'); goog.require('Blockly.constants'); @@ -113,20 +114,6 @@ Blockly.clipboardSource_ = null; */ Blockly.dragMode_ = Blockly.DRAG_NONE; -/** - * Wrapper function called when a touch mouseUp occurs during a drag operation. - * @type {Array.} - * @private - */ -Blockly.onTouchUpWrapper_ = null; - -/** - * Which touch events are we currently paying attention to? - * @type {DOMString} - * @private - */ -Blockly.touchIdentifier_ = null; - /** * Convert a hue (HSV model) into an RGB hex triplet. * @param {number} hue Hue on a colour wheel (0-360). @@ -156,68 +143,6 @@ Blockly.resizeSvgContents = function(workspace) { workspace.resizeContents(); }; -/** - * Clear the touch identifier that tracks which touch stream to pay attention - * to. - */ -Blockly.clearTouchIdentifier = function() { - console.trace('\tclearing touch identifier'); - if (Blockly.touchIdentifier_ == null) { - console.log('\t\ttouch identifier was already null'); - } - Blockly.touchIdentifier_ = null; -}; - -/** - * Decide whether Blockly should handle or ignore this event. - * Mouse and touch events require special checks because we only want to deal - * with one touch stream at a time. All other events should always be handled. - * @param {!Event} e The event to check. - * @return {boolean} True if this event should be passed through to the - * registered handler; false if it should be blocked. - */ -Blockly.shouldHandleEvent = function(e) { - return !Blockly.isMouseOrTouchEvent(e) || Blockly.checkTouchIdentifier(e); -}; - -/** - * Check whether the touch identifier on the event matches the current saved - * identifier. If there is no identifier, that means it's a mouse event and - * we'll use the identifier "mouse". This means we won't deal well with - * multiple mice being used at the same time. That seems okay. - * If the current identifier was unset, save the identifier from the - * event. - * @param {!Event} e Mouse event or touch event. - * @return {boolean} Whether the identifier on the event matches the current - * saved identifier. - */ -Blockly.checkTouchIdentifier = function(e) { - var identifier = (e.changedTouches && e.changedTouches.item(0) && - e.changedTouches.item(0).identifier != undefined && - e.changedTouches.item(0).identifier != null) ? - e.changedTouches.item(0).identifier : 'mouse'; - - // if (Blockly.touchIdentifier_ )is insufficient because android touch - // identifiers may be zero. - if (Blockly.touchIdentifier_ != undefined && - Blockly.touchIdentifier_ != null) { - // We're already tracking some touch/mouse event. Is this from the same - // source? - return Blockly.touchIdentifier_ == identifier; - } - if (e.type == 'mousedown' || e.type == 'touchstart') { - // No identifier set yet, and this is the start of a drag. Set it and - // return. - console.trace('setting touch identfier'); - Blockly.touchIdentifier_ = identifier; - return true; - } - // There was no identifier yet, but this wasn't a start event so we're going - // to ignore it. This probably means that another drag finished while this - // pointer was down. - return false; -}; - /** * Size the SVG image to completely fill its container. Call this when the view * actually changes sizes (e.g. on a window resize/device orientation change). @@ -250,63 +175,6 @@ Blockly.svgResize = function(workspace) { mainWorkspace.resize(); }; -/** - * Handle a mouse-up anywhere on the page. - * @param {!Event} e Mouse up event. - * @private - */ -Blockly.onMouseUp_ = function(e) { - var workspace = Blockly.getMainWorkspace(); - if (workspace.dragMode_ == Blockly.DRAG_NONE) { - return; - } - Blockly.clearTouchIdentifier(); - Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN); - workspace.dragMode_ = Blockly.DRAG_NONE; - // Unbind the touch event if it exists. - if (Blockly.onTouchUpWrapper_) { - Blockly.unbindEvent_(Blockly.onTouchUpWrapper_); - Blockly.onTouchUpWrapper_ = null; - } - if (Blockly.onMouseMoveWrapper_) { - Blockly.unbindEvent_(Blockly.onMouseMoveWrapper_); - Blockly.onMouseMoveWrapper_ = null; - } -}; - -/** - * Handle a mouse-move on SVG drawing surface. - * @param {!Event} e Mouse move event. - * @private - */ -Blockly.onMouseMove_ = function(e) { - var workspace = Blockly.getMainWorkspace(); - if (workspace.dragMode_ != Blockly.DRAG_NONE) { - var dx = e.clientX - workspace.startDragMouseX; - var dy = e.clientY - workspace.startDragMouseY; - var metrics = workspace.startDragMetrics; - var x = workspace.startScrollX + dx; - var y = workspace.startScrollY + dy; - x = Math.min(x, -metrics.contentLeft); - y = Math.min(y, -metrics.contentTop); - x = Math.max(x, metrics.viewWidth - metrics.contentLeft - - metrics.contentWidth); - y = Math.max(y, metrics.viewHeight - metrics.contentTop - - metrics.contentHeight); - - // Move the scrollbars and the page will scroll automatically. - workspace.scrollbar.set(-x - metrics.contentLeft, - -y - metrics.contentTop); - // Cancel the long-press if the drag has moved too far. - if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) { - Blockly.longStop_(); - workspace.dragMode_ = Blockly.DRAG_FREE; - } - e.stopPropagation(); - e.preventDefault(); - } -}; - /** * Handle a key-down on SVG drawing surface. * @param {!Event} e Key down event. @@ -380,43 +248,6 @@ Blockly.terminateDrag_ = function() { Blockly.Flyout.terminateDrag_(); }; -/** - * PID of queued long-press task. - * @private - */ -Blockly.longPid_ = 0; - -/** - * Context menus on touch devices are activated using a long-press. - * Unfortunately the contextmenu touch event is currently (2015) only suported - * by Chrome. This function is fired on any touchstart event, queues a task, - * which after about a second opens the context menu. The tasks is killed - * if the touch event terminates early. - * @param {!Event} e Touch start event. - * @param {!Blockly.Block|!Blockly.WorkspaceSvg} uiObject The block or workspace - * under the touchstart event. - * @private - */ -Blockly.longStart_ = function(e, uiObject) { - Blockly.longStop_(); - Blockly.longPid_ = setTimeout(function() { - e.button = 2; // Simulate a right button click. - uiObject.onMouseDown_(e); - }, Blockly.LONGPRESS); -}; - -/** - * Nope, that's not a long-press. Either touchend or touchcancel was fired, - * or a drag hath begun. Kill the queued long-press task. - * @private - */ -Blockly.longStop_ = function() { - if (Blockly.longPid_) { - clearTimeout(Blockly.longPid_); - Blockly.longPid_ = 0; - } -}; - /** * Copy a block onto the local clipboard. * @param {!Blockly.Block} block Block to be copied. diff --git a/core/bubble.js b/core/bubble.js index 413a510cf..a07d8a696 100644 --- a/core/bubble.js +++ b/core/bubble.js @@ -150,7 +150,7 @@ Blockly.Bubble.unbindDragEvents_ = function() { * @private */ Blockly.Bubble.bubbleMouseUp_ = function(/*e*/) { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN); Blockly.Bubble.unbindDragEvents_(); }; diff --git a/core/flyout.js b/core/flyout.js index 27a5a754e..2d98e3434 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -844,11 +844,18 @@ Blockly.Flyout.prototype.addBlockListeners_ = function(root, block, rect) { block.removeSelect)); }; +/** + * Actions to take when a block in the flyout is right-clicked. + * @param {!Event} e Event that triggered the right-click. Could originate from + * a long-press in a touch environment. + * @param {Blockly.BlockSvg} block The block that was clicked. + */ Blockly.Flyout.blockRightClick_ = function(e, block) { Blockly.terminateDrag_(); Blockly.hideChaff(true); block.showContextMenu_(e); - Blockly.clearTouchIdentifier(); + // This was a right-click, so end the gesture immediately. + Blockly.Touch.clearTouchIdentifier(); }; /** @@ -891,7 +898,7 @@ Blockly.Flyout.prototype.blockMouseDown_ = function(block) { */ Blockly.Flyout.prototype.onMouseDown_ = function(e) { if (Blockly.isRightButton(e)) { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); return; } Blockly.hideChaff(true); @@ -917,7 +924,8 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { */ Blockly.Flyout.prototype.onMouseUp_ = function(e) { if (!this.workspace_.isDragging()) { - Blockly.clearTouchIdentifier(); + // This was a click, not a drag. End the gesture. + Blockly.Touch.clearTouchIdentifier(); if (this.autoClose) { this.createBlockFunc_(Blockly.Flyout.startBlock_)( Blockly.Flyout.startDownEvent_); @@ -1237,7 +1245,7 @@ Blockly.Flyout.terminateDrag_ = function() { if (Blockly.Flyout.startFlyout_) { // User was dragging the flyout background, and has stopped. if (Blockly.Flyout.startFlyout_.dragMode_ == Blockly.DRAG_FREE) { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); } Blockly.Flyout.startFlyout_.dragMode_ = Blockly.DRAG_NONE; Blockly.Flyout.startFlyout_ = null; diff --git a/core/scrollbar.js b/core/scrollbar.js index cc9318624..eb1c7d403 100644 --- a/core/scrollbar.js +++ b/core/scrollbar.js @@ -599,7 +599,7 @@ Blockly.Scrollbar.prototype.setVisible = function(visible) { * @private */ Blockly.Scrollbar.prototype.onMouseDownBar_ = function(e) { - Blockly.clearTouchIdentifier(); // This is really a click. + Blockly.Touch.clearTouchIdentifier(); // This is really a click. this.cleanUp_(); if (Blockly.isRightButton(e)) { // Right-click. @@ -676,7 +676,7 @@ Blockly.Scrollbar.prototype.onMouseMoveHandle_ = function(e) { * @private */ Blockly.Scrollbar.prototype.onMouseUpHandle_ = function() { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); this.cleanUp_(); }; diff --git a/core/toolbox.js b/core/toolbox.js index 61b82a879..15141c471 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -163,7 +163,7 @@ Blockly.Toolbox.prototype.init = function() { // Just close popups. Blockly.hideChaff(true); } - Blockly.clearTouchIdentifier(); // Don't block future drags. + Blockly.Touch.clearTouchIdentifier(); // Don't block future drags. }); var workspaceOptions = { disabledPatternId: workspace.options.disabledPatternId, @@ -476,7 +476,7 @@ Blockly.Toolbox.TreeControl.prototype.enterDocument = function() { this.handleTouchEvent_); } - Blockly.bindEvent_(el, 'mouseup', this, Blockly.clearTouchIdentifier); + Blockly.bindEvent_(el, 'mouseup', this, Blockly.Touch.clearTouchIdentifier); }; /** diff --git a/core/touch.js b/core/touch.js new file mode 100644 index 000000000..a25b28f51 --- /dev/null +++ b/core/touch.js @@ -0,0 +1,274 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2016 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Touch handling for Blockly. + * @author fenichel@google.com (Rachel Fenichel) + */ +'use strict'; + +goog.provide('Blockly.Touch'); + + +/** + * Which touch events are we currently paying attention to? + * @type {DOMString} + * @private + */ +Blockly.Touch.touchIdentifier_ = null; + +/** + * Wrapper function called when a touch mouseUp occurs during a drag operation. + * @type {Array.} + * @private + */ +Blockly.Touch.onTouchUpWrapper_ = null; + +/** + * The TOUCH_MAP lookup dictionary specifies additional touch events to fire, + * in conjunction with mouse events. + * @type {Object} + */ +Blockly.Touch.TOUCH_MAP = {}; +if (goog.events.BrowserFeature.TOUCH_ENABLED) { + Blockly.Touch.TOUCH_MAP = { + 'mousedown': ['touchstart'], + 'mousemove': ['touchmove'], + 'mouseup': ['touchend', 'touchcancel'] + }; +} + +/** + * PID of queued long-press task. + * @private + */ +Blockly.longPid_ = 0; + +/** + * Context menus on touch devices are activated using a long-press. + * Unfortunately the contextmenu touch event is currently (2015) only suported + * by Chrome. This function is fired on any touchstart event, queues a task, + * which after about a second opens the context menu. The tasks is killed + * if the touch event terminates early. + * @param {!Event} e Touch start event. + * @param {!Blockly.Block|!Blockly.WorkspaceSvg} uiObject The block or workspace + * under the touchstart event. + * @private + */ +Blockly.longStart_ = function(e, uiObject) { + Blockly.longStop_(); + Blockly.longPid_ = setTimeout(function() { + e.button = 2; // Simulate a right button click. + uiObject.onMouseDown_(e); + }, Blockly.LONGPRESS); +}; + +/** + * Nope, that's not a long-press. Either touchend or touchcancel was fired, + * or a drag hath begun. Kill the queued long-press task. + * @private + */ +Blockly.longStop_ = function() { + if (Blockly.longPid_) { + clearTimeout(Blockly.longPid_); + Blockly.longPid_ = 0; + } +}; + + +/** + * Handle a mouse-up anywhere on the page. + * @param {!Event} e Mouse up event. + * @private + */ +Blockly.onMouseUp_ = function(e) { + var workspace = Blockly.getMainWorkspace(); + if (workspace.dragMode_ == Blockly.DRAG_NONE) { + return; + } + Blockly.Touch.clearTouchIdentifier(); + Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN); + workspace.dragMode_ = Blockly.DRAG_NONE; + // Unbind the touch event if it exists. + if (Blockly.Touch.onTouchUpWrapper_) { + Blockly.unbindEvent_(Blockly.Touch.onTouchUpWrapper_); + Blockly.Touch.onTouchUpWrapper_ = null; + } + if (Blockly.onMouseMoveWrapper_) { + Blockly.unbindEvent_(Blockly.onMouseMoveWrapper_); + Blockly.onMouseMoveWrapper_ = null; + } +}; + +/** + * Handle a mouse-move on SVG drawing surface. + * @param {!Event} e Mouse move event. + * @private + */ +Blockly.onMouseMove_ = function(e) { + var workspace = Blockly.getMainWorkspace(); + if (workspace.dragMode_ != Blockly.DRAG_NONE) { + var dx = e.clientX - workspace.startDragMouseX; + var dy = e.clientY - workspace.startDragMouseY; + var metrics = workspace.startDragMetrics; + var x = workspace.startScrollX + dx; + var y = workspace.startScrollY + dy; + x = Math.min(x, -metrics.contentLeft); + y = Math.min(y, -metrics.contentTop); + x = Math.max(x, metrics.viewWidth - metrics.contentLeft - + metrics.contentWidth); + y = Math.max(y, metrics.viewHeight - metrics.contentTop - + metrics.contentHeight); + + // Move the scrollbars and the page will scroll automatically. + workspace.scrollbar.set(-x - metrics.contentLeft, + -y - metrics.contentTop); + // Cancel the long-press if the drag has moved too far. + if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) { + Blockly.longStop_(); + workspace.dragMode_ = Blockly.DRAG_FREE; + } + e.stopPropagation(); + e.preventDefault(); + } +}; + +/** + * Clear the touch identifier that tracks which touch stream to pay attention + * to. This ends the current drag/gesture and allows other pointers to be + * captured. + */ +Blockly.Touch.clearTouchIdentifier = function() { + console.trace('\tclearing touch identifier'); + if (Blockly.Touch.touchIdentifier_ == null) { + console.log('\t\ttouch identifier was already null'); + } + Blockly.Touch.touchIdentifier_ = null; +}; + +/** + * Decide whether Blockly should handle or ignore this event. + * Mouse and touch events require special checks because we only want to deal + * with one touch stream at a time. All other events should always be handled. + * @param {!Event} e The event to check. + * @return {boolean} True if this event should be passed through to the + * registered handler; false if it should be blocked. + */ +Blockly.Touch.shouldHandleEvent = function(e) { + return !Blockly.Touch.isMouseOrTouchEvent(e) || + Blockly.Touch.checkTouchIdentifier(e); +}; + +/** + * Check whether the touch identifier on the event matches the current saved + * identifier. If there is no identifier, that means it's a mouse event and + * we'll use the identifier "mouse". This means we won't deal well with + * multiple mice being used at the same time. That seems okay. + * If the current identifier was unset, save the identifier from the + * event. This starts a drag/gesture, during which touch events with other + * identifiers will be silently ignored. + * @param {!Event} e Mouse event or touch event. + * @return {boolean} Whether the identifier on the event matches the current + * saved identifier. + */ +Blockly.Touch.checkTouchIdentifier = function(e) { + var identifier; + // TODO (fenichel): Improve splitEventByTouches to get rid of this try/catch. + try { + identifier = (e.changedTouches && e.changedTouches.item(0) && + e.changedTouches.item(0).identifier != undefined && + e.changedTouches.item(0).identifier != null) ? + e.changedTouches.item(0).identifier : 'mouse'; + } catch (ex) { + identifier = (e.changedTouches && e.changedTouches[0] && + e.changedTouches[0].identifier != undefined && + e.changedTouches[0].identifier != null) ? + e.changedTouches[0].identifier : 'mouse'; + } + + // if (Blockly.touchIdentifier_ )is insufficient because android touch + // identifiers may be zero. + if (Blockly.Touch.touchIdentifier_ != undefined && + Blockly.Touch.touchIdentifier_ != null) { + // We're already tracking some touch/mouse event. Is this from the same + // source? + return Blockly.Touch.touchIdentifier_ == identifier; + } + if (e.type == 'mousedown' || e.type == 'touchstart') { + // No identifier set yet, and this is the start of a drag. Set it and + // return. + console.trace('setting touch identfier'); + Blockly.Touch.touchIdentifier_ = identifier; + return true; + } + // There was no identifier yet, but this wasn't a start event so we're going + // to ignore it. This probably means that another drag finished while this + // pointer was down. + return false; +}; + +/** + * Set an event's clientX and clientY from its first changed touch. Use this to + * make a touch event work in a mouse event handler. + * @param {!Event} e A touch event. + */ +Blockly.Touch.setClientFromTouch = function(e) { + if (e.type.indexOf('touch') == 0) { + // Map the touch event's properties to the event. + var touchPoint = e.changedTouches[0]; + e.clientX = touchPoint.clientX; + e.clientY = touchPoint.clientY; + } +}; + +/** + * Check whether a given event is a mouse or touch event. + * @param {!Event} e An event. + * @return {boolean} true if it is a mouse or touch event; false otherwise. + */ +Blockly.Touch.isMouseOrTouchEvent = function(e) { + return e.type.indexOf('touch') == 0 || e.type.indexOf('mouse') == 0; +}; + +/** + * Split an event into an array of events, one per changed touch or mouse + * point. + * @param {!Event} e A mouse event or a touch event with one or more changed + * touches. + * @return {!Array.} An array of mouse or touch events. Each touch + * event will have exactly one changed touch. + */ +Blockly.Touch.splitEventByTouches = function(e) { + var events = []; + if (e.changedTouches) { + for (var i = 0; i < e.changedTouches.length; i++) { + var newEvent = { + type: e.type, + changedTouches: [e.changedTouches[i]], + stopPropagation: function(){ e.stopPropagation(); }, + preventDefault: function(){ e.preventDefault(); } + }; + events.push(newEvent); + } + } else { + events.push(e); + } + return events; +}; diff --git a/core/utils.js b/core/utils.js index 998c3e63b..6886a7d24 100644 --- a/core/utils.js +++ b/core/utils.js @@ -28,6 +28,7 @@ goog.provide('Blockly.utils'); +goog.require('Blockly.Touch'); goog.require('goog.dom'); goog.require('goog.events.BrowserFeature'); goog.require('goog.math.Coordinate'); @@ -105,12 +106,12 @@ Blockly.bindEvent_ = function(node, name, thisObject, func, var captureIdentifier = !opt_noCaptureIdentifier; // Handle each touch point separately. If the event was a mouse event, this // will hand back an array with one element, which we're fine handling. - var events = Blockly.bindEvent_.splitEventByTouches(e); + var events = Blockly.Touch.splitEventByTouches(e); for (var i = 0, event; event = events[i]; i++) { - if (captureIdentifier && !Blockly.shouldHandleEvent(event)) { + if (captureIdentifier && !Blockly.Touch.shouldHandleEvent(event)) { continue; } - Blockly.bindEvent_.setClientFromTouch(event); + Blockly.Touch.setClientFromTouch(event); if (thisObject) { func.call(thisObject, event); } else { @@ -124,7 +125,7 @@ Blockly.bindEvent_ = function(node, name, thisObject, func, var bindData = [[node, name, wrapFunc]]; // Add equivalent touch event. - if (name in Blockly.bindEvent_.TOUCH_MAP) { + if (name in Blockly.Touch.TOUCH_MAP) { var touchWrapFunc = function(e) { wrapFunc(e); // Stop the browser from scrolling/zooming the page. @@ -133,7 +134,7 @@ Blockly.bindEvent_ = function(node, name, thisObject, func, } }; for (var i = 0, eventName; - eventName = Blockly.bindEvent_.TOUCH_MAP[name][i]; i++) { + eventName = Blockly.Touch.TOUCH_MAP[name][i]; i++) { node.addEventListener(eventName, touchWrapFunc, false); bindData.push([node, eventName, touchWrapFunc]); } @@ -141,70 +142,6 @@ Blockly.bindEvent_ = function(node, name, thisObject, func, return bindData; }; -/** - * The TOUCH_MAP lookup dictionary specifies additional touch events to fire, - * in conjunction with mouse events. - * @type {Object} - */ -Blockly.bindEvent_.TOUCH_MAP = {}; -if (goog.events.BrowserFeature.TOUCH_ENABLED) { - Blockly.bindEvent_.TOUCH_MAP = { - 'mousedown': ['touchstart'], - 'mousemove': ['touchmove'], - 'mouseup': ['touchend', 'touchcancel'] - }; -} - -/** - * Set an event's clientX and clientY from its first changed touch. Use this to - * make a touch event work in a mouse event handler. - * @param {!Event} e A touch event. - */ -Blockly.bindEvent_.setClientFromTouch = function(e) { - if (e.type.indexOf('touch') == 0) { - // Map the touch event's properties to the event. - var touchPoint = e.changedTouches[0]; - e.clientX = touchPoint.clientX; - e.clientY = touchPoint.clientY; - } -}; - -/** - * Check whether a given event is a mouse or touch event. - * @param {!Event} e An event. - * @return {boolean} true if it is a mouse or touch event; false otherwise. - */ -Blockly.isMouseOrTouchEvent = function(e) { - return e.type.indexOf('touch') == 0 || e.type.indexOf('mouse') == 0; -}; - -/** - * TODO (rachel-fenichel): consider moving all of this to touch.js - * Split an event into an array of events, one per changed touch or mouse - * point. - * @param {!Event} e A mouse event or a touch event with one or more changed - * touches. - * @return {!Array.} An array of mouse or touch events. Each touch - * event will have exactly one changed touch. - */ -Blockly.bindEvent_.splitEventByTouches = function(e) { - var events = []; - if (e.changedTouches && e.changedTouches.length > 1) { - for (var i = 0; i < e.changedTouches.length; i++) { - var newEvent = { - type: e.type, - changedTouches: [e.changedTouches[i]], - stopPropagation: function(){ e.stopPropagation(); }, - preventDefault: function(){ e.preventDefault(); } - }; - events.push(newEvent); - } - } else { - events.push(e); - } - return events; -}; - /** * Unbind one or more events event from a function call. * @param {!Array.} bindData Opaque data from bindEvent_. This list is diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 36180ac93..bbf6c8b23 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -705,7 +705,7 @@ Blockly.WorkspaceSvg.prototype.isDeleteArea = function(e) { Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { this.markFocused(); if (Blockly.isTargetInput_(e)) { - Blockly.clearTouchIdentifier(); + Blockly.Touch.clearTouchIdentifier(); return; } Blockly.terminateDrag_(); // In case mouse-up event was lost. @@ -720,7 +720,8 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { if (Blockly.isRightButton(e)) { // Right-click. this.showContextMenu_(e); - Blockly.clearTouchIdentifier(); + // Since this was a click, not a drag, end the gesture immediately. + Blockly.Touch.clearTouchIdentifier(); } else if (this.scrollbar) { this.dragMode_ = Blockly.DRAG_BEGIN; // Record the current mouse position. @@ -734,9 +735,9 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { // is turned off and double move events are not performed on a block. // See comment in inject.js Blockly.init_ as to why mouseup events are // bound to the document instead of the SVG's surface. - if ('mouseup' in Blockly.bindEvent_.TOUCH_MAP) { - Blockly.onTouchUpWrapper_ = Blockly.onTouchUpWrapper_ || []; - Blockly.onTouchUpWrapper_ = Blockly.onTouchUpWrapper_.concat( + if ('mouseup' in Blockly.Touch.TOUCH_MAP) { + Blockly.Touch.onTouchUpWrapper_ = Blockly.Touch.onTouchUpWrapper_ || []; + Blockly.Touch.onTouchUpWrapper_ = Blockly.Touch.onTouchUpWrapper_.concat( Blockly.bindEvent_(document, 'mouseup', null, Blockly.onMouseUp_)); } Blockly.onMouseMoveWrapper_ = Blockly.onMouseMoveWrapper_ || []; diff --git a/core/zoom_controls.js b/core/zoom_controls.js index e4c730721..dd9b85f20 100644 --- a/core/zoom_controls.js +++ b/core/zoom_controls.js @@ -165,19 +165,19 @@ Blockly.ZoomControls.prototype.createDom = function() { Blockly.bindEvent_(zoomresetSvg, 'mousedown', null, function(e) { workspace.setScale(1); workspace.scrollCenter(); - Blockly.clearTouchIdentifier(); // Don't block future drags. + Blockly.Touch.clearTouchIdentifier(); // Don't block future drags. e.stopPropagation(); // Don't start a workspace scroll. e.preventDefault(); // Stop double-clicking from selecting text. }); Blockly.bindEvent_(zoominSvg, 'mousedown', null, function(e) { workspace.zoomCenter(1); - Blockly.clearTouchIdentifier(); // Don't block future drags. + Blockly.Touch.clearTouchIdentifier(); // Don't block future drags. e.stopPropagation(); // Don't start a workspace scroll. e.preventDefault(); // Stop double-clicking from selecting text. }); Blockly.bindEvent_(zoomoutSvg, 'mousedown', null, function(e) { workspace.zoomCenter(-1); - Blockly.clearTouchIdentifier(); // Don't block future drags. + Blockly.Touch.clearTouchIdentifier(); // Don't block future drags. e.stopPropagation(); // Don't start a workspace scroll. e.preventDefault(); // Stop double-clicking from selecting text. });