LightboxNano.js 8.5 KB

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