@@ -87,34 +87,40 @@ document.getElementById('reference-association').addEventListener('load', ({ tar
8787 const content = target . contentDocument . body || target . contentWindow . document . body ;
8888
8989 // If copy button used
90- if ( content . querySelector ( '#jform_id' ) . value !== target . getAttribute ( 'data-id' ) ) {
90+ if ( content . querySelector ( '#jform_id' ) && content . querySelector ( '#jform_id' ) . value !== target . getAttribute ( 'data-id' ) ) {
9191 const targetAssociation = document . getElementById ( 'target-association' ) ;
9292 targetAssociation . src = `${ targetAssociation . getAttribute ( 'data-editurl' ) } &task=${ targetAssociation . getAttribute ( 'data-item' ) } .edit&id=${ content . querySelector ( '#jform_id' ) . value } ` ;
9393 target . src = `${ target . getAttribute ( 'data-editurl' ) } &task=${ target . getAttribute ( 'data-item' ) } .edit&id=${ target . getAttribute ( 'data-id' ) } ` ;
9494 }
9595
9696 // Disable language field.
97- content . querySelector ( '#jform_language' ) . setAttribute ( 'disabled' , 'disabled' ) ;
97+ if ( content . querySelector ( '#jform_language' ) ) {
98+ content . querySelector ( '#jform_language' ) . setAttribute ( 'disabled' , 'disabled' ) ;
99+ }
98100
99101 // Remove modal buttons on the reference
100- content . querySelector ( '#associations .btn' ) . remove ( ) ;
101-
102- document . querySelectorAll ( '#jform_itemlanguage option' ) . forEach ( ( option ) => {
103- const parse = option . value . split ( ':' ) ;
104-
105- if ( typeof parse [ 0 ] !== 'undefined' ) {
106- // - For modal association selectors.
107- const langAssociation = parse [ 0 ] . replace ( / - / , '_' ) ;
102+ const associationBtn = content . querySelector ( '#associations .btn' ) ;
103+ if ( associationBtn ) {
104+ associationBtn . remove ( ) ;
105+ }
108106
109- const langAssociationId = content . querySelector ( `#jform_associations_${ langAssociation } _id` ) ;
110- if ( langAssociationId && langAssociationId . value === '' ) {
111- const referenceAssociation = document . getElementById ( 'reference-association' ) ;
112- if ( referenceAssociation . hasAttribute ( 'data-no-assoc' ) ) {
113- content . querySelector ( `#jform_associations_${ langAssociation } _name` ) . value = referenceAssociation . getAttribute ( 'data-no-assoc' ) ;
107+ const itemLanguageOptions = document . querySelectorAll ( '#jform_itemlanguage option' ) ;
108+ if ( itemLanguageOptions ) {
109+ itemLanguageOptions . forEach ( ( option ) => {
110+ const parse = option . value . split ( ':' ) ;
111+ if ( typeof parse [ 0 ] !== 'undefined' ) {
112+ // - For modal association selectors.
113+ const langAssociation = parse [ 0 ] . replace ( / - / , '_' ) ;
114+ const langAssociationId = content . querySelector ( `#jform_associations_${ langAssociation } _id` ) ;
115+ if ( langAssociationId && langAssociationId . value === '' ) {
116+ const referenceAssociation = document . getElementById ( 'reference-association' ) ;
117+ if ( referenceAssociation . hasAttribute ( 'data-no-assoc' ) ) {
118+ content . querySelector ( `#jform_associations_${ langAssociation } _name` ) . value = referenceAssociation . getAttribute ( 'data-no-assoc' ) ;
119+ }
114120 }
115121 }
116- }
117- } ) ;
122+ } ) ;
123+ }
118124
119125 // Iframe load finished, hide Joomla loading layer.
120126 const spinner = document . querySelector ( 'joomla-core-loader' ) ;
@@ -143,17 +149,20 @@ document.getElementById('target-association').addEventListener('load', ({ target
143149 // content.querySelector('#associations .btn').forEach(btn => btn.remove());
144150
145151 // Always show General tab first if associations tab is selected on the reference
146- if ( content . querySelector ( '#associations' ) . classList . contains ( 'active' ) ) {
152+ const associations = content . querySelector ( '#associations' ) ;
153+ if ( associations && associations . classList . contains ( 'active' ) ) {
147154 content . querySelector ( 'a[href="#associations"]' ) . parentNode . classList . remove ( 'active' ) ;
148- content . querySelector ( '# associations' ) . classList . remove ( 'active' ) ;
155+ associations . classList . remove ( 'active' ) ;
149156
150157 content . querySelector ( '.nav-tabs li' ) . classList . add ( 'active' ) ;
151158 content . querySelector ( '.tab-content .tab-pane' ) . classList . add ( 'active' ) ;
152159 }
153160
154161 // Update language field with the selected language and them disable it.
155- content . querySelector ( '#jform_language' ) . value = targetLanguage ;
156- content . querySelector ( '#jform_language' ) . setAttribute ( 'disabled' , 'disabled' ) ;
162+ if ( content . querySelector ( '#jform_language' ) ) {
163+ content . querySelector ( '#jform_language' ) . value = targetLanguage ;
164+ content . querySelector ( '#jform_language' ) . setAttribute ( 'disabled' , 'disabled' ) ;
165+ }
157166
158167 // If we are creating a new association (before save) we need to add the new association.
159168 if ( targetLoadedId === '0' ) {
@@ -174,7 +183,10 @@ document.getElementById('target-association').addEventListener('load', ({ target
174183 // If we created a new association (after save).
175184 if ( targetLoadedId !== targetId ) {
176185 // Refresh the language selector with the new id (used after save).
177- document . querySelector ( `#jform_itemlanguage option[value^="${ targetLanguage } :${ targetId } :add"]` ) . value = `${ targetLanguage } :${ targetLoadedId } :edit` ;
186+ const languageSelector = document . querySelector ( `#jform_itemlanguage option[value^="${ targetLanguage } :${ targetId } :add"]` ) ;
187+ if ( languageSelector ) {
188+ languageSelector . value = `${ targetLanguage } :${ targetLoadedId } :edit` ;
189+ }
178190
179191 // Update main frame data-id attribute (used after save).
180192 target . setAttribute ( 'data-id' , targetLoadedId ) ;
@@ -221,23 +233,31 @@ document.getElementById('target-association').addEventListener('load', ({ target
221233
222234 // - For chosen association selectors (menus).
223235 let chosenField = content . querySelector ( `#jform_associations_${ referenceLanguageCode } ` ) ;
224- chosenField . appendChild ( createOption ( referenceId , referenceTitle ) ) ;
225- chosenField . value = referenceId ;
236+ if ( chosenField ) {
237+ chosenField . appendChild ( createOption ( referenceId , referenceTitle ) ) ;
238+ chosenField . value = referenceId ;
239+ }
226240
227241 document . querySelectorAll ( '#jform_itemlanguage option' ) . forEach ( ( option ) => {
228242 const parse = option . value . split ( ':' ) ;
229243
230244 if ( typeof parse [ 1 ] !== 'undefined' && parse [ 1 ] !== '0' ) {
231245 // - For modal association selectors.
232246 const langAssociation = parse [ 0 ] . replace ( / - / , '_' ) ;
233- // eslint-disable-next-line prefer-destructuring
234- content . querySelector ( `#jform_associations_${ langAssociation } _id` ) . value = parse [ 1 ] ;
247+
248+ const formAssociationId = content . querySelector ( `#jform_associations_${ langAssociation } _id` ) ;
249+ if ( formAssociationId ) {
250+ // eslint-disable-next-line prefer-destructuring
251+ content . querySelector ( `#jform_associations_${ langAssociation } _id` ) . value = parse [ 1 ] ;
252+ }
235253
236254 // - For chosen association selectors (menus).
237255 chosenField = content . querySelector ( `#jform_associations_${ langAssociation } ` ) ;
238- chosenField . appendChild ( createOption ( parse [ 1 ] , '' ) ) ;
239- // eslint-disable-next-line prefer-destructuring
240- chosenField . value = parse [ 1 ] ;
256+ if ( chosenField ) {
257+ chosenField . appendChild ( createOption ( parse [ 1 ] , '' ) ) ;
258+ // eslint-disable-next-line prefer-destructuring
259+ chosenField . value = parse [ 1 ] ;
260+ }
241261 }
242262 } ) ;
243263
@@ -297,10 +317,17 @@ Joomla.submitbutton = (task) => {
297317 if ( lang ) {
298318 lang = lang . replace ( / - / , '_' ) ;
299319
300- // - For modal association selectors.
301- target . querySelector ( `#jform_associations_${ lang } _id` ) . value = '' ;
302- // - For chosen association selectors (menus).
303- target . querySelector ( `#jform_associations_${ lang } ` ) . value = '' ;
320+ const formAssociationId = target . querySelector ( `#jform_associations_${ lang } _id` ) ;
321+ if ( formAssociationId ) {
322+ // - For modal association selectors.
323+ formAssociationId . value = '' ;
324+ }
325+
326+ const formAssociation = target . querySelector ( `#jform_associations_${ lang } ` ) ;
327+ if ( formAssociation ) {
328+ // - For chosen association selectors (menus).
329+ formAssociation . value = '' ;
330+ }
304331 }
305332 } ) ;
306333
@@ -325,7 +352,10 @@ Joomla.submitbutton = (task) => {
325352 const currentLangSelect = document . getElementById ( 'jform_itemlanguage' ) ;
326353 const currentSwitcher = currentLangSelect . value ;
327354 const currentLang = targetLang . replace ( / _ / , '-' ) ;
328- document . querySelector ( `#jform_itemlanguage option[value="${ currentSwitcher } "]` ) . value = `${ currentLang } :0:add` ;
355+ const itemLanguageItem = document . querySelector ( `#jform_itemlanguage option[value="${ currentSwitcher } "]` ) ;
356+ if ( itemLanguageItem ) {
357+ itemLanguageItem . value = `${ currentLang } :0:add` ;
358+ }
329359 currentLangSelect . value = '' ;
330360 currentLangSelect . dispatchEvent ( new CustomEvent ( 'change' , {
331361 bubbles : true ,
@@ -339,7 +369,10 @@ Joomla.submitbutton = (task) => {
339369 // We need to re-enable the language field to save.
340370 const el = document . getElementById ( `${ task } -association` ) ;
341371 const content = el . contentDocument . body || el . contentWindow . document . body ;
342- content . querySelector ( '#jform_language' ) . removeAttribute ( 'disabled' ) ;
372+ const languageButton = content . querySelector ( '#jform_language' ) ;
373+ if ( languageButton ) {
374+ languageButton . removeAttribute ( 'disabled' ) ;
375+ }
343376 window . frames [ `${ task } -association` ] . Joomla . submitbutton ( `${ document . getElementById ( 'adminForm' ) . getAttribute ( 'data-associatedview' ) } .apply` ) ;
344377 }
345378
0 commit comments