1 /* ***************************************************************************
2 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 * ***************************************************************************
23 * Author: Minkyu Kang <mk7.kang@samsung.com>
31 * data-role: set to 'gallery'
32 * data-index: start index
33 * data-vertical-align: set to top or middle or bottom.
37 * add(file): add the image (parameter: url of iamge)
38 * remove(index): remove the image (parameter: index of image)
39 * refresh(index): refresh the widget, should be called after add or remove. (parameter: start index)
40 * empty: remove all of images from the gallery
41 * length: get length of images
42 * value(index): get or set current index of gallery (parameter: index of image)
50 * <div data-role="gallery" id="gallery" data-index="3" data-vertical-align="middle">
59 * $('#gallery-add').bind('vmouseup', function ( e ) {
60 * $('#gallery').gallery('add', '9.jpg');
61 * $('#gallery').gallery('add', '10.jpg');
62 * $('#gallery').gallery('refresh');
65 * $('#gallery-del').bind('vmouseup', function ( e ) {
66 * $('#gallery').gallery('remove');
73 The gallery widget shows images in a gallery on the screen. <br/><br/> To add an gallery widget to the application, use the following code:
75 <div data-role="gallery" id="gallery" data-vertical-align="middle" data-index="3">
84 @property {Integer} data-index
85 Defines the index number of the first image in the gallery.
86 <br/>The default value is 0.
89 @property {String} data-vertical-align
90 Defines the image alignment. The alignment options are top, middle, and bottom.
91 <br/>The default value is top.
95 The add method is used to add an image to the gallery. The image_file attribute defines the image file URL.
97 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
98 $("#gallery").gallery('add', [image_file]);
102 The remove method is used to delete an image from the gallery. The image_index attribute defines the index of the image to be deleted. If not set removes current image.
104 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
105 $("#gallery").gallery('remove', [image_index]);
109 The refresh method is used to refresh the gallery. This method must be called after adding images to the gallery.
111 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
112 $("#gallery").gallery('refresh');
116 The empty method is used to remove all of images from the gallery.
118 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
119 $("#gallery").gallery('empty');
123 The length method is used to get length of images.
125 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
126 length = $("#gallery").gallery('length');
130 The value method is used to get or set current index of gallery. The image_index attribute defines the index of the image to be set. If not get current index.
132 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
133 value = $("#gallery").gallery('value');
134 $("#gallery").gallery('value', [image_index]);
136 (function ( $, window, undefined ) {
137 $.widget( "tizen.gallery", $.mobile.widget, {
160 _resize: function ( index ) {
161 var img = this.images[index],
162 width = this.images[index].width(),
163 height = this.images[index].height(),
166 img_max_width = this.max_width - margin,
167 img_max_height = this.max_height - margin;
169 ratio = height / width;
171 if ( width > img_max_width ) {
172 img.width( img_max_width );
173 img.height( img_max_width * ratio );
176 height = img.height();
178 if ( height > img_max_height ) {
179 img.height( img_max_height );
180 img.width( img_max_height / ratio );
184 _align: function ( index, obj ) {
185 var img = this.images[index],
195 if ( this.align_type == "middle" ) {
196 img_top = ( this.max_height - img.height() ) / 2;
197 } else if ( this.align_type == "bottom" ) {
198 img_top = this.max_height - img.height();
203 obj.css( 'top', img_top + 'px' );
206 _attach: function ( index, obj ) {
208 processing = function () {
209 self._resize( index );
210 self._align( index, obj );
222 if ( index >= this.images.length ) {
226 obj.css( "display", "block" );
227 obj.append( this.images[index] );
229 if ( this.images[index].height() ) {
232 this.loader[index] = setInterval( function () {
233 if ( !self.images[index].height() ) {
238 clearInterval( self.loader[index] );
243 _detach: function ( index, obj ) {
253 if ( index >= this.images.length ) {
257 obj.css( "display", "none" );
258 this.images[index].removeAttr("style");
259 this.images[index].detach();
261 clearInterval( this.loader[index] );
264 _detach_all: function () {
267 for ( i = 0; i < this.images.length; i++ ) {
268 this.images[i].detach();
272 _drag: function ( _x ) {
276 if ( !this.dragging ) {
280 if ( this.options.flicking === false ) {
281 delta = this.org_x - _x;
284 if ( delta < 0 && !this.prev_img.length ) {
288 if ( delta > 0 && !this.next_img.length ) {
293 coord_x = _x - this.org_x;
295 this.cur_img.css( 'left', coord_x + 'px' );
296 if ( this.next_img.length ) {
297 this.next_img.css( 'left', coord_x + this.window_width + 'px' );
299 if ( this.prev_img.length ) {
300 this.prev_img.css( 'left', coord_x - this.window_width + 'px' );
304 _move: function ( _x ) {
305 var delta = this.org_x - _x,
316 flip = delta < ( this.max_width * 0.45 ) ? 0 : 1;
318 flip = -delta < ( this.max_width * 0.45 ) ? 0 : 1;
322 drag_time = Date.now() - this.org_time;
324 if ( Math.abs( delta ) / drag_time > 1 ) {
330 if ( delta > 0 && this.next_img.length ) {
332 this._detach( this.index - 1, this.prev_img );
334 this.prev_img = this.cur_img;
335 this.cur_img = this.next_img;
336 this.next_img = this.next_img.next();
340 if ( this.next_img.length ) {
341 this.next_img.css( 'left', this.window_width + 'px' );
342 this._attach( this.index + 1, this.next_img );
347 } else if ( delta < 0 && this.prev_img.length ) {
349 this._detach( this.index + 1, this.next_img );
351 this.next_img = this.cur_img;
352 this.cur_img = this.prev_img;
353 this.prev_img = this.prev_img.prev();
357 if ( this.prev_img.length ) {
358 this.prev_img.css( 'left', -this.window_width + 'px' );
359 this._attach( this.index - 1, this.prev_img );
366 sec = this.options.duration;
371 setTimeout( function () {
375 this.cur_img.animate( { left: 0 }, sec );
376 if ( this.next_img.length ) {
377 this.next_img.animate( { left: this.window_width }, sec );
379 if ( this.prev_img.length ) {
380 this.prev_img.animate( { left: -this.window_width }, sec );
384 _add_event: function () {
388 this.container.bind( 'vmousemove', function ( e ) {
394 if ( !self.dragging ) {
398 self._drag( e.pageX );
401 this.container.bind( 'vmousedown', function ( e ) {
408 self.dragging = true;
410 self.org_x = e.pageX;
412 self.org_time = Date.now();
415 this.container.bind( 'vmouseup', function ( e ) {
420 self.dragging = false;
422 self._move( e.pageX );
425 this.container.bind( 'vmouseout', function ( e ) {
429 if ( !self.dragging ) {
433 if ( ( e.pageX < 20 ) ||
434 ( e.pageX > ( self.max_width - 20 ) ) ) {
435 self._move( e.pageX );
436 self.dragging = false;
441 _del_event: function () {
442 this.container.unbind( 'vmousemove' );
443 this.container.unbind( 'vmousedown' );
444 this.container.unbind( 'vmouseup' );
445 this.container.unbind( 'vmouseout' );
450 this.window_width = $( window ).width();
451 this.max_width = this._get_width();
452 this.max_height = this._get_height();
453 this.container.css( 'height', this.max_height );
455 this.cur_img = $( 'div' ).find( '.ui-gallery-bg:eq(' + this.index + ')' );
456 this.prev_img = this.cur_img.prev();
457 this.next_img = this.cur_img.next();
459 this._attach( this.index - 1, this.prev_img );
460 this._attach( this.index, this.cur_img );
461 this._attach( this.index + 1, this.next_img );
463 if ( this.prev_img.length ) {
464 this.prev_img.css( 'left', -this.window_width + 'px' );
467 this.cur_img.css( 'left', '0px' );
469 if ( this.next_img.length ) {
470 this.next_img.css( 'left', this.window_width + 'px' );
480 this._detach( this.index - 1, this.prev_img );
481 this._detach( this.index, this.cur_img );
482 this._detach( this.index + 1, this.next_img );
490 _get_width: function () {
491 return $( this.element ).width();
494 _get_height: function () {
495 var $page = $( this.element ).parentsUntil( 'ui-page' ),
496 $content = $page.children( '.ui-content' ),
497 header_h = $page.children( '.ui-header' ).outerHeight() || 0,
498 footer_h = $page.children( '.ui-footer' ).outerHeight() || 0,
499 padding = parseFloat( $content.css( 'padding-top' ) )
500 + parseFloat( $content.css( 'padding-bottom' ) ),
501 content_h = $( window ).height() - header_h - footer_h - padding;
506 _create: function () {
512 $( this.element ).wrapInner( '<div class="ui-gallery"></div>' );
513 $( this.element ).find( 'img' ).wrap( '<div class="ui-gallery-bg"></div>' );
515 this.container = $( this.element ).find('.ui-gallery');
517 temp_img = $( 'div' ).find( '.ui-gallery-bg:first' );
519 while ( temp_img.length ) {
520 this.images[i] = temp_img.find( 'img' );
521 temp_img = temp_img.next();
527 index = parseInt( $( this.element ).jqmData( 'index' ), 10 );
534 if ( index >= this.images.length ) {
535 index = this.images.length - 1;
540 this.align_type = $( this.element ).jqmData( 'vertical-align' );
542 $( window ).bind( 'resize', function () {
547 _update: function () {
552 while ( this.images_hold.length ) {
553 image_file = this.images_hold.shift();
555 bg_html = $( '<div class="ui-gallery-bg"></div>' );
556 temp_img = $( '<img src="' + image_file + '"></div>' );
558 bg_html.append( temp_img );
559 this.container.append( bg_html );
560 this.images.push( temp_img );
566 refresh: function ( start_index ) {
571 if ( start_index === undefined ) {
572 start_index = this.index;
574 if ( start_index < 0 ) {
577 if ( start_index >= this.images.length ) {
578 start_index = this.images.length - 1;
581 this.index = start_index;
588 add: function ( file ) {
589 this.images_hold.push( file );
592 remove: function ( index ) {
595 if ( index === undefined ) {
599 if ( index < 0 || index >= this.images.length ) {
603 if ( index == this.index ) {
604 temp_img = this.cur_img;
606 if ( this.index == 0 ) {
608 } else if ( this.index == this.images.length - 1 ) {
612 if ( this.direction < 0 ) {
613 this.cur_img = this.prev_img;
614 this.prev_img = this.prev_img.prev();
615 if ( this.prev_img.length ) {
616 this.prev_img.css( 'left', -this.window_width );
617 this._attach( index - 2, this.prev_img );
621 this.cur_img = this.next_img;
622 this.next_img = this.next_img.next();
623 if ( this.next_img.length ) {
624 this.next_img.css( 'left', this.window_width );
625 this._attach( index + 2, this.next_img );
629 this.cur_img.animate( { left: 0 }, this.options.duration );
631 } else if ( index == this.index - 1 ) {
632 temp_img = this.prev_img;
633 this.prev_img = this.prev_img.prev();
634 if ( this.prev_img.length ) {
635 this.prev_img.css( 'left', -this.window_width );
636 this._attach( index - 1, this.prev_img );
640 } else if ( index == this.index + 1 ) {
641 temp_img = this.next_img;
642 this.next_img = this.next_img.next();
643 if ( this.next_img.length ) {
644 this.next_img.css( 'left', this.window_width );
645 this._attach( index + 1, this.next_img );
649 temp_img = $( 'div' ).find( '.ui-gallery-bg:eq(' + index + ')' );
652 this.images.splice( index, 1 );
657 this.images.splice( 0, this.images.length );
658 this.container.find('.ui-gallery-bg').detach();
661 length: function () {
662 return this.images.length;
665 value: function ( index ) {
666 if ( index === undefined ) {
670 this.refresh( index );
672 }); /* End of widget */
674 // auto self-init widgets
675 $( document ).bind( "pagecreate", function ( e ) {
676 $( e.target ).find( ":jqmData(role='gallery')" ).gallery();
679 $( document ).bind( "pageshow", function ( e ) {
680 $( e.target ).find( ":jqmData(role='gallery')" ).gallery( 'show' );
683 $( document ).bind( "pagebeforehide", function ( e ) {
684 $( e.target ).find( ":jqmData(role='gallery')" ).gallery( 'hide' );