1/** 2 * @file 3 * JS for Agenda. 4 */ 5(function ($, Drupal, drupalSettings) { 6 'use strict'; 7 8 var menuAdminBO = { 9 context_class: '.toolbar-menu-administration', 10 menu_system_admin_class: '.menu-item__system-admin_content', 11 item_video_class: 'toolbar-icon-admin-toolbar-tools-extra-linksmedia-add-video', 12 init: function () { 13 // Remove media video menu if role Contributeur 14 if ($(menuAdminBO.menu_system_admin_class, menuAdminBO.context_class).length && drupalSettings.user_role === 'contributeur') { 15 $(menuAdminBO.menu_system_admin_class + " li a", menuAdminBO.context_class).each(function( index ) { 16 if ($(this).hasClass(menuAdminBO.item_video_class)) { 17 $(this).remove(); 18 } 19 }); 20 } 21 } 22 } 23 24 var agendaFicheBO = { 25 geo_check: '#hdd_geo_check', 26 lon: '#edit-field-longitude-0-value', 27 lat: '#edit-field-latitude-0-value', 28 adresse: '#edit-field-adresse-agenda-0-value', 29 code_postal: '#edit-field-code-postal-0-value', 30 ville: '#edit-field-ville-0-value', 31 pays: '#edit-field-pays-0-value', 32 init: function () { 33 if ($(".node-agenda-form").length || $(".node-agenda-edit-form").length) { 34 var event_adresse = ''; 35 var event_code_postal = ''; 36 37 // Champ Adresse 38 $(agendaFicheBO.adresse).on('change', function () { 39 if ($(agendaFicheBO.lon).val() === '' && $(agendaFicheBO.lat).val() === '' && $(agendaFicheBO.geo_check).length && $(agendaFicheBO.geo_check).val() === '') { 40 $(agendaFicheBO.geo_check).val('call'); 41 event_adresse = $(this).val(); 42 event_code_postal = $(agendaFicheBO.code_postal).val(); 43 44 agendaFicheBO.callAjax(event_adresse, event_code_postal); 45 } 46 }); 47 48 // Champ Code postal 49 $(agendaFicheBO.code_postal).on('change', function () { 50 if ($(agendaFicheBO.geo_check).length && $(agendaFicheBO.geo_check).val() === '') { 51 $(agendaFicheBO.geo_check).val('call'); 52 event_adresse = $(agendaFicheBO.adresse).val(); 53 event_code_postal = $(this).val(); 54 55 agendaFicheBO.callAjax(event_adresse, event_code_postal); 56 } 57 }); 58 59 // Bloc Dates - set end date same as start date 60 $('input[id^=edit-field-agenda-dates-][class*=js-input-date]').each(function () { 61 var date_field = $(this).attr('id'); 62 63 if (date_field.indexOf("-end-value-date") === -1) { 64 var start_date = date_field; 65 var end_date = date_field.replace("-value-date", "-end-value-date"); 66 67 var field_start_date = 'input[id=' + start_date + '][class*=js-input-date]'; 68 var field_end_date = 'input[id=' + end_date + '][class*=js-input-date]'; 69 70 $(field_start_date).on('change', function () { 71 if ($(field_end_date).length) { 72 $(field_end_date).val($(field_start_date).val()); 73 } 74 }); 75 76 $(field_end_date).on('change', function () { 77 if ($(field_start_date).length) { 78 $(field_start_date).val($(field_end_date).val()); 79 } 80 }); 81 } 82 }); 83 84 // Bloc Heures 85 $('input[id^=edit-field-agenda-time-][class*=form-element--type-time],input[id^=edit-field-agenda-dates-][class*=form-element--type-time]').each(function () { 86 var time_field = $(this).attr('id'); 87 88 if (time_field.indexOf("-to") === -1 || time_field.indexOf("-end-value-time") === -1) { 89 var start_time = time_field; 90 var end_time = time_field.replace("-from", "-to").replace("-value-time", "-end-value-time"); 91 92 var field_start_time = 'input[id=' + start_time + '][class*=form-element--type-time]'; 93 var field_end_time = 'input[id=' + end_time + '][class*=form-element--type-time]'; 94 95 $(field_start_time).on('change', function () { 96 if ($(field_start_time).val() > $(field_end_time).val()) { 97 $(field_end_time).val($(field_start_time).val()); 98 } 99 }); 100 101 $(field_end_time).on('change', function () { 102 if ($(field_end_time).val() < $(field_start_time).val()) { 103 $(field_start_time).val($(field_end_time).val()); 104 } 105 }); 106 } 107 }); 108 109 } 110 }, 111 callAjax: function (event_adresse, event_code_postal) { 112 if (event_adresse !== '') { 113 $.ajax({ 114 url: drupalSettings.path.baseUrl + 'geo/coordinates', 115 method: 'GET', 116 data: { 117 adresse: event_adresse,
118 code_postal: event_code_postal, 119 ville: $(agendaFicheBO.ville).val(), 120 pays: $(agendaFicheBO.pays).val() 121 }, 122 success: function (data) { 123 if (data.coordinates) { 124 $(agendaFicheBO.lon).val(data.coordinates.lon); 125 $(agendaFicheBO.lat).val(data.coordinates.lat); 126 $(agendaFicheBO.geo_check).val(''); 127 } 128 } 129 }) 130 .fail(function () { 131 console.log('FAIL'); 132 }); 133 } 134 } 135 } 136 137 var agendaListe = { 138 date_debut: 'edit-date-debut', 139 date_fin: 'edit-date-fin', 140 hdd_filter: 'filter', 141 is_active: 'is-active', 142 form_id: 'views-exposed-form-agenda-page-liste', 143 form_class: '.views-exposed-form-agenda-page-liste', 144 submit_id: 'edit-submit-agenda', 145 init: function () { 146 if ($(agendaListe.form_class).length) { 147 const params = new Proxy(new URLSearchParams(window.location.search), { 148 get: (searchParams, prop) => searchParams.get(prop), 149 }); 150 151 if (params.filter !== null) { 152 switch (params.filter) { 153 case 'today': 154 $('[id=agenda_today]', agendaListe.form_class).parent().addClass(agendaListe.is_active); 155 break; 156 case 'week': 157 $('[id=agenda_this_week]', agendaListe.form_class).parent().addClass(agendaListe.is_active); 158 break; 159 case 'weekend': 160 $('[id=agenda_this_weekend]', agendaListe.form_class).parent().addClass(agendaListe.is_active); 161 break; 162 case 'periode': 163 $('[id=agenda_periode]', agendaListe.form_class).parent().addClass(agendaListe.is_active); 164 $('[id=date-periode]', agendaListe.form_class).removeClass('u-hidden'); 165 break; 166 } 167 } else { 168 $('[id=agenda_all]', agendaListe.form_class).parent().addClass(agendaListe.is_active); 169 } 170 171 $('#agenda_all').click(function (e) { 172 e.preventDefault(); 173 window.location = drupalSettings.agenda.link; 174 }); 175 $('#agenda_today').click(function (e) { 176 e.preventDefault(); 177 agendaListe.setToday(agendaListe.date_debut, agendaListe.date_fin, agendaListe.form_id); 178 }); 179 $('#agenda_this_week').click(function (e) { 180 e.preventDefault(); 181 agendaListe.setThisWeek(agendaListe.date_debut, agendaListe.date_fin, agendaListe.form_id); 182 }); 183 $('#agenda_this_weekend').click(function (e) { 184 e.preventDefault(); 185 agendaListe.setThisWeekEnd(agendaListe.date_debut, agendaListe.date_fin, agendaListe.form_id); 186 }); 187 188 document.querySelector('.js-periode-toggle').addEventListener('click', function (e) { 189 e.preventDefault(); 190 191 $('[id^=' + agendaListe.hdd_filter + ']', agendaListe.form_class).val('periode'); 192 193 var mapDiv = document.getElementById(this.getAttribute('aria-controls')); 194 195 // Toggle the "show" class on the div 196 mapDiv.classList.toggle('u-hidden'); 197 198 // Toggle the aria-expanded attribute 199 var isExpanded = this.getAttribute('aria-expanded') === 'true'; 200 this.setAttribute('aria-expanded', !isExpanded); 201 }); 202 203 // On enter only, trigger click 204 $('#agenda_all, #agenda_today, #agenda_this_week, #agenda_this_weekend').on('keydown', function (e) { 205 if (e.key === 'Enter') { 206 $(this).trigger('click'); 207 } else if (e.key === ' ') { // Spacebar check 208 e.preventDefault(); // Prevent default scrolling or activation 209 } 210 }); 211 212 // Mot clé 213 $('#edit-mot-cle').on('keypress', function (e) { 214 if (e.which === 13) { 215 e.preventDefault(); 216 $('[id=' + agendaListe.submit_id + ']', agendaListe.form_class).trigger('click'); 217 } 218 }); 219 } 220 }, 221 formattedDate: function (date) { 222 var mm = ("0" + (date.getMonth() + 1)).slice(-2); // in javascript month start from 0. 223 var dd = ("0" + date.getDate()).slice(-2); // add leading zero 224 var yy = date.getFullYear(); 225 226 return yy + '-' + mm + '-' + dd; 227 }, 228 setToday: function (date_debut, date_fin, submit_btn) { 229 var date = new Date(); 230 var today = agendaListe.formattedDate(new Date(date.getFullYear(), date.getMonth(), date.getDate())); 231 232 if ($('[id^=' + date_debut + ']').length) { 233 $('[id^=' + date_debut + ']').val(today); 234 } 235 if ($('[id^=' + date_fin + ']').length) { 236 $('[id^=' + date_fin + ']').val(today); 237 } 238 if ($('[id^=' + submit_btn + ']').length) { 239 $('[id^=' + agendaListe.hdd_filter + ']').val('today'); 240 $('[id^=' + submit_btn + ']').submit(); 241 } 242 243 return false;
244 }, 245 setThisWeek: function (date_debut, date_fin, submit_btn) { 246 var date = new Date(); 247 date.setDate(date.getDate() + (5 + 7 - date.getDay()) % 7); 248 var monday = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 4); 249 var sunday = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 2); 250 251 var next_monday = agendaListe.formattedDate(monday); 252 var next_sunday = agendaListe.formattedDate(sunday); 253 254 if ($('[id^=' + date_debut + ']').length) { 255 $('[id^=' + date_debut + ']').val(next_monday); 256 } 257 if ($('[id^=' + date_fin + ']').length) { 258 $('[id^=' + date_fin + ']').val(next_sunday); 259 } 260 if ($('[id^=' + submit_btn + ']').length) { 261 $('[id^=' + agendaListe.hdd_filter + ']').val('week'); 262 $('[id^=' + submit_btn + ']').submit(); 263 } 264 265 return false; 266 }, 267 setThisWeekEnd: function (date_debut, date_fin, submit_btn) { 268 var date = new Date(); 269 date.setDate(date.getDate() + (5 + 7 - date.getDay()) % 7); 270 var friday = new Date(date.getFullYear(), date.getMonth(), date.getDate()); 271 var sunday = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 2); 272 273 var next_friday = agendaListe.formattedDate(friday); 274 var next_sunday = agendaListe.formattedDate(sunday); 275 276 if ($('[id^=' + date_debut + ']').length) { 277 $('[id^=' + date_debut + ']').val(next_friday); 278 } 279 if ($('[id^=' + date_fin + ']').length) { 280 $('[id^=' + date_fin + ']').val(next_sunday); 281 } 282 if ($('[id^=' + submit_btn + ']').length) { 283 $('[id^=' + agendaListe.hdd_filter + ']').val('weekend'); 284 $('[id^=' + submit_btn + ']').submit(); 285 } 286 287 return false; 288 } 289 } 290 291 var agendaFicheFO = { 292 page_class: '.fiche-agenda', 293 tab_agenda: '#tab-label-agenda', 294 disabled_attr: 'disabled', 295 init: function () { 296 if ($(agendaFicheFO.page_class).length) { 297 // Default header height 298 var headerHeight = '110px'; 299 // header height with admin toolbar 300 if ($('body').hasClass('toolbar-horizontal')) { 301 headerHeight = '163px'; 302 } 303 // mobile header height 304 if ($(window).width() < 792) { 305 headerHeight = '60px'; 306 } 307 // Convert headerHeight from string to an integer (removing 'px') 308 var headerHeightValue = parseInt(headerHeight, 10); 309 310 //Events calendar navigation 311 $('.autres-dates-dispo').on('click', function(e) { 312 $([document.documentElement, document.body]).animate({ 313 scrollTop: $('#autres_dates').offset().top - headerHeightValue - 30 314 }, 1000); 315 }); 316 317 // Trouver un itinéraire 318 $('.trouver-itineraire').on('click', function(e) { 319 const targetElement = $('#block_go_event'); 320 321 if (targetElement.length) { // Check if the element exists 322 $([document.documentElement, document.body]).animate({ 323 scrollTop: targetElement.offset().top - headerHeightValue - 35 324 }, 1000); 325 } 326 }); 327 328 // When the "previous month" button is clicked 329 $('.js-prev-month').on('click', function () { 330 agendaFicheFO.navigateMonth('prev'); 331 }); 332 333 // When the "next month" button is clicked 334 $('.js-next-month').on('click', function () { 335 agendaFicheFO.navigateMonth('next'); 336 }); 337 338 // Hide navigation arrows conditionally 339 agendaFicheFO.hideNavigationArrows(); 340 341 if ($(agendaFicheFO.tab_agenda).length) { 342 // Move tab Agenda on first position 343 var before = $(agendaFicheFO.tab_agenda).parent().prev(); 344 $(agendaFicheFO.tab_agenda).parent().insertBefore(before); 345 346 // Show tab Agenda content 347 $(agendaFicheFO.tab_agenda).click(); 348 349 // Reset focus 350 $(':focus').blur(); 351 } 352 } 353 }, 354 navigateMonth: function (direction) { 355 // Find the currently active month and list 356 var currentMonth = $('.c-calendar__header-item.active-month'); 357 var currentList = $('.c-calendar__list.active-list'); 358 359 // Move to the next or previous month 360 var newMonth; 361 if (direction === 'next') { 362 newMonth = currentMonth.next('.c-calendar__header-item'); 363 } else { 364 newMonth = currentMonth.prev('.c-calendar__header-item'); 365 } 366 367 // Only proceed if a next or previous month exists 368 if (newMonth.length > 0) { 369 // Update the active month 370 currentMonth.removeClass('active-month'); 371 newMonth.addClass('active-month'); 372 373 // Update the corresponding list of dates 374 var newListId = newMonth.attr('aria-controls'); 375 currentList.removeClass('active-list'); 376 $('#' + newListId).addClass('active-list'); 377 } 378 379 agendaFicheFO.hideNavigationArrows(); 380 }, 381 hideNavigationArrows: function () { 382 // Reset arrows visibility 383 $('.js-prev-month').removeAttr(agendaFicheFO.disabled_attr); 384 $('.js-next-month').removeAttr(agendaFicheFO.disabled_attr); 385 386 var el_header = $('.c-calendar__header p', '.c-calendar'); 387 388 el_header.each(function (index) { 389 if ($(this).hasClass('active-month')) { 390 if (index === 0) { 391 $('.js-prev-month').attr(agendaFicheFO.disabled_attr, agendaFicheFO.disabled_attr); 392 } 393 else if (index === (el_header.length - 1)) { 394 $('.js-next-month').attr(agendaFicheFO.disabled_attr, agendaFicheFO.disabled_attr); 395 } 396 } 397 }); 398 } 399 } 400 401 var pageReload = { 402 init: function () { 403 if ($('body').hasClass('page-404')) { 404 /* if my "reload" var isn't set locally.. getItem will be false */ 405 if (!localStorage.getItem("reload")) { 406 /* set reload to true and then reload the page */ 407 localStorage.setItem("reload", "true"); 408 location.reload(); 409 } 410 /* after reloading remove "reload" from localStorage */ 411 else { 412 localStorage.removeItem("reload"); 413 // localStorage.clear(); // or clear it, instead 414 } 415 } 416 } 417 } 418 419 window.addEventListener("DOMContentLoaded", function() {
420 menuAdminBO.init(); 421 agendaFicheBO.init(); 422 agendaListe.init(); 423 agendaFicheFO.init(); 424 pageReload.init(); 425 }); 426 427 $(document).ajaxComplete(function(event,request, settings){ 428 agendaFicheBO.init(); 429 }); 430})(jQuery, Drupal, drupalSettings);
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.