LightboxNano.js 8.8 KB

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