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-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 * delete(index): delete the image (parameter: index of image)
39 * refresh(index): refresh the widget, should be called after add or del. (parameter: start index)
47 * <div data-role="imageslider" id="imageslider" data-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('delete');
68 (function ( $, window, undefined ) {
69 $.widget( "tizen.imageslider", $.mobile.widget, {
92 _resize: function ( index ) {
93 var img = this.images[index],
94 width = this.images[index].width(),
95 height = this.images[index].height(),
98 img_max_width = this.max_width - margin,
99 img_max_height = this.max_height - margin;
101 ratio = height / width;
103 if ( width > img_max_width ) {
104 img.width( img_max_width );
105 img.height( img_max_width * ratio );
108 height = img.height();
110 if ( height > img_max_height ) {
111 img.height( img_max_height );
112 img.width( img_max_height / ratio );
116 _align: function ( index, obj ) {
117 var img = this.images[index],
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 _attach: function ( index, obj ) {
137 processing = function () {
138 self._resize( index );
139 self._align( index, obj );
148 if ( index >= this.images.length ) {
152 obj.css( "display", "block" );
153 obj.append( this.images[index] );
155 if ( this.images[index].height() ) {
158 this.loader[index] = setInterval( function () {
159 if ( !self.images[index].height() ) {
164 clearInterval( self.loader[index] );
169 _detach: function ( index, obj ) {
176 if ( index >= this.images.length ) {
180 obj.css( "display", "none" );
181 this.images[index].removeAttr("style");
182 this.images[index].detach();
184 clearInterval( this.loader[index] );
187 _drag: function ( _x ) {
191 if ( !this.dragging ) {
195 if ( this.options.flicking === false ) {
196 delta = this.org_x - _x;
199 if ( delta < 0 && !this.prev_img.length ) {
203 if ( delta > 0 && !this.next_img.length ) {
208 coord_x = _x - this.org_x;
210 this.cur_img.css( 'left', coord_x + 'px' );
211 if ( this.next_img.length ) {
212 this.next_img.css( 'left', coord_x + this.window_width + 'px' );
214 if ( this.prev_img.length ) {
215 this.prev_img.css( 'left', coord_x - this.window_width + 'px' );
219 _move: function ( _x ) {
220 var delta = this.org_x - _x,
232 flip = delta < ( this.max_width * 0.45 ) ? 0 : 1;
234 flip = -delta < ( this.max_width * 0.45 ) ? 0 : 1;
239 drag_time = date.getTime() - this.org_time;
241 if ( Math.abs( delta ) / drag_time > 1 ) {
247 if ( delta > 0 && this.next_img.length ) {
249 this._detach( this.index - 1, this.prev_img );
251 this.prev_img = this.cur_img;
252 this.cur_img = this.next_img;
253 this.next_img = this.next_img.next();
257 if ( this.next_img.length ) {
258 this.next_img.css( 'left', this.window_width + 'px' );
259 this._attach( this.index + 1, this.next_img );
264 } else if ( delta < 0 && this.prev_img.length ) {
266 this._detach( this.index + 1, this.next_img );
268 this.next_img = this.cur_img;
269 this.cur_img = this.prev_img;
270 this.prev_img = this.prev_img.prev();
274 if ( this.prev_img.length ) {
275 this.prev_img.css( 'left', -this.window_width + 'px' );
276 this._attach( this.index - 1, this.prev_img );
283 sec = this.options.duration;
288 setTimeout( function () {
292 this.cur_img.animate( { left: 0 }, sec );
293 if ( this.next_img.length ) {
294 this.next_img.animate( { left: this.window_width }, sec );
296 if ( this.prev_img.length ) {
297 this.prev_img.animate( { left: -this.window_width }, sec );
301 _add_event: function () {
305 this.container.bind( 'vmousemove', function ( e ) {
311 if ( !self.dragging ) {
315 self._drag( e.pageX );
318 this.container.bind( 'vmousedown', function ( e ) {
325 self.dragging = true;
327 self.org_x = e.pageX;
330 self.org_time = date.getTime();
333 this.container.bind( 'vmouseup', function ( e ) {
338 self.dragging = false;
340 self._move( e.pageX );
343 this.container.bind( 'vmouseout', function ( e ) {
347 if ( !self.dragging ) {
351 if ( ( e.pageX < 20 ) ||
352 ( e.pageX > ( self.max_width - 20 ) ) ) {
353 self._move( e.pageX );
354 self.dragging = false;
359 _del_event: function () {
360 this.container.unbind( 'vmousemove' );
361 this.container.unbind( 'vmousedown' );
362 this.container.unbind( 'vmouseup' );
363 this.container.unbind( 'vmouseout' );
368 this.window_width = $( window ).width();
369 this.max_width = this._get_width();
370 this.max_height = this._get_height();
371 this.container.css( 'height', this.max_height );
373 this.cur_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + this.index + ')' );
374 this.prev_img = this.cur_img.prev();
375 this.next_img = this.cur_img.next();
377 this._attach( this.index - 1, this.prev_img );
378 this._attach( this.index, this.cur_img );
379 this._attach( this.index + 1, this.next_img );
381 if ( this.prev_img.length ) {
382 this.prev_img.css( 'left', -this.window_width + 'px' );
385 this.cur_img.css( 'left', '0px' );
387 if ( this.next_img.length ) {
388 this.next_img.css( 'left', this.window_width + 'px' );
398 this._detach( this.index - 1, this.prev_img );
399 this._detach( this.index, this.cur_img );
400 this._detach( this.index + 1, this.next_img );
408 _get_width: function () {
409 var $page = $( this.element ).parentsUntil( 'ui-page' ),
410 $content = $page.children( '.ui-content' ),
411 padding = parseFloat( $content.css( 'padding-left' ) )
412 + parseFloat( $content.css( 'padding-right' ) ),
413 content_w = $( window ).width() - padding;
418 _get_height: function () {
419 var $page = $( this.element ).parentsUntil( 'ui-page' ),
420 $content = $page.children( '.ui-content' ),
421 header_h = $page.children( '.ui-header' ).outerHeight() || 0,
422 footer_h = $page.children( '.ui-footer' ).outerHeight() || 0,
423 padding = parseFloat( $content.css( 'padding-top' ) )
424 + parseFloat( $content.css( 'padding-bottom' ) ),
425 content_h = $( window ).height() - header_h - footer_h - padding;
430 _create: function () {
436 $( this.element ).wrapInner( '<div class="ui-imageslider"></div>' );
437 $( this.element ).find( 'img' ).wrap( '<div class="ui-imageslider-bg"></div>' );
439 this.container = $( this.element ).find('.ui-imageslider');
441 temp_img = $( 'div' ).find( '.ui-imageslider-bg:first' );
443 while ( temp_img.length ) {
444 this.images[i] = temp_img.find( 'img' );
445 temp_img = temp_img.next();
449 for ( i = 0; i < this.images.length; i++ ) {
450 this.images[i].detach();
453 index = parseInt( $( this.element ).jqmData( 'index' ), 10 );
460 if ( index >= this.images.length ) {
461 index = this.images.length - 1;
466 this.align_type = $( this.element ).jqmData( 'vertical-align' );
468 $( window ).bind( 'resize', function () {
473 _update: function () {
478 while ( this.images_hold.length ) {
479 image_file = this.images_hold.shift();
481 bg_html = $( '<div class="ui-imageslider-bg"></div>' );
482 temp_img = $( '<img src="' + image_file + '"></div>' );
484 bg_html.append( temp_img );
485 this.container.append( bg_html );
486 this.images.push( temp_img );
490 refresh: function ( start_index ) {
495 if ( start_index === undefined ) {
496 start_index = this.index;
498 if ( start_index < 0 ) {
501 if ( start_index >= this.images.length ) {
502 start_index = this.images.length - 1;
505 this.index = start_index;
510 add: function ( file ) {
511 this.images_hold.push( file );
514 delete: function ( index ) {
517 if ( index === undefined ) {
521 if ( index < 0 || index >= this.images.length ) {
525 if ( index == this.index ) {
526 temp_img = this.cur_img;
528 if ( this.index == 0 ) {
530 } else if ( this.index == this.images.length - 1 ) {
534 if ( this.direction < 0 ) {
535 this.cur_img = this.prev_img;
536 this.prev_img = this.prev_img.prev();
537 if ( this.prev_img.length ) {
538 this.prev_img.css( 'left', -this.window_width );
539 this._attach( index - 2, this.prev_img );
543 this.cur_img = this.next_img;
544 this.next_img = this.next_img.next();
545 if ( this.next_img.length ) {
546 this.next_img.css( 'left', this.window_width );
547 this._attach( index + 2, this.next_img );
551 this.cur_img.animate( { left: 0 }, this.options.duration );
553 } else if ( index == this.index - 1 ) {
554 temp_img = this.prev_img;
555 this.prev_img = this.prev_img.prev();
556 if ( this.prev_img.length ) {
557 this.prev_img.css( 'left', -this.window_width );
558 this._attach( index - 1, this.prev_img );
562 } else if ( index == this.index + 1 ) {
563 temp_img = this.next_img;
564 this.next_img = this.next_img.next();
565 if ( this.next_img.length ) {
566 this.next_img.css( 'left', this.window_width );
567 this._attach( index + 1, this.next_img );
571 temp_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + index + ')' );
574 this.images.splice( index, 1 );
577 }); /* End of widget */
579 // auto self-init widgets
580 $( document ).bind( "pagecreate", function ( e ) {
581 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider();
584 $( document ).bind( "pageshow", function ( e ) {
585 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'show' );
588 $( document ).bind( "pagebeforehide", function ( e ) {
589 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'hide' );