| 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 |
|
| 19 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
href: "", |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
duration: 500, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
preloadDelay: 5000, |
| 52 |
|
| 53 |
constructor: function(p, n){ |
| 54 |
|
| 55 |
|
| 56 |
var _this = this, a; |
| 57 |
|
| 58 |
lang.mixin(_this, p); |
| 59 |
n = _this._node = dom.byId(n); |
| 60 |
|
| 61 |
|
| 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 |
|
| 96 |
|
| 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(e){ |
| 104 |
|
| 105 |
|
| 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 |
|
| 148 |
|
| 149 |
if(this._loadingNode){ |
| 150 |
domStyle.set(this._loadingNode, "display", "none"); |
| 151 |
} |
| 152 |
this._loadingNode = false; |
| 153 |
}, |
| 154 |
|
| 155 |
_show: function(){ |
| 156 |
|
| 157 |
|
| 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 |
|
| 209 |
|
| 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(e){ |
| 220 |
|
| 221 |
|
| 222 |
e.preventDefault(); |
| 223 |
this._hide(); |
| 224 |
}, |
| 225 |
|
| 226 |
_coords: function(s, e){ |
| 227 |
|
| 228 |
|
| 229 |
return { |
| 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 |
|
| 239 |
|
| 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 |
|
| 251 |
|
| 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(node, args, onEnd){ |
| 260 |
|
| 261 |
|
| 262 |
return baseFx.animateProperty({ |
| 263 |
node: node, |
| 264 |
duration: this.duration, |
| 265 |
properties: args, |
| 266 |
onEnd: onEnd ? lang.hitch(this, onEnd) : null |
| 267 |
}); |
| 268 |
}, |
| 269 |
|
| 270 |
show: function(args){ |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
args = args || {}; |
| 285 |
this.href = args.href || this.href; |
| 286 |
|
| 287 |
var n = dom.byId(args.origin), |
| 288 |
vp = getViewport(); |
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 306 |
|
| 307 |
if(!n){ |
| 308 |
domConstruct.destroy(this._node); |
| 309 |
} |
| 310 |
} |
| 311 |
}); |
| 312 |
}); |