Export 0.1.42
[framework/web/web-ui-fw.git] / src / widgets / searchbar / js / jquery.mobile.tizen.searchbar.js
1 /* ***************************************************************************
2  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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  * ***************************************************************************
22  */
23 /*
24 * jQuery Mobile Framework : "textinput" plugin for text inputs, textareas
25 * Copyright (c) jQuery Project
26 * Dual licensed under the MIT or GPL Version 2 licenses.
27 * http://jquery.org/license
28 * Authors: Jinhyuk Jun <jinhyuk.jun@samsung.com>
29 *          Wongi Lee <wongi11.lee@samsung.com>
30 */
31
32 /**
33  * Searchbar can be created using <input> element with type=search
34  * <input type="search" name="search" id="search1" value=""  />
35  *
36  * Searchbar can be inserted 3 cases
37  * content : seachbar behave same as content element
38  * header : searchbar placed below title(header), It doesn't move when scrolling page
39  * inside optionheader : Searchbar placed inside optionheader, searchbar can be seen only expand optionheader
40  *
41  * Examples:
42  *
43  *      HTML markup for creating Searchbar
44  *              <input type="search"/>
45  *
46  *      How to make searchbar in content
47  *              <input type="search" name="" id="" value=""  />
48  *
49  *      How to make searchbar in title
50  *              <div data-role="header" data-position ="fixed" >
51  *                      <h1>Searchbar</h1>
52  *                      <input type="search" name="" id="" value=""  />
53  *              </div>
54  *
55  *      How to make searchbar inside optionheader
56  *              <div data-role="header" data-position ="fixed" >
57  *                      <h1>Searchbar</h1>
58  *                      <div id="myoptionheader2" data-role="optionheader">
59  *                              <input type="search" name="" id="" value=""  />
60  *                      </div>
61  *              </div>
62 */
63
64 (function ( $, undefined ) {
65
66         $.widget( "tizen.searchbar", $.mobile.widget, {
67                 options: {
68                         theme: null,
69                         initSelector: "input[type='search'],:jqmData(type='search'), input[type='tizen-search'],:jqmData(type='tizen-search')"
70                 },
71
72                 _create: function () {
73                         var input = this.element,
74                                 o = this.options,
75                                 theme = o.theme || $.mobile.getInheritedTheme( this.element, "c" ),
76                                 themeclass  = " ui-body-" + theme,
77                                 focusedEl,
78                                 clearbtn,
79                                 searchicon,
80                                 cancelbtn,
81                                 defaultText,
82                                 defaultTextClass,
83                                 trimedText,
84                                 newClassName,
85                                 newStyle,
86                                 newDiv,
87                                 inputedText;
88
89                         function toggleClear() {
90                                 setTimeout(function () {
91                                         clearbtn.toggleClass( "ui-input-clear-hidden", !input.val() );
92                                 }, 0);
93                         }
94
95                         function showCancel() {
96                                 focusedEl
97                                         .addClass( "ui-input-search-default" )
98                                         .removeClass( "ui-input-search-wide" );
99                                 cancelbtn
100                                         .addClass( "ui-btn-cancel-show" )
101                                         .removeClass( "ui-btn-cancel-hide" );
102                                 searchicon.hide();
103                         }
104
105                         function hideCancel() {
106                                 focusedEl
107                                         .addClass( "ui-input-search-wide" )
108                                         .removeClass( "ui-input-search-default" );
109                                 cancelbtn
110                                         .addClass( "ui-btn-cancel-hide" )
111                                         .removeClass( "ui-btn-cancel-show" );
112
113                                 if ( input.val() == "" ) {
114                                         searchicon.show();
115                                 }
116
117                                 toggleClear();
118                         }
119
120                         $( "label[for='" + input.attr( "id" ) + "']" ).addClass( "ui-input-text" );
121
122                         focusedEl = input.addClass( "ui-input-text ui-body-" + theme );
123
124                         // XXX: Temporary workaround for issue 785 (Apple bug 8910589).
125                         //      Turn off autocorrect and autocomplete on non-iOS 5 devices
126                         //      since the popup they use can't be dismissed by the user. Note
127                         //      that we test for the presence of the feature by looking for
128                         //      the autocorrect property on the input element. We currently
129                         //      have no test for iOS 5 or newer so we're temporarily using
130                         //      the touchOverflow support flag for jQM 1.0. Yes, I feel dirty. - jblas
131                         if ( typeof input[0].autocorrect !== "undefined" && !$.support.touchOverflow ) {
132                                 // Set the attribute instead of the property just in case there
133                                 // is code that attempts to make modifications via HTML.
134                                 input[0].setAttribute( "autocorrect", "off" );
135                                 input[0].setAttribute( "autocomplete", "off" );
136                         }
137
138                         focusedEl = input.wrap( "<div class='ui-input-search ui-shadow-inset ui-corner-all ui-btn-shadow" + themeclass + "'></div>" ).parent();
139                         clearbtn = $( "<a href='#' class='ui-input-clear' title='clear text'>clear text</a>" )
140                                 .bind('click', function ( event ) {
141                                         if ( input.attr( "disabled" ) == "disabled" ) {
142                                                 return false;
143                                         }
144                                         input
145                                                 .val( "" )
146                                                 .focus()
147                                                 .trigger( "change" );
148                                         clearbtn.addClass( "ui-input-clear-hidden" );
149                                         event.preventDefault();
150                                 })
151                                 .appendTo( focusedEl )
152                                 .buttonMarkup({
153                                         icon: "deleteSearch",
154                                         iconpos: "notext",
155                                         corners: true,
156                                         shadow: true
157                                 });
158
159                         toggleClear();
160
161
162                         input.bind( 'paste cut keyup focus change blur', toggleClear );
163
164                         //SLP --start search bar with cancel button
165                         focusedEl.wrapAll( "<div class='input-search-bar'></div>" );
166
167                         searchicon = $("<div class='ui-image-search ui-image-searchfield'></div>")
168                                 .bind('click', function ( event ) {
169                                         if ( input.attr( "disabled" ) == "disabled" ) {
170                                                 return false;
171                                         }
172                                         searchicon.hide();
173
174                                         input
175                                                 .blur()
176                                                 .focus();
177                                 } )
178                                 .appendTo( focusedEl );
179
180                         cancelbtn = $( "<a href='#' class='ui-input-cancel' title='clear text'>Cancel</a>" )
181                                 .bind('click', function ( event ) {
182                                         if ( input.attr( "disabled" ) == "disabled" ) {
183                                                 return false;
184                                         }
185                                         event.preventDefault();
186                                         event.stopPropagation();
187
188                                         input
189                                                 .val( "" )
190                                                 .blur()
191                                                 .trigger( "change" );
192
193                                         hideCancel();
194                                 } )
195                                 .appendTo( focusedEl.parent() )
196                                 .buttonMarkup( {
197                                         iconpos: "cancel",
198                                         corners: true,
199                                         shadow: true
200                                 } );
201
202                         // Input Focused
203                         input
204                                 .focus( function () {
205                                         if ( input.attr( "disabled" ) == "disabled" ) {
206                                                 return false;
207                                         }
208                                         showCancel();
209                                         focusedEl.addClass( $.mobile.focusClass );
210                                 })
211                                 .blur(function () {
212                                         focusedEl.removeClass( $.mobile.focusClass );
213                                 });
214
215                         // Input Blured
216                         /* When user touch on page, it's same to blur */
217                         /* FIXME : if there is no problem, please remove this codes..
218                         $( "div.input-search-bar" ).tap( function ( event ) {
219                                 if ( input.attr( "disabled" ) == "disabled" ) {
220                                         return false;
221                                 }
222                                 input.focus();
223                                 event.stopPropagation();
224                         } );
225
226                         var currentPage = input.closest( ".ui-page" );
227                         $( currentPage ).bind("tap", function ( e ) {
228                                 if ( input.attr( "disabled" ) == "disabled" ) {
229                                         return;
230                                 }
231
232                                 if ( $( input ).is( ":focus" ) ) {
233                                         focusedEl.removeClass( "ui-focus" );
234                                         hideCancel();
235                                         input.blur();
236                                 }
237                         } );*/
238
239                         // Default Text
240                         defaultText = input.jqmData( "default-text" );
241
242                         if ( ( defaultText != undefined ) && ( defaultText.length > 0 ) ) {
243                                 defaultTextClass = "ui-input-default-text";
244                                 trimedText = defaultText.replace(/\s/g, "");
245
246                                 /* Make new class for default text string */
247                                 newClassName = defaultTextClass + "-" + trimedText;
248                                 newStyle = $( "<style>" + '.' + newClassName + ":after" + "{content:" + "'" + defaultText + "'" + "}" + "</style>" );
249                                 $( 'html > head' ).append( newStyle );
250
251                                 /* Make new empty <DIV> for default text */
252                                 newDiv = $( "<div></div>" );
253
254                                 /* Add class and append new div */
255                                 newDiv.addClass( defaultTextClass );
256                                 newDiv.addClass( newClassName );
257                                 newDiv.tap( function ( event ) {
258                                         input.blur();
259                                         input.focus();
260                                 } );
261
262                                 input.parent().append( newDiv );
263
264                                 /* When focus, default text will be hide. */
265                                 input
266                                         .focus( function () {
267                                                 input.parent().find( "div.ui-input-default-text" ).addClass( "ui-input-default-hidden" );
268                                         } )
269                                         .blur( function () {
270                                                 var inputedText = input.val();
271                                                 if ( inputedText.length > 0 ) {
272                                                         input.parent().find( "div.ui-input-default-text" ).addClass( "ui-input-default-hidden" );
273                                                 } else {
274                                                         input.parent().find( "div.ui-input-default-text" ).removeClass( "ui-input-default-hidden" );
275                                                 }
276                                         } );
277                         }
278
279                         if ( input.val() ) {
280                                 searchicon.hide();
281                         }
282                 },
283
284                 disable: function () {
285                         this.element.attr( "disabled", true );
286                         this.element.parent().addClass( "ui-disabled" );
287                         this.element.parent().parent().find(".ui-input-cancel").addClass( "ui-disabled" );
288                         $( this.element ).blur();
289                 },
290
291                 enable: function () {
292                         this.element.attr( "disabled", false );
293                         this.element.parent().removeClass( "ui-disabled" );
294                         this.element.parent().parent().find(".ui-input-cancel").removeClass( "ui-disabled" );
295                         $( this.element ).focus();
296                 }
297         } );
298
299         //auto self-init widgets
300         $( document ).bind( "pagecreate create", function ( e ) {
301                 $.tizen.searchbar.prototype.enhanceWithin( e.target );
302         } );
303
304 }( jQuery ) );