From a60943a1731b80b285356b53fc31bdcc7fdf4245 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 4 Apr 2019 17:40:33 +0200 Subject: [PATCH 1/3] Polyfill insertAdjacentElement and dataset on old browsers --- app/javascript/shared/polyfills.js | 2 + app/javascript/shared/polyfills/dataset.js | 75 +++++++++++++++++++ .../shared/polyfills/insertAdjacentElement.js | 47 ++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 app/javascript/shared/polyfills/dataset.js create mode 100644 app/javascript/shared/polyfills/insertAdjacentElement.js diff --git a/app/javascript/shared/polyfills.js b/app/javascript/shared/polyfills.js index 5ff61d17c..a19bbef8c 100644 --- a/app/javascript/shared/polyfills.js +++ b/app/javascript/shared/polyfills.js @@ -3,3 +3,5 @@ // required by the browsers we support will be included. import '@babel/polyfill'; import 'dom4'; +import './polyfills/insertAdjacentElement'; +import './polyfills/dataset'; diff --git a/app/javascript/shared/polyfills/dataset.js b/app/javascript/shared/polyfills/dataset.js new file mode 100644 index 000000000..00d648ea1 --- /dev/null +++ b/app/javascript/shared/polyfills/dataset.js @@ -0,0 +1,75 @@ +/* + @preserve dataset polyfill for IE < 11. See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset and http://caniuse.com/#search=dataset + + @author ShirtlessKirk copyright 2015 + @license WTFPL (http://www.wtfpl.net/txt/copying) +*/ + +const dash = /-([a-z])/gi; +const dataRegEx = /^data-(.+)/; +const hasEventListener = !!document.addEventListener; +const test = document.createElement('_'); +const DOMAttrModified = 'DOMAttrModified'; + +let mutationSupport = false; + +function clearDataset(event) { + delete event.target._datasetCache; +} + +function toCamelCase(string) { + return string.replace(dash, function(_, letter) { + return letter.toUpperCase(); + }); +} + +function getDataset() { + const dataset = {}; + + for (let attribute of this.attributes) { + let match = attribute.name.match(dataRegEx); + if (match) { + dataset[toCamelCase(match[1])] = attribute.value; + } + } + + return dataset; +} + +function mutation() { + if (hasEventListener) { + test.removeEventListener(DOMAttrModified, mutation, false); + } else { + test.detachEvent(`on${DOMAttrModified}`, mutation); + } + + mutationSupport = true; +} + +if (!test.dataset) { + if (hasEventListener) { + test.addEventListener(DOMAttrModified, mutation, false); + } else { + test.attachEvent(`on${DOMAttrModified}`, mutation); + } + + // trigger event (if supported) + test.setAttribute('foo', 'bar'); + + Object.defineProperty(Element.prototype, 'dataset', { + get: mutationSupport + ? function get() { + if (!this._datasetCache) { + this._datasetCache = getDataset.call(this); + } + + return this._datasetCache; + } + : getDataset + }); + + if (mutationSupport && hasEventListener) { + // < IE9 supports neither + document.addEventListener(DOMAttrModified, clearDataset, false); + } +} diff --git a/app/javascript/shared/polyfills/insertAdjacentElement.js b/app/javascript/shared/polyfills/insertAdjacentElement.js new file mode 100644 index 000000000..4df8a031c --- /dev/null +++ b/app/javascript/shared/polyfills/insertAdjacentElement.js @@ -0,0 +1,47 @@ +/* + Updated w/ insertAdjacentElement + @author Dan Levy @justsml + 2016-06-23 + + Credit: @lyleunderwood - afterend patch/fix + + 2011-10-10 + + By Eli Grey, http://eligrey.com + Public Domain. + NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. +*/ + +function insertAdjacentElement(position, elem) { + const _this = this; + const parent = this.parentNode; + let node, first; + + switch (position.toLowerCase()) { + case 'beforebegin': + while ((node = elem.firstChild)) { + parent.insertBefore(node, _this); + } + break; + case 'afterbegin': + first = _this.firstChild; + while ((node = elem.lastChild)) { + first = _this.insertBefore(node, first); + } + break; + case 'beforeend': + while ((node = elem.firstChild)) { + _this.appendChild(node); + } + break; + case 'afterend': + parent.insertBefore(elem, _this.nextSibling); + break; + } + return elem; +} + +// Method missing in Firefox < 48 +if (!HTMLElement.prototype.insertAdjacentElement) { + HTMLElement.prototype.insertAdjacentElement = insertAdjacentElement; +} From adb6255d5d449d611bfd75da0cfd6183d44817a5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 9 Apr 2019 17:15:45 +0200 Subject: [PATCH 2/3] Do not crash if element to remove is not found --- app/helpers/application_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 91df7a137..f2e84bd75 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -53,7 +53,7 @@ module ApplicationHelper script = "(function() {"; script << "var el = document.querySelector('#{selector}');" method = (inner ? "el.innerHTML = ''" : "el.parentNode.removeChild(el)") - script << "setTimeout(function() { #{method}; }, #{timeout});"; + script << "if (el) { setTimeout(function() { #{method}; }, #{timeout}); }"; script << "})();" # rubocop:disable Rails/OutputSafety raw(script); From 833bafaba629074079db9e761e524c706390d9b1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 9 Apr 2019 17:16:18 +0200 Subject: [PATCH 3/3] Align with behaviour on user dossier form --- .../dossiers/purge_champ_piece_justificative.js.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/gestionnaires/dossiers/purge_champ_piece_justificative.js.erb b/app/views/gestionnaires/dossiers/purge_champ_piece_justificative.js.erb index b564293b9..9d8807116 100644 --- a/app/views/gestionnaires/dossiers/purge_champ_piece_justificative.js.erb +++ b/app/views/gestionnaires/dossiers/purge_champ_piece_justificative.js.erb @@ -1,2 +1,3 @@ <%= render_flash(timeout: 5000, sticky: true) %> <%= remove_element("#piece_justificative_#{@champ.id}") %> +<%= show_element("#champs_#{@champ.id}") %>