1 | define(["dojo/_base/kernel", "dojo/_base/declare", "dijit/_Widget", "dojo/dom-construct", "dojo/dom-style", "dojo/dom-geometry", "dojo/_base/window", "dojo/_base/lang"], function(kernel, declare, _Widget, construct, style, geometry, window, lang){ |
---|
2 | |
---|
3 | kernel.experimental("dojox.image.MagnifierLite"); |
---|
4 | |
---|
5 | return declare("dojox.image.MagnifierLite", _Widget, { |
---|
6 | // summary: Adds magnification on a portion of an image element |
---|
7 | // |
---|
8 | // description: An unobtrusive way to add an unstyled overlay |
---|
9 | // above the srcNode image element. The overlay/glass is a |
---|
10 | // scaled version of the src image (so larger images sized down |
---|
11 | // are clearer). |
---|
12 | // |
---|
13 | // The logic behind requiring the src image to be large is |
---|
14 | // "it's going to be downloaded, anyway" so this method avoids |
---|
15 | // having to make thumbnails and 2 http requests among other things. |
---|
16 | // |
---|
17 | // glassSize: Int |
---|
18 | // the width and height of the bounding box |
---|
19 | glassSize: 125, |
---|
20 | |
---|
21 | // scale: Decimal |
---|
22 | // the multiplier of the Mangification. |
---|
23 | scale: 6, |
---|
24 | |
---|
25 | postCreate: function(){ |
---|
26 | this.inherited(arguments); |
---|
27 | |
---|
28 | // images are hard to make into workable templates, so just add outer overlay |
---|
29 | // and skip using dijit._Templated |
---|
30 | this._adjustScale(); |
---|
31 | this._createGlass(); |
---|
32 | |
---|
33 | this.connect(this.domNode,"onmouseenter","_showGlass"); |
---|
34 | this.connect(this.glassNode,"onmousemove","_placeGlass"); |
---|
35 | this.connect(this.img,"onmouseout","_hideGlass"); |
---|
36 | |
---|
37 | // when position of domNode changes, _adjustScale needs to run. |
---|
38 | // window.resize isn't it always, FIXME: |
---|
39 | this.connect(window,"onresize","_adjustScale"); |
---|
40 | }, |
---|
41 | |
---|
42 | _createGlass: function(){ |
---|
43 | // summary: make img and glassNode elements as children of the body |
---|
44 | |
---|
45 | var node = this.glassNode = construct.create('div', { |
---|
46 | style: { |
---|
47 | height: this.glassSize + "px", |
---|
48 | width: this.glassSize + "px" |
---|
49 | }, |
---|
50 | className: "glassNode" |
---|
51 | }, window.body()); |
---|
52 | |
---|
53 | this.surfaceNode = node.appendChild(construct.create('div')); |
---|
54 | |
---|
55 | this.img = construct.place(lang.clone(this.domNode), node); |
---|
56 | // float the image around inside the .glassNode |
---|
57 | style.set(this.img, { |
---|
58 | position: "relative", |
---|
59 | top: 0, left: 0, |
---|
60 | width: this._zoomSize.w + "px", |
---|
61 | height: this._zoomSize.h + "px" |
---|
62 | }); |
---|
63 | }, |
---|
64 | |
---|
65 | _adjustScale: function(){ |
---|
66 | // summary: update the calculations should this.scale change |
---|
67 | |
---|
68 | this.offset = geometry.position(this.domNode, true); |
---|
69 | console.dir(this.offset); |
---|
70 | this._imageSize = { w: this.offset.w, h:this.offset.h }; |
---|
71 | this._zoomSize = { |
---|
72 | w: this._imageSize.w * this.scale, |
---|
73 | h: this._imageSize.h * this.scale |
---|
74 | }; |
---|
75 | }, |
---|
76 | |
---|
77 | _showGlass: function(e){ |
---|
78 | // summary: show the overlay |
---|
79 | this._placeGlass(e); |
---|
80 | style.set(this.glassNode, { |
---|
81 | visibility: "visible", |
---|
82 | display:"" |
---|
83 | }); |
---|
84 | }, |
---|
85 | |
---|
86 | _hideGlass: function(e){ |
---|
87 | // summary: hide the overlay |
---|
88 | style.set(this.glassNode, { |
---|
89 | visibility: "hidden", |
---|
90 | display:"none" |
---|
91 | }); |
---|
92 | }, |
---|
93 | |
---|
94 | _placeGlass: function(e){ |
---|
95 | // summary: position the overlay centered under the cursor |
---|
96 | |
---|
97 | this._setImage(e); |
---|
98 | var sub = Math.floor(this.glassSize / 2); |
---|
99 | style.set(this.glassNode,{ |
---|
100 | top: Math.floor(e.pageY - sub) + "px", |
---|
101 | left:Math.floor(e.pageX - sub) + "px" |
---|
102 | }); |
---|
103 | }, |
---|
104 | |
---|
105 | _setImage: function(e){ |
---|
106 | // summary: set the image's offset in the clipping window relative to the mouse position |
---|
107 | |
---|
108 | var xOff = (e.pageX - this.offset.x) / this.offset.w, |
---|
109 | yOff = (e.pageY - this.offset.y) / this.offset.h, |
---|
110 | x = (this._zoomSize.w * xOff * -1) + (this.glassSize * xOff), |
---|
111 | y = (this._zoomSize.h * yOff * -1) + (this.glassSize * yOff); |
---|
112 | |
---|
113 | style.set(this.img, { |
---|
114 | top: y + "px", |
---|
115 | left: x + "px" |
---|
116 | }); |
---|
117 | }, |
---|
118 | |
---|
119 | destroy: function(finalize){ |
---|
120 | construct.destroy(this.glassNode); |
---|
121 | this.inherited(arguments); |
---|
122 | } |
---|
123 | |
---|
124 | }); |
---|
125 | }); |
---|