2.0_beta sync to rsa
[framework/web/web-ui-fw.git] / src / widgets / optionheader / js / jquery.mobile.tizen.optionheader.js
1 /*
2  * jQuery Mobile Widget @VERSION
3  *
4  * This software is licensed under the MIT licence (as defined by the OSI at
5  * http://www.opensource.org/licenses/mit-license.php)
6  *
7  * ***************************************************************************
8  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
9  * Copyright (c) 2011 by Intel Corporation Ltd.
10  *
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:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
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  * ***************************************************************************
29  *
30  * Authors: Elliot Smith <elliot.smith@intel.com>
31  */
32
33 // optionheader provides a collapsible toolbar for buttons and
34 // segmented controls directly under the title bar. It
35 // wraps a jQuery Mobile grid in a collapsible container; clicking
36 // on the container, or on one of the buttons inside the container,
37 // will collapse it.
38 //
39 // To add an option header to a page, mark up the header with a
40 // data-role="optionheader" attribute, as shown in this example:
41 //
42 // <div data-role="header">
43 //     <h1>Option header - 3 buttons example</h1>
44 //     <div data-role="optionheader">
45 //        <div class="ui-grid-b">
46 //             <div class="ui-block-a"><a data-role="button">Previous</a></div>
47 //             <div class="ui-block-b"><a data-role="button">Cancel</a></div>
48 //             <div class="ui-block-c"><a data-role="button">Next</a></div>
49 //        </div>
50 //     </div>
51 // </div>
52 //
53 // The optionheader can also be used inline (e.g. in a content block or
54 // a widget).
55 //
56 // Alternatively, use $('...').optionheader() to apply programmatically.
57 //
58 // The grid inside the optionheader should be marked up as for
59 // a standard jQuery Mobile grid. (The widget has been tested with one
60 // or two rows of 2-4 columns each.)
61 //
62 // Note that if you use this with fixed headers, you may find that
63 // expanding the option header increases the page size so that scrollbars
64 // appear (jQuery Mobile's own collapsible content areas cause the
65 // same issue). You can alleviate this somewhat by calling the show() method
66 // on the page toolbars each time the size of the header changes.
67 //
68 // The widget is configurable via a data-options attribute on the same
69 // div as the data-role="optionheader" attribute, e.g.
70 //
71 // <div data-role="header">
72 //     <h1>Option header - configured</h1>
73 //     <div data-role="optionheader" data-options='{"collapsed":true, "duration":1.5}'>
74 //        <div class="ui-grid-b">
75 //             <div class="ui-block-a"><a data-role="button">Previous</a></div>
76 //             <div class="ui-block-b"><a data-role="button">Cancel</a></div>
77 //             <div class="ui-block-c"><a data-role="button">Next</a></div>
78 //        </div>
79 //     </div>
80 // </div>
81 //
82 // Options can also be set with $(...).optionheader('option', 'name', value).
83 // However, if you do this, you'll need to call $(...).optionheader('refresh')
84 // afterwards for the new values to take effect (note that optionheader()
85 // can be applied multiple times to an element without side effects).
86 //
87 // See below for the available options.
88 //
89 // Theme: by default, gets a 'b' swatch; override with data-theme="X"
90 // as per usual
91 //
92 // Options (can be set with a data-options attribute):
93 //
94 //   {Boolean} [showIndicator=true] Set to true (the default) to show
95 //   the upward-pointing arrow indicator on top of the title bar.
96 //   {Boolean} [startCollapsed=false] Sets the appearance when the option
97 //   header is first displayed; defaults to false (i.e. show the header
98 //   expanded on first draw). NB setting this option later has no
99 //   effect: use collapse() to collapse a widget which is already
100 //   drawn.
101 //   {Boolean} [expandable=true] Sets whether the header will expand
102 //   in response to clicks; default = true.
103 //   {Float} [duration=0.25] Duration of the expand/collapse animation.
104 //
105 // Methods (see below for docs):
106 //
107 //   toggle(options)
108 //   expand(options)
109 //   collapse(options)
110 //
111 // Events:
112 //
113 //   expand: Triggered when the option header is expanded
114 //   collapse: Triggered when the option header is collapsed
115 //
116
117
118 (function ($, undefined) {
119         $.widget("tizen.optionheader", $.mobile.widget, {
120                 options: {
121                         initSelector: ":jqmData(role='optionheader')",
122                         showIndicator: true,
123                         theme: 's',
124                         startCollapsed: false,
125                         expandable: true,
126                         duration: 0.25,
127                         collapseOnInit : true,
128                         default_font_size : $('html').css('font-size')
129                 },
130                 collapsedHeight: '5px',
131
132                 _create: function () {
133                         var options,
134                                 theme,
135                                 self = this,
136                                 elementHeight = 106,
137                                 parentPage,
138                                 dataOptions = this.element.jqmData( 'options' ),
139                                 page = this.element.closest( ':jqmData(role="page")' );
140                         // parse data-options
141                         $.extend( this.options, dataOptions );
142
143                         this.isCollapsed = this.options.collapseOnInit;
144                         this.expandedHeight = null;
145
146                         // parse data-theme and reset options.theme if it's present
147                         theme = this.element.jqmData( 'theme' ) || this.options.theme;
148                         this.options.theme = theme;
149
150                         this.element.closest( ':jqmData(role="header")' ).addClass( "ui-option-header-resizing" );
151
152                         // set up the click handler; it's done here so it can
153                         // easily be removed, as there should only be one instance
154                         // of the handler function for each class instance
155                         this.clickHandler = function () {
156                                 self.toggle();
157                         };
158
159                         /* Apply REM scaling */
160                         elementHeight = elementHeight / ( 36 / parseInt( $('html').css('font-size'), 10 ) );
161
162                         if ( this.element.height() < elementHeight ) {
163                                 this.element.css( "height", elementHeight );
164                         }
165
166                         // get the element's dimensions
167                         // and to set its initial collapse state;
168                         // either do it now (if the page is visible already)
169                         // or on pageshow
170
171                         if ( page.is(":visible") ) {
172                                 self.refresh();
173                                 self._realize();
174                         } else {
175                                 self.refresh();
176
177                                 page.bind( "pagebeforeshow", function () {
178                                         self._setArrowLeft();
179                                         self._realize();
180                                 });
181                         }
182                         self._setArrowLeft();
183         //        this.refresh();
184                 },
185
186                 _realize: function () {
187                         if ( !this.expandedHeight ) {
188                                 this.expandedHeight = this.element.height();
189                         }
190
191                         if ( this.isCollapsed ) {
192         //        if (this.options.startCollapsed) {
193                                 this.collapse( {duration: 0} );
194                         }
195                 },
196
197                 _setArrowLeft: function () {
198                         var matchingBtn = $( this.element ).jqmData( "for" ),
199                                 arrowCenter = 14,
200                                 btn2Position = 10,
201                                 btn3Position = 144,
202                                 matchBtn = $( this.element ).parents( ".ui-page" ).find( "#" + matchingBtn ),
203                                 buttonRight = matchBtn.nextAll().is( "a" ) ? btn3Position : btn2Position,
204                                 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
205
206                         if ( $(this.element).parents(".ui-page").find( "#" + matchingBtn ).length != 0 ) {
207
208                                 if ( this.options.expandable ) {
209                                         matchBtn.bind( 'vclick', this.clickHandler );
210                                 } else {
211                                         matchBtn.unbind( 'vclick', this.clickHandler );
212                                 }
213
214                                 // decide arrow Button position
215                                 if ( matchBtn.css( "left" ) && matchBtn.css( "left" ) != "auto" ) {
216                                         $( ".ui-triangle-image" ).css( "left", matchBtn.width() / 2 + parseInt(matchBtn.css( "left" ), 10) - arrowCenter + "px" );
217                                 } else if ( matchBtn.css("right") ) {
218                                         $( ".ui-triangle-image" ).css( "left", document.documentElement.clientWidth - ( matchBtn.width() / 2 + buttonRight / scaleFactor ) - arrowCenter + "px" );
219                                 }
220                         } else {
221                                 $( ".ui-triangle-image" ).css( "left", document.documentElement.clientWidth / 2 - arrowCenter + "px" );
222                         }
223                 },
224                 // Draw the option header, according to current options
225                 refresh: function () {
226                         var el = this.element,
227                                 arrow = $( '<div class="ui-option-header-triangle-arrow"></div>' ),
228                                 optionHeaderClass = 'ui-option-header',
229                                 gridRowSelector = '.ui-grid-a,.ui-grid-b,.ui-grid-c,.ui-grid-d,.ui-grid-e',
230                                 theme = this.options.theme,
231                                 numRows,
232                                 rowsClass,
233                                 themeClass,
234                                 klass,
235                                 o = $.extend( {grid: null} ),
236                                 $kids = el.find( "div" ).eq( 0 ).children().children(),
237                                 letter,
238                                 gridCols = {solo: 1, a: 2, b: 3, c: 4, d: 5},
239                                 grid = o.grid;
240
241                         if ( !grid ) {
242                                 if ( $kids.length <= 5 ) {
243                                         for ( letter in gridCols ) {
244                                                 if ( gridCols[ letter ] === $kids.length ) {
245                                                         grid = letter;
246                                                 }
247                                         }
248                                         numRows = $kids.length / gridCols[grid];
249                                 } else {
250                                         numRows = 2;
251                                 }
252                         }
253
254                 // count ui-grid-* elements to get number of rows
255         //        numRows = el.find(gridRowSelector).length;
256
257                 // ...at least one row
258         //        numRows = Math.max(1, numRows);
259
260                 // add classes to outer div:
261                 //   ui-option-header-N-row, where N = options.rows
262                 //   ui-bar-X, where X = options.theme (defaults to 'c')
263                 //   ui-option-header
264                         rowsClass = 'ui-option-header-' + numRows + '-row';
265                         themeClass = 'ui-body-' + this.options.theme;
266
267                         el.removeClass( rowsClass ).addClass( rowsClass );
268                         el.removeClass( themeClass ).addClass( themeClass );
269                         el.removeClass( optionHeaderClass ).addClass( optionHeaderClass );
270
271                         // remove any arrow currently visible
272                         el.prev( '.ui-option-header-triangle-arrow' ).remove();
273         //        el.prev('.ui-triangle-container').remove();
274
275                         // if there are elements inside the option header
276                         // and this.options.showIndicator,
277                         // insert a triangle arrow as the first element inside the
278                         // optionheader div to show the header has hidden content
279                         if ( this.options.showIndicator ) {
280                                 el.before( arrow );
281                                 arrow.append("<div class='ui-triangle-image'></div>");
282         //            arrow.triangle({"color": el.css('background-color'), offset: "50%"});
283                         }
284
285                 // if expandable, bind clicks to the toggle() method
286                         if ( this.options.expandable ) {
287         //            el.unbind('vclick', this.clickHandler).bind('vclick', this.clickHandler);
288         //            arrow.unbind('vclick', this.clickHandler).bind('vclick', this.clickHandler);
289                                 el.bind( 'vclick', this.clickHandler );
290                                 arrow.bind( 'vclick', this.clickHandler );
291
292                         } else {
293                                 el.unbind( 'vclick', this.clickHandler );
294                                 arrow.unbind( 'vclick', this.clickHandler );
295                         }
296
297                         // for each ui-grid-a element, add a class ui-option-header-row-M
298                         // to it, where M is the xpath position() of the div
299         /*        el.find(gridRowSelector).each(function (index) {
300                     var klass = 'ui-option-header-row-' + (index + 1);
301                     $(this).removeClass(klass).addClass(klass);
302                 });*/
303                         klass = 'ui-option-header-row-' + ( numRows );
304                         el.find( "div" ).eq( 0 ).removeClass( klass ).addClass( klass );
305
306                         // redraw the buttons (now that the optionheader has the right
307                         // swatch)
308                         el.find( '.ui-btn' ).each(function () {
309                                 $( this ).attr( 'data-' + $.mobile.ns + 'theme', theme );
310
311                                 // hack the class of the button to remove the old swatch
312                                 var klass = $( this ).attr( 'class' );
313                                 klass = klass.replace(/ui-btn-up-\w{1}\s*/, '');
314                                 klass = klass + ' ui-btn-up-' + theme;
315                                 $( this ).attr( 'class', klass );
316                         });
317                 },
318
319                 _setHeight: function ( height, isCollapsed, options ) {
320                         var self = this,
321                                 elt = this.element.get( 0 ),
322                                 duration,
323                                 commonCallback,
324                                 callback,
325                                 handler;
326
327                         options = options || {};
328
329                         // set default duration if not specified
330                         duration = options.duration;
331                         if ( typeof duration == 'undefined' ) {
332                                 duration = this.options.duration;
333                         }
334
335                         // the callback to always call after expanding or collapsing
336                         commonCallback = function () {
337                                 self.isCollapsed = isCollapsed;
338
339                                 if ( isCollapsed ) {
340                                         self.element.trigger( 'collapse' );
341                                 } else {
342                                         self.element.trigger( 'expand' );
343                                 }
344                         };
345
346                         // combine commonCallback with any user-specified callback
347                         if ( options.callback ) {
348                                 callback = function () {
349                                         options.callback();
350                                         commonCallback();
351                                 };
352                         } else {
353                                 callback = function () {
354                                         commonCallback();
355                                 };
356                         }
357
358                         // apply the animation
359                         if ( duration > 0 && $.support.cssTransitions ) {
360                                 // add a handler to invoke a callback when the animation is done
361
362                                 handler = {
363                                         handleEvent: function ( e ) {
364                                                 elt.removeEventListener( 'webkitTransitionEnd', this );
365                                                 self.element.css( '-webkit-transition', null );
366                                                 callback();
367                                         }
368                                 };
369
370                                 elt.addEventListener( 'webkitTransitionEnd', handler, false );
371
372                                 // apply the transition
373                                 this.element.css( '-webkit-transition', 'height ' + duration + 's ease-out' );
374                                 this.element.css( 'height', height );
375                         } else {
376                         // make sure the callback gets called even when there's no
377                         // animation
378                                 this.element.css( 'height', height );
379                                 callback();
380                         }
381                 },
382
383                 /**
384                 * Toggle the expanded/collapsed state of the widget.
385                 * {Object} [options] Configuration for the expand/collapse
386                 * {Integer} [options.duration] Duration of the expand/collapse;
387                 * defaults to this.options.duration
388                 * {Function} options.callback Function to call after toggle completes
389                 */
390
391                 toggle: function ( options ) {
392                         var toggle_header = this.element.parents( ":jqmData(role='header')" ),
393                                 toggle_content = this.element.parents( ":jqmData(role='page')" ).find( ".ui-content" ),
394                                 CollapsedTop = 110,
395                                 ExpandedTop = 206,
396                                 CalculateTime,
397                                 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
398
399                         if ( toggle_header.children().is( ".input-search-bar" ) ) {
400                                 CollapsedTop = 218;
401                                 ExpandedTop = 314;
402                         }
403
404                         /* Scale Factor */
405                         CollapsedTop = ( CollapsedTop / scaleFactor );
406                         ExpandedTop = ( ExpandedTop / scaleFactor );
407
408                         if ( $( window ).scrollTop() <= CollapsedTop ) {
409                                 toggle_header.css( "position", "relative" );
410                                 toggle_content.css( "top", "0px" );
411                         }
412
413                         if ( this.isCollapsed ) {
414                                 this.expand( options );
415
416                                 if ( $( window ).scrollTop() <= ExpandedTop ) {
417                                         CalculateTime = setTimeout( function () {
418                                                 toggle_header.css( 'position', 'fixed' );
419                                                 toggle_content.css( 'top', ExpandedTop + "px" );
420                                         }, 500 );
421                                 } else {
422                                         //   Need to move scroll top
423                                         toggle_header.css( 'position', 'fixed' );
424                                         toggle_content.css( 'top', ExpandedTop + "px" );
425                                 }
426                                 this.options.collapseOnInit = false;
427                         } else {
428                                 this.collapse( options );
429                                 if ( $(window).scrollTop() <= ExpandedTop ) {
430                                         CalculateTime = setTimeout( function () {
431                                                 toggle_header.css( 'position', 'fixed' );
432                                                 toggle_content.css( 'top', CollapsedTop + "px" );
433                                         }, 500 );
434                                 } else {
435                                         toggle_header.css( 'position', 'fixed' );
436                                         toggle_content.css( 'top', CollapsedTop + "px" );
437                                 }
438                         }
439                         this.options.collapseOnInit = true;
440                 },
441
442                 _setDisabled: function ( value ) {
443                         $.Widget.prototype._setOption.call( this, "disabled", value );
444                         this.element.add( this.element.prev( ".ui-triangle-container" ) )[value ? "addClass" : "removeClass"]("ui-disabled");
445                 },
446                 /**
447                 * Takes the same options as toggle()
448                 */
449                 collapse: function ( options ) {
450                         var collapsedBarHeight = 10,
451                                 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
452
453                         collapsedBarHeight = collapsedBarHeight / scaleFactor;
454
455         //        if (!this.isCollapsed) {
456                         this._setHeight( collapsedBarHeight + "px", true, options );
457         //        }
458                 },
459
460                 /**
461                 * Takes the same options as toggle()
462                 */
463                 expand: function ( options ) {
464         //        if (this.isCollapsed) {
465                         this._setHeight( this.expandedHeight, false, options );
466         //        }
467                 }
468         });
469
470         // auto self-init widgets
471         $(document).bind("pagecreate create", function ( e ) {
472             $($.tizen.optionheader.prototype.options.initSelector, e.target)
473                         .not(":jqmData(role='none'), :jqmData(role='nojs')")
474                         .optionheader();
475         });
476
477 }(jQuery) );