Export 0.1.47
[platform/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                                 cancelbtn,
80                                 defaultText,
81                                 defaultTextClass,
82                                 trimedText,
83                                 newClassName,
84                                 newStyle,
85                                 newDiv,
86                                 searchimage,
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                         }
103
104                         function hideCancel() {
105                                 focusedEl
106                                         .addClass( "ui-input-search-wide" )
107                                         .removeClass( "ui-input-search-default" );
108                                 cancelbtn
109                                         .addClass( "ui-btn-cancel-hide" )
110                                         .removeClass( "ui-btn-cancel-show" );
111
112                                 toggleClear();
113                         }
114
115                         $( "label[for='" + input.attr( "id" ) + "']" ).addClass( "ui-input-text" );
116
117                         focusedEl = input.addClass( "ui-input-text ui-body-" + theme );
118
119                         // XXX: Temporary workaround for issue 785 (Apple bug 8910589).
120                         //      Turn off autocorrect and autocomplete on non-iOS 5 devices
121                         //      since the popup they use can't be dismissed by the user. Note
122                         //      that we test for the presence of the feature by looking for
123                         //      the autocorrect property on the input element. We currently
124                         //      have no test for iOS 5 or newer so we're temporarily using
125                         //      the touchOverflow support flag for jQM 1.0. Yes, I feel dirty. - jblas
126                         if ( typeof input[0].autocorrect !== "undefined" && !$.support.touchOverflow ) {
127                                 // Set the attribute instead of the property just in case there
128                                 // is code that attempts to make modifications via HTML.
129                                 input[0].setAttribute( "autocorrect", "off" );
130                                 input[0].setAttribute( "autocomplete", "off" );
131                         }
132
133                         focusedEl = input.wrap( "<div class='ui-input-search ui-shadow-inset ui-corner-all ui-btn-shadow" + themeclass + "'></div>" ).parent();
134                         clearbtn = $( "<a href='#' class='ui-input-clear' title='clear text'>clear text</a>" )
135                                 .bind('click', function ( event ) {
136                                         if ( input.attr( "disabled" ) == "disabled" ) {
137                                                 return false;
138                                         }
139                                         input
140                                                 .val( "" )
141                                                 .focus()
142                                                 .trigger( "change" );
143                                         clearbtn.addClass( "ui-input-clear-hidden" );
144                                         event.preventDefault();
145                                 })
146                                 .appendTo( focusedEl )
147                                 .buttonMarkup({
148                                         icon: "deleteSearch",
149                                         iconpos: "notext",
150                                         corners: true,
151                                         shadow: true
152                                 });
153
154                         toggleClear();
155
156
157                         input.bind( 'paste cut keyup focus change blur', toggleClear );
158
159                         //SLP --start search bar with cancel button
160                         focusedEl.wrapAll( "<div class='input-search-bar'></div>" );
161
162                         cancelbtn = $( "<a href='#' class='ui-input-cancel' title='clear text'>Cancel</a>" )
163                                 .bind('click', function ( event ) {
164                                         if ( input.attr( "disabled" ) == "disabled" ) {
165                                                 return false;
166                                         }
167                                         event.preventDefault();
168                                         event.stopPropagation();
169
170                                         input
171                                                 .val( "" )
172                                                 .blur()
173                                                 .trigger( "change" );
174
175                                         hideCancel();
176                                 } )
177                                 .appendTo( focusedEl.parent() )
178                                 .buttonMarkup( {
179                                         iconpos: "cancel",
180                                         corners: true,
181                                         shadow: true
182                                 } );
183
184                         searchimage = $("<div class='ui-image-search'></div>").appendTo( focusedEl );
185
186                         // Input Focused
187                         input
188                                 .focus( function () {
189                                         if ( input.attr( "disabled" ) == "disabled" ) {
190                                                 return false;
191                                         }
192                                         showCancel();
193                                         focusedEl.addClass( $.mobile.focusClass );
194                                 })
195                                 .blur(function () {
196                                         focusedEl.removeClass( $.mobile.focusClass );
197                                 });
198
199                         // Default Text
200                         defaultText = input.jqmData( "default-text" );
201
202                         if ( ( defaultText != undefined ) && ( defaultText.length > 0 ) ) {
203                                 defaultTextClass = "ui-input-default-text";
204                                 trimedText = defaultText.replace(/\s/g, "");
205
206                                 /* Make new class for default text string */
207                                 newClassName = defaultTextClass + "-" + trimedText;
208                                 newStyle = $( "<style>" + '.' + newClassName + ":after" + "{content:" + "'" + defaultText + "'" + "}" + "</style>" );
209                                 $( 'html > head' ).append( newStyle );
210
211                                 /* Make new empty <DIV> for default text */
212                                 newDiv = $( "<div></div>" );
213
214                                 /* Add class and append new div */
215                                 newDiv.addClass( defaultTextClass );
216                                 newDiv.addClass( newClassName );
217                                 newDiv.tap( function ( event ) {
218                                         input.blur();
219                                         input.focus();
220                                 } );
221
222                                 input.parent().append( newDiv );
223
224                                 /* When focus, default text will be hide. */
225                                 input
226                                         .focus( function () {
227                                                 input.parent().find( "div.ui-input-default-text" ).addClass( "ui-input-default-hidden" );
228                                         } )
229                                         .blur( function () {
230                                                 var inputedText = input.val();
231                                                 if ( inputedText.length > 0 ) {
232                                                         input.parent().find( "div.ui-input-default-text" ).addClass( "ui-input-default-hidden" );
233                                                 } else {
234                                                         input.parent().find( "div.ui-input-default-text" ).removeClass( "ui-input-default-hidden" );
235                                                 }
236                                         } );
237                         }
238
239                         if ( !input.attr("placeholder") ) {
240                                 input.attr( "placeholder", "Search" );
241                         }
242                 },
243
244                 disable: function () {
245                         this.element.attr( "disabled", true );
246                         this.element.parent().addClass( "ui-disabled" );
247                         this.element.parent().parent().find(".ui-input-cancel").addClass( "ui-disabled" );
248                         $( this.element ).blur();
249                 },
250
251                 enable: function () {
252                         this.element.attr( "disabled", false );
253                         this.element.parent().removeClass( "ui-disabled" );
254                         this.element.parent().parent().find(".ui-input-cancel").removeClass( "ui-disabled" );
255                         $( this.element ).focus();
256                 }
257         } );
258
259         //auto self-init widgets
260         $( document ).bind( "pagecreate create", function ( e ) {
261                 $.tizen.searchbar.prototype.enhanceWithin( e.target );
262         } );
263
264 }( jQuery ) );