[483] | 1 | define(["dojo", "../util/oo", "dojox/gfx"], |
---|
| 2 | function(dojo, oo, gfx){ |
---|
| 3 | |
---|
| 4 | //dojox.drawing.manager.Canvas = |
---|
| 5 | return oo.declare( |
---|
| 6 | function(/*Object*/options){ |
---|
| 7 | dojo.mixin(this, options); |
---|
| 8 | |
---|
| 9 | var dim = dojo.contentBox(this.srcRefNode); |
---|
| 10 | this.height = this.parentHeight = options.height || dim.h; |
---|
| 11 | this.width = this.parentWidth = options.width || dim.w; |
---|
| 12 | this.domNode = dojo.create("div", {id:"canvasNode"}, this.srcRefNode); |
---|
| 13 | dojo.style(this.domNode, { |
---|
| 14 | width:this.width, |
---|
| 15 | height:"auto" |
---|
| 16 | }); |
---|
| 17 | |
---|
| 18 | dojo.setSelectable(this.domNode, false); |
---|
| 19 | |
---|
| 20 | this.id = this.id || this.util.uid("surface"); |
---|
| 21 | |
---|
| 22 | console.info("create canvas"); |
---|
| 23 | this.gfxSurface = gfx.createSurface(this.domNode, this.width, this.height); |
---|
| 24 | this.gfxSurface.whenLoaded(this, function(){ |
---|
| 25 | setTimeout(dojo.hitch(this, function(){ |
---|
| 26 | this.surfaceReady = true; |
---|
| 27 | if(dojo.isIE){ |
---|
| 28 | //this.gfxSurface.rawNode.parentNode.id = this.id; |
---|
| 29 | }else if(gfx.renderer == "silverlight"){ |
---|
| 30 | this.id = this.domNode.firstChild.id |
---|
| 31 | }else{ |
---|
| 32 | //this.gfxSurface.rawNode.id = this.id; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | this.underlay = this.gfxSurface.createGroup(); |
---|
| 36 | this.surface = this.gfxSurface.createGroup(); |
---|
| 37 | this.overlay = this.gfxSurface.createGroup(); |
---|
| 38 | this.surface.setTransform({dx:0, dy:0,xx:1,yy:1}); |
---|
| 39 | |
---|
| 40 | this.gfxSurface.getDimensions = dojo.hitch(this.gfxSurface, "getDimensions"); |
---|
| 41 | if(options.callback){ |
---|
| 42 | options.callback(this.domNode); |
---|
| 43 | } |
---|
| 44 | }),500); |
---|
| 45 | }); |
---|
| 46 | this._mouseHandle = this.mouse.register(this); |
---|
| 47 | }, |
---|
| 48 | { |
---|
| 49 | // summary: |
---|
| 50 | // Creates a dojox.gfx.surface to be used for Drawing. Note that |
---|
| 51 | // The 'surface' that Drawing uses is actually a dojox.gfx.group. |
---|
| 52 | // This allows for more versatility. |
---|
| 53 | // |
---|
| 54 | // Called internally from a dojox.Drawing. |
---|
| 55 | // |
---|
| 56 | // Note: Surface creation is asynchronous. Connect to |
---|
| 57 | // onSurfaceReady in Drawing. |
---|
| 58 | |
---|
| 59 | // zoom: [readonly] Number |
---|
| 60 | // The amount the canvas is zoomed |
---|
| 61 | zoom:1, |
---|
| 62 | |
---|
| 63 | useScrollbars: true, |
---|
| 64 | baseClass:"drawingCanvas", |
---|
| 65 | |
---|
| 66 | resize: function(width, height){ |
---|
| 67 | // summary: |
---|
| 68 | // Method used to change size of canvas. Potentially |
---|
| 69 | // called from a container like ContentPane. May be |
---|
| 70 | // called directly. |
---|
| 71 | |
---|
| 72 | this.parentWidth = width; |
---|
| 73 | this.parentHeight = height; |
---|
| 74 | this.setDimensions(width, height); |
---|
| 75 | }, |
---|
| 76 | |
---|
| 77 | setDimensions: function(width, height, scrollx, scrolly){ |
---|
| 78 | // summary: |
---|
| 79 | // Internal. Changes canvas size and sets scroll position. |
---|
| 80 | // Do not call this, use resize(). |
---|
| 81 | |
---|
| 82 | // changing the size of the surface and setting scroll |
---|
| 83 | // if items are off screen |
---|
| 84 | var sw = this.getScrollWidth(); //+ 10; |
---|
| 85 | this.width = Math.max(width, this.parentWidth); |
---|
| 86 | this.height = Math.max(height, this.parentHeight); |
---|
| 87 | |
---|
| 88 | if(this.height>this.parentHeight){ |
---|
| 89 | this.width -= sw; |
---|
| 90 | } |
---|
| 91 | if(this.width>this.parentWidth){ |
---|
| 92 | this.height -= sw; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | this.mouse.resize(this.width,this.height); |
---|
| 96 | this.gfxSurface.setDimensions(this.width, this.height); |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | this.domNode.parentNode.scrollTop = scrolly || 0; |
---|
| 100 | this.domNode.parentNode.scrollLeft = scrollx || 0; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | if(this.useScrollbars){ |
---|
| 104 | //console.info("Set Canvas Scroll", (this.height > this.parentHeight), this.height, this.parentHeight) |
---|
| 105 | dojo.style(this.domNode.parentNode, { |
---|
| 106 | overflowY: this.height > this.parentHeight ? "scroll" : "hidden", |
---|
| 107 | overflowX: this.width > this.parentWidth ? "scroll" : "hidden" |
---|
| 108 | }); |
---|
| 109 | }else{ |
---|
| 110 | dojo.style(this.domNode.parentNode, { |
---|
| 111 | overflowY: "hidden", |
---|
| 112 | overflowX: "hidden" |
---|
| 113 | }); |
---|
| 114 | } |
---|
| 115 | }, |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | setZoom: function(zoom){ |
---|
| 119 | // summary: |
---|
| 120 | // Internal. Zooms canvas in and out. |
---|
| 121 | this.zoom = zoom; |
---|
| 122 | this.surface.setTransform({xx:zoom, yy:zoom}); |
---|
| 123 | this.setDimensions(this.width*zoom, this.height*zoom) |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | onScroll: function(){ |
---|
| 127 | // summary: |
---|
| 128 | // Event fires on scroll.NOT IMPLEMENTED |
---|
| 129 | }, |
---|
| 130 | |
---|
| 131 | getScrollOffset: function(){ |
---|
| 132 | // summary: |
---|
| 133 | // Get the scroll position of the canvas |
---|
| 134 | return { |
---|
| 135 | top:this.domNode.parentNode.scrollTop, |
---|
| 136 | left:this.domNode.parentNode.scrollLeft |
---|
| 137 | }; // Object |
---|
| 138 | }, |
---|
| 139 | |
---|
| 140 | getScrollWidth: function(){ |
---|
| 141 | // summary: |
---|
| 142 | // Special method used to detect the width (and height) |
---|
| 143 | // of the browser scrollbars. Becomes memoized. |
---|
| 144 | |
---|
| 145 | var p = dojo.create('div'); |
---|
| 146 | p.innerHTML = '<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:0;left:-1000px;"><div style="height:100px;"></div>'; |
---|
| 147 | var div = p.firstChild; |
---|
| 148 | dojo.body().appendChild(div); |
---|
| 149 | var noscroll = dojo.contentBox(div).h; |
---|
| 150 | dojo.style(div, "overflow", "scroll"); |
---|
| 151 | var scrollWidth = noscroll - dojo.contentBox(div).h; |
---|
| 152 | dojo.destroy(div); |
---|
| 153 | this.getScrollWidth = function(){ |
---|
| 154 | return scrollWidth; |
---|
| 155 | }; |
---|
| 156 | return scrollWidth; // Object |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | ); |
---|
| 160 | |
---|
| 161 | }); |
---|