Lightbox.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. require({cache:{
  2. 'url:dojox/image/resources/Lightbox.html':"<div class=\"dojoxLightbox\" dojoAttachPoint=\"containerNode\">\n\t<div style=\"position:relative\">\n\t\t<div dojoAttachPoint=\"imageContainer\" class=\"dojoxLightboxContainer\" dojoAttachEvent=\"onclick: _onImageClick\">\n\t\t\t<img dojoAttachPoint=\"imgNode\" src=\"${imgUrl}\" class=\"dojoxLightboxImage\" alt=\"${title}\">\n\t\t\t<div class=\"dojoxLightboxFooter\" dojoAttachPoint=\"titleNode\">\n\t\t\t\t<div class=\"dijitInline LightboxClose\" dojoAttachPoint=\"closeButtonNode\"></div>\n\t\t\t\t<div class=\"dijitInline LightboxNext\" dojoAttachPoint=\"nextButtonNode\"></div>\t\n\t\t\t\t<div class=\"dijitInline LightboxPrev\" dojoAttachPoint=\"prevButtonNode\"></div>\n\t\t\t\t<div class=\"dojoxLightboxText\" dojoAttachPoint=\"titleTextNode\"><span dojoAttachPoint=\"textNode\">${title}</span><span dojoAttachPoint=\"groupCount\" class=\"dojoxLightboxGroupText\"></span></div>\n\t\t\t</div>\n\t\t</div>\n\t</div>\n</div>"}});
  3. define("dojox/image/Lightbox", ["dojo", "dijit", "dojox", "dojo/text!./resources/Lightbox.html", "dijit/Dialog", "dojox/fx/_base"], function(dojo, dijit, dojox, template){
  4. dojo.experimental("dojox.image.Lightbox");
  5. dojo.getObject("image", true, dojox);
  6. dojo.declare("dojox.image.Lightbox", dijit._Widget, {
  7. // summary:
  8. // A dojo-based Lightbox implementation.
  9. //
  10. // description:
  11. // An Elegant, keyboard accessible, markup and store capable Lightbox widget to show images
  12. // in a modal dialog-esque format. Can show individual images as Modal dialog, or can group
  13. // images with multiple entry points, all using a single "master" Dialog for visualization
  14. //
  15. // key controls:
  16. // ESC - close
  17. // Down Arrow / Rt Arrow / N - Next Image
  18. // Up Arrow / Lf Arrow / P - Previous Image
  19. //
  20. // example:
  21. // | <a href="image1.jpg" dojoType="dojox.image.Lightbox">show lightbox</a>
  22. //
  23. // example:
  24. // | <a href="image2.jpg" dojoType="dojox.image.Lightbox" group="one">show group lightbox</a>
  25. // | <a href="image3.jpg" dojoType="dojox.image.Lightbox" group="one">show group lightbox</a>
  26. //
  27. // example:
  28. // | not implemented fully yet, though works with basic datastore access. need to manually call
  29. // | widget._attachedDialog.addImage(item,"fromStore") for each item in a store result set.
  30. // | <div dojoType="dojox.image.Lightbox" group="fromStore" store="storeName"></div>
  31. //
  32. // group: String
  33. // Grouping images in a page with similar tags will provide a 'slideshow' like grouping of images
  34. group: "",
  35. // title: String
  36. // A string of text to be shown in the Lightbox beneath the image (empty if using a store)
  37. title: "",
  38. // href; String
  39. // Link to image to use for this Lightbox node (empty if using a store).
  40. href: "",
  41. // duration: Integer
  42. // Generic time in MS to adjust the feel of widget. could possibly add various
  43. // durations for the various actions (dialog fadein, sizeing, img fadein ...)
  44. duration: 500,
  45. // modal: Boolean
  46. // If true, this Dialog instance will be truly modal and prevent closing until
  47. // explicitly told to by calling hide() or clicking the (x) - Defaults to false
  48. // to preserve previous behaviors. (aka: enable click-to-click on the underlay)
  49. modal: false,
  50. // _allowPassthru: Boolean
  51. // Privately set this to disable/enable natural link of anchor tags
  52. _allowPassthru: false,
  53. // _attachedDialg: dojox.image._LightboxDialog
  54. // The pointer to the global lightbox dialog for this widget
  55. _attachedDialog: null, // try to share a single underlay per page?
  56. startup: function(){
  57. this.inherited(arguments);
  58. // setup an attachment to the masterDialog (or create the masterDialog)
  59. var tmp = dijit.byId('dojoxLightboxDialog');
  60. if(tmp){
  61. this._attachedDialog = tmp;
  62. }else{
  63. // this is the first instance to start, so we make the masterDialog
  64. this._attachedDialog = new dojox.image.LightboxDialog({ id: "dojoxLightboxDialog" });
  65. this._attachedDialog.startup();
  66. }
  67. if(!this.store){
  68. // FIXME: full store support lacking, have to manually call this._attachedDialog.addImage(imgage,group) as it stands
  69. this._addSelf();
  70. this.connect(this.domNode, "onclick", "_handleClick");
  71. }
  72. },
  73. _addSelf: function(){
  74. // summary: Add this instance to the master LightBoxDialog
  75. this._attachedDialog.addImage({
  76. href: this.href,
  77. title: this.title
  78. }, this.group || null);
  79. },
  80. _handleClick: function(/* Event */e){
  81. // summary: Handle the click on the link
  82. if(!this._allowPassthru){ e.preventDefault(); }
  83. else{ return; }
  84. this.show();
  85. },
  86. show: function(){
  87. // summary: Show the Lightbox with this instance as the starting point
  88. this._attachedDialog.show(this);
  89. },
  90. hide: function(){
  91. // summary: Hide the Lightbox currently showing
  92. this._attachedDialog.hide();
  93. },
  94. // FIXME: switch to .attr, deprecate eventually.
  95. disable: function(){
  96. // summary: Disables event clobbering and dialog, and follows natural link
  97. this._allowPassthru = true;
  98. },
  99. enable: function(){
  100. // summary: Enables the dialog (prevents default link)
  101. this._allowPassthru = false;
  102. },
  103. onClick: function(){
  104. // summary:
  105. // Stub fired when the image in the lightbox is clicked.
  106. },
  107. destroy: function(){
  108. this._attachedDialog.removeImage(this);
  109. this.inherited(arguments);
  110. }
  111. });
  112. dojo.declare("dojox.image.LightboxDialog",
  113. dijit.Dialog, {
  114. // summary:
  115. // The "dialog" shared between any Lightbox instances on the page, publically available
  116. // for programatic manipulation.
  117. //
  118. // description:
  119. //
  120. // A widget that intercepts anchor links (typically around images)
  121. // and displays a modal Dialog. this is the actual Dialog, which you can
  122. // create and populate manually, though should use simple Lightbox's
  123. // unless you need the direct access.
  124. //
  125. // There should only be one of these on a page, so all dojox.image.Lightbox's will us it
  126. // (the first instance of a Lightbox to be show()'n will create me If i do not exist)
  127. //
  128. // example:
  129. // | // show a single image from a url
  130. // | var url = "http://dojotoolkit.org/logo.png";
  131. // | var dialog = new dojox.image.LightboxDialog().startup();
  132. // | dialog.show({ href: url, title:"My Remote Image"});
  133. //
  134. // title: String
  135. // The current title, read from object passed to show()
  136. title: "",
  137. // FIXME: implement titleTemplate
  138. // inGroup: Array
  139. // Array of objects. this is populated by from the JSON object _groups, and
  140. // should not be populate manually. it is a placeholder for the currently
  141. // showing group of images in this master dialog
  142. inGroup: null,
  143. // imgUrl: String
  144. // The src="" attribute of our imageNode (can be null at statup)
  145. imgUrl: dijit._Widget.prototype._blankGif,
  146. // errorMessage: String
  147. // The text to display when an unreachable image is linked
  148. errorMessage: "Image not found.",
  149. // adjust: Boolean
  150. // If true, ensure the image always stays within the viewport
  151. // more difficult than necessary to disable, but enabled by default
  152. // seems sane in most use cases.
  153. adjust: true,
  154. // modal: Boolean
  155. // If true, this Dialog instance will be truly modal and prevent closing until
  156. // explicitly told to by calling hide() or clicking the (x) - Defaults to false
  157. // to preserve previous behaviors. (aka: enable click-to-click on the underlay)
  158. modal: false,
  159. /*=====
  160. // _groups: Object
  161. // an object of arrays, each array (of objects) being a unique 'group'
  162. _groups: { XnoGroupX: [] },
  163. =====*/
  164. // errorImg: Url
  165. // Path to the image used when a 404 is encountered
  166. errorImg: dojo.moduleUrl("dojox.image","resources/images/warning.png"),
  167. templateString: template,
  168. constructor: function(args){
  169. this._groups = this._groups || (args && args._groups) || { XnoGroupX:[] };
  170. },
  171. startup: function(){
  172. // summary: Add some extra event handlers, and startup our superclass.
  173. //
  174. // returns: dijit._Widget
  175. // Perhaps the only `dijit._Widget` that returns itself to allow
  176. // 'chaining' or var referencing with .startup()
  177. this.inherited(arguments);
  178. this._animConnects = [];
  179. this.connect(this.nextButtonNode, "onclick", "_nextImage");
  180. this.connect(this.prevButtonNode, "onclick", "_prevImage");
  181. this.connect(this.closeButtonNode, "onclick", "hide");
  182. this._makeAnims();
  183. this._vp = dojo.window.getBox();
  184. return this;
  185. },
  186. show: function(/* Object */groupData){
  187. // summary: Show the Master Dialog. Starts the chain of events to show
  188. // an image in the dialog, including showing the dialog if it is
  189. // not already visible
  190. //
  191. // groupData: Object
  192. // needs href and title attributes. the values for this image.
  193. //
  194. //
  195. var _t = this; // size
  196. this._lastGroup = groupData;
  197. // we only need to call dijit.Dialog.show() if we're not already open.
  198. if(!_t.open){
  199. _t.inherited(arguments);
  200. _t._modalconnects.push(
  201. dojo.connect(dojo.global, "onscroll", this, "_position"),
  202. dojo.connect(dojo.global, "onresize", this, "_position"),
  203. dojo.connect(dojo.body(), "onkeypress", this, "_handleKey")
  204. );
  205. if(!groupData.modal){
  206. _t._modalconnects.push(
  207. dojo.connect(dijit._underlay.domNode, "onclick", this, "onCancel")
  208. );
  209. }
  210. }
  211. if(this._wasStyled){
  212. // ugly fix for IE being stupid. place the new image relative to the old
  213. // image to allow for overriden templates to adjust the location of the
  214. // titlebar. DOM will remain "unchanged" between views.
  215. var tmpImg = dojo.create("img", null, _t.imgNode, "after");
  216. dojo.destroy(_t.imgNode);
  217. _t.imgNode = tmpImg;
  218. _t._makeAnims();
  219. _t._wasStyled = false;
  220. }
  221. dojo.style(_t.imgNode,"opacity","0");
  222. dojo.style(_t.titleNode,"opacity","0");
  223. var src = groupData.href;
  224. if((groupData.group && groupData !== "XnoGroupX") || _t.inGroup){
  225. if(!_t.inGroup){
  226. _t.inGroup = _t._groups[(groupData.group)];
  227. // determine where we were or are in the show
  228. dojo.forEach(_t.inGroup, function(g, i){
  229. if(g.href == groupData.href){
  230. _t._index = i;
  231. //return false;
  232. }
  233. //return true;
  234. });
  235. }
  236. if(!_t._index){
  237. _t._index = 0;
  238. var sr = _t.inGroup[_t._index];
  239. src = (sr && sr.href) || _t.errorImg;
  240. }
  241. // FIXME: implement titleTemplate
  242. _t.groupCount.innerHTML = " (" + (_t._index + 1) + " of " + Math.max(1, _t.inGroup.length) + ")";
  243. _t.prevButtonNode.style.visibility = "visible";
  244. _t.nextButtonNode.style.visibility = "visible";
  245. }else{
  246. // single images don't have buttons, or counters:
  247. _t.groupCount.innerHTML = "";
  248. _t.prevButtonNode.style.visibility = "hidden";
  249. _t.nextButtonNode.style.visibility = "hidden";
  250. }
  251. if(!groupData.leaveTitle){
  252. _t.textNode.innerHTML = groupData.title;
  253. }
  254. _t._ready(src);
  255. },
  256. _ready: function(src){
  257. // summary: A function to trigger all 'real' showing of some src
  258. var _t = this;
  259. // listen for 404's:
  260. _t._imgError = dojo.connect(_t.imgNode, "error", _t, function(){
  261. dojo.disconnect(_t._imgError);
  262. // trigger the above onload with a new src:
  263. _t.imgNode.src = _t.errorImg;
  264. _t.textNode.innerHTML = _t.errorMessage;
  265. });
  266. // connect to the onload of the image
  267. _t._imgConnect = dojo.connect(_t.imgNode, "load", _t, function(e){
  268. _t.resizeTo({
  269. w: _t.imgNode.width,
  270. h: _t.imgNode.height,
  271. duration:_t.duration
  272. });
  273. // cleanup
  274. dojo.disconnect(_t._imgConnect);
  275. if(_t._imgError){
  276. dojo.disconnect(_t._imgError);
  277. }
  278. });
  279. _t.imgNode.src = src;
  280. },
  281. _nextImage: function(){
  282. // summary: Load next image in group
  283. if(!this.inGroup){ return; }
  284. if(this._index + 1 < this.inGroup.length){
  285. this._index++;
  286. }else{
  287. this._index = 0;
  288. }
  289. this._loadImage();
  290. },
  291. _prevImage: function(){
  292. // summary: Load previous image in group
  293. if(this.inGroup){
  294. if(this._index == 0){
  295. this._index = this.inGroup.length - 1;
  296. }else{
  297. this._index--;
  298. }
  299. this._loadImage();
  300. }
  301. },
  302. _loadImage: function(){
  303. // summary: Do the prep work before we can show another image
  304. this._loadingAnim.play(1);
  305. },
  306. _prepNodes: function(){
  307. // summary: A localized hook to accompany _loadImage
  308. this._imageReady = false;
  309. if(this.inGroup && this.inGroup[this._index]){
  310. this.show({
  311. href: this.inGroup[this._index].href,
  312. title: this.inGroup[this._index].title
  313. });
  314. }else{
  315. this.show({
  316. title: this.errorMessage,
  317. href: this.errorImg
  318. });
  319. }
  320. },
  321. _calcTitleSize: function(){
  322. var sizes = dojo.map(dojo.query("> *", this.titleNode).position(), function(s){ return s.h; });
  323. return { h: Math.max.apply(Math, sizes) };
  324. },
  325. resizeTo: function(/* Object */size, forceTitle){
  326. // summary: Resize our dialog container, and fire _showImage
  327. var adjustSize = dojo.boxModel == "border-box" ?
  328. dojo._getBorderExtents(this.domNode).w : 0,
  329. titleSize = forceTitle || this._calcTitleSize()
  330. ;
  331. this._lastTitleSize = titleSize;
  332. if(this.adjust &&
  333. (size.h + titleSize.h + adjustSize + 80 > this._vp.h ||
  334. size.w + adjustSize + 60 > this._vp.w
  335. )
  336. ){
  337. this._lastSize = size;
  338. size = this._scaleToFit(size);
  339. }
  340. this._currentSize = size;
  341. var _sizeAnim = dojox.fx.sizeTo({
  342. node: this.containerNode,
  343. duration: size.duration||this.duration,
  344. width: size.w + adjustSize,
  345. height: size.h + titleSize.h + adjustSize
  346. });
  347. this.connect(_sizeAnim, "onEnd", "_showImage");
  348. _sizeAnim.play(15);
  349. },
  350. _scaleToFit: function(/* Object */size){
  351. // summary: resize an image to fit within the bounds of the viewport
  352. // size: Object
  353. // The 'size' object passed around for this image
  354. var ns = {}, // New size
  355. nvp = {
  356. w: this._vp.w - 80,
  357. h: this._vp.h - 60 - this._lastTitleSize.h
  358. }; // New viewport
  359. // Calculate aspect ratio
  360. var viewportAspect = nvp.w / nvp.h,
  361. imageAspect = size.w / size.h;
  362. // Calculate new image size
  363. if(imageAspect >= viewportAspect){
  364. ns.h = nvp.w / imageAspect;
  365. ns.w = nvp.w;
  366. }else{
  367. ns.w = imageAspect * nvp.h;
  368. ns.h = nvp.h;
  369. }
  370. // we actually have to style this image, it's too big
  371. this._wasStyled = true;
  372. this._setImageSize(ns);
  373. ns.duration = size.duration;
  374. return ns; // Object
  375. },
  376. _setImageSize: function(size){
  377. // summary: Reset the image size to some actual size.
  378. var s = this.imgNode;
  379. s.height = size.h;
  380. s.width = size.w;
  381. },
  382. // clobber inherited function, it is useless.
  383. _size: function(){},
  384. _position: function(/* Event */e){
  385. // summary: we want to know the viewport size any time it changes
  386. this._vp = dojo.window.getBox();
  387. this.inherited(arguments);
  388. // determine if we need to scale up or down, if at all.
  389. if(e && e.type == "resize"){
  390. if(this._wasStyled){
  391. this._setImageSize(this._lastSize);
  392. this.resizeTo(this._lastSize);
  393. }else{
  394. if(this.imgNode.height + 80 > this._vp.h || this.imgNode.width + 60 > this._vp.h){
  395. this.resizeTo({
  396. w: this.imgNode.width, h: this.imgNode.height
  397. });
  398. }
  399. }
  400. }
  401. },
  402. _showImage: function(){
  403. // summary: Fade in the image, and fire showNav
  404. this._showImageAnim.play(1);
  405. },
  406. _showNav: function(){
  407. // summary: Fade in the footer, and setup our connections.
  408. var titleSizeNow = dojo.marginBox(this.titleNode);
  409. if(titleSizeNow.h > this._lastTitleSize.h){
  410. this.resizeTo(this._wasStyled ? this._lastSize : this._currentSize, titleSizeNow);
  411. }else{
  412. this._showNavAnim.play(1);
  413. }
  414. },
  415. hide: function(){
  416. // summary: Hide the Master Lightbox
  417. dojo.fadeOut({
  418. node: this.titleNode,
  419. duration: 200,
  420. // #5112 - if you _don't_ change the .src, safari will
  421. // _never_ fire onload for this image
  422. onEnd: dojo.hitch(this, function(){
  423. this.imgNode.src = this._blankGif;
  424. })
  425. }).play(5);
  426. this.inherited(arguments);
  427. this.inGroup = null;
  428. this._index = null;
  429. },
  430. addImage: function(child, group){
  431. // summary: Add an image to this Master Lightbox
  432. //
  433. // child: Object
  434. // The image information to add.
  435. // href: String - link to image (required)
  436. // title: String - title to display
  437. //
  438. // group: String?
  439. // attach to group of similar tag or null for individual image instance
  440. var g = group;
  441. if(!child.href){ return; }
  442. if(g){
  443. if(!this._groups[g]){
  444. this._groups[g] = [];
  445. }
  446. this._groups[g].push(child);
  447. }else{ this._groups["XnoGroupX"].push(child); }
  448. },
  449. removeImage: function(/* Widget */child){
  450. // summary: Remove an image instance from this LightboxDialog.
  451. // child: Object
  452. // A reference to the Lightbox child that was added (or an object literal)
  453. // only the .href member is compared for uniqueness. The object may contain
  454. // a .group member as well.
  455. var g = child.group || "XnoGroupX";
  456. dojo.every(this._groups[g], function(item, i, ar){
  457. if(item.href == child.href){
  458. ar.splice(i, 1);
  459. return false;
  460. }
  461. return true;
  462. });
  463. },
  464. removeGroup: function(group){
  465. // summary: Remove all images in a passed group
  466. if(this._groups[group]){ this._groups[group] = []; }
  467. },
  468. _handleKey: function(/* Event */e){
  469. // summary: Handle keyboard navigation internally
  470. if(!this.open){ return; }
  471. var dk = dojo.keys;
  472. switch(e.charOrCode){
  473. case dk.ESCAPE:
  474. this.hide();
  475. break;
  476. case dk.DOWN_ARROW:
  477. case dk.RIGHT_ARROW:
  478. case 78: // key "n"
  479. this._nextImage();
  480. break;
  481. case dk.UP_ARROW:
  482. case dk.LEFT_ARROW:
  483. case 80: // key "p"
  484. this._prevImage();
  485. break;
  486. }
  487. },
  488. _makeAnims: function(){
  489. // summary: make and cleanup animation and animation connections
  490. dojo.forEach(this._animConnects, dojo.disconnect);
  491. this._animConnects = [];
  492. this._showImageAnim = dojo.fadeIn({
  493. node: this.imgNode,
  494. duration: this.duration
  495. });
  496. this._animConnects.push(dojo.connect(this._showImageAnim, "onEnd", this, "_showNav"));
  497. this._loadingAnim = dojo.fx.combine([
  498. dojo.fadeOut({ node:this.imgNode, duration:175 }),
  499. dojo.fadeOut({ node:this.titleNode, duration:175 })
  500. ]);
  501. this._animConnects.push(dojo.connect(this._loadingAnim, "onEnd", this, "_prepNodes"));
  502. this._showNavAnim = dojo.fadeIn({ node: this.titleNode, duration:225 });
  503. },
  504. onClick: function(groupData){
  505. // summary: a stub function, called with the currently displayed image as the only argument
  506. },
  507. _onImageClick: function(e){
  508. if(e && e.target == this.imgNode){
  509. this.onClick(this._lastGroup);
  510. // also fire the onclick for the Lightbox widget which triggered, if you
  511. // aren't working directly with the LBDialog
  512. if(this._lastGroup.declaredClass){
  513. this._lastGroup.onClick(this._lastGroup);
  514. }
  515. }
  516. }
  517. });
  518. return dojox.image.Lightbox;
  519. });