|
| 1 | +/* global progressPlannerYoastFocusElement, MutationObserver */ |
| 2 | +/** |
| 3 | + * yoast-focus-element script. |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Check if the value of the element matches the value specified in the task. |
| 9 | + * |
| 10 | + * @param {Element} element The element to check. |
| 11 | + * @param {Object} task The task to check. |
| 12 | + * @return {boolean} True if the value matches, false otherwise. |
| 13 | + */ |
| 14 | +function checkTaskValue( element, task ) { |
| 15 | + if ( ! task.valueElement ) { |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + const attributeName = task.valueElement.attributeName || 'value'; |
| 20 | + const attributeValue = task.valueElement.attributeValue; |
| 21 | + const operator = task.valueElement.operator || '='; |
| 22 | + const currentValue = element.getAttribute( attributeName ) || ''; |
| 23 | + |
| 24 | + return '!=' === operator |
| 25 | + ? currentValue !== attributeValue |
| 26 | + : currentValue === attributeValue; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Observe the Yoast sidebar clicks. |
| 31 | + */ |
| 32 | +function observeYoastSidebarClicks() { |
| 33 | + const container = document.querySelector( '#yoast-seo-settings' ); |
| 34 | + |
| 35 | + if ( ! container ) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const waitForNav = new MutationObserver( ( mutationsList, observer ) => { |
| 40 | + const nav = container.querySelector( |
| 41 | + 'nav.yst-sidebar-navigation__sidebar' |
| 42 | + ); |
| 43 | + if ( nav ) { |
| 44 | + // Sidebar nav loaded. |
| 45 | + observer.disconnect(); |
| 46 | + |
| 47 | + nav.addEventListener( 'click', ( e ) => { |
| 48 | + const link = e.target.closest( 'a' ); |
| 49 | + if ( link ) { |
| 50 | + // Sidebar link clicked. |
| 51 | + waitForMainAndObserveContent(); // re-run logic after clicking |
| 52 | + } |
| 53 | + } ); |
| 54 | + } |
| 55 | + } ); |
| 56 | + |
| 57 | + waitForNav.observe( container, { |
| 58 | + childList: true, |
| 59 | + subtree: true, |
| 60 | + } ); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Wait for the main content to load and observe the content. |
| 65 | + */ |
| 66 | +function waitForMainAndObserveContent() { |
| 67 | + const container = document.querySelector( '#yoast-seo-settings' ); |
| 68 | + if ( ! container ) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const waitForMain = new MutationObserver( ( mutationsList, observer ) => { |
| 73 | + const main = container.querySelector( 'main.yst-paper' ); |
| 74 | + if ( main ) { |
| 75 | + // Main loaded. |
| 76 | + observer.disconnect(); |
| 77 | + |
| 78 | + const childObserver = new MutationObserver( ( mutations ) => { |
| 79 | + for ( const mutation of mutations ) { |
| 80 | + if ( |
| 81 | + mutation.type === 'attributes' && |
| 82 | + mutation.attributeName === 'class' |
| 83 | + ) { |
| 84 | + const el = mutation.target; |
| 85 | + if ( |
| 86 | + el.parentElement === main && |
| 87 | + el.classList.contains( 'yst-opacity-100' ) |
| 88 | + ) { |
| 89 | + // Fully loaded content. |
| 90 | + childObserver.disconnect(); |
| 91 | + |
| 92 | + // Loop through the tasks and add the focus element. |
| 93 | + for ( const task of progressPlannerYoastFocusElement.tasks ) { |
| 94 | + // Try to find the toggleButton. |
| 95 | + const valueElement = el.querySelector( |
| 96 | + task.valueElement.elementSelector |
| 97 | + ); |
| 98 | + let raviIconPositionAbsolute = true; |
| 99 | + |
| 100 | + if ( valueElement ) { |
| 101 | + // We usually add icon to the option header. |
| 102 | + let addIconElement = valueElement.closest( |
| 103 | + task.iconElement |
| 104 | + ); |
| 105 | + |
| 106 | + // Exception is the upload input field. |
| 107 | + if ( |
| 108 | + ! addIconElement && |
| 109 | + valueElement.type === 'hidden' |
| 110 | + ) { |
| 111 | + addIconElement = valueElement |
| 112 | + .closest( 'fieldset' ) |
| 113 | + .querySelector( task.iconElement ); |
| 114 | + |
| 115 | + raviIconPositionAbsolute = false; |
| 116 | + } |
| 117 | + |
| 118 | + // Upload input field. |
| 119 | + if ( ! addIconElement ) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + // Append next to the valueElemen, only if it's not already there. |
| 124 | + if ( |
| 125 | + ! addIconElement.querySelector( |
| 126 | + '.prpl-form-row-ravi' |
| 127 | + ) |
| 128 | + ) { |
| 129 | + // Check for value if specified in task. |
| 130 | + const valueMatches = checkTaskValue( |
| 131 | + valueElement, |
| 132 | + task |
| 133 | + ); |
| 134 | + |
| 135 | + // Create a new span with the class prpl-form-row-ravi. |
| 136 | + const raviIconWrapper = |
| 137 | + document.createElement( 'span' ); |
| 138 | + raviIconWrapper.classList.add( |
| 139 | + 'prpl-form-row-ravi', |
| 140 | + 'prpl-element-awards-points-icon-wrapper' |
| 141 | + ); |
| 142 | + |
| 143 | + if ( valueMatches ) { |
| 144 | + raviIconWrapper.classList.add( |
| 145 | + 'complete' |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + // Styling for absolute positioning. |
| 150 | + if ( raviIconPositionAbsolute ) { |
| 151 | + addIconElement.style.position = |
| 152 | + 'relative'; |
| 153 | + |
| 154 | + raviIconWrapper.style.position = |
| 155 | + 'absolute'; |
| 156 | + raviIconWrapper.style.right = |
| 157 | + '3.5rem'; |
| 158 | + raviIconWrapper.style.top = '-7px'; |
| 159 | + } |
| 160 | + |
| 161 | + raviIconWrapper.appendChild( |
| 162 | + document.createElement( 'span' ) |
| 163 | + ); |
| 164 | + |
| 165 | + // Create an icon image. |
| 166 | + const iconImg = |
| 167 | + document.createElement( 'img' ); |
| 168 | + iconImg.src = |
| 169 | + progressPlannerYoastFocusElement.base_url + |
| 170 | + '/assets/images/icon_progress_planner.svg'; |
| 171 | + iconImg.alt = 'Ravi'; |
| 172 | + iconImg.width = 16; |
| 173 | + iconImg.height = 16; |
| 174 | + |
| 175 | + // Append the icon image to the raviIconWrapper. |
| 176 | + raviIconWrapper |
| 177 | + .querySelector( 'span' ) |
| 178 | + .appendChild( iconImg ); |
| 179 | + |
| 180 | + // Add the points to the raviIconWrapper. |
| 181 | + const pointsWrapper = |
| 182 | + document.createElement( 'span' ); |
| 183 | + pointsWrapper.classList.add( |
| 184 | + 'prpl-form-row-points' |
| 185 | + ); |
| 186 | + pointsWrapper.textContent = valueMatches |
| 187 | + ? '✓' |
| 188 | + : '+1'; |
| 189 | + raviIconWrapper.appendChild( |
| 190 | + pointsWrapper |
| 191 | + ); |
| 192 | + |
| 193 | + // Finally add the raviIconWrapper to the DOM. |
| 194 | + addIconElement.appendChild( |
| 195 | + raviIconWrapper |
| 196 | + ); |
| 197 | + |
| 198 | + // Watch for changes in aria-checked to update the icon dynamically |
| 199 | + const valueElementObserver = |
| 200 | + new MutationObserver( () => { |
| 201 | + // Check value again if specified |
| 202 | + const currentValueMatches = |
| 203 | + checkTaskValue( |
| 204 | + valueElement, |
| 205 | + task |
| 206 | + ); |
| 207 | + |
| 208 | + if ( currentValueMatches ) { |
| 209 | + raviIconWrapper.classList.add( |
| 210 | + 'complete' |
| 211 | + ); |
| 212 | + |
| 213 | + pointsWrapper.textContent = |
| 214 | + '✓'; |
| 215 | + } else { |
| 216 | + raviIconWrapper.classList.remove( |
| 217 | + 'complete' |
| 218 | + ); |
| 219 | + |
| 220 | + pointsWrapper.textContent = |
| 221 | + '+1'; |
| 222 | + } |
| 223 | + } ); |
| 224 | + |
| 225 | + valueElementObserver.observe( |
| 226 | + valueElement, |
| 227 | + { |
| 228 | + attributes: true, |
| 229 | + attributeFilter: [ |
| 230 | + task.valueElement |
| 231 | + .attributeName, |
| 232 | + ], |
| 233 | + } |
| 234 | + ); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } ); |
| 242 | + |
| 243 | + // Watch direct children of main.yst-paper |
| 244 | + main.querySelectorAll( ':scope > *' ).forEach( ( child ) => { |
| 245 | + childObserver.observe( child, { |
| 246 | + attributes: true, |
| 247 | + attributeFilter: [ 'class' ], |
| 248 | + } ); |
| 249 | + } ); |
| 250 | + } |
| 251 | + } ); |
| 252 | + |
| 253 | + waitForMain.observe( container, { |
| 254 | + childList: true, |
| 255 | + subtree: true, |
| 256 | + } ); |
| 257 | +} |
| 258 | + |
| 259 | +// Run once on initial page load. |
| 260 | +waitForMainAndObserveContent(); |
| 261 | +observeYoastSidebarClicks(); |
0 commit comments