2 /* ***************************************************************************
3 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 * ***************************************************************************
24 * Author: Minkyu Kang <mk7.kang@samsung.com>
32 * data-role: set to 'gallery'
33 * data-index: start index
34 * data-vertical-align: set to top or middle or bottom.
38 * add(file): add the image (parameter: url of iamge)
39 * remove(index): remove the image (parameter: index of image)
40 * refresh(index): refresh the widget, should be called after add or remove. (parameter: start index)
41 * empty: remove all of images from the gallery
42 * length: get length of images
43 * value(index): get or set current index of gallery (parameter: index of image)
51 * <div data-role="gallery" id="gallery" data-index="3" data-vertical-align="middle">
60 * $('#gallery-add').bind('vmouseup', function ( e ) {
61 * $('#gallery').gallery('add', '9.jpg');
62 * $('#gallery').gallery('add', '10.jpg');
63 * $('#gallery').gallery('refresh');
66 * $('#gallery-del').bind('vmouseup', function ( e ) {
67 * $('#gallery').gallery('remove');
74 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:
76 <div data-role="gallery" id="gallery" data-vertical-align="middle" data-index="3">
85 @property {Integer} data-index
86 Defines the index number of the first image in the gallery.
87 <br/>The default value is 0.
90 @property {String} data-vertical-align
91 Defines the image alignment. The alignment options are top, middle, and bottom.
92 <br/>The default value is top.
96 The add method is used to add an image to the gallery. The image_file attribute defines the image file URL.
98 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
99 $("#gallery").gallery('add', [image_file]);
103 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.
105 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
106 $("#gallery").gallery('remove', [image_index]);
110 The refresh method is used to refresh the gallery. This method must be called after adding images to the gallery.
112 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
113 $("#gallery").gallery('refresh');
117 The empty method is used to remove all of images from the gallery.
119 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
120 $("#gallery").gallery('empty');
124 The length method is used to get length of images.
126 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
127 length = $("#gallery").gallery('length');
131 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.
133 <div id="gallery" data-role="gallery" data-vertical-align="middle"></div>
134 value = $("#gallery").gallery('value');
135 $("#gallery").gallery('value', [image_index]);
137 (function ( $, window, undefined ) {
138 $.widget( "tizen.gallery", $.mobile.widget, {
159 orientationEventFire: false,
161 _resize: function ( index ) {
162 var img = this.images[index],
163 width = this.images[index].width(),
164 height = this.images[index].height(),
167 img_max_width = this.max_width - margin,
168 img_max_height = this.max_height - margin;
170 ratio = height / width;
172 if ( width > img_max_width ) {
173 img.width( img_max_width );
174 img.height( img_max_width * ratio );
177 height = img.height();
179 if ( height > img_max_height ) {
180 img.height( img_max_height );
181 img.width( img_max_height / ratio );
185 _align: function ( index, obj ) {
186 var img = this.images[index],
196 if ( this.align_type == "middle" ) {
197 img_top = ( this.max_height - img.height() ) / 2;
198 } else if ( this.align_type == "bottom" ) {
199 img_top = this.max_height - img.height();
204 obj.css( 'top', img_top + 'px' );
207 _attach: function ( index, obj ) {
209 processing = function () {
210 self._resize( index );
211 self._align( index, obj );
214 loading = function () {
215 if ( self.images[index] === undefined ) {
219 if ( !self.images[index].height() ) {
220 setTimeout( loading, 10 );
236 if ( !this.images.length ) {
239 if ( index >= this.images.length ) {
243 obj.css( "display", "block" );
244 obj.css( "visibility", "hidden" );
245 obj.append( this.images[index] );
249 _detach: function ( index, obj ) {
259 if ( index >= this.images.length ) {
263 obj.css( "display", "none" );
264 this.images[index].removeAttr("style");
265 this.images[index].detach();
268 _detach_all: function () {
271 for ( i = 0; i < this.images.length; i++ ) {
272 this.images[i].detach();
276 _drag: function ( _x ) {
280 if ( !this.dragging ) {
284 if ( this.options.flicking === false ) {
285 delta = this.org_x - _x;
288 if ( delta < 0 && !this.prev_img.length ) {
292 if ( delta > 0 && !this.next_img.length ) {
297 coord_x = _x - this.org_x;
299 this._moveLeft( this.cur_img , coord_x + 'px' );
300 if ( this.next_img.length ) {
301 this._moveLeft( this.next_img , coord_x + this.window_width + 'px' );
303 if ( this.prev_img.length ) {
304 this._moveLeft( this.prev_img , coord_x - this.window_width + 'px' );
308 _move: function ( _x ) {
309 var delta = this.org_x - _x,
320 flip = delta < ( this.max_width * 0.45 ) ? 0 : 1;
322 flip = -delta < ( this.max_width * 0.45 ) ? 0 : 1;
326 drag_time = Date.now() - this.org_time;
328 if ( Math.abs( delta ) / drag_time > 1 ) {
334 if ( delta > 0 && this.next_img.length ) {
336 this._detach( this.index - 1, this.prev_img );
338 this.prev_img = this.cur_img;
339 this.cur_img = this.next_img;
340 this.next_img = this.next_img.next();
344 if ( this.next_img.length ) {
345 this._moveLeft( this.next_img , this.window_width + 'px' );
346 this._attach( this.index + 1, this.next_img );
351 } else if ( delta < 0 && this.prev_img.length ) {
353 this._detach( this.index + 1, this.next_img );
355 this.next_img = this.cur_img;
356 this.cur_img = this.prev_img;
357 this.prev_img = this.prev_img.prev();
361 if ( this.prev_img.length ) {
362 this._moveLeft( this.prev_img , -this.window_width + 'px' );
363 this._attach( this.index - 1, this.prev_img );
370 sec = this.options.duration;
375 setTimeout( function () {
379 this._moveLeft( this.cur_img, 0 + 'px', sec );
380 if ( this.next_img.length ) {
381 this._moveLeft( this.next_img, this.window_width + 'px', sec );
383 if ( this.prev_img.length ) {
384 this._moveLeft( this.prev_img, -this.window_width + 'px', sec );
388 _add_event: function () {
392 this.container.bind( 'vmousemove', function ( e ) {
398 if ( !self.dragging ) {
402 self._drag( e.pageX );
405 this.container.bind( 'vmousedown', function ( e ) {
412 self.dragging = true;
414 self.org_x = e.pageX;
416 self.org_time = Date.now();
419 this.container.bind( 'vmouseup', function ( e ) {
424 self.dragging = false;
426 self._move( e.pageX );
429 this.container.bind( 'vmouseout', function ( e ) {
433 if ( !self.dragging ) {
437 if ( ( e.pageX < 20 ) ||
438 ( e.pageX > ( self.max_width - 20 ) ) ) {
439 self._move( e.pageX );
440 self.dragging = false;
445 _del_event: function () {
446 this.container.unbind( 'vmousemove' );
447 this.container.unbind( 'vmousedown' );
448 this.container.unbind( 'vmouseup' );
449 this.container.unbind( 'vmouseout' );
451 _setTranslateposition : function ( $ele, value ) {
456 if ( $.support.cssTransform3d ) {
457 translate = "translate3d(" + value + ", 0px, 0px)";
459 translate = "translate(" + value + ", 0px)";
461 cssArray = {"-moz-transform": translate,
462 "-webkit-transform": translate,
463 "-ms-transform": translate,
464 "-o-transform": translate,
465 "transform": translate};
470 _hidePrevNext : function() {
473 if( self.next_img ) {
474 self.next_img.css( "visibility", "hidden" );
476 if( self.prev_img ) {
477 self.prev_img.css( "visibility", "hidden" );
481 _hideCur : function() {
484 self.cur_img.css( "visibility", "hidden" );
487 _moveLeft : function ( $ele , value , duration ) {
493 if ( $.support.cssTransform3d ) {
494 translate = "translate3d(" + value + ", 0px, 0px)";
496 translate = "translate(" + value + ", 0px)";
498 if( duration !== undefined ) {
499 transition = "-webkit-transform " + (duration / 1000)+ "s ease";
501 cssArray = {"-moz-transform": translate,
502 "-webkit-transform": translate,
503 "-ms-transform": translate,
504 "-o-transform": translate,
505 "transform": translate};
506 if( transition !== "" ) {
507 cssArray["-webkit-transition"] = transition ;
508 if( value == "0px" ) {
509 $ele.one( 'webkitTransitionEnd', self._hidePrevNext );
511 $ele.one( 'webkitTransitionEnd', self._hideCur );
514 if( value == "0px" ) {
515 $ele.css( "visibility", "visible" );
523 this.window_width = $( window ).width();
524 this.max_width = this._get_width();
525 this.max_height = this._get_height();
526 this.container.css( 'height', this.max_height );
528 this.cur_img = $( 'div' ).find( '.ui-gallery-bg:eq(' + this.index + ')' );
529 this.prev_img = this.cur_img.prev();
530 this.next_img = this.cur_img.next();
532 this._attach( this.index - 1, this.prev_img );
533 this._attach( this.index, this.cur_img );
534 this._attach( this.index + 1, this.next_img );
536 this.cur_img.css( 'visibility', 'visible' );
537 if ( this.prev_img.length ) {
538 this._setTranslateposition( this.prev_img, -this.window_width + 'px');
541 this._moveLeft( this.cur_img, '0px');
542 if ( this.next_img.length ) {
543 this._setTranslateposition( this.next_img, this.window_width + 'px' );
548 if ( !this.images.length ) {
557 this._detach( this.index - 1, this.prev_img );
558 this._detach( this.index, this.cur_img );
559 this._detach( this.index + 1, this.next_img );
567 _get_width: function () {
568 return $( this.element ).width();
571 _get_height: function () {
572 var $page = $( this.element ).parentsUntil( 'ui-page' ),
573 $content = $page.children( '.ui-content' ),
574 header_h = $page.children( '.ui-header' ).outerHeight() || 0,
575 footer_h = $page.children( '.ui-footer' ).outerHeight() || 0,
576 padding = parseFloat( $content.css( 'padding-top' ) )
577 + parseFloat( $content.css( 'padding-bottom' ) ),
578 content_h = $( window ).height() - header_h - footer_h - padding;
583 _create: function () {
589 $( this.element ).wrapInner( '<div class="ui-gallery"></div>' );
590 $( this.element ).find( 'img' ).wrap( '<div class="ui-gallery-bg"></div>' );
592 this.container = $( this.element ).find('.ui-gallery');
594 temp_img = $( 'div' ).find( '.ui-gallery-bg:first' );
596 while ( temp_img.length ) {
597 this.images[i] = temp_img.find( 'img' );
598 temp_img = temp_img.next();
604 index = parseInt( $( this.element ).jqmData( 'index' ), 10 );
611 if ( index >= this.images.length ) {
612 index = this.images.length - 1;
617 this.align_type = $( this.element ).jqmData( 'vertical-align' );
624 orientationchange: $.proxy( this, "_orientationHandler" ),
625 resize: $.proxy( this, "_resizeHandler" )
631 $.each( this._globalHandlers, function( idx, value ) {
632 value.src.bind( value.handler );
636 _update: function () {
641 while ( this.images_hold.length ) {
642 image_file = this.images_hold.shift();
644 bg_html = $( '<div class="ui-gallery-bg"></div>' );
645 temp_img = $( '<img src="' + image_file + '"></div>' );
647 bg_html.append( temp_img );
648 this.container.append( bg_html );
649 this.images.push( temp_img );
654 _resizeHandler: function() {
656 if( self.orientationEventFire ) {
658 self.orientationEventFire = false;
661 _orientationHandler: function() {
664 self.orientationEventFire = true;
666 refresh: function ( start_index ) {
671 if ( start_index === undefined ) {
672 start_index = this.index;
674 if ( start_index < 0 ) {
677 if ( start_index >= this.images.length ) {
678 start_index = this.images.length - 1;
681 this.index = start_index;
688 add: function ( file ) {
689 this.images_hold.push( file );
692 remove: function ( index ) {
695 if ( index === undefined ) {
699 if ( index < 0 || index >= this.images.length ) {
703 if ( index == this.index ) {
704 temp_img = this.cur_img;
706 if ( this.index == 0 ) {
708 } else if ( this.index == this.images.length - 1 ) {
712 if ( this.direction < 0 ) {
713 this.cur_img = this.prev_img;
714 this.prev_img = this.prev_img.prev();
715 if ( this.prev_img.length ) {
716 this._moveLeft( this.prev_img, -this.window_width + 'px' );
717 this._attach( index - 2, this.prev_img );
721 this.cur_img = this.next_img;
722 this.next_img = this.next_img.next();
723 if ( this.next_img.length ) {
724 this._moveLeft( this.next_img, this.window_width + 'px' );
725 this._attach( index + 2, this.next_img );
728 this._moveLeft( this.cur_img, '0px', this.options.duration );
730 } else if ( index == this.index - 1 ) {
731 temp_img = this.prev_img;
732 this.prev_img = this.prev_img.prev();
733 if ( this.prev_img.length ) {
734 this._moveLeft( this.prev_img, -this.window_width + 'px' );
735 this._attach( index - 1, this.prev_img );
739 } else if ( index == this.index + 1 ) {
740 temp_img = this.next_img;
741 this.next_img = this.next_img.next();
742 if ( this.next_img.length ) {
743 this._moveLeft( this.next_img, this.window_width + 'px' );
744 this._attach( index + 1, this.next_img );
748 temp_img = $( 'div' ).find( '.ui-gallery-bg:eq(' + index + ')' );
751 this.images.splice( index, 1 );
756 this.images.splice( 0, this.images.length );
757 this.container.find('.ui-gallery-bg').detach();
760 length: function () {
761 return this.images.length;
764 value: function ( index ) {
765 if ( index === undefined ) {
769 this.refresh( index );
772 destory: function() {
773 $( window ).unbind( 'resize', this._resizeHandler );
774 $( window ).unbind( 'orientationchange' , this._orientationHandler );
777 }); /* End of widget */
779 // auto self-init widgets
780 $( document ).bind( "pagecreate create", function ( e ) {
781 $( e.target ).find( ":jqmData(role='gallery')" ).gallery();
784 $( document ).bind( "pageshow", function ( e ) {
785 $( e.target ).find( ":jqmData(role='gallery')" ).gallery( 'show' );
788 $( document ).bind( "pagebeforehide", function ( e ) {
789 $( e.target ).find( ":jqmData(role='gallery')" ).gallery( 'hide' );