1(function () { 2 const form = document.querySelector('.elsykkel-form'); 3 if( !form ) { 4 return; 5 } 6 7 let activeDate = new Date(); // At current month 8 activeDate.setDate(1); 9 let disabledDates = []; 10 11 const radios = document.querySelectorAll('.elsykkel-form__radios input'); 12 const next = document.querySelector('.elsykkel-form__dates__next'); 13 const prev = document.querySelector('.elsykkel-form__dates__prev'); 14 const date = document.querySelector('.elsykkel-form__dates__title'); 15 const dateList = document.querySelector('.elsykkel-form__dates__list'); 16 const monthNames = ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember']; 17 18 function getDisabledDates() { 19 const selected = document.querySelector('.elsykkel-form__radios input:checked'); 20 if(!selected) { 21 return; 22 } 23 24 const userId = selected.value; 25 26 let request = new XMLHttpRequest(); 27 const action = "&action=/elsykkel/butikk"; 28 request.open('GET', window.ajaxUrl + action + "&userId=" + userId, true); 29 request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 30 request.onload = function() { 31 if (this.status >= 200 && this.status < 400) { 32 // Success! 33 var resp = this.response; 34 const data = JSON.parse(this.response); 35 setupDates(data.disabledDates) 36 } else { 37 // We reached our target server, but it returned an error 38 39 } 40 }; 41 42 request.onerror = function() { 43 // There was a connection error of some sort 44 }; 45 46 request.send(); 47 48 } 49 50 function dateToString(date) { 51 const year = date.getFullYear(); 52 let month = date.getMonth() + 1; 53 if(month < 10) { 54 month = "0" + month; 55 } 56 let monthDate = date.getDate(); 57 if( monthDate < 10) { 58 monthDate = "0" + monthDate; 59 } 60 return year + "-" + month + "-" + monthDate; 61 } 62 63 function appendSingleDate( tempDate ) { 64 let date = new Date(tempDate.getTime()); 65 const unique = date.getTime();// Something unique 66 let radio = document.createElement('input'); 67 68 let dateValue = dateToString(date); 69 let isDisabled = false; 70 for (var i = disabledDates.length - 1; i >= 0; i--) { 71 if( disabledDates[i] == dateValue ) { 72 isDisabled = true; 73 } 74 } 75 let writtenDate = date.getDate() + " " + monthNames[date.getMonth()]; 76 77 date.setDate(date.getDate() + 6); 78 79 if ( date <= new Date()) { 80 isDisabled = true; 81 } 82 83 radio.disabled = isDisabled; 84 85 radio.setAttribute("type", "radio"); 86 radio.setAttribute("id", unique); 87 radio.setAttribute("name", "date"); 88 89 90 dateValue = dateValue + "," + dateToString(date); 91 writtenDate = writtenDate + " - " + date.getDate() + " " + monthNames[date.getMonth()]; 92 radio.setAttribute("value", dateValue); // Comma separated list of start and end date 93 94 let label = document.createElement('label'); 95 label.setAttribute("for", unique); 96 label.innerHTML = '<span class="elsykkel-form__dates__item__date">' + writtenDate + '</span>' + 97 '<span class="elsykkel-form__dates__item__subtext">Dato</span>'; 98 99 let wrapper = document.createElement("div"); 100 wrapper.classList.add("elsykkel-form__dates__item"); 101 wrapper.appendChild(radio); 102 wrapper.appendChild(label); 103 dateList.appendChild(wrapper); 104 } 105 106 const startDay = 3; // Wednesday 107 const endDay = 2; // Tuesday 108 function rewriteDates() { 109 let endDate = new Date(activeDate.getTime()); 110 endDate.setMonth(endDate.getMonth() + 1); 111 endDate.setDate(1); // First day of next month 112 113 if(date) { 114 // Change month + year of dateTitle element 115 date.innerText = monthNames[activeDate.getMonth()] + " " + activeDate.getFullYear(); 116 } 117 if(dateList) { 118 // Add dates to list 119 dateList.innerHTML = ""; 120 let tempDate = new Date(activeDate.getTime()); 121 tempDate.setDate(tempDate.getDate() - 7); // Go one week back, then we always start on correct week 122 // Goto first startDay 123 const beginning = tempDate.getDay(); 124 if( beginning < startDay ) { 125 tempDate.setDate(tempDate.getDate() + startDay - tempDate.getDay()) 126 } else if(beginning > startDay) { 127 tempDate.setDate(tempDate.getDate() + 7 + startDay - tempDate.getDay()) 128 } 129 130 // Now we are on a wednesday: let us continue adding weeks untill we are done 131 while(tempDate <= endDate) { 132 appendSingleDate(tempDate); 133 tempDate.setDate(tempDate.getDate() + 7); 134 } 135 } 136 } 137 138 if( next ) { 139 next.addEventListener('click', function() { 140 activeDate.setMonth(activeDate.getMonth() + 1); 141 rewriteDates(); 142 }); 143 } 144 145 if( prev ) { 146 prev.addEventListener('click', function() { 147 activeDate.setMonth(activeDate.getMonth() - 1); 148 rewriteDates(); 149 }); 150 } 151 152 if(radios.length > 0) { 153 for (var i = radios.length - 1; i >= 0; i--) { 154 radios[i].addEventListener('input', getDisabledDates); 155 } 156 } 157 158 getDisabledDates(); 159 160 function setupDates(newDisabledDates) { 161 disabledDates = newDisabledDates; 162 rewriteDates(); 163 } 164 165 166})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.