Skip to content

Commit c88f5fc

Browse files
committed
Merge pull request jquery-archive#1781 from gseguin/nested-lists
Nested lists id generation
2 parents 9e696b8 + 888a3d4 commit c88f5fc

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

js/jquery.mobile.listview.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* http://jquery.org/license
66
*/
77
(function($, undefined ) {
8+
//Keeps track of the number of lists per page UID
9+
//This allows support for multiple nested list in the same page
10+
//https://github.com/jquery/jquery-mobile/issues/1617
11+
var listCountPerPage = {};
812

913
$.widget( "mobile.listview", $.mobile.widget, {
1014
options: {
@@ -189,23 +193,31 @@ $.widget( "mobile.listview", $.mobile.widget, {
189193
_idStringEscape: function( str ){
190194
return str.replace(/[^a-zA-Z0-9]/g, '-');
191195
},
192-
196+
193197
_createSubPages: function() {
194198
var parentList = this.element,
195199
parentPage = parentList.closest( ".ui-page" ),
196-
parentId = parentPage.jqmData( "url" ),
200+
parentUrl = parentPage.jqmData( "url" ),
201+
parentId = parentUrl || parentPage[ 0 ][ $.expando ],
202+
parentListId = parentList.attr( "id" ),
197203
o = this.options,
198204
dns = "data-" + $.mobile.ns,
199205
self = this,
200206
persistentFooterID = parentPage.find( ":jqmData(role='footer')" ).jqmData( "id" );
201207

208+
if ( typeof( listCountPerPage[ parentId ] ) === 'undefined' ) {
209+
listCountPerPage[ parentId ] = -1;
210+
}
211+
parentListId = parentListId || ++listCountPerPage[ parentId ];
212+
202213
$( parentList.find( "li>ul, li>ol" ).toArray().reverse() ).each(function( i ) {
203214
var list = $( this ),
215+
listId = list.attr( "id" ) || parentListId + "-" + i,
204216
parent = list.parent(),
205217
nodeEls = $( list.prevAll().toArray().reverse() ),
206218
nodeEls = nodeEls.length ? nodeEls : $( "<span>" + $.trim(parent.contents()[ 0 ].nodeValue) + "</span>" ),
207219
title = nodeEls.first().text(),//url limits to first 30 chars of text
208-
id = parentId + "&" + $.mobile.subPageUrlKey + "=" + self._idStringEscape(title + " " + i),
220+
id = ( parentUrl || "" ) + "&" + $.mobile.subPageUrlKey + "=" + listId;
209221
theme = list.jqmData( "theme" ) || o.theme,
210222
countTheme = list.jqmData( "counttheme" ) || parentList.jqmData( "counttheme" ) || o.countTheme,
211223
newPage = list.detach()

tests/unit/listview/listview_core.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363

6464
function(){
6565
ok($('#nested-list-test').hasClass('ui-page-active'), "makes nested list test page active");
66-
ok($(':jqmData(url="nested-list-test&ui-page=More-animals-0")').length == 1, "Adds first UL to the page");
67-
ok($(':jqmData(url="nested-list-test&ui-page=Groups-of-animals-1")').length == 1, "Adds second nested UL to the page");
66+
ok($(':jqmData(url="nested-list-test&ui-page=0-0")').length == 1, "Adds first UL to the page");
67+
ok($(':jqmData(url="nested-list-test&ui-page=0-1")').length == 1, "Adds second nested UL to the page");
6868
start();
6969
}
7070
]);
@@ -82,7 +82,7 @@
8282
},
8383

8484
function(){
85-
var $new_page = $(':jqmData(url="nested-list-test&ui-page=More-animals-0")');
85+
var $new_page = $(':jqmData(url="nested-list-test&ui-page=0-0")');
8686

8787
ok($new_page.hasClass('ui-page-active'), 'Makes the nested page the active page.');
8888
ok($('.ui-listview', $new_page).find(":contains('Rhumba of rattlesnakes')").length == 1, "The current page should have the proper text in the list.");
@@ -95,7 +95,7 @@
9595
asyncTest( "should go back to top level when the back button is clicked", function() {
9696
$.testHelper.pageSequence([
9797
function(){
98-
$.testHelper.openPage("#nested-list-test&ui-page=More-animals-0");
98+
$.testHelper.openPage("#nested-list-test&ui-page=0-0");
9999
},
100100

101101
function(){
@@ -113,16 +113,19 @@
113113
ok($('#nested-list-test .linebreaknode').text() === "More animals", 'Text should be "More animals"');
114114
});
115115

116-
asyncTest( "Multiple nested lists on a page", function() {
116+
asyncTest( "Multiple nested lists on a page with same labels", function() {
117117
$.testHelper.pageSequence([
118118
function(){
119119
// https://github.com/jquery/jquery-mobile/issues/1617
120120
$.testHelper.openPage("#nested-lists-test");
121121
},
122122

123123
function(){
124+
// Click on the link of the third li element
124125
$('.ui-page-active li:eq(2) a:eq(0)').click();
125-
126+
},
127+
128+
function(){
126129
equal($('.ui-page-active .ui-content .ui-listview li').text(), "Sub Item 10Sub Item 11Sub Item 12", 'Text should be "Sub Item 10Sub Item 11Sub Item 12"');
127130
start();
128131
}

0 commit comments

Comments
 (0)