forked from jquery-archive/jquery-mobile
/
listview_core.js
executable file
·767 lines (628 loc) · 20.9 KB
/
listview_core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
* mobile listview unit tests
*/
// TODO split out into seperate test files
(function($){
var home = $.mobile.path.parseUrl( location.href ).pathname;
$.mobile.defaultTransition = "none";
module( "Basic Linked list", {
setup: function(){
$.testHelper.openPage( "#basic-linked-test" );
}
});
asyncTest( "The page should enhanced correctly", function(){
setTimeout(function() {
ok($('#basic-linked-test .ui-li').length, ".ui-li classes added to li elements");
start();
}, 800);
});
asyncTest( "Slides to the listview page when the li a is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#basic-linked-test");
},
function(){
$('#basic-linked-test li a').first().click();
},
function(){
ok($('#basic-link-results').hasClass('ui-page-active'));
start();
}
]);
});
asyncTest( "Slides back to main page when back button is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#basic-link-results");
},
function(){
window.history.back();
},
function(){
ok($('#basic-linked-test').hasClass('ui-page-active'));
start();
}
]);
});
asyncTest( "Presence of ui-li-has- classes", function(){
$.testHelper.pageSequence( [
function() {
$.testHelper.openPage( "#ui-li-has-test" );
},
function() {
var page = $( ".ui-page-active" ),
items = page.find( "li" );
ok( items.eq( 0 ).hasClass( "ui-li-has-count"), "First LI should have ui-li-has-count class" );
ok( items.eq( 0 ).hasClass( "ui-li-has-arrow"), "First LI should have ui-li-has-arrow class" );
ok( !items.eq( 1 ).hasClass( "ui-li-has-count"), "Second LI should NOT have ui-li-has-count class" );
ok( items.eq( 1 ).hasClass( "ui-li-has-arrow"), "Second LI should have ui-li-has-arrow class" );
ok( !items.eq( 2 ).hasClass( "ui-li-has-count"), "Third LI should NOT have ui-li-has-count class" );
ok( !items.eq( 2 ).hasClass( "ui-li-has-arrow"), "Third LI should NOT have ui-li-has-arrow class" );
ok( items.eq( 3 ).hasClass( "ui-li-has-count"), "Fourth LI should have ui-li-has-count class" );
ok( !items.eq( 3 ).hasClass( "ui-li-has-arrow"), "Fourth LI should NOT have ui-li-has-arrow class" );
ok( !items.eq( 4 ).hasClass( "ui-li-has-count"), "Fifth LI should NOT have ui-li-has-count class" );
ok( !items.eq( 4 ).hasClass( "ui-li-has-arrow"), "Fifth LI should NOT have ui-li-has-arrow class" );
start();
}
]);
});
module('Nested List Test');
asyncTest( "Changes page to nested list test and enhances", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#nested-list-test");
},
function(){
ok($('#nested-list-test').hasClass('ui-page-active'), "makes nested list test page active");
ok($(':jqmData(url="nested-list-test&ui-page=0-0")').length == 1, "Adds first UL to the page");
ok($(':jqmData(url="nested-list-test&ui-page=0-1")').length == 1, "Adds second nested UL to the page");
start();
}
]);
});
asyncTest( "change to nested page when the li a is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#nested-list-test");
},
function(){
$('.ui-page-active li:eq(1) a:eq(0)').click();
},
function(){
var $new_page = $(':jqmData(url="nested-list-test&ui-page=0-0")');
ok($new_page.hasClass('ui-page-active'), 'Makes the nested page the active page.');
ok($('.ui-listview', $new_page).find(":contains('Rhumba of rattlesnakes')").length == 1, "The current page should have the proper text in the list.");
ok($('.ui-listview', $new_page).find(":contains('Shoal of Bass')").length == 1, "The current page should have the proper text in the list.");
start();
}
]);
});
asyncTest( "should go back to top level when the back button is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#nested-list-test&ui-page=0-0");
},
function(){
window.history.back();
},
function(){
ok($('#nested-list-test').hasClass('ui-page-active'), 'Transitions back to the parent nested page');
start();
}
]);
});
test( "nested list title should use first text node, regardless of line breaks", function(){
ok($('#nested-list-test .linebreaknode').text() === "More animals", 'Text should be "More animals"');
});
asyncTest( "Multiple nested lists on a page with same labels", function() {
$.testHelper.pageSequence([
function(){
// https://github.com/jquery/jquery-mobile/issues/1617
$.testHelper.openPage("#nested-lists-test");
},
function(){
// Click on the link of the third li element
$('.ui-page-active li:eq(2) a:eq(0)').click();
},
function(){
equal($('.ui-page-active .ui-content .ui-listview li').text(), "Item A-3-0Item A-3-1Item A-3-2", 'Text should be "Item A-3-0Item A-3-1Item A-3-2"');
start();
}
]);
});
module('Ordered Lists');
asyncTest( "changes to the numbered list page and enhances it", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#numbered-list-test");
},
function(){
var $new_page = $('#numbered-list-test');
ok($new_page.hasClass('ui-page-active'), "Makes the new page active when the hash is changed.");
ok($('.ui-link-inherit', $new_page).first().text() == "Number 1", "The text of the first LI should be Number 1");
start();
}
]);
});
asyncTest( "changes to number 1 page when the li a is clicked", function() {
$.testHelper.pageSequence([
function(){
$('#numbered-list-test li a').first().click();
},
function(){
ok($('#numbered-list-results').hasClass('ui-page-active'), "The new numbered page was transitioned correctly.");
start();
}
]);
});
asyncTest( "takes us back to the numbered list when the back button is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage('#numbered-list-test');
},
function(){
$.testHelper.openPage('#numbered-list-results');
},
function(){
window.history.back();
},
function(){
ok($('#numbered-list-test').hasClass('ui-page-active'));
start();
}
]);
});
module('Read only list');
asyncTest( "changes to the read only page when hash is changed", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#read-only-list-test");
},
function(){
var $new_page = $('#read-only-list-test');
ok($new_page.hasClass('ui-page-active'), "makes the read only page the active page");
ok($('li', $new_page).first().text() === "Read", "The first LI has the proper text.");
start();
}
]);
});
module('Split view list');
asyncTest( "changes the page to the split view list and enhances it correctly.", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#split-list-test");
},
function(){
var $new_page = $('#split-list-test');
ok($('.ui-li-link-alt', $new_page).length == 3);
ok($('.ui-link-inherit', $new_page).length == 3);
start();
}
]);
});
asyncTest( "change the page to the split view page 1 when the first link is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#split-list-test");
},
function(){
$('.ui-page-active .ui-li a:eq(0)').click();
},
function(){
ok($('#split-list-link1').hasClass('ui-page-active'));
start();
}
]);
});
asyncTest( "Slide back to the parent list view when the back button is clicked", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#split-list-test");
},
function(){
$('.ui-page-active .ui-listview a:eq(0)').click();
},
function(){
history.back();
},
function(){
ok($('#split-list-test').hasClass('ui-page-active'));
start();
}
]);
});
asyncTest( "Clicking on the icon (the second link) should take the user to other a href of this LI", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#split-list-test");
},
function(){
$('.ui-page-active .ui-li-link-alt:eq(0)').click();
},
function(){
ok($('#split-list-link2').hasClass('ui-page-active'));
start();
}
]);
});
module( "List Dividers" );
asyncTest( "Makes the list divider page the active page and enhances it correctly.", function() {
$.testHelper.pageSequence([
function(){
$.testHelper.openPage("#list-divider-test");
},
function(){
var $new_page = $('#list-divider-test');
ok($new_page.find('.ui-li-divider').length == 2);
ok($new_page.hasClass('ui-page-active'));
start();
}
]);
});
module( "Search Filter");
var searchFilterId = "#search-filter-test";
asyncTest( "Filter downs results when the user enters information", function() {
var $searchPage = $(searchFilterId);
$.testHelper.pageSequence([
function() {
$.testHelper.openPage(searchFilterId);
},
function() {
$searchPage.find('input').val('at');
$searchPage.find('input').trigger('change');
same($searchPage.find('li.ui-screen-hidden').length, 2);
start();
}
]);
});
asyncTest( "Redisplay results when user removes values", function() {
var $searchPage = $(searchFilterId);
$.testHelper.pageSequence([
function() {
$.testHelper.openPage(searchFilterId);
},
function() {
$searchPage.find('input').val('a');
$searchPage.find('input').trigger('change');
same($searchPage.find("li[style^='display: none;']").length, 0);
start();
}
]);
});
asyncTest( "Filter works fine with \\W- or regexp-special-characters", function() {
var $searchPage = $(searchFilterId);
$.testHelper.pageSequence([
function() {
$.testHelper.openPage(searchFilterId);
},
function() {
$searchPage.find('input').val('*');
$searchPage.find('input').trigger('change');
same($searchPage.find('li.ui-screen-hidden').length, 4);
start();
}
]);
});
test( "Refresh applies thumb styling", function(){
var ul = $('.ui-page-active ul');
ul.append("<li id='fiz'><img/></li>");
ok(!ul.find("#fiz img").hasClass("ui-li-thumb"));
ul.listview('refresh');
ok(ul.find("#fiz img").hasClass("ui-li-thumb"));
});
asyncTest( "Filter downs results and dividers when the user enters information", function() {
var $searchPage = $("#search-filter-with-dividers-test");
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#search-filter-with-dividers-test");
},
// wait for the page to become active/enhanced
function(){
$searchPage.find('input').val('at');
$searchPage.find('input').trigger('change');
setTimeout(function() {
//there should be four hidden list entries
same($searchPage.find('li.ui-screen-hidden').length, 4);
//there should be two list entries that are list dividers and hidden
same($searchPage.find('li.ui-screen-hidden:jqmData(role=list-divider)').length, 2);
//there should be two list entries that are not list dividers and hidden
same($searchPage.find('li.ui-screen-hidden:not(:jqmData(role=list-divider))').length, 2);
start();
}, 1000);
}
]);
});
asyncTest( "Redisplay results when user removes values", function() {
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#search-filter-with-dividers-test");
},
function() {
$('.ui-page-active input').val('a');
$('.ui-page-active input').trigger('change');
setTimeout(function() {
same($('.ui-page-active input').val(), 'a');
same($('.ui-page-active li[style^="display: none;"]').length, 0);
start();
}, 1000);
}
]);
});
asyncTest( "Dividers are hidden when preceding hidden rows and shown when preceding shown rows", function () {
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#search-filter-with-dividers-test");
},
function() {
var $page = $('.ui-page-active');
$page.find('input').val('at');
$page.find('input').trigger('change');
setTimeout(function() {
same($page.find('li:jqmData(role=list-divider):hidden').length, 2);
same($page.find('li:jqmData(role=list-divider):hidden + li:not(:jqmData(role=list-divider)):hidden').length, 2);
same($page.find('li:jqmData(role=list-divider):not(:hidden) + li:not(:jqmData(role=list-divider)):not([:hidden)').length, 2);
start();
}, 1000);
}
]);
});
asyncTest( "Inset List View should refresh corner classes after filtering", 4 * 2, function () {
var checkClasses = function() {
var $page = $( ".ui-page-active" ),
$li = $page.find( "li:visible" );
ok($li.first().hasClass( "ui-corner-top" ), $li.length+" li elements: First visible element should have class ui-corner-top");
ok($li.last().hasClass( "ui-corner-bottom" ), $li.length+" li elements: Last visible element should have class ui-corner-bottom");
};
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#search-filter-inset-test");
},
function() {
var $page = $('.ui-page-active');
$.testHelper.sequence([
function() {
checkClasses();
$page.find('input').val('man');
$page.find('input').trigger('change');
},
function() {
checkClasses();
$page.find('input').val('at');
$page.find('input').trigger('change');
},
function() {
checkClasses();
$page.find('input').val('catwoman');
$page.find('input').trigger('change');
},
function() {
checkClasses();
start();
}
], 50);
}
]);
});
module( "Programmatically generated list items", {
setup: function(){
var item,
data = [
{
id: 1,
label: "Item 1"
},
{
id: 2,
label: "Item 2"
},
{
id: 3,
label: "Item 3"
},
{
id: 4,
label: "Item 4"
}
];
$( "#programmatically-generated-list-items" ).html("");
for ( var i = 0, len = data.length; i < len; i++ ) {
item = $( '<li id="myItem' + data[i].id + '">' );
label = $( "<strong>" + data[i].label + "</strong>").appendTo( item );
$( "#programmatically-generated-list-items" ).append( item );
}
}
});
asyncTest( "Corner styling on programmatically created list items", function() {
// https://github.com/jquery/jquery-mobile/issues/1470
$.testHelper.pageSequence([
function() {
$.testHelper.openPage( "#programmatically-generated-list" );
},
function() {
ok(!$( "#programmatically-generated-list-items li:first-child" ).hasClass( "ui-corner-bottom" ), "First list item should not have class ui-corner-bottom" );
start();
}
]);
});
module("Programmatic list items manipulation");
asyncTest("Removing list items", 4, function() {
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#removing-items-from-list-test");
},
function() {
var ul = $('#removing-items-from-list-test ul');
ul.find("li").first().remove();
equal(ul.find("li").length, 3, "There should be only 3 list items left");
ul.listview('refresh');
ok(ul.find("li").first().hasClass("ui-corner-top"), "First list item should have class ui-corner-top");
ul.find("li").last().remove();
equal(ul.find("li").length, 2, "There should be only 2 list items left");
ul.listview('refresh');
ok(ul.find("li").last().hasClass("ui-corner-bottom"), "Last list item should have class ui-corner-bottom");
start();
}
]);
});
module("Rounded corners");
asyncTest("Top and bottom corners rounded in inset list", 14, function() {
$.testHelper.pageSequence([
function() {
$.testHelper.openPage("#corner-rounded-test");
},
function() {
var ul = $('#corner-rounded-test ul');
for( var t = 0; t<3; t++){
ul.append("<li>Item " + t + "</li>");
ul.listview('refresh');
equals(ul.find(".ui-corner-top").length, 1, "There should be only one element with class ui-corner-top");
equals(ul.find("li:visible").first()[0], ul.find(".ui-corner-top")[0], "First list item should have class ui-corner-top in list with " + ul.find("li").length + " item(s)");
equals(ul.find(".ui-corner-bottom").length, 1, "There should be only one element with class ui-corner-bottom");
equals(ul.find("li:visible").last()[0], ul.find(".ui-corner-bottom")[0], "Last list item should have class ui-corner-bottom in list with " + ul.find("li").length + " item(s)");
}
ul.find( "li" ).first().hide();
ul.listview( "refresh" );
equals(ul.find("li:visible").first()[0], ul.find(".ui-corner-top")[0], "First visible list item should have class ui-corner-top");
ul.find( "li" ).last().hide();
ul.listview( "refresh" );
equals(ul.find("li:visible").last()[0], ul.find(".ui-corner-bottom")[0], "Last visible list item should have class ui-corner-bottom");
start();
}
]);
});
test( "Listview will create when inside a container that receives a 'create' event", function(){
ok( !$("#enhancetest").appendTo(".ui-page-active").find(".ui-listview").length, "did not have enhancements applied" );
ok( $("#enhancetest").trigger("create").find(".ui-listview").length, "enhancements applied" );
});
module( "Cached Linked List" );
var findNestedPages = function(selector){
return $( selector + " #topmost" ).listview( 'childPages' );
};
asyncTest( "nested pages are removed from the dom by default", function(){
$.testHelper.pageSequence([
function(){
//reset for relative url refs
$.testHelper.openPage( "#" + home );
},
function(){
$.testHelper.openPage( "#cache-tests/uncached-nested.html" );
},
function(){
ok( findNestedPages( "#uncached-nested-list" ).length > 0, "verify that there are nested pages" );
$.testHelper.openPage( "#" + home );
},
function() {
$.testHelper.openPage( "#cache-tests/clear.html" );
},
function(){
same( findNestedPages( "#uncached-nested-list" ).length, 0 );
start();
}
]);
});
asyncTest( "nested pages preserved when parent page is cached", function(){
$.testHelper.pageSequence([
function(){
//reset for relative url refs
$.testHelper.openPage( "#" + home );
},
function(){
$.testHelper.openPage( "#cache-tests/cached-nested.html" );
},
function(){
ok( findNestedPages( "#cached-nested-list" ).length > 0, "verify that there are nested pages" );
$.testHelper.openPage( "#" + home );
},
function() {
$.testHelper.openPage( "#cache-tests/clear.html" );
},
function(){
ok( findNestedPages( "#cached-nested-list" ).length > 0, "nested pages remain" );
start();
}
]);
});
asyncTest( "parent page is not removed when visiting a sub page", function(){
$.testHelper.pageSequence([
function(){
//reset for relative url refs
$.testHelper.openPage( "#" + home );
},
function(){
$.testHelper.openPage( "#cache-tests/cached-nested.html" );
},
function(){
same( $("#cached-nested-list").length, 1 );
$.testHelper.openPage( "#" + home );
},
function() {
$.testHelper.openPage( "#cache-tests/clear.html" );
},
function(){
same( $("#cached-nested-list").length, 1 );
start();
}
]);
});
asyncTest( "filterCallback can be altered after widget creation", function(){
var listPage = $( "#search-filter-test" );
expect( listPage.find("li").length );
$.testHelper.pageSequence( [
function(){
//reset for relative url refs
$.testHelper.openPage( "#" + home );
},
function() {
$.testHelper.openPage( "#search-filter-test" );
},
function() {
// set the listview instance callback
listPage.find( "ul" ).listview( "option", "filterCallback", function() {
ok(true, "custom callback invoked");
});
// trigger a change in the search filter
listPage.find( "input" ).val( "foo" ).trigger( "change" );
//NOTE beware a poossible issue with timing here
start();
}
]);
});
asyncTest( "nested pages hash key is always in the hash (replaceState)", function(){
$.testHelper.pageSequence([
function(){
//reset for relative url refs
$.testHelper.openPage( "#" + home );
},
function(){
// https://github.com/jquery/jquery-mobile/issues/1617
$.testHelper.openPage("#nested-lists-test");
},
function(){
// Click on the link of the third li element
$('.ui-page-active li:eq(2) a:eq(0)').click();
},
function(){
ok( location.hash.search($.mobile.subPageUrlKey) >= 0 );
start();
}
]);
});
asyncTest( "embedded listview page with nested pages is not removed from the dom", function() {
$.testHelper.pageSequence([
function() {
// open the nested list page
same( $("div#nested-list-test").length, 1 );
$( "a#nested-list-test-anchor" ).click();
},
function() {
// go back to the origin page
window.history.back();
},
function() {
// make sure the page is still in place
same( $("div#nested-list-test").length, 1 );
start();
}
]);
});
})(jQuery);