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],
127 if ( this.align_type == "middle" ) {
128 img_top = ( this.max_height - img.height() ) / 2;
129 } else if ( this.align_type == "bottom" ) {
130 img_top = this.max_height - img.height();
135 obj.css( 'top', img_top + 'px' );
138 _attach: function ( index, obj ) {
140 processing = function () {
141 self._resize( index );
142 self._align( index, obj );
154 if ( index >= this.images.length ) {
158 obj.css( "display", "block" );
159 obj.append( this.images[index] );
161 if ( this.images[index].height() ) {
164 this.loader[index] = setInterval( function () {
165 if ( !self.images[index].height() ) {
170 clearInterval( self.loader[index] );
175 _detach: function ( index, obj ) {
185 if ( index >= this.images.length ) {
189 obj.css( "display", "none" );
190 this.images[index].removeAttr("style");
191 this.images[index].detach();
193 clearInterval( this.loader[index] );
196 _drag: function ( _x ) {
200 if ( !this.dragging ) {
204 if ( this.options.flicking === false ) {
205 delta = this.org_x - _x;
208 if ( delta < 0 && !this.prev_img.length ) {
212 if ( delta > 0 && !this.next_img.length ) {
217 coord_x = _x - this.org_x;
219 this.cur_img.css( 'left', coord_x + 'px' );
220 if ( this.next_img.length ) {
221 this.next_img.css( 'left', coord_x + this.window_width + 'px' );
223 if ( this.prev_img.length ) {
224 this.prev_img.css( 'left', coord_x - this.window_width + 'px' );
228 _move: function ( _x ) {
229 var delta = this.org_x - _x,
240 flip = delta < ( this.max_width * 0.45 ) ? 0 : 1;
242 flip = -delta < ( this.max_width * 0.45 ) ? 0 : 1;
246 drag_time = Date.now() - this.org_time;
248 if ( Math.abs( delta ) / drag_time > 1 ) {
254 if ( delta > 0 && this.next_img.length ) {
256 this._detach( this.index - 1, this.prev_img );
258 this.prev_img = this.cur_img;
259 this.cur_img = this.next_img;
260 this.next_img = this.next_img.next();
264 if ( this.next_img.length ) {
265 this.next_img.css( 'left', this.window_width + 'px' );
266 this._attach( this.index + 1, this.next_img );
271 } else if ( delta < 0 && this.prev_img.length ) {
273 this._detach( this.index + 1, this.next_img );
275 this.next_img = this.cur_img;
276 this.cur_img = this.prev_img;
277 this.prev_img = this.prev_img.prev();
281 if ( this.prev_img.length ) {
282 this.prev_img.css( 'left', -this.window_width + 'px' );
283 this._attach( this.index - 1, this.prev_img );
290 sec = this.options.duration;
295 setTimeout( function () {
299 this.cur_img.animate( { left: 0 }, sec );
300 if ( this.next_img.length ) {
301 this.next_img.animate( { left: this.window_width }, sec );
303 if ( this.prev_img.length ) {
304 this.prev_img.animate( { left: -this.window_width }, sec );
308 _add_event: function () {
312 this.container.bind( 'vmousemove', function ( e ) {
318 if ( !self.dragging ) {
322 self._drag( e.pageX );
325 this.container.bind( 'vmousedown', function ( e ) {
332 self.dragging = true;
334 self.org_x = e.pageX;
336 self.org_time = Date.now();
339 this.container.bind( 'vmouseup', function ( e ) {
344 self.dragging = false;
346 self._move( e.pageX );
349 this.container.bind( 'vmouseout', function ( e ) {
353 if ( !self.dragging ) {
357 if ( ( e.pageX < 20 ) ||
358 ( e.pageX > ( self.max_width - 20 ) ) ) {
359 self._move( e.pageX );
360 self.dragging = false;
365 _del_event: function () {
366 this.container.unbind( 'vmousemove' );
367 this.container.unbind( 'vmousedown' );
368 this.container.unbind( 'vmouseup' );
369 this.container.unbind( 'vmouseout' );
374 this.window_width = $( window ).width();
375 this.max_width = this._get_width();
376 this.max_height = this._get_height();
377 this.container.css( 'height', this.max_height );
379 this.cur_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + this.index + ')' );
380 this.prev_img = this.cur_img.prev();
381 this.next_img = this.cur_img.next();
383 this._attach( this.index - 1, this.prev_img );
384 this._attach( this.index, this.cur_img );
385 this._attach( this.index + 1, this.next_img );
387 if ( this.prev_img.length ) {
388 this.prev_img.css( 'left', -this.window_width + 'px' );
391 this.cur_img.css( 'left', '0px' );
393 if ( this.next_img.length ) {
394 this.next_img.css( 'left', this.window_width + 'px' );
404 this._detach( this.index - 1, this.prev_img );
405 this._detach( this.index, this.cur_img );
406 this._detach( this.index + 1, this.next_img );
414 _get_width: function () {
415 var $page = $( this.element ).parentsUntil( 'ui-page' ),
416 $content = $page.children( '.ui-content' ),
417 padding = parseFloat( $content.css( 'padding-left' ) )
418 + parseFloat( $content.css( 'padding-right' ) ),
419 content_w = $( window ).width() - padding;
424 _get_height: function () {
425 var $page = $( this.element ).parentsUntil( 'ui-page' ),
426 $content = $page.children( '.ui-content' ),
427 header_h = $page.children( '.ui-header' ).outerHeight() || 0,
428 footer_h = $page.children( '.ui-footer' ).outerHeight() || 0,
429 padding = parseFloat( $content.css( 'padding-top' ) )
430 + parseFloat( $content.css( 'padding-bottom' ) ),
431 content_h = $( window ).height() - header_h - footer_h - padding;
436 _create: function () {
442 $( this.element ).wrapInner( '<div class="ui-imageslider"></div>' );
443 $( this.element ).find( 'img' ).wrap( '<div class="ui-imageslider-bg"></div>' );
445 this.container = $( this.element ).find('.ui-imageslider');
447 temp_img = $( 'div' ).find( '.ui-imageslider-bg:first' );
449 while ( temp_img.length ) {
450 this.images[i] = temp_img.find( 'img' );
451 temp_img = temp_img.next();
455 for ( i = 0; i < this.images.length; i++ ) {
456 this.images[i].detach();
459 index = parseInt( $( this.element ).jqmData( 'index' ), 10 );
466 if ( index >= this.images.length ) {
467 index = this.images.length - 1;
472 this.align_type = $( this.element ).jqmData( 'vertical-align' );
474 $( window ).bind( 'resize', function () {
479 _update: function () {
484 while ( this.images_hold.length ) {
485 image_file = this.images_hold.shift();
487 bg_html = $( '<div class="ui-imageslider-bg"></div>' );
488 temp_img = $( '<img src="' + image_file + '"></div>' );
490 bg_html.append( temp_img );
491 this.container.append( bg_html );
492 this.images.push( temp_img );
496 refresh: function ( start_index ) {
501 if ( start_index === undefined ) {
502 start_index = this.index;
504 if ( start_index < 0 ) {
507 if ( start_index >= this.images.length ) {
508 start_index = this.images.length - 1;
511 this.index = start_index;
516 add: function ( file ) {
517 this.images_hold.push( file );
520 delete: function ( index ) {
523 if ( index === undefined ) {
527 if ( index < 0 || index >= this.images.length ) {
531 if ( index == this.index ) {
532 temp_img = this.cur_img;
534 if ( this.index == 0 ) {
536 } else if ( this.index == this.images.length - 1 ) {
540 if ( this.direction < 0 ) {
541 this.cur_img = this.prev_img;
542 this.prev_img = this.prev_img.prev();
543 if ( this.prev_img.length ) {
544 this.prev_img.css( 'left', -this.window_width );
545 this._attach( index - 2, this.prev_img );
549 this.cur_img = this.next_img;
550 this.next_img = this.next_img.next();
551 if ( this.next_img.length ) {
552 this.next_img.css( 'left', this.window_width );
553 this._attach( index + 2, this.next_img );
557 this.cur_img.animate( { left: 0 }, this.options.duration );
559 } else if ( index == this.index - 1 ) {
560 temp_img = this.prev_img;
561 this.prev_img = this.prev_img.prev();
562 if ( this.prev_img.length ) {
563 this.prev_img.css( 'left', -this.window_width );
564 this._attach( index - 1, this.prev_img );
568 } else if ( index == this.index + 1 ) {
569 temp_img = this.next_img;
570 this.next_img = this.next_img.next();
571 if ( this.next_img.length ) {
572 this.next_img.css( 'left', this.window_width );
573 this._attach( index + 1, this.next_img );
577 temp_img = $( 'div' ).find( '.ui-imageslider-bg:eq(' + index + ')' );
580 this.images.splice( index, 1 );
583 }); /* End of widget */
585 // auto self-init widgets
586 $( document ).bind( "pagecreate", function ( e ) {
587 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider();
590 $( document ).bind( "pageshow", function ( e ) {
591 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'show' );
594 $( document ).bind( "pagebeforehide", function ( e ) {
595 $( e.target ).find( ":jqmData(role='imageslider')" ).imageslider( 'hide' );