From 86b9ed20791f2a98861316fc4d4364b0262b38bd Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 1 Aug 2019 15:22:37 +0200 Subject: [PATCH] Run load scripts after ujs responces --- app/javascript/new_design/champs/carte.js | 2 +- app/javascript/new_design/messagerie.js | 2 +- app/javascript/new_design/procedure-context.js | 2 +- app/javascript/new_design/select2.js | 2 +- app/javascript/new_design/support.js | 2 +- app/javascript/packs/application-old.js | 1 + app/javascript/packs/application.js | 1 + app/javascript/shared/autocomplete.js | 12 ++---------- app/javascript/shared/franceconnect.js | 3 +-- app/javascript/shared/page-update-event.js | 9 +++++++++ app/javascript/shared/react-ujs.js | 2 +- 11 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 app/javascript/shared/page-update-event.js diff --git a/app/javascript/new_design/champs/carte.js b/app/javascript/new_design/champs/carte.js index f91b3b0d5..c4f8a3cb8 100644 --- a/app/javascript/new_design/champs/carte.js +++ b/app/javascript/new_design/champs/carte.js @@ -39,7 +39,7 @@ function diplayMap(element, data, initial = false) { } } -addEventListener('turbolinks:load', initialize); +addEventListener('ds:page:update', initialize); addEventListener('carte:update', ({ detail: { selector, data } }) => { const element = document.querySelector(selector); diff --git a/app/javascript/new_design/messagerie.js b/app/javascript/new_design/messagerie.js index 91e9190db..d2e056feb 100644 --- a/app/javascript/new_design/messagerie.js +++ b/app/javascript/new_design/messagerie.js @@ -14,4 +14,4 @@ export function scrollMessagerie() { } } -addEventListener('turbolinks:load', scrollMessagerie); +addEventListener('ds:page:update', scrollMessagerie); diff --git a/app/javascript/new_design/procedure-context.js b/app/javascript/new_design/procedure-context.js index a6de9431d..4ec90a6f2 100644 --- a/app/javascript/new_design/procedure-context.js +++ b/app/javascript/new_design/procedure-context.js @@ -14,7 +14,7 @@ function expandProcedureDescription() { descBody.classList.remove('read-more-collapsed'); } -addEventListener('turbolinks:load', updateReadMoreVisibility); +addEventListener('ds:page:update', updateReadMoreVisibility); addEventListener('resize', updateReadMoreVisibility); delegate('click', '.read-more-button', expandProcedureDescription); diff --git a/app/javascript/new_design/select2.js b/app/javascript/new_design/select2.js index ccd14b117..bbc8cdb86 100644 --- a/app/javascript/new_design/select2.js +++ b/app/javascript/new_design/select2.js @@ -1,7 +1,7 @@ import $ from 'jquery'; import 'select2'; -addEventListener('turbolinks:load', () => { +addEventListener('ds:page:update', () => { $('select.select2').select2({ language: 'fr', width: '100%' diff --git a/app/javascript/new_design/support.js b/app/javascript/new_design/support.js index 1e6485c30..1c637ed88 100644 --- a/app/javascript/new_design/support.js +++ b/app/javascript/new_design/support.js @@ -12,5 +12,5 @@ function updateContactElementsVisibility() { } } -addEventListener('turbolinks:load', updateContactElementsVisibility); +addEventListener('ds:page:update', updateContactElementsVisibility); delegate('change', '#contact-form #type', updateContactElementsVisibility); diff --git a/app/javascript/packs/application-old.js b/app/javascript/packs/application-old.js index 7f06ccb32..180698e4b 100644 --- a/app/javascript/packs/application-old.js +++ b/app/javascript/packs/application-old.js @@ -4,6 +4,7 @@ import Rails from '@rails/ujs'; import * as ActiveStorage from '@rails/activestorage'; import jQuery from 'jquery'; +import '../shared/page-update-event'; import '../shared/activestorage/ujs'; import '../shared/rails-ujs-fix'; import '../shared/safari-11-file-xhr-workaround'; diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 13014a91a..784ba4497 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -9,6 +9,7 @@ import Highcharts from 'highcharts'; import ReactUJS from '../shared/react-ujs'; import reactComponents from '../shared/react-components'; +import '../shared/page-update-event'; import '../shared/activestorage/ujs'; import '../shared/activestorage/attachment-checker'; import '../shared/rails-ujs-fix'; diff --git a/app/javascript/shared/autocomplete.js b/app/javascript/shared/autocomplete.js index db9db0180..4aefffd57 100644 --- a/app/javascript/shared/autocomplete.js +++ b/app/javascript/shared/autocomplete.js @@ -37,22 +37,14 @@ function source(url) { }; } -addEventListener('turbolinks:load', function() { - autocompleteSetup(); -}); - -addEventListener('ajax:success', function() { - autocompleteSetup(); -}); - -function autocompleteSetup() { +addEventListener('ds:page:update', function() { for (let { type, url } of sources) { for (let element of document.querySelectorAll(selector(type))) { element.removeAttribute('data-autocomplete'); autocompleteInitializeElement(element, url); } } -} +}); function autocompleteInitializeElement(element, url) { const select = autocomplete(element, options, [source(url)]); diff --git a/app/javascript/shared/franceconnect.js b/app/javascript/shared/franceconnect.js index 59b6aa9cc..c6d7653a7 100644 --- a/app/javascript/shared/franceconnect.js +++ b/app/javascript/shared/franceconnect.js @@ -20,8 +20,7 @@ function init() { } } -addEventListener('DOMContentLoaded', init); -addEventListener('turbolinks:load', init); +addEventListener('ds:page:update', init); function toggleElement(event) { event.preventDefault(); diff --git a/app/javascript/shared/page-update-event.js b/app/javascript/shared/page-update-event.js new file mode 100644 index 000000000..c5038f81d --- /dev/null +++ b/app/javascript/shared/page-update-event.js @@ -0,0 +1,9 @@ +import { fire } from '@utils'; + +addEventListener('turbolinks:load', function() { + fire(document, 'ds:page:update'); +}); + +addEventListener('ajax:success', function() { + fire(document, 'ds:page:update'); +}); diff --git a/app/javascript/shared/react-ujs.js b/app/javascript/shared/react-ujs.js index f42ccf132..c6e937984 100644 --- a/app/javascript/shared/react-ujs.js +++ b/app/javascript/shared/react-ujs.js @@ -56,6 +56,6 @@ export default class ReactUJS { } start() { - addEventListener('turbolinks:load', () => this.mountComponents()); + addEventListener('ds:page:update', () => this.mountComponents()); } }