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: Elliot Smith <elliot.smith@intel.com>
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,
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:
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>
53 // The optionheader can also be used inline (e.g. in a content block or
56 // Alternatively, use $('...').optionheader() to apply programmatically.
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.)
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.
68 // The widget is configurable via a data-options attribute on the same
69 // div as the data-role="optionheader" attribute, e.g.
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>
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).
87 // See below for the available options.
89 // Theme: by default, gets a 'b' swatch; override with data-theme="X"
92 // Options (can be set with a data-options attribute):
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
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.
105 // Methods (see below for docs):
113 // expand: Triggered when the option header is expanded
114 // collapse: Triggered when the option header is collapsed
118 (function ($, undefined) {
119 $.widget("tizen.optionheader", $.mobile.widget, {
121 initSelector: ":jqmData(role='optionheader')",
124 startCollapsed: false,
127 collapseOnInit : true,
128 default_font_size : $('html').css('font-size')
130 collapsedHeight: '5px',
132 _create: function () {
138 dataOptions = this.element.jqmData( 'options' ),
139 page = this.element.closest( ':jqmData(role="page")' );
140 // parse data-options
141 $.extend( this.options, dataOptions );
143 this.isCollapsed = this.options.collapseOnInit;
144 this.expandedHeight = null;
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;
150 this.element.closest( ':jqmData(role="header")' ).addClass( "ui-option-header-resizing" );
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 () {
159 /* Apply REM scaling */
160 elementHeight = elementHeight / ( 36 / parseInt( $('html').css('font-size'), 10 ) );
162 if ( this.element.height() < elementHeight ) {
163 this.element.css( "height", elementHeight );
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)
171 if ( page.is(":visible") ) {
177 page.bind( "pagebeforeshow", function () {
178 self._setArrowLeft();
182 self._setArrowLeft();
186 _realize: function () {
187 if ( !this.expandedHeight ) {
188 this.expandedHeight = this.element.height();
191 if ( this.isCollapsed ) {
192 // if (this.options.startCollapsed) {
193 this.collapse( {duration: 0} );
197 _setArrowLeft: function () {
198 var matchingBtn = $( this.element ).jqmData( "for" ),
200 matchBtn = $( this.element ).parents( ".ui-page" ).find( "#" + matchingBtn ),
201 siblingBtnCnt = matchBtn.prevAll(".ui-btn-right").length,
202 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
205 if ( $(this.element).parents(".ui-page").find( "#" + matchingBtn ).length != 0 ) {
206 if ( this.options.expandable ) {
207 matchBtn.bind( 'vclick', this.clickHandler );
209 matchBtn.unbind( 'vclick', this.clickHandler );
212 $( ".ui-triangle-image" ).css( "right", ( matchBtn.width() / 2 + matchBtn.width() * siblingBtnCnt - ( arrowCenter / scaleFactor ) ) + "px");
213 $( ".ui-triangle-image" ).css( "left", "auto" );
216 // Draw the option header, according to current options
217 refresh: function () {
218 var el = this.element,
219 arrow = $( '<div class="ui-option-header-triangle-arrow"></div>' ),
220 optionHeaderClass = 'ui-option-header',
221 gridRowSelector = '.ui-grid-a,.ui-grid-b,.ui-grid-c,.ui-grid-d,.ui-grid-e',
222 theme = this.options.theme,
227 o = $.extend( {grid: null} ),
228 $kids = el.find( "div" ).eq( 0 ).children().children(),
230 gridCols = {solo: 1, a: 2, b: 3, c: 4, d: 5},
234 if ( $kids.length <= 5 ) {
235 for ( letter in gridCols ) {
236 if ( gridCols[ letter ] === $kids.length ) {
240 numRows = $kids.length / gridCols[grid];
246 // count ui-grid-* elements to get number of rows
247 // numRows = el.find(gridRowSelector).length;
249 // ...at least one row
250 // numRows = Math.max(1, numRows);
252 // add classes to outer div:
253 // ui-option-header-N-row, where N = options.rows
254 // ui-bar-X, where X = options.theme (defaults to 'c')
256 rowsClass = 'ui-option-header-' + numRows + '-row';
257 themeClass = 'ui-body-' + this.options.theme;
259 el.removeClass( rowsClass ).addClass( rowsClass );
260 el.removeClass( themeClass ).addClass( themeClass );
261 el.removeClass( optionHeaderClass ).addClass( optionHeaderClass );
263 // remove any arrow currently visible
264 el.prev( '.ui-option-header-triangle-arrow' ).remove();
265 // el.prev('.ui-triangle-container').remove();
267 // if there are elements inside the option header
268 // and this.options.showIndicator,
269 // insert a triangle arrow as the first element inside the
270 // optionheader div to show the header has hidden content
271 if ( this.options.showIndicator ) {
273 arrow.append("<div class='ui-triangle-image'></div>");
274 // arrow.triangle({"color": el.css('background-color'), offset: "50%"});
277 // if expandable, bind clicks to the toggle() method
278 if ( this.options.expandable ) {
279 // el.unbind('vclick', this.clickHandler).bind('vclick', this.clickHandler);
280 // arrow.unbind('vclick', this.clickHandler).bind('vclick', this.clickHandler);
281 el.bind( 'vclick', this.clickHandler );
282 arrow.bind( 'vclick', this.clickHandler );
285 el.unbind( 'vclick', this.clickHandler );
286 arrow.unbind( 'vclick', this.clickHandler );
289 // for each ui-grid-a element, add a class ui-option-header-row-M
290 // to it, where M is the xpath position() of the div
291 /* el.find(gridRowSelector).each(function (index) {
292 var klass = 'ui-option-header-row-' + (index + 1);
293 $(this).removeClass(klass).addClass(klass);
295 klass = 'ui-option-header-row-' + ( numRows );
296 el.find( "div" ).eq( 0 ).removeClass( klass ).addClass( klass );
298 // redraw the buttons (now that the optionheader has the right
300 el.find( '.ui-btn' ).each(function () {
301 $( this ).attr( 'data-' + $.mobile.ns + 'theme', theme );
303 // hack the class of the button to remove the old swatch
304 var klass = $( this ).attr( 'class' );
305 klass = klass.replace(/ui-btn-up-\w{1}\s*/, '');
306 klass = klass + ' ui-btn-up-' + theme;
307 $( this ).attr( 'class', klass );
311 _setHeight: function ( height, isCollapsed, options ) {
313 elt = this.element.get( 0 ),
319 options = options || {};
321 // set default duration if not specified
322 duration = options.duration;
323 if ( typeof duration == 'undefined' ) {
324 duration = this.options.duration;
327 // the callback to always call after expanding or collapsing
328 commonCallback = function () {
329 self.isCollapsed = isCollapsed;
332 self.element.trigger( 'collapse' );
334 self.element.trigger( 'expand' );
338 // combine commonCallback with any user-specified callback
339 if ( options.callback ) {
340 callback = function () {
345 callback = function () {
350 // apply the animation
351 if ( duration > 0 && $.support.cssTransitions ) {
352 // add a handler to invoke a callback when the animation is done
355 handleEvent: function ( e ) {
356 elt.removeEventListener( 'webkitTransitionEnd', this );
357 self.element.css( '-webkit-transition', null );
362 elt.addEventListener( 'webkitTransitionEnd', handler, false );
364 // apply the transition
365 this.element.css( '-webkit-transition', 'height ' + duration + 's ease-out' );
366 this.element.css( 'height', height );
368 // make sure the callback gets called even when there's no
370 this.element.css( 'height', height );
376 * Toggle the expanded/collapsed state of the widget.
377 * {Object} [options] Configuration for the expand/collapse
378 * {Integer} [options.duration] Duration of the expand/collapse;
379 * defaults to this.options.duration
380 * {Function} options.callback Function to call after toggle completes
383 toggle: function ( options ) {
384 var toggle_header = this.element.parents( ":jqmData(role='header')" ),
385 toggle_content = this.element.parents( ":jqmData(role='page')" ).find( ".ui-content" ),
389 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
391 if ( toggle_header.children().is( ".input-search-bar" ) ) {
397 CollapsedTop = ( CollapsedTop / scaleFactor );
398 ExpandedTop = ( ExpandedTop / scaleFactor );
400 if ( $( window ).scrollTop() <= CollapsedTop ) {
401 /* toggle_header.css( "position", "relative" );
402 toggle_content.css( "top", "0px" ); */
403 /* tizen beta request : optionheader remove slide */
406 if ( this.isCollapsed ) {
407 this.expand( options );
409 if ( $( window ).scrollTop() <= ExpandedTop ) {
410 CalculateTime = setTimeout( function () {
411 /* toggle_header.css( 'position', 'fixed' );
412 toggle_content.css( 'top', ExpandedTop + "px" );*/
413 /* tizen beta request : optionheader remove slide */
416 // Need to move scroll top
417 toggle_header.css( 'position', 'fixed' );
418 toggle_content.css( 'top', ExpandedTop + "px" );
420 this.options.collapseOnInit = false;
422 this.collapse( options );
423 if ( $(window).scrollTop() <= ExpandedTop ) {
424 CalculateTime = setTimeout( function () {
425 /* toggle_header.css( 'position', 'fixed' );
426 toggle_content.css( 'top', CollapsedTop + "px" );*/
427 /* tizen beta request : optionheader remove slide */
430 toggle_header.css( 'position', 'fixed' );
431 toggle_content.css( 'top', CollapsedTop + "px" );
434 this.options.collapseOnInit = true;
437 _setDisabled: function ( value ) {
438 $.Widget.prototype._setOption.call( this, "disabled", value );
439 this.element.add( this.element.prev( ".ui-triangle-container" ) )[value ? "addClass" : "removeClass"]("ui-disabled");
442 * Takes the same options as toggle()
444 collapse: function ( options ) {
445 var collapsedBarHeight = 10,
446 scaleFactor = ( 36 / parseInt( $('html').css('font-size'), 10 ) );
448 collapsedBarHeight = collapsedBarHeight / scaleFactor;
450 // if (!this.isCollapsed) {
451 this._setHeight( collapsedBarHeight + "px", true, options );
456 * Takes the same options as toggle()
458 expand: function ( options ) {
459 // if (this.isCollapsed) {
460 this._setHeight( this.expandedHeight, false, options );
465 // auto self-init widgets
466 $(document).bind("pagecreate create", function ( e ) {
467 $($.tizen.optionheader.prototype.options.initSelector, e.target)
468 .not(":jqmData(role='none'), :jqmData(role='nojs')")