@@ -23,10 +23,17 @@ <h2>LOCAL TAPAS</h2>
2323 < input type ="text " name ="item " placeholder ="Item Name " required >
2424 < input type ="submit " value ="+ Add Item ">
2525 </ form >
26+ < p class ="buttons ">
27+ < button class ="clear "> Clear List</ button >
28+ < button class ="all-checks " data-checked ="1 "> Check All</ button >
29+ < button class ="all-checks " data-checked =""> Uncheck All</ button >
30+ </ p >
2631</ div >
2732
2833< script >
2934 const addItems = document . querySelector ( '.add-items' ) ;
35+ const clearItems = document . querySelector ( '.clear' ) ;
36+ const checkButtons = document . querySelectorAll ( '.all-checks' ) ;
3037 const itemsList = document . querySelector ( '.plates' ) ;
3138 const items = JSON . parse ( localStorage . getItem ( 'items' ) ) || [ ] ;
3239
@@ -46,6 +53,10 @@ <h2>LOCAL TAPAS</h2>
4653 }
4754
4855 function populateList ( plates = [ ] , platesList ) {
56+ if ( plates . length === 0 ) {
57+ platesList . innerHTML = '<li>Loading Tapas...</li>' ;
58+ return ;
59+ }
4960 platesList . innerHTML = plates . map ( ( plate , i ) => {
5061 return `
5162 <li>
@@ -56,19 +67,50 @@ <h2>LOCAL TAPAS</h2>
5667 } ) . join ( '' ) ;
5768 }
5869
59- function toggleDone ( e ) {
70+ function handleSingleCheckbox ( e ) {
6071 if ( ! e . target . matches ( 'input' ) ) {
6172 return ;
6273 }
6374 const el = e . target ;
6475 const itemIndex = el . dataset . index ;
76+ toggleDone ( itemIndex ) ;
77+ }
78+
79+ function handleAllCheckboxes ( e ) {
80+ const checked = Boolean ( e . target . dataset . checked ) ;
81+ const checkboxes = itemsList . querySelectorAll ( 'input[type="checkbox"]' ) ;
82+ checkboxes . forEach ( checkbox => {
83+ const itemIndex = checkbox . dataset . index ;
84+ if ( checked ) {
85+ checkbox . setAttribute ( 'checked' , '' ) ;
86+ items [ itemIndex ] . done = true ;
87+ } else {
88+ checkbox . removeAttribute ( 'checked' ) ;
89+ items [ itemIndex ] . done = false ;
90+ }
91+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
92+ } ) ;
93+ }
94+
95+ function toggleDone ( itemIndex ) {
6596 items [ itemIndex ] . done = ! items [ itemIndex ] . done ;
6697 populateList ( items , itemsList ) ;
6798 localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
6899 }
69100
101+ function clearList ( ) {
102+ items . splice ( 0 , items . length ) ;
103+ populateList ( items , itemsList ) ;
104+ localStorage . setItem ( 'items' , JSON . stringify ( items ) ) ;
105+ }
106+
70107 addItems . addEventListener ( 'submit' , addItem ) ;
71- itemsList . addEventListener ( 'click' , toggleDone ) ;
108+ clearItems . addEventListener ( 'click' , clearList ) ;
109+ checkButtons . forEach ( button => {
110+ button . addEventListener ( 'click' , handleAllCheckboxes ) ;
111+ } ) ;
112+ itemsList . addEventListener ( 'click' , handleSingleCheckbox ) ;
113+
72114 populateList ( items , itemsList ) ;
73115
74116</ script >
0 commit comments