2 * jQuery Mobile Widget @VERSION
4 * This software is licensed under the MIT licence (as defined by the OSI at
5 * http://www.opensource.org/licenses/mit-license.php)
7 * ***************************************************************************
8 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
9 * Copyright (c) 2011 by Intel Corporation Ltd.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 * ***************************************************************************
30 * Authors: Kalyan Kondapally <kalyan.kondapally@intel.com>,
31 * Elliot Smith <elliot.smith@intel.com>
32 * Hyunjung Kim <hjnim.kim@samsung.com>
35 // Widget which turns a list into a "swipe":
36 // i.e. each list item has a sliding "cover" which can be swiped
37 // to the right (to reveal buttons underneath) or left (to
38 // cover the buttons again). Clicking on a button under a swipe
39 // also moves the cover back to the left.
41 // In this case, the cover is over a grid of buttons;
42 // but it is should also be possible to use other types of markup under the
45 // WARNING: This doesn't work well inside a scrollview widget, as
46 // the touch events currently interfere with each other badly (e.g.
47 // a swipe will work but cause a scroll as well).
49 // Theme: default is to use the theme on the target element,
50 // theme passed in options, parent theme, or 'c' if none of the above.
51 // If list items are themed individually, the cover will pick up the
52 // theme of the list item which is its parent.
57 The swipe widget shows a list view on the screen where the list items can be swiped vertically to show a menu.
58 To add a swipe list widget to the application, use the following code:
60 <ul data-role="swipe">
62 <div data-role="swipe-cover">
63 <div data-role="button" data-inline="true">OK</div>
64 <div data-role="button" data-inline="true">Cancel</div>
66 <div data-role="swipe-item-cover">
67 <p>This is a swipe item cover.<br>
68 This will be swiped out when swipe event comes.</p>
73 You can use methods with the swipe as described in the jQueryMobile documentation for list view methods.
76 @property {String} data-role
77 Creates a swipe list using the HTML unordered list (>ul<) element.
78 The default value is swipe.
80 Creates a swipe list item cover using an HTML $gt;div$lt; element. This cover can be swiped to show the content beneath it.
81 The default value is swipelist-item-cover.
85 uncover swipelist item
93 return coveritem status( coverd or uncovred )
97 The swipe list can define a callback for the animationstart event, which is fired after a list item is swipe and the swipe animation is start:
101 The swipe list can define a callback for the animationend event, which is fired after a list item is swiped and the swipe animation is complete:
103 <ul data-role="swipelist">
105 <div data-role="button" data-inline="true">OK</div>
106 <div data-role="button" data-inline="true">Cancel</div>
107 <div data-role="swipelist-item-cover" id="foo">
108 <p>This is a swipelist item cover.<br>
109 This will be swiped out when swipe event comes.</p>
113 $("#foo").bind("animationend", function (ev)
115 Console.log("Swipelist cover's animation is complete.");
120 $.widget("tizen.swipe", $.mobile.widget, {
125 _create: function () {
126 // use the theme set on the element, set in options,
127 // the parent theme, or 'c' (in that order of preference)
128 var theme = this.element.jqmData('theme') ||
129 this.options.theme ||
130 this.element.parent().jqmData('theme') ||
133 this.options.theme = theme;
134 this._isopen = false;
138 refresh: function () {
148 defaultCoverTheme = 'ui-body-' + this.options.theme;
150 // swipe is a listview
151 if ( !this.element.parent().hasClass('ui-listview') ) {
152 this.element.parent().listview();
154 this.element.addClass('ui-swipe');
156 // get the list item covers
157 covers = this.element.find(':jqmData(role="swipe-item-cover")');
158 item = this.element.find(':jqmData(role="swipe-item")');
160 this._covers = covers;
161 item.addClass('ui-swipe-item');
162 coverTheme = defaultCoverTheme;
163 itemHasThemeClass = item.parent().attr('class')
164 .match(/ui\-body\-[a-z]|ui\-bar\-[a-z]/);
166 covers.each( function () {
167 var cover = $( this );
169 if ( itemHasThemeClass ) {
170 coverTheme = itemHasThemeClass[0];
173 cover.addClass('ui-swipe-item-cover');
174 cover.addClass( coverTheme );
176 if ( cover.has('.ui-swipe-item-cover-inner').length === 0 ) {
177 cover.wrapInner( $('<span/>').addClass('ui-swipe-item-cover-inner') );
180 if ( !( cover.data('animateRight') && cover.data('animateLeft') ) ) {
181 cover.data('animateRight', function () {
182 self._animateCover( cover, 110 );
185 cover.data('animateLeft', function () {
186 self._animateCover( cover, 0 );
190 // bind to synthetic events
191 item.bind( 'swipeleft', cover.data('animateLeft') );
192 cover.bind( 'swiperight', cover.data('animateRight') );
193 item.find( '.ui-btn' ).bind( 'vclick', cover.data('animateLeft') );
198 _cleanupDom: function () {
202 coverTheme = defaultCoverTheme,
209 defaultCoverTheme = 'ui-body-' + this.options.theme;
211 this.element.removeClass('ui-swipe');
213 // get the list item covers
214 cover = this.element.find(':jqmData(role="swipe-item-cover")');
215 item = this.element.find(':jqmData(role="swipe-item")');
217 item.removeClass('ui-swipe-item');
218 cover.removeClass('ui-swipe-item-cover');
220 itemClass = item.attr('class');
221 itemHasThemeClass = itemClass &&
222 itemClass.match(/ui\-body\-[a-z]|ui\-bar\-[a-z]/);
224 if ( itemHasThemeClass ) {
225 coverTheme = itemHasThemeClass[0];
228 cover.removeClass(coverTheme);
230 // remove wrapper HTML
231 wrapper = cover.find('.ui-swipe-item-cover-inner');
232 wrapper.children().unwrap();
233 text = wrapper.text();
236 cover.append( text );
240 // unbind swipe events
241 if ( cover.data('animateRight') && cover.data('animateLeft') ) {
242 cover.unbind( 'swiperight', cover.data('animateRight') );
243 item.unbind( 'swipeleft', cover.data('animateLeft') );
245 // unbind clicks on buttons inside the item
246 item.find('.ui-btn').unbind( 'vclick', cover.data('animateLeft') );
248 cover.data( 'animateRight', null );
249 cover.data( 'animateLeft', null );
253 // NB I tried to use CSS animations for this, but the performance
254 // and appearance was terrible on Android 2.2 browser;
255 // so I reverted to jQuery animations
257 // once the cover animation is done, the cover emits an
258 // animationComplete event
259 _animateCover: function ( cover, leftPercentage ) {
265 complete: function () {
266 cover.trigger('animationComplete');
270 $( this.element.parent() )
271 .find(":jqmData(role='swipe')")
274 if ( this !== self.element.get(0) &&
275 $( this ).swipe("isOpen") ) {
276 $( this ).swipe("close");
281 if ( leftPercentage == 110 ) {
284 this._isopen = false;
289 cover.trigger('animationStart');
290 cover.animate( {left: leftPercentage + '%'}, animationOptions );
293 destroy: function () {
300 $( self._covers ).each( function () {
301 var cover = $( this );
302 self._animateCover( cover, 110 );
306 isOpen: function () {
313 $( self._covers ).each( function () {
314 var cover = $( this );
315 self._animateCover( cover, 0 );
321 $( document ).bind("pagecreate", function ( e ) {
322 $( e.target ).find(":jqmData(role='swipe')").swipe();