Bugzilla – Attachment 151751 Details for
Bug 27378
Enable compliance with EU Cookie Legislation via cookie consent
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 27378: Stop the codemirror editor from duplicating
Bug-27378-Stop-the-codemirror-editor-from-duplicat.patch (text/plain), 11.20 KB, created by
Matt Blenkinsop
on 2023-05-26 12:19:03 UTC
(
hide
)
Description:
Bug 27378: Stop the codemirror editor from duplicating
Filename:
MIME Type:
Creator:
Matt Blenkinsop
Created:
2023-05-26 12:19:03 UTC
Size:
11.20 KB
patch
obsolete
>From fefd7836532ad395320a7127b2747783cf671c8b Mon Sep 17 00:00:00 2001 >From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com> >Date: Thu, 25 May 2023 15:41:51 +0000 >Subject: [PATCH] Bug 27378: Stop the codemirror editor from duplicating > >Previously, if the "Add new code button" was clicked in the CookieConsentedJS editor, the original entry would have duplicated CodeMirror editors. >This was exponential, i.e adding two new lines would result in three codemirror editors appearing on the first entry, two on the second and so on. >The click event was not being applied properly and was being applied to every element with the .expand-textarea class, rather than specifically the new elements being created. The addExpandHandler function now loops through each element individually and decides whether to apply the click event handler. >--- > .../prog/js/pages/preferences.js | 145 ++++++++++-------- > 1 file changed, 78 insertions(+), 67 deletions(-) > >diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js >index 0d5972ddb1..6bde33bba4 100644 >--- a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js >+++ b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js >@@ -67,58 +67,69 @@ window.onbeforeunload = function () { > // Add event handlers to any elements with .expand-textarea classes > // this allows the CodeMirror widget to be displayed > function addExpandHandler() { >- // Don't duplicate click event handlers >- const ev = $._data($('.expand-textarea'), 'events'); >- if (ev && ev.click) { >- return; >- } >- $(".expand-textarea").on("click", function (e) { >- e.preventDefault(); >- $(this).hide(); >- var target = $(this).data("target"); >- var syntax = $(this).data("syntax"); >- $("#collapse_" + target).show(); >- if (syntax) { >- var editor = CodeMirror.fromTextArea(document.getElementById("pref_" + target), { >- lineNumbers: true, >- mode: syntax, >- lineWrapping: true, >- viewportMargin: Infinity, >- gutters: ["CodeMirror-lint-markers"], >- lint: true >- }); >- editor.on("change", function () { >- mark_modified.call($("#pref_" + target)[0]); >- }); >- editor.on("blur", function () { >- editor.save(); >- }); >- } else { >- $("#pref_" + target).show(); >+ const textAreas = $('.expand-textarea').map(function() { >+ return this >+ }).get(); >+ textAreas.forEach((expandTextArea) => { >+ // Don't duplicate click event handlers >+ const ev = $._data($(expandTextArea).get(0), 'events'); >+ if (ev && ev.click) { >+ return; > } >- }); >+ // Add the click event >+ $(expandTextArea).on("click", function (e) { >+ e.preventDefault(); >+ $(this).hide(); >+ var target = $(this).data("target"); >+ var syntax = $(this).data("syntax"); >+ $("#collapse_" + target).show(); >+ if (syntax) { >+ var editor = CodeMirror.fromTextArea(document.getElementById("pref_" + target), { >+ lineNumbers: true, >+ mode: syntax, >+ lineWrapping: true, >+ viewportMargin: Infinity, >+ gutters: ["CodeMirror-lint-markers"], >+ lint: true >+ }); >+ editor.on("change", function () { >+ mark_modified.call($("#pref_" + target)[0]); >+ }); >+ editor.on("blur", function () { >+ editor.save(); >+ }); >+ } else { >+ $("#pref_" + target).show(); >+ } >+ }); >+ }) > } > > // Add event handlers to any elements with .collapse-textarea classes > // this allows the hiding of the CodeMirror widget > function addCollapseHandler() { >- // Don't duplicate click event handlers >- const ev = $._data($('.collapse-textarea'), 'events'); >- if (ev && ev.click) { >- return; >- } >- $(".collapse-textarea").on("click", function (e) { >- e.preventDefault(); >- $(this).hide(); >- var target = $(this).data("target"); >- var syntax = $(this).data("syntax"); >- $("#expand_" + target).show(); >- if (syntax) { >- var editor = $("#pref_" + target).next(".CodeMirror")[0].CodeMirror; >- editor.toTextArea(); >+ const textAreas = $('.collapse-textarea').map(function() { >+ return this >+ }).get(); >+ textAreas.forEach((collapseTextArea) => { >+ // Don't duplicate click event handlers >+ const ev = $._data($(collapseTextArea).get(0), 'events'); >+ if (ev && ev.click) { >+ return; > } >- $("#pref_" + target).hide(); >- }); >+ $(collapseTextArea).on("click", function (e) { >+ e.preventDefault(); >+ $(this).hide(); >+ var target = $(this).data("target"); >+ var syntax = $(this).data("syntax"); >+ $("#expand_" + target).show(); >+ if (syntax) { >+ var editor = $("#pref_" + target).next(".CodeMirror")[0].CodeMirror; >+ editor.toTextArea(); >+ } >+ $("#pref_" + target).hide(); >+ }); >+ }) > } > > // Add a handler for any consent delete links >@@ -335,12 +346,12 @@ $( document ).ready( function () { > '<input id="' + id + '" type="checkbox" class="' + className + '">'; > } > >- // Create a consentJS item, correctly populated >- function createConsentJSItem(item, idx) { >- const id = 'ConsentJS_' + idx; >+ // Create a cookieConsentedJS item, correctly populated >+ function createCookieConsentedJSItem(item, idx) { >+ const id = 'CookieConsentedJS_' + idx; > const code = item.code && item.code.length > 0 ? atob(item.code) : ''; > const itemId = item.id && item.id.length > 0 ? item.id : ''; >- return '<div id="' + id + '" class="consentJSItem" data-id="' + itemId + '">' + >+ return '<div id="' + id + '" class="cookieConsentedJSItem" data-id="' + itemId + '">' + > ' <div class="consentRow">' + > ' <div class="consentItem">' + > ' <label class="required" for="name_' + id + '">' + __('Name') + ':</label>' + >@@ -382,16 +393,16 @@ $( document ).ready( function () { > '</div > '; > } > >- // Return the markup for all consentJS items concatenated >+ // Return the markup for all cookieConsentedJS items concatenated > function populateConsentMarkup(items) { > return items.reduce(function (acc, current, idx) { >- return acc + createConsentJSItem(current, idx); >+ return acc + createCookieConsentedJSItem(current, idx); > }, ''); > } > > // Return the markup for a validation warning > function populateValidationWarning() { >- return '<div id="consentJSWarning" class="error" style="display:none;">' + __('You must complete all fields') + '</div>'; >+ return '<div id="cookieConsentedJSWarning" class="error" style="display:none;">' + __('You must complete all fields') + '</div>'; > } > > // Return a new, empty consent item >@@ -410,26 +421,26 @@ $( document ).ready( function () { > > // Add the handler for a new empty consent item > function addNewHandler() { >- $("#consentJSAddNew").on("click", function (e) { >+ $("#cookieConsentedJSAddNew").on("click", function (e) { > e.preventDefault(); >- const currentLen = $('.consentJSItem').length; >+ const currentLen = $('.cookieConsentedJSItem').length; > const newItem = emptyConsentItem(); >- const markup = createConsentJSItem(newItem, currentLen); >- $('#prefModal .modal-body #consentJSItems').append($(markup)); >+ const markup = createCookieConsentedJSItem(newItem, currentLen); >+ $('#prefModal .modal-body #cookieConsentedJSItems').append($(markup)); > addExpandHandler(); > addCollapseHandler(); > addConsentDeleteHandler(); > }); > } > >- // Populate the consentJS modal, we also initialise any >+ // Populate the cookieConsentedJS modal, we also initialise any > // event handlers that are required. This function is added > // to the window object so we can call it if we are passed it's name > // as a data-initiator attribute > // (e.g.) >- // const f = 'populateConsentJS'; >+ // const f = 'populateCookieConsentedJS'; > // window[f](); >- window.populateConsentJS = function(el) { >+ window.populateCookieConsentedJS = function(el) { > let items = []; > let decoded = ''; > if (el.value && el.value.length > 0) { >@@ -437,7 +448,7 @@ $( document ).ready( function () { > decoded = atob(el.value); > } catch (err) { > throw (__( >- 'Unable to Base64 decode value stored in ConsentJS syspref: ' + >+ 'Unable to Base64 decode value stored in CookieConsentedJS syspref: ' + > err.message > )); > } >@@ -445,7 +456,7 @@ $( document ).ready( function () { > items = JSON.parse(decoded); > } catch (err) { > throw (__( >- 'Unable to JSON parse decoded value stored in ConsentJS syspref: ' + >+ 'Unable to JSON parse decoded value stored in CookieConsentedJS syspref: ' + > err.message > )); > } >@@ -455,7 +466,7 @@ $( document ).ready( function () { > const pref_name = el.id.replace(/pref_/, ''); > $('#saveModalPrefs').data('target', el.id); > $('#prefModalLabel').text( pref_name ); >- $('#prefModal .modal-body').html($('<div id="consentJSItems">' + validationWarning + markup + '</div><div><a href="#" id="consentJSAddNew">' + __('Add new code') + '</a></div>')); >+ $('#prefModal .modal-body').html($('<div id="cookieConsentedJSItems">' + validationWarning + markup + '</div><div><a href="#" id="cookieConsentedJSAddNew">' + __('Add new code') + '</a></div>')); > addExpandHandler(); > addCollapseHandler(); > addNewHandler(); >@@ -467,10 +478,10 @@ $( document ).ready( function () { > // to the window object so we can call it if we are passed it's name > // as a data-initiator attribute > // e.g. >- // const f = 'prepareConsentJS'; >+ // const f = 'prepareCookieConsentedJS'; > // window[f](); >- window.prepareConsentJS = function () { >- const items = $('.consentJSItem'); >+ window.prepareCookieConsentedJS = function () { >+ const items = $('.cookieConsentedJSItem'); > const invalid = []; > const valid = []; > items.each(function () { >@@ -523,10 +534,10 @@ $( document ).ready( function () { > }); > // We failed validation > if (invalid.length > 0) { >- $('#consentJSWarning').show(); >+ $('#cookieConsentedJSWarning').show(); > return false; > } >- $('#consentJSWarning').hide(); >+ $('#cookieConsentedJSWarning').hide(); > if (valid.length === 0) { > return ''; > } >-- >2.37.1 (Apple Git-137.1)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 27378
:
115544
|
115545
|
115546
|
115547
|
115548
|
115756
|
116397
|
116398
|
116399
|
116400
|
116401
|
116402
|
116403
|
116416
|
116417
|
116418
|
116419
|
116420
|
116421
|
116422
|
116423
|
116978
|
116979
|
116980
|
116981
|
116982
|
116983
|
116984
|
116985
|
116986
|
121728
|
121729
|
121730
|
121731
|
121732
|
121733
|
121734
|
121735
|
121995
|
121996
|
121997
|
121998
|
121999
|
122000
|
122001
|
122002
|
122635
|
122636
|
122637
|
122638
|
122639
|
122640
|
122641
|
122642
|
124957
|
124958
|
124959
|
124960
|
124961
|
124962
|
124963
|
124964
|
124965
|
130072
|
130073
|
130074
|
130075
|
130076
|
130077
|
130078
|
130079
|
130080
|
135309
|
135310
|
135311
|
135312
|
135313
|
135314
|
135315
|
135316
|
135317
|
135318
|
151735
|
151736
|
151737
|
151738
|
151739
|
151740
|
151741
|
151742
|
151743
|
151744
|
151745
|
151746
|
151747
|
151748
|
151749
|
151750
|
151751
|
151752
|
151753
|
151826
|
151827
|
151828
|
151829
|
151830
|
151831
|
151832
|
151833
|
151834
|
151835
|
151836
|
151837
|
151838
|
151839
|
151840
|
151841
|
151842
|
151843
|
151844
|
153326
|
153327
|
153328
|
153329
|
153330
|
153331
|
153332
|
153333
|
153334
|
153335
|
153336
|
154015
|
154016
|
154017
|
154018
|
154019
|
154020
|
154021
|
154022
|
154023
|
154024
|
154025
|
154093
|
154264
|
154265
|
154266
|
154267
|
154268
|
154269
|
154270
|
154271
|
154272
|
154273
|
154274
|
154275
|
154276