| 1 |
define(["require", "dojo", "dijit", "dojox", "dojo/text!./resources/Lightbox.html", "dijit/Dialog", "dojox/fx/_base"], function(require, dojo, dijit, dojox, template){ |
| 2 |
|
| 3 |
dojo.experimental("dojox.image.Lightbox"); |
| 4 |
dojo.getObject("image", true, dojox); |
| 5 |
|
| 6 |
var Lightbox = dojo.declare("dojox.image.Lightbox", dijit._Widget, { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
group: "", |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
title: "", |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
href: "", |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
duration: 500, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
modal: false, |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
_allowPassthru: false, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
_attachedDialog: null, |
| 59 |
|
| 60 |
startup: function(){ |
| 61 |
this.inherited(arguments); |
| 62 |
|
| 63 |
var tmp = dijit.byId('dojoxLightboxDialog'); |
| 64 |
if(tmp){ |
| 65 |
this._attachedDialog = tmp; |
| 66 |
}else{ |
| 67 |
|
| 68 |
this._attachedDialog = new dojox.image.LightboxDialog({ id: "dojoxLightboxDialog" }); |
| 69 |
this._attachedDialog.startup(); |
| 70 |
} |
| 71 |
if(!this.store){ |
| 72 |
|
| 73 |
this._addSelf(); |
| 74 |
this.connect(this.domNode, "onclick", "_handleClick"); |
| 75 |
} |
| 76 |
|
| 77 |
}, |
| 78 |
|
| 79 |
_addSelf: function(){ |
| 80 |
|
| 81 |
|
| 82 |
this._attachedDialog.addImage({ |
| 83 |
href: this.href, |
| 84 |
title: this.title |
| 85 |
}, this.group || null); |
| 86 |
}, |
| 87 |
|
| 88 |
_handleClick: function(e){ |
| 89 |
|
| 90 |
|
| 91 |
if(!this._allowPassthru){ e.preventDefault(); } |
| 92 |
else{ return; } |
| 93 |
this.show(); |
| 94 |
}, |
| 95 |
|
| 96 |
show: function(){ |
| 97 |
|
| 98 |
|
| 99 |
this._attachedDialog.show(this); |
| 100 |
}, |
| 101 |
|
| 102 |
hide: function(){ |
| 103 |
|
| 104 |
|
| 105 |
this._attachedDialog.hide(); |
| 106 |
}, |
| 107 |
|
| 108 |
|
| 109 |
disable: function(){ |
| 110 |
|
| 111 |
|
| 112 |
this._allowPassthru = true; |
| 113 |
}, |
| 114 |
|
| 115 |
enable: function(){ |
| 116 |
|
| 117 |
|
| 118 |
this._allowPassthru = false; |
| 119 |
}, |
| 120 |
|
| 121 |
onClick: function(){ |
| 122 |
|
| 123 |
|
| 124 |
}, |
| 125 |
|
| 126 |
destroy: function(){ |
| 127 |
this._attachedDialog.removeImage(this); |
| 128 |
this.inherited(arguments); |
| 129 |
} |
| 130 |
|
| 131 |
}); |
| 132 |
|
| 133 |
Lightbox.LightboxDialog = dojo.declare("dojox.image.LightboxDialog", |
| 134 |
dijit.Dialog, { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
title: "", |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
inGroup: null, |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
imgUrl: dijit._Widget.prototype._blankGif, |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
errorMessage: "Image not found.", |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
adjust: true, |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
modal: false, |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
imageClass: "dojoxLightboxImage", |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
errorImg: require.toUrl("./resources/images/warning.png"), |
| 198 |
|
| 199 |
templateString: template, |
| 200 |
|
| 201 |
constructor: function(args){ |
| 202 |
this._groups = this._groups || (args && args._groups) || { XnoGroupX:[] }; |
| 203 |
}, |
| 204 |
|
| 205 |
startup: function(){ |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
this.inherited(arguments); |
| 213 |
|
| 214 |
this._animConnects = []; |
| 215 |
this.connect(this.nextButtonNode, "onclick", "_nextImage"); |
| 216 |
this.connect(this.prevButtonNode, "onclick", "_prevImage"); |
| 217 |
this.connect(this.closeButtonNode, "onclick", "hide"); |
| 218 |
this._makeAnims(); |
| 219 |
this._vp = dojo.window.getBox(); |
| 220 |
return this; |
| 221 |
}, |
| 222 |
|
| 223 |
show: function(groupData){ |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
var _t = this; |
| 232 |
this._lastGroup = groupData; |
| 233 |
|
| 234 |
|
| 235 |
if(!_t.open){ |
| 236 |
_t.inherited(arguments); |
| 237 |
_t._modalconnects.push( |
| 238 |
dojo.connect(dojo.global, "onscroll", this, "_position"), |
| 239 |
dojo.connect(dojo.global, "onresize", this, "_position"), |
| 240 |
dojo.connect(dojo.body(), "onkeypress", this, "_handleKey") |
| 241 |
); |
| 242 |
if(!groupData.modal){ |
| 243 |
_t._modalconnects.push( |
| 244 |
dojo.connect(dijit._underlay.domNode, "onclick", this, "onCancel") |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if(this._wasStyled){ |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
var tmpImg = dojo.create("img", { |
| 254 |
className: _t.imageClass |
| 255 |
}, _t.imgNode, "after"); |
| 256 |
dojo.destroy(_t.imgNode); |
| 257 |
_t.imgNode = tmpImg; |
| 258 |
_t._makeAnims(); |
| 259 |
_t._wasStyled = false; |
| 260 |
} |
| 261 |
|
| 262 |
dojo.style(_t.imgNode,"opacity","0"); |
| 263 |
dojo.style(_t.titleNode,"opacity","0"); |
| 264 |
|
| 265 |
var src = groupData.href; |
| 266 |
|
| 267 |
if((groupData.group && groupData !== "XnoGroupX") || _t.inGroup){ |
| 268 |
if(!_t.inGroup){ |
| 269 |
_t.inGroup = _t._groups[(groupData.group)]; |
| 270 |
|
| 271 |
dojo.forEach(_t.inGroup, function(g, i){ |
| 272 |
if(g.href == groupData.href){ |
| 273 |
_t._index = i; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
}); |
| 278 |
} |
| 279 |
if(!_t._index){ |
| 280 |
_t._index = 0; |
| 281 |
var sr = _t.inGroup[_t._index]; |
| 282 |
src = (sr && sr.href) || _t.errorImg; |
| 283 |
} |
| 284 |
|
| 285 |
_t.groupCount.innerHTML = " (" + (_t._index + 1) + " of " + Math.max(1, _t.inGroup.length) + ")"; |
| 286 |
_t.prevButtonNode.style.visibility = "visible"; |
| 287 |
_t.nextButtonNode.style.visibility = "visible"; |
| 288 |
}else{ |
| 289 |
|
| 290 |
_t.groupCount.innerHTML = ""; |
| 291 |
_t.prevButtonNode.style.visibility = "hidden"; |
| 292 |
_t.nextButtonNode.style.visibility = "hidden"; |
| 293 |
} |
| 294 |
if(!groupData.leaveTitle){ |
| 295 |
_t.textNode.innerHTML = groupData.title; |
| 296 |
} |
| 297 |
_t._ready(src); |
| 298 |
}, |
| 299 |
|
| 300 |
_ready: function(src){ |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
var _t = this; |
| 305 |
|
| 306 |
|
| 307 |
_t._imgError = dojo.connect(_t.imgNode, "error", _t, function(){ |
| 308 |
dojo.disconnect(_t._imgError); |
| 309 |
|
| 310 |
_t.imgNode.src = _t.errorImg; |
| 311 |
_t.textNode.innerHTML = _t.errorMessage; |
| 312 |
}); |
| 313 |
|
| 314 |
|
| 315 |
_t._imgConnect = dojo.connect(_t.imgNode, "load", _t, function(e){ |
| 316 |
_t.resizeTo({ |
| 317 |
w: _t.imgNode.width, |
| 318 |
h: _t.imgNode.height, |
| 319 |
duration:_t.duration |
| 320 |
}); |
| 321 |
|
| 322 |
dojo.disconnect(_t._imgConnect); |
| 323 |
if(_t._imgError){ |
| 324 |
dojo.disconnect(_t._imgError); |
| 325 |
} |
| 326 |
}); |
| 327 |
|
| 328 |
_t.imgNode.src = src; |
| 329 |
}, |
| 330 |
|
| 331 |
_nextImage: function(){ |
| 332 |
|
| 333 |
|
| 334 |
if(!this.inGroup){ return; } |
| 335 |
if(this._index + 1 < this.inGroup.length){ |
| 336 |
this._index++; |
| 337 |
}else{ |
| 338 |
this._index = 0; |
| 339 |
} |
| 340 |
this._loadImage(); |
| 341 |
}, |
| 342 |
|
| 343 |
_prevImage: function(){ |
| 344 |
|
| 345 |
|
| 346 |
if(this.inGroup){ |
| 347 |
if(this._index == 0){ |
| 348 |
this._index = this.inGroup.length - 1; |
| 349 |
}else{ |
| 350 |
this._index--; |
| 351 |
} |
| 352 |
this._loadImage(); |
| 353 |
} |
| 354 |
}, |
| 355 |
|
| 356 |
_loadImage: function(){ |
| 357 |
|
| 358 |
|
| 359 |
this._loadingAnim.play(1); |
| 360 |
}, |
| 361 |
|
| 362 |
_prepNodes: function(){ |
| 363 |
|
| 364 |
|
| 365 |
this._imageReady = false; |
| 366 |
if(this.inGroup && this.inGroup[this._index]){ |
| 367 |
this.show({ |
| 368 |
href: this.inGroup[this._index].href, |
| 369 |
title: this.inGroup[this._index].title |
| 370 |
}); |
| 371 |
}else{ |
| 372 |
this.show({ |
| 373 |
title: this.errorMessage, |
| 374 |
href: this.errorImg |
| 375 |
}); |
| 376 |
} |
| 377 |
|
| 378 |
}, |
| 379 |
|
| 380 |
_calcTitleSize: function(){ |
| 381 |
var sizes = dojo.map(dojo.query("> *", this.titleNode).position(), function(s){ return s.h; }); |
| 382 |
return { h: Math.max.apply(Math, sizes) }; |
| 383 |
}, |
| 384 |
|
| 385 |
resizeTo: function(size, forceTitle){ |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
var adjustSize = dojo.boxModel == "border-box" ? |
| 390 |
dojo._getBorderExtents(this.domNode).w : 0, |
| 391 |
titleSize = forceTitle || this._calcTitleSize() |
| 392 |
; |
| 393 |
|
| 394 |
this._lastTitleSize = titleSize; |
| 395 |
|
| 396 |
if(this.adjust && |
| 397 |
(size.h + titleSize.h + adjustSize + 80 > this._vp.h || |
| 398 |
size.w + adjustSize + 60 > this._vp.w |
| 399 |
) |
| 400 |
){ |
| 401 |
this._lastSize = size; |
| 402 |
size = this._scaleToFit(size); |
| 403 |
} |
| 404 |
this._currentSize = size; |
| 405 |
|
| 406 |
var _sizeAnim = dojox.fx.sizeTo({ |
| 407 |
node: this.containerNode, |
| 408 |
duration: size.duration||this.duration, |
| 409 |
width: size.w + adjustSize, |
| 410 |
height: size.h + titleSize.h + adjustSize |
| 411 |
}); |
| 412 |
this.connect(_sizeAnim, "onEnd", "_showImage"); |
| 413 |
_sizeAnim.play(15); |
| 414 |
}, |
| 415 |
|
| 416 |
_scaleToFit: function(size){ |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
var ns = {}, |
| 423 |
nvp = { |
| 424 |
w: this._vp.w - 80, |
| 425 |
h: this._vp.h - 60 - this._lastTitleSize.h |
| 426 |
}; |
| 427 |
|
| 428 |
|
| 429 |
var viewportAspect = nvp.w / nvp.h, |
| 430 |
imageAspect = size.w / size.h; |
| 431 |
|
| 432 |
|
| 433 |
if(imageAspect >= viewportAspect){ |
| 434 |
ns.h = nvp.w / imageAspect; |
| 435 |
ns.w = nvp.w; |
| 436 |
}else{ |
| 437 |
ns.w = imageAspect * nvp.h; |
| 438 |
ns.h = nvp.h; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
this._wasStyled = true; |
| 443 |
this._setImageSize(ns); |
| 444 |
|
| 445 |
ns.duration = size.duration; |
| 446 |
return ns; |
| 447 |
}, |
| 448 |
|
| 449 |
_setImageSize: function(size){ |
| 450 |
|
| 451 |
|
| 452 |
var s = this.imgNode; |
| 453 |
s.height = size.h; |
| 454 |
s.width = size.w; |
| 455 |
}, |
| 456 |
|
| 457 |
|
| 458 |
_size: function(){}, |
| 459 |
|
| 460 |
_position: function(e){ |
| 461 |
|
| 462 |
|
| 463 |
this._vp = dojo.window.getBox(); |
| 464 |
this.inherited(arguments); |
| 465 |
|
| 466 |
|
| 467 |
if(e && e.type == "resize"){ |
| 468 |
if(this._wasStyled){ |
| 469 |
this._setImageSize(this._lastSize); |
| 470 |
this.resizeTo(this._lastSize); |
| 471 |
}else{ |
| 472 |
if(this.imgNode.height + 80 > this._vp.h || this.imgNode.width + 60 > this._vp.h){ |
| 473 |
this.resizeTo({ |
| 474 |
w: this.imgNode.width, h: this.imgNode.height |
| 475 |
}); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
}, |
| 480 |
|
| 481 |
_showImage: function(){ |
| 482 |
|
| 483 |
|
| 484 |
this._showImageAnim.play(1); |
| 485 |
}, |
| 486 |
|
| 487 |
_showNav: function(){ |
| 488 |
|
| 489 |
|
| 490 |
var titleSizeNow = dojo.marginBox(this.titleNode); |
| 491 |
if(titleSizeNow.h > this._lastTitleSize.h){ |
| 492 |
this.resizeTo(this._wasStyled ? this._lastSize : this._currentSize, titleSizeNow); |
| 493 |
}else{ |
| 494 |
this._showNavAnim.play(1); |
| 495 |
} |
| 496 |
}, |
| 497 |
|
| 498 |
hide: function(){ |
| 499 |
|
| 500 |
|
| 501 |
dojo.fadeOut({ |
| 502 |
node: this.titleNode, |
| 503 |
duration: 200, |
| 504 |
|
| 505 |
|
| 506 |
onEnd: dojo.hitch(this, function(){ |
| 507 |
this.imgNode.src = this._blankGif; |
| 508 |
}) |
| 509 |
}).play(5); |
| 510 |
|
| 511 |
this.inherited(arguments); |
| 512 |
|
| 513 |
this.inGroup = null; |
| 514 |
this._index = null; |
| 515 |
}, |
| 516 |
|
| 517 |
addImage: function(child, group){ |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
var g = group; |
| 529 |
if(!child.href){ return; } |
| 530 |
if(g){ |
| 531 |
if(!this._groups[g]){ |
| 532 |
this._groups[g] = []; |
| 533 |
} |
| 534 |
this._groups[g].push(child); |
| 535 |
}else{ this._groups["XnoGroupX"].push(child); } |
| 536 |
}, |
| 537 |
|
| 538 |
removeImage: function(child){ |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
var g = child.group || "XnoGroupX"; |
| 547 |
dojo.every(this._groups[g], function(item, i, ar){ |
| 548 |
if(item.href == child.href){ |
| 549 |
ar.splice(i, 1); |
| 550 |
return false; |
| 551 |
} |
| 552 |
return true; |
| 553 |
}); |
| 554 |
}, |
| 555 |
|
| 556 |
removeGroup: function(group){ |
| 557 |
|
| 558 |
|
| 559 |
if(this._groups[group]){ this._groups[group] = []; } |
| 560 |
}, |
| 561 |
|
| 562 |
_handleKey: function(e){ |
| 563 |
|
| 564 |
|
| 565 |
if(!this.open){ return; } |
| 566 |
|
| 567 |
var dk = dojo.keys; |
| 568 |
switch(e.charOrCode){ |
| 569 |
|
| 570 |
case dk.ESCAPE: |
| 571 |
this.hide(); |
| 572 |
break; |
| 573 |
|
| 574 |
case dk.DOWN_ARROW: |
| 575 |
case dk.RIGHT_ARROW: |
| 576 |
case 78: |
| 577 |
this._nextImage(); |
| 578 |
break; |
| 579 |
|
| 580 |
case dk.UP_ARROW: |
| 581 |
case dk.LEFT_ARROW: |
| 582 |
case 80: |
| 583 |
this._prevImage(); |
| 584 |
break; |
| 585 |
} |
| 586 |
}, |
| 587 |
|
| 588 |
_makeAnims: function(){ |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
dojo.forEach(this._animConnects, dojo.disconnect); |
| 593 |
this._animConnects = []; |
| 594 |
this._showImageAnim = dojo.fadeIn({ |
| 595 |
node: this.imgNode, |
| 596 |
duration: this.duration |
| 597 |
}); |
| 598 |
this._animConnects.push(dojo.connect(this._showImageAnim, "onEnd", this, "_showNav")); |
| 599 |
this._loadingAnim = dojo.fx.combine([ |
| 600 |
dojo.fadeOut({ node:this.imgNode, duration:175 }), |
| 601 |
dojo.fadeOut({ node:this.titleNode, duration:175 }) |
| 602 |
]); |
| 603 |
this._animConnects.push(dojo.connect(this._loadingAnim, "onEnd", this, "_prepNodes")); |
| 604 |
this._showNavAnim = dojo.fadeIn({ node: this.titleNode, duration:225 }); |
| 605 |
}, |
| 606 |
|
| 607 |
onClick: function(groupData){ |
| 608 |
|
| 609 |
|
| 610 |
}, |
| 611 |
|
| 612 |
_onImageClick: function(e){ |
| 613 |
if(e && e.target == this.imgNode){ |
| 614 |
this.onClick(this._lastGroup); |
| 615 |
|
| 616 |
|
| 617 |
if(this._lastGroup.declaredClass){ |
| 618 |
this._lastGroup.onClick(this._lastGroup); |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
}); |
| 623 |
|
| 624 |
return Lightbox; |
| 625 |
}); |
| 626 |
|