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 'imageslider'
32 * data-start-index: start index
33 * data-vertical-align: set to top or middle or bottom.
37 * add(image_file): add the image (parameter: url of iamge)
38 * del(image_index): delete the image (parameter: index of image)
39 * refresh(): refresh the widget, should be called after add or del.
47 * <div data-role="imageslider" id="imageslider" data-start-index="3" data-vertical-align="middle">
56 * $('#imageslider-add').bind('vmouseup', function ( e ) {
57 * $('#imageslider').imageslider('add', '9.jpg');
58 * $('#imageslider').imageslider('add', '10.jpg');
59 * $('#imageslider').imageslider('refresh');
62 * $('#imageslider-del').bind('vmouseup', function ( e ) {
63 * $('#imageslider').imageslider('del');
68 (function ( $, window, undefined ) {
69 $.widget( "tizen.imageslider", $.mobile.widget, {
91 _resize: function ( obj ) {
96 img_max_width = this.max_width - margin,
97 img_max_height = this.max_height - margin;
99 height = obj.height();
102 ratio = height / width;
104 if ( width > img_max_width ) {
105 obj.width( img_max_width );
106 obj.height( img_max_width * ratio );
109 height = obj.height();
111 if ( height > img_max_height ) {
112 obj.height( img_max_height );
113 obj.width( img_max_height / ratio );
117 _align: function ( obj, img ) {
124 if ( this.align_type == "middle" ) {
125 img_top = ( this.max_height - img.height() ) / 2;
126 } else if ( this.align_type == "bottom" ) {
127 img_top = this.max_height - img.height();
132 obj.css( 'top', img_top + 'px' );
135 _detach: function ( image_index, obj ) {
139 if ( image_index < 0 ) {
142 if ( image_index >= this.images.length ) {
146 this.images[image_index].detach();
147 obj.css( "display", "none" );
150 _attach: function ( image_index, obj ) {
154 if ( image_index < 0 ) {
157 if ( image_index >= this.images.length ) {
161 obj.css( "display", "block" );
162 obj.append( this.images[image_index] );
163 this._resize( this.images[image_index] );
164 this._align( obj, this.images[image_index] );
167 _drag: function ( _x ) {
171 if ( !this.dragging ) {
175 if ( this.options.photoFlicking === false ) {
176 delta = this.org_x - _x;
179 if ( delta < 0 && !this.prev_img.length ) {
183 if ( delta > 0 && !this.next_img.length ) {
188 coord_x = _x - this.org_x;
190 this.cur_img.css( 'left', coord_x + 'px' );
191 if ( this.next_img.length ) {
192 this.next_img.css( 'left', coord_x + this.max_width + 'px' );
194 if ( this.prev_img.length ) {
195 this.prev_img.css( 'left', coord_x - this.max_width + 'px' );
199 _move: function ( _x ) {
200 var delta = this.org_x - _x,
212 flip = delta < ( this.max_width * 0.45 ) ? 0 : 1;
214 flip = -delta < ( this.max_width * 0.45 ) ? 0 : 1;
219 drag_time = date.getTime() - this.org_time;
221 if ( Math.abs( delta ) / drag_time > 1 ) {
227 if ( delta > 0 && this.next_img.length ) {
229 this._detach( this.index - 1, this.prev_img );
231 this.prev_img = this.cur_img;
232 this.cur_img = this.next_img;
233 this.next_img = this.next_img.next();
237 if ( this.next_img.length ) {
238 this.next_img.css( 'left', this.max_width + 'px' );
239 this._attach( this.index + 1, this.next_img );
244 } else if ( delta < 0 && this.prev_img.length ) {
246 this._detach( this.index + 1, this.next_img );
248 this.next_img = this.cur_img;
249 this.cur_img = this.prev_img;
250 this.prev_img = this.prev_img.prev();
254 if ( this.prev_img.length ) {
255 this.prev_img.css( 'left', -this.max_width + 'px' );
256 this._attach( this.index - 1, this.prev_img );
268 this.interval = setInterval( function () {
270 clearInterval( self.interval );
273 this.cur_img.animate( { left: 0 }, sec );
274 if ( this.next_img.length ) {
275 this.next_img.animate( { left: this.max_width }, sec );
277 if ( this.prev_img.length ) {
278 this.prev_img.animate( { left: -this.max_width }, sec );
282 _add_event: function () {
286 this.container.bind( 'vmousemove', function ( e ) {
292 if ( !self.dragging ) {
296 self._drag( e.pageX );
299 this.container.bind( 'vmousedown', function ( e ) {
306 self.dragging = true;
308 self.org_x = e.pageX;
311 self.org_time = date.getTime();
314 this.container.bind( 'vmouseup', function ( e ) {
319 self.dragging = false;
321 self._move( e.pageX );
324 this.container.bind( 'vmouseout', function ( e ) {
328 if ( !self.dragging ) {
332 if ( ( e.pageX < 20 ) ||
333 ( e.pageX > ( self.max_width - 20 ) ) ) {
334 self._move( e.pageX );
335 self.dragging = false;
340 _del_event: function () {
341 this.container.unbind( 'vmousemove' );
342 this.container.unbind( 'vmousedown' );
343 this.container.unbind( 'vmouseup' );
344 this.container.unbind( 'vmouseout' );
348 this.cur_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + this.index + ')' );
349 this.prev_img = this.cur_img.prev();
350 this.next_img = this.cur_img.next();
352 this._attach( this.index - 1, this.prev_img );
353 this._attach( this.index, this.cur_img );
354 this._attach( this.index + 1, this.next_img );
356 if ( this.prev_img.length ) {
357 this.prev_img.css( 'left', -this.max_width + 'px' );
360 this.cur_img.css( 'left', '0px' );
362 if ( this.next_img.length ) {
363 this.next_img.css( 'left', this.max_width + 'px' );
373 this._detach( this.index - 1, this.prev_img );
374 this._detach( this.index, this.cur_img );
375 this._detach( this.index + 1, this.next_img );
383 _get_height: function () {
384 var $page = $( '.ui-page' ),
385 $content = $page.children( '.ui-content' ),
386 $header = $page.children( '.ui-header' ),
387 $footer = $page.children( '.ui-footer' ),
388 header_h = $header.outerHeight(),
389 footer_h = $footer.outerHeight(),
390 padding = parseFloat( $content.css( 'padding-top' ) ) + parseFloat( $content.css( 'padding-bottom' ) ),
391 content_h = $( window ).height() - header_h - footer_h - padding * 2;
396 _create: function () {
401 $( this.element ).wrapInner( '<div class="ui-imageslider"></div>' );
402 $( this.element ).find( 'img' ).wrap( '<div class="ui-imageslider-bg"></div>' );
404 this.container = $( this.element ).find('.ui-imageslider');
406 this.max_width = $( window ).width();
407 this.max_height = this._get_height();
408 this.container.css( 'height', this.max_height );
410 temp_img = $( 'div' ).find( '.ui-imageslider-bg:first' );
412 while ( temp_img.length ) {
413 this.images[i] = temp_img.find( 'img' );
414 temp_img = temp_img.next();
418 for ( i = 0; i < this.images.length; i++ ) {
419 this.images[i].detach();
422 start_index = parseInt( $( this.element ).attr( 'data-start-index' ), 10 );
423 if ( start_index === undefined ) {
426 if ( start_index < 0 ) {
429 if ( start_index >= this.images.length ) {
430 start_index = this.images.length - 1;
433 this.index = start_index;
435 this.align_type = $( this.element ).attr( 'data-vertical-align' );
438 _update: function () {
443 while ( this.images_hold.length ) {
444 image_file = this.images_hold.shift();
446 bg_html = $( '<div class="ui-imageslider-bg"></div>' );
447 temp_img = $( '<img src="' + image_file + '"></div>' );
449 bg_html.append( temp_img );
450 this.container.append( bg_html );
451 this.images.push( temp_img );
455 refresh: function ( start_index ) {
460 if ( start_index === undefined ) {
461 start_index = this.index;
463 if ( start_index < 0 ) {
466 if ( start_index >= this.images.length ) {
467 start_index = this.images.length - 1;
470 this.index = start_index;
475 add: function ( image_file ) {
476 this.images_hold.push( image_file );
479 del: function ( image_index ) {
482 if ( image_index === undefined ) {
483 image_index = this.index;
486 if ( image_index < 0 || image_index >= this.images.length ) {
490 if ( image_index == this.index ) {
491 temp_img = this.cur_img;
493 if ( this.index == 0 ) {
495 } else if ( this.index == this.images.length - 1 ) {
499 if ( this.direction < 0 ) {
500 this.cur_img = this.prev_img;
501 this.prev_img = this.prev_img.prev();
502 if ( this.prev_img.length ) {
503 this.prev_img.css( 'left', -this.max_width );
504 this._attach( image_index - 2, this.prev_img );
508 this.cur_img = this.next_img;
509 this.next_img = this.next_img.next();
510 if ( this.next_img.length ) {
511 this.next_img.css( 'left', this.max_width );
512 this._attach( image_index + 2, this.next_img );
516 this.cur_img.animate( { left: 0 }, 500 );
518 } else if ( image_index == this.index - 1 ) {
519 temp_img = this.prev_img;
520 this.prev_img = this.prev_img.prev();
521 if ( this.prev_img.length ) {
522 this.prev_img.css( 'left', -this.max_width );
523 this._attach( image_index - 1, this.prev_img );
527 } else if ( image_index == this.index + 1 ) {
528 temp_img = this.next_img;
529 this.next_img = this.next_img.next();
530 if ( this.next_img.length ) {
531 this.next_img.css( 'left', this.max_width );
532 this._attach( image_index + 1, this.next_img );
536 temp_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + image_index + ')' );
539 this.images.splice( image_index, 1 );
542 }); /* End of widget */
544 // auto self-init widgets
545 $( document ).bind( "pagecreate", function ( e ) {
546 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider();
549 $( document ).bind( "pageshow", function ( e ) {
550 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'show' );
553 $( document ).bind( "pagebeforehide", function ( e ) {
554 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'hide' );