Debian Codegraph @

dojox/image/LightboxNano.js

raw symbols SCIP
1 define(["dojo/_base/lang",
2 "dojo/_base/declare",
3 "dojo/_base/array",
4 "dojo/_base/fx",
5 "dojo/dom",
6 "dojo/dom-construct",
7 "dojo/dom-geometry",
8 "dojo/dom-style",
9 "dojo/dom-class",
10 "dojo/on",
11 "dojo/query",
12 "dojo/fx"],
13 function(lang, declare, array, baseFx, dom, domConstruct, domGeometry, domStyle, domClass, on, query, fx) {
14
15 var abs = "absolute",
16 vis = "visibility";
17 getViewport = function(){
18 // summary:
19 // Returns the dimensions and scroll position of the viewable area of a browser window
20 var scrollRoot = (document.compatMode == "BackCompat") ? document.body : document.documentElement,
21 scroll = domGeometry.docScroll();
22 return { w: scrollRoot.clientWidth, h: scrollRoot.clientHeight, l: scroll.x, t: scroll.y };
23 };
24
25 return declare("dojox.image.LightboxNano", null, {
26 // summary:
27 // A simple "nano" version of the lightbox.
28 // description:
29 // Very lightweight lightbox which only displays a larger image. There is
30 // no support for a caption or description. The lightbox can be closed by
31 // clicking any where or pressing any key. This widget is intended to be
32 // used on `<a>` and `<img>` tags. Upon creation, if the domNode is `<img>` tag,
33 // then it is wrapped in an `<a>` tag, then a `<div class="enlarge">` is placed
34 // inside the `<a>` and can be styled to display an icon that the original
35 // can be enlarged.
36 // example:
37 // | <a dojoType="dojox.image.LightboxNano" href="/path/to/largeimage.jpg"><img src="/path/to/thumbnail.jpg"></a>
38 // example:
39 // | <img dojoType="dojox.image.LightboxNano" src="/path/to/thumbnail.jpg" href="/path/to/largeimage.jpg">
40
41 // href: string
42 // URL to the large image to show in the lightbox.
43 href: "",
44
45 // duration: int
46 // The delay in milliseconds of the LightboxNano open and close animation.
47 duration: 500,
48
49 // preloadDelay: int
50 // The delay in milliseconds after the LightboxNano is created before preloading the larger image.
51 preloadDelay: 5000,
52
53 constructor: function(/*Object?*/p, /*DomNode?*/n){
54 // summary:
55 // Initializes the DOM node and connect onload event
56 var _this = this, a;
57
58 lang.mixin(_this, p);
59 n = _this._node = dom.byId(n);
60
61 // if we have a origin node, then prepare it to show the LightboxNano
62 if(n){
63 if(!/a/i.test(n.tagName)){
64 a = domConstruct.create("a", {
65 href: _this.href,
66 "class": n.className
67 }, n, "after");
68 n.className = "";
69 a.appendChild(n);
70 n = a;
71 }
72
73 domStyle.set(n, "position", "relative");
74 domConstruct.create("div", {
75 "class": "nano-enlarge",
76 style: {
77 position: "absolute",
78 display: "none"
79 }
80 }, n);
81 dom.setSelectable(n, false);
82
83 _this._onClickEvt = on(n, "click", lang.hitch(_this, "_load"));
84 }
85
86 if(_this.href){
87 setTimeout(function(){
88 (new Image()).src = _this.href;
89 _this._hideLoading();
90 }, _this.preloadDelay);
91 }
92 },
93
94 destroy: function(){
95 // summary:
96 // Destroys the LightboxNano and it's DOM node
97 var a = this._connects || [];
98 a.push(this._onClickEvt);
99 array.forEach(a, function(signal) { signal.remove(); });
100 domConstruct.destroy(this._node);
101 },
102
103 _load: function(/*Event?*/e){
104 // summary:
105 // Creates the large image and begins to show it
106 var _this = this;
107
108 e && e.preventDefault();
109
110 if(!_this._loading){
111 _this._loading = true;
112 _this._reset();
113
114 var i = _this._img = domConstruct.create("img", { "class": "nano-image nano-image-hidden" }, document.body),
115 l, ln = _this._loadingNode,
116 n = query("img", _this._node)[0] || _this._node,
117 a = domGeometry.position(n, true),
118 c = domGeometry.getContentBox(n),
119 b = domGeometry.getBorderExtents(n);
120
121 if(ln == null){
122 _this._loadingNode = ln = domConstruct.create("div", {
123 "class": "nano-loading",
124 style: {
125 position: "absolute",
126 display: ""
127 }
128 }, _this._node, "after");
129 l = domGeometry.getMarginBox(ln);
130 domStyle.set(ln, {
131 left: parseInt((c.w - l.w) / 2) + "px",
132 top: parseInt((c.h - l.h) / 2) + "px"
133 });
134 }
135
136 c.x = a.x - 10 + b.l;
137 c.y = a.y - 10 + b.t;
138 _this._start = c;
139
140 _this._connects = [on(i, "load", lang.hitch(_this, "_show"))];
141
142 i.src = _this.href;
143 }
144 },
145
146 _hideLoading: function(){
147 // summary:
148 // Hides the animated loading indicator
149 if(this._loadingNode){
150 domStyle.set(this._loadingNode, "display", "none");
151 }
152 this._loadingNode = false;
153 },
154
155 _show: function(){
156 // summary:
157 // The image is now loaded, calculate size and display according to viewport size.
158 var _this = this,
159 vp = getViewport(),
160 w = _this._img.width,
161 h = _this._img.height,
162 vpw = parseInt((vp.w - 20) * 0.9),
163 vph = parseInt((vp.h - 20) * 0.9),
164 bg = _this._bg = domConstruct.create("div", {
165 "class": "nano-background",
166 style: { opacity: 0}
167 }, document.body);
168
169 if(_this._loadingNode){
170 _this._hideLoading();
171 }
172 domClass.remove(_this._img, "nano-image-hidden");
173 domStyle.set(_this._node, vis, "hidden");
174
175 _this._loading = false;
176
177 _this._connects = _this._connects.concat([
178 on(document, "mousedown", lang.hitch(_this, "_hide")),
179 on(document, "keypress", lang.hitch(_this, "_key")),
180 on(window, "resize", lang.hitch(_this, "_sizeBg"))
181 ]);
182
183 if(w > vpw){
184 h = h * vpw / w;
185 w = vpw;
186 }
187 if(h > vph){
188 w = w * vph / h;
189 h = vph;
190 }
191
192 _this._end = {
193 x: (vp.w - 20 - w) / 2 + vp.l,
194 y: (vp.h - 20 - h) / 2 + vp.t,
195 w: w,
196 h: h
197 };
198
199 _this._sizeBg();
200
201 fx.combine([
202 _this._anim(_this._img, _this._coords(_this._start, _this._end)),
203 _this._anim(bg, { opacity: 0.5 })
204 ]).play();
205 },
206
207 _sizeBg: function(){
208 // summary:
209 // Resize the background to fill the page
210 var dd = document.documentElement;
211 domStyle.set(this._bg, {
212 top: 0,
213 left: 0,
214 width: dd.scrollWidth + "px",
215 height: dd.scrollHeight + "px"
216 });
217 },
218
219 _key: function(/*Event*/e){
220 // summary:
221 // A key was pressed, so hide the lightbox
222 e.preventDefault();
223 this._hide();
224 },
225
226 _coords: function(/*Object*/s, /*Object*/e){
227 // summary:
228 // Returns animation parameters with the start and end coords
229 return { // Object
230 left: { start: s.x, end: e.x },
231 top: { start: s.y, end: e.y },
232 width: { start: s.w, end: e.w },
233 height: { start: s.h, end: e.h }
234 };
235 },
236
237 _hide: function(){
238 // summary:
239 // Closes the lightbox
240 var _this = this;
241 array.forEach(_this._connects, function(signal) { signal.remove(); });
242 _this._connects = [];
243 fx.combine([
244 _this._anim(_this._img, _this._coords(_this._end, _this._start), "_reset"),
245 _this._anim(_this._bg, {opacity:0})
246 ]).play();
247 },
248
249 _reset: function(){
250 // summary:
251 // Destroys the lightbox
252 domStyle.set(this._node, vis, "visible");
253 domConstruct.destroy(this._img);
254 domConstruct.destroy(this._bg);
255 this._img = this._bg = null;
256 this._node.focus();
257 },
258
259 _anim: function(/*DomNode*/node, /*Object*/args, /*Function*/onEnd){
260 // summary:
261 // Creates the lightbox open/close and background fadein/out animations
262 return baseFx.animateProperty({ // dojo.Animation
263 node: node,
264 duration: this.duration,
265 properties: args,
266 onEnd: onEnd ? lang.hitch(this, onEnd) : null
267 });
268 },
269
270 show: function(/*Object?*/args){
271 // summary:
272 // Shows this LightboxNano programatically. Allows passing a new href and
273 // a programmatic origin.
274 // args: Object?
275 // An object with optional members of `href` and `origin`.
276 // `origin` can be be a String|Id of a DomNode to use when
277 // animating the opening of the image (the 'box' effect starts
278 // from this origin point. eg: { origin: e.target })
279 // If there's no origin, it will use the center of the viewport.
280 // The `href` member is a string URL for the image to be
281 // displayed. Omitting either of these members will revert to
282 // the default href (which could be absent in some cases) and
283 // the original srcNodeRef for the widget.
284 args = args || {};
285 this.href = args.href || this.href;
286
287 var n = dom.byId(args.origin),
288 vp = getViewport();
289
290 // if we don't have a valid origin node, then create one as a reference
291 // that is centered in the viewport
292 this._node = n || domConstruct.create("div", {
293 style: {
294 position: abs,
295 width: 0,
296 hieght: 0,
297 left: (vp.l + (vp.w / 2)) + "px",
298 top: (vp.t + (vp.h / 2)) + "px"
299 }
300 }, document.body)
301 ;
302
303 this._load();
304
305 // if we don't have a valid origin node, then destroy the centered reference
306 // node since load() has already been called and it's not needed anymore.
307 if(!n){
308 domConstruct.destroy(this._node);
309 }
310 }
311 });
312 });