|
| 1 | +/* global customElements, HTMLElement */ |
| 2 | +/* |
| 3 | + * Badge Progress Bar |
| 4 | + * |
| 5 | + * A web component to display a badge progress bar. |
| 6 | + * |
| 7 | + * Dependencies: progress-planner/l10n, progress-planner/web-components/prpl-badge |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Register the custom web component. |
| 12 | + */ |
| 13 | +customElements.define( |
| 14 | + 'prpl-badge-progress-bar', |
| 15 | + class extends HTMLElement { |
| 16 | + constructor( badgeId, points, maxPoints ) { |
| 17 | + // Get parent class properties |
| 18 | + super(); |
| 19 | + badgeId = badgeId || this.getAttribute( 'data-badge-id' ); |
| 20 | + points = points || this.getAttribute( 'data-points' ); |
| 21 | + maxPoints = maxPoints || this.getAttribute( 'data-max-points' ); |
| 22 | + const progress = ( points / maxPoints ) * 100; |
| 23 | + |
| 24 | + this.innerHTML = ` |
| 25 | + <div class="prpl-badge-progress-bar-container" style="padding: 1rem 0;"> |
| 26 | + <div |
| 27 | + class="prpl-badge-progress-bar" |
| 28 | + style=" |
| 29 | + width: 100%; |
| 30 | + height: 1rem; |
| 31 | + background-color: var(--prpl-color-gray-1); |
| 32 | + border-radius: 0.5rem; |
| 33 | + position: relative;" |
| 34 | + > |
| 35 | + <div |
| 36 | + class="prpl-badge-progress-bar-progress" |
| 37 | + style=" |
| 38 | + width: ${ progress }%; |
| 39 | + height: 100%; |
| 40 | + background-color: var(--prpl-color-accent-orange); |
| 41 | + border-radius: 0.5rem;" |
| 42 | + ></div> |
| 43 | + <prpl-badge |
| 44 | + badge-id="${ badgeId }" |
| 45 | + style=" |
| 46 | + display:flex; |
| 47 | + width: 5rem; |
| 48 | + height: 5rem; |
| 49 | + position: absolute; |
| 50 | + left: calc(${ progress }% - 2.5rem); |
| 51 | + top: -2.5rem;" |
| 52 | + ></prpl-badge> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + `; |
| 56 | + } |
| 57 | + } |
| 58 | +); |
| 59 | + |
| 60 | +/** |
| 61 | + * Update the previous month badge progress bar. |
| 62 | + * |
| 63 | + * @param {number} pointsDiff The points difference. |
| 64 | + * |
| 65 | + * @return {void} |
| 66 | + */ |
| 67 | +// eslint-disable-next-line no-unused-vars |
| 68 | +const prplUpdatePreviousMonthBadgeProgressBar = ( pointsDiff ) => { |
| 69 | + const progressBars = document.querySelectorAll( |
| 70 | + '.prpl-previous-month-badge-progress-bar-wrapper prpl-badge-progress-bar' |
| 71 | + ); |
| 72 | + |
| 73 | + // Bail early if no badge progress bars are found. |
| 74 | + if ( ! progressBars.length ) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Get the 1st incomplete badge progress bar. |
| 79 | + const progressBar = |
| 80 | + parseInt( progressBars[ 0 ]?.getAttribute( 'data-points' ) ) >= |
| 81 | + parseInt( progressBars[ 0 ]?.getAttribute( 'data-max-points' ) ) |
| 82 | + ? progressBars[ 1 ] |
| 83 | + : progressBars[ 0 ]; |
| 84 | + |
| 85 | + // Bail early if no badge progress bar is found. |
| 86 | + if ( ! progressBar ) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Get the badge progress bar properties. |
| 91 | + const badgeId = progressBar.getAttribute( 'data-badge-id' ); |
| 92 | + const badgePoints = progressBar.getAttribute( 'data-points' ); |
| 93 | + const badgeMaxPoints = progressBar.getAttribute( 'data-max-points' ); |
| 94 | + const badgeProgress = customElements.get( 'prpl-badge-progress-bar' ); |
| 95 | + const badgeNewPoints = parseInt( badgePoints ) + pointsDiff; |
| 96 | + |
| 97 | + // Create a new badge progress bar. |
| 98 | + const newProgressBar = new badgeProgress( |
| 99 | + badgeId, |
| 100 | + badgeNewPoints, |
| 101 | + badgeMaxPoints |
| 102 | + ); |
| 103 | + newProgressBar.setAttribute( 'data-badge-id', badgeId ); |
| 104 | + newProgressBar.setAttribute( 'data-points', badgeNewPoints ); |
| 105 | + newProgressBar.setAttribute( 'data-max-points', badgeMaxPoints ); |
| 106 | + |
| 107 | + // Replace the old badge progress bar with the new one. |
| 108 | + progressBar.replaceWith( newProgressBar ); |
| 109 | + |
| 110 | + // Update the remaining points. |
| 111 | + const remainingPointsEl = document.querySelector( |
| 112 | + `.prpl-previous-month-badge-progress-bar-wrapper[data-badge-id="${ badgeId }"] .prpl-previous-month-badge-progress-bar-remaining` |
| 113 | + ); |
| 114 | + |
| 115 | + if ( remainingPointsEl ) { |
| 116 | + remainingPointsEl.textContent = remainingPointsEl.textContent.replace( |
| 117 | + remainingPointsEl.getAttribute( 'data-remaining' ), |
| 118 | + badgeMaxPoints - badgeNewPoints |
| 119 | + ); |
| 120 | + remainingPointsEl.setAttribute( |
| 121 | + 'data-remaining', |
| 122 | + badgeMaxPoints - badgeNewPoints |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + // Update the previous month badge points number. |
| 127 | + const badgePointsNumberEl = document.querySelector( |
| 128 | + `.prpl-previous-month-badge-progress-bar-wrapper[data-badge-id="${ badgeId }"] .prpl-widget-previous-ravi-points-number` |
| 129 | + ); |
| 130 | + if ( badgePointsNumberEl ) { |
| 131 | + badgePointsNumberEl.textContent = badgeNewPoints + 'pt'; |
| 132 | + } |
| 133 | + |
| 134 | + // If the previous month badge is completed, update badge elements. |
| 135 | + if ( badgeNewPoints >= parseInt( badgeMaxPoints ) ) { |
| 136 | + document |
| 137 | + .querySelectorAll( |
| 138 | + `.prpl-badge-row-wrapper-inner .prpl-badge prpl-badge[complete="false"][badge-id="${ badgeId }"]` |
| 139 | + ) |
| 140 | + ?.forEach( ( badge ) => { |
| 141 | + badge.setAttribute( 'complete', 'true' ); |
| 142 | + } ); |
| 143 | + |
| 144 | + // Remove the previous month badge progress bar. |
| 145 | + document |
| 146 | + .querySelector( |
| 147 | + `.prpl-previous-month-badge-progress-bar-wrapper[data-badge-id="${ badgeId }"]` |
| 148 | + ) |
| 149 | + ?.remove(); |
| 150 | + |
| 151 | + // If there are no more progress bars, remove the previous month badge progress bar wrapper. |
| 152 | + if ( |
| 153 | + ! document.querySelector( |
| 154 | + '.prpl-previous-month-badge-progress-bar-wrapper' |
| 155 | + ) |
| 156 | + ) { |
| 157 | + document |
| 158 | + .querySelector( |
| 159 | + '.prpl-previous-month-badge-progress-bars-wrapper' |
| 160 | + ) |
| 161 | + ?.remove(); |
| 162 | + } |
| 163 | + } |
| 164 | +}; |
0 commit comments