|
| 1 | +/* global progressPlannerFocusElement */ |
| 2 | + |
| 3 | +const prplGetIndicatorElement = ( content, taskId, points ) => { |
| 4 | + // Create an <img> element. |
| 5 | + const imgEl = document.createElement( 'img' ); |
| 6 | + imgEl.src = |
| 7 | + progressPlannerFocusElement.base_url + |
| 8 | + '/assets/images/icon_progress_planner.svg'; |
| 9 | + imgEl.alt = points |
| 10 | + ? progressPlannerFocusElement.l10n.fixThisIssue.replace( '%d', points ) |
| 11 | + : ''; |
| 12 | + |
| 13 | + // Create a span element for the points. |
| 14 | + const spanEl = document.createElement( 'span' ); |
| 15 | + spanEl.textContent = content; |
| 16 | + |
| 17 | + // Create a span element for the wrapper. |
| 18 | + const wrapperEl = document.createElement( 'span' ); |
| 19 | + wrapperEl.classList.add( 'prpl-element-awards-points-icon-wrapper' ); |
| 20 | + wrapperEl.setAttribute( 'data-prpl-task-id', taskId ); |
| 21 | + |
| 22 | + // Add the image and span to the wrapper. |
| 23 | + wrapperEl.appendChild( imgEl ); |
| 24 | + wrapperEl.appendChild( spanEl ); |
| 25 | + |
| 26 | + return wrapperEl; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Maybe focus on the element, based on the URL. |
| 31 | + * |
| 32 | + * @param {Object} task The task object. |
| 33 | + */ |
| 34 | +const prplMaybeFocusOnElement = ( task ) => { |
| 35 | + // Check if we want to focus on the element, based on the URL. |
| 36 | + const url = new URL( window.location.href ); |
| 37 | + const focusOnElement = url.searchParams.get( 'pp-focus-el' ); |
| 38 | + if ( focusOnElement === task.task_id ) { |
| 39 | + let focused = false; |
| 40 | + const iconEls = document.querySelectorAll( |
| 41 | + `[data-prpl-task-id="${ task.task_id }"]` |
| 42 | + ); |
| 43 | + iconEls.forEach( ( el ) => { |
| 44 | + el.classList.add( 'focused' ); |
| 45 | + if ( ! focused ) { |
| 46 | + el.focus(); |
| 47 | + el.scrollIntoView( { behavior: 'smooth' } ); |
| 48 | + focused = true; |
| 49 | + } |
| 50 | + } ); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Add the points indicator to the element. |
| 56 | + * |
| 57 | + * @param {Object} task The task object. |
| 58 | + */ |
| 59 | +const prplAddPointsIndicatorToElement = ( task ) => { |
| 60 | + const points = task.points || 1; |
| 61 | + document.querySelectorAll( task.link_setting.iconEl ).forEach( ( el ) => { |
| 62 | + const iconEl = prplGetIndicatorElement( |
| 63 | + task.is_complete ? '✓' : '+' + points, |
| 64 | + task.task_id, |
| 65 | + points |
| 66 | + ); |
| 67 | + if ( task.is_complete ) { |
| 68 | + iconEl.classList.add( 'complete' ); |
| 69 | + } |
| 70 | + |
| 71 | + // Create a positioning wrapper. |
| 72 | + const wrapperEl = document.createElement( 'span' ); |
| 73 | + wrapperEl.classList.add( |
| 74 | + 'prpl-element-awards-points-icon-positioning-wrapper' |
| 75 | + ); |
| 76 | + |
| 77 | + // Add the icon to the wrapper. |
| 78 | + wrapperEl.appendChild( iconEl ); |
| 79 | + el.appendChild( wrapperEl ); |
| 80 | + } ); |
| 81 | +}; |
| 82 | + |
| 83 | +if ( progressPlannerFocusElement.tasks ) { |
| 84 | + /** |
| 85 | + * Add the points indicator to the element and maybe focus on it. |
| 86 | + */ |
| 87 | + progressPlannerFocusElement.tasks.forEach( ( task ) => { |
| 88 | + prplAddPointsIndicatorToElement( task ); |
| 89 | + prplMaybeFocusOnElement( task ); |
| 90 | + } ); |
| 91 | + |
| 92 | + /** |
| 93 | + * Add the points indicator to the page title. |
| 94 | + */ |
| 95 | + const prplPageTitle = document.querySelector( 'h1' ); |
| 96 | + const prplPageTitleIndicator = prplGetIndicatorElement( |
| 97 | + progressPlannerFocusElement.completedPoints + |
| 98 | + '/' + |
| 99 | + progressPlannerFocusElement.totalPoints, |
| 100 | + 'total' |
| 101 | + ); |
| 102 | + prplPageTitle.appendChild( prplPageTitleIndicator ); |
| 103 | +} |
0 commit comments