3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
17 Ext.ux.Lightbox = (function(){
33 this.resizeDuration = this.animate ? ((11 - this.resizeSpeed) * 0.15) : 0;
34 this.overlayDuration = this.animate ? 0.2 : 0;
37 Ext.apply(this, Ext.util.Observable.prototype);
38 Ext.util.Observable.constructor.call(this);
39 this.addEvents('open', 'close');
46 initMarkup: function() {
47 els.shim = Ext.DomHelper.append(document.body, {
49 id: 'ux-lightbox-shim'
51 els.overlay = Ext.DomHelper.append(document.body, {
52 id: 'ux-lightbox-overlay'
55 var lightboxTpl = new Ext.Template(this.getTemplate());
56 els.lightbox = lightboxTpl.append(document.body, {}, true);
59 ['outerImageContainer', 'imageContainer', 'image', 'hoverNav', 'navPrev', 'navNext', 'loading', 'loadingLink',
60 'outerDataContainer', 'dataContainer', 'data', 'details', 'caption', 'imageNumber', 'bottomNav', 'navClose'];
62 Ext.each(ids, function(id){
63 els[id] = Ext.get('ux-lightbox-' + id);
66 Ext.each([els.overlay, els.lightbox, els.shim], function(el){
67 el.setVisibilityMode(Ext.Element.DISPLAY)
71 var size = (this.animate ? 250 : 1) + 'px';
72 els.outerImageContainer.setStyle({
78 getTemplate : function() {
80 '<div id="ux-lightbox">',
81 '<div id="ux-lightbox-outerImageContainer">',
82 '<div id="ux-lightbox-imageContainer">',
83 '<img id="ux-lightbox-image">',
84 '<div id="ux-lightbox-hoverNav">',
85 '<a href="#" id="ux-lightbox-navPrev"></a>',
86 '<a href="#" id="ux-lightbox-navNext"></a>',
88 '<div id="ux-lightbox-loading">',
89 '<a id="ux-lightbox-loadingLink"></a>',
93 '<div id="ux-lightbox-outerDataContainer">',
94 '<div id="ux-lightbox-dataContainer">',
95 '<div id="ux-lightbox-data">',
96 '<div id="ux-lightbox-details">',
97 '<span id="ux-lightbox-caption"></span>',
98 '<span id="ux-lightbox-imageNumber"></span>',
100 '<div id="ux-lightbox-bottomNav">',
101 '<a href="#" id="ux-lightbox-navClose"></a>',
110 initEvents: function() {
111 var close = function(ev) {
116 els.overlay.on('click', close, this);
117 els.loadingLink.on('click', close, this);
118 els.navClose.on('click', close, this);
120 els.lightbox.on('click', function(ev) {
121 if(ev.getTarget().id == 'ux-lightbox') {
126 els.navPrev.on('click', function(ev) {
128 this.setImage(activeImage - 1);
131 els.navNext.on('click', function(ev) {
133 this.setImage(activeImage + 1);
137 register: function(sel, group) {
138 if(selectors.indexOf(sel) === -1) {
141 Ext.fly(document).on('click', function(ev){
142 var target = ev.getTarget(sel);
146 this.open(target, sel, group);
152 open: function(image, sel, group) {
153 group = group || false;
156 duration: this.overlayDuration,
157 endOpacity: this.overlayOpacity,
158 callback: function() {
163 images.push([image.href, image.title]);
166 var setItems = Ext.query(sel);
167 Ext.each(setItems, function(item) {
169 images.push([item.href, item.title]);
173 while (images[index][0] != image.href) {
178 // calculate top and left offset for the lightbox
179 var pageScroll = Ext.fly(document).getScroll();
181 var lightboxTop = pageScroll.top + (Ext.lib.Dom.getViewportHeight() / 10);
182 var lightboxLeft = pageScroll.left;
183 els.lightbox.setStyle({
184 top: lightboxTop + 'px',
185 left: lightboxLeft + 'px'
188 this.setImage(index);
190 this.fireEvent('open', images[index]);
196 setViewSize: function(){
197 var viewSize = this.getViewSize();
198 els.overlay.setStyle({
199 width: viewSize[0] + 'px',
200 height: viewSize[1] + 'px'
203 width: viewSize[0] + 'px',
204 height: viewSize[1] + 'px'
208 setImage: function(index){
211 this.disableKeyNav();
220 els.dataContainer.setOpacity(0.0001);
221 els.imageNumber.hide();
223 var preload = new Image();
224 preload.onload = (function(){
225 els.image.dom.src = images[activeImage][0];
226 this.resizeImage(preload.width, preload.height);
227 }).createDelegate(this);
228 preload.src = images[activeImage][0];
231 resizeImage: function(w, h){
232 var wCur = els.outerImageContainer.getWidth();
233 var hCur = els.outerImageContainer.getHeight();
235 var wNew = (w + this.borderSize * 2);
236 var hNew = (h + this.borderSize * 2);
238 var wDiff = wCur - wNew;
239 var hDiff = hCur - hNew;
241 var afterResize = function(){
242 els.hoverNav.setWidth(els.imageContainer.getWidth() + 'px');
244 els.navPrev.setHeight(h + 'px');
245 els.navNext.setHeight(h + 'px');
247 els.outerDataContainer.setWidth(wNew + 'px');
252 if (hDiff != 0 || wDiff != 0) {
253 els.outerImageContainer.shift({
256 duration: this.resizeDuration,
258 callback: afterResize,
263 afterResize.call(this);
267 showImage: function(){
270 duration: this.resizeDuration,
272 callback: function(){
273 this.updateDetails();
276 this.preloadImages();
279 updateDetails: function(){
280 var detailsWidth = els.data.getWidth(true) - els.navClose.getWidth() - 10;
281 els.details.setWidth((detailsWidth > 0 ? detailsWidth : 0) + 'px');
283 els.caption.update(images[activeImage][1]);
286 if (images.length > 1) {
287 els.imageNumber.update(this.labelImage + ' ' + (activeImage + 1) + ' ' + this.labelOf + ' ' + images.length);
288 els.imageNumber.show();
291 els.dataContainer.fadeIn({
292 duration: this.resizeDuration/2,
294 callback: function() {
295 var viewSize = this.getViewSize();
296 els.overlay.setHeight(viewSize[1] + 'px');
302 updateNav: function(){
307 // if not first image in set, display prev image button
311 // if not last image in set, display next image button
312 if (activeImage < (images.length - 1))
316 enableKeyNav: function() {
317 Ext.fly(document).on('keydown', this.keyNavAction, this);
320 disableKeyNav: function() {
321 Ext.fly(document).un('keydown', this.keyNavAction, this);
324 keyNavAction: function(ev) {
325 var keyCode = ev.getKey();
328 keyCode == 88 || // x
329 keyCode == 67 || // c
334 else if (keyCode == 80 || keyCode == 37){ // display previous image
335 if (activeImage != 0){
336 this.setImage(activeImage - 1);
339 else if (keyCode == 78 || keyCode == 39){ // display next image
340 if (activeImage != (images.length - 1)){
341 this.setImage(activeImage + 1);
346 preloadImages: function(){
348 if (images.length > activeImage + 1) {
350 next.src = images[activeImage + 1][0];
352 if (activeImage > 0) {
354 prev.src = images[activeImage - 1][0];
359 this.disableKeyNav();
361 els.overlay.fadeOut({
362 duration: this.overlayDuration
365 this.fireEvent('close', activeImage);
368 getViewSize: function() {
369 return [Ext.lib.Dom.getViewWidth(), Ext.lib.Dom.getViewHeight()];
374 Ext.onReady(Ext.ux.Lightbox.init, Ext.ux.Lightbox);</pre>