-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autodividers functionality for listviews
Can be used to autogenerate dividers for a listview by setting data-autodividers="alpha" (dividers are unique, uppercased single characters from list items) or data-autodividers="full" (unique full text strings selected from list items) on the ul element of the listview. It's also possible to apply a custom selector to find the elements to be used for divider text, with data-autodividers-selector="...". This relates to jquery-archive/jquery-mobile#2466, but is a different implementation which will automatically update the list dividers if the list elements change. It also has a fairly thorough test suite and documentation.
- Loading branch information
1 parent
9c5135a
commit 8e499dd
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); | ||
//>>description: Generates dividers for listview items | ||
//>>label: Listview Autodividers | ||
define( [ "jquery", "./jquery.mobile.listview" ], function( $ ) { | ||
//>>excludeEnd("jqmBuildExclude"); | ||
(function( $, undefined ) { | ||
|
||
$.mobile.listview.prototype.options.autodividers = false; | ||
$.mobile.listview.prototype.options.autodividersSelector = function( elt ) { | ||
// look for the first anchor in the item | ||
var text = elt.find( 'a' ).text() || elt.text() || null; | ||
|
||
if ( !text ) { | ||
return null; | ||
} | ||
|
||
// create the text for the divider (first uppercased letter) | ||
text = text.slice( 0, 1 ).toUpperCase(); | ||
|
||
return text; | ||
}; | ||
|
||
$( document ).on( "listviewcreate", "ul,ol", function() { | ||
|
||
var list = $( this ), | ||
listview = list.data( "listview" ); | ||
|
||
if ( !listview.options.autodividers ) { | ||
return; | ||
} | ||
|
||
var replaceDividers = function () { | ||
list.find( 'li:jqmData(role=list-divider)' ).remove(); | ||
|
||
var lis = list.find( 'li' ); | ||
|
||
var lastDividerText = null; | ||
var li; | ||
var dividerText; | ||
var i = 0; | ||
|
||
for ( i ; i < lis.length ; i++ ) { | ||
li = lis[i]; | ||
dividerText = listview.options.autodividersSelector( $( li ) ); | ||
|
||
if ( dividerText && lastDividerText !== dividerText ) { | ||
var divider = document.createElement( 'li' ); | ||
divider.appendChild( document.createTextNode( dividerText ) ); | ||
divider.setAttribute( 'data-' + $.mobile.ns + 'role', 'list-divider' ); | ||
li.parentNode.insertBefore( divider, li ); | ||
} | ||
|
||
lastDividerText = dividerText; | ||
} | ||
}; | ||
|
||
var afterListviewRefresh = function () { | ||
list.off( 'listviewafterrefresh', afterListviewRefresh ); | ||
replaceDividers(); | ||
listview.refresh(); | ||
list.on( 'listviewafterrefresh', afterListviewRefresh ); | ||
}; | ||
|
||
afterListviewRefresh(); | ||
|
||
}); | ||
|
||
})( jQuery ); | ||
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); | ||
}); | ||
//>>excludeEnd("jqmBuildExclude"); |