|
| 1 | +//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); |
| 2 | +//>>description: Generates dividers for listview items |
| 3 | +//>>label: Listview Autodividers |
| 4 | +define( [ "jquery", "./jquery.mobile.listview" ], function( $ ) { |
| 5 | +//>>excludeEnd("jqmBuildExclude"); |
| 6 | +(function( $, undefined ) { |
| 7 | + |
| 8 | +$.mobile.listview.prototype.options.autodividers = false; |
| 9 | +$.mobile.listview.prototype.options.autodividersSelector = function( elt ) { |
| 10 | + // look for the first anchor in the item |
| 11 | + var text = elt.find( 'a' ).text() || elt.text() || null; |
| 12 | + |
| 13 | + if ( !text ) { |
| 14 | + return null; |
| 15 | + } |
| 16 | + |
| 17 | + // create the text for the divider (first uppercased letter) |
| 18 | + text = text.slice( 0, 1 ).toUpperCase(); |
| 19 | + |
| 20 | + return text; |
| 21 | +}; |
| 22 | + |
| 23 | +$( document ).on( "listviewcreate", "ul,ol", function() { |
| 24 | + |
| 25 | + var list = $( this ), |
| 26 | + listview = list.data( "listview" ); |
| 27 | + |
| 28 | + if ( !listview.options.autodividers ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + var replaceDividers = function () { |
| 33 | + list.find( 'li:jqmData(role=list-divider)' ).remove(); |
| 34 | + |
| 35 | + var lis = list.find( 'li' ); |
| 36 | + |
| 37 | + var lastDividerText = null; |
| 38 | + var li; |
| 39 | + var dividerText; |
| 40 | + var i = 0; |
| 41 | + |
| 42 | + for ( i ; i < lis.length ; i++ ) { |
| 43 | + li = lis[i]; |
| 44 | + dividerText = listview.options.autodividersSelector( $( li ) ); |
| 45 | + |
| 46 | + if ( dividerText && lastDividerText !== dividerText ) { |
| 47 | + var divider = document.createElement( 'li' ); |
| 48 | + divider.appendChild( document.createTextNode( dividerText ) ); |
| 49 | + divider.setAttribute( 'data-' + $.mobile.ns + 'role', 'list-divider' ); |
| 50 | + li.parentNode.insertBefore( divider, li ); |
| 51 | + } |
| 52 | + |
| 53 | + lastDividerText = dividerText; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + var afterListviewRefresh = function () { |
| 58 | + list.off( 'listviewafterrefresh', afterListviewRefresh ); |
| 59 | + replaceDividers(); |
| 60 | + listview.refresh(); |
| 61 | + list.on( 'listviewafterrefresh', afterListviewRefresh ); |
| 62 | + }; |
| 63 | + |
| 64 | + afterListviewRefresh(); |
| 65 | + |
| 66 | +}); |
| 67 | + |
| 68 | +})( jQuery ); |
| 69 | +//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); |
| 70 | +}); |
| 71 | +//>>excludeEnd("jqmBuildExclude"); |
0 commit comments