@@ -9,10 +9,10 @@ function getFromStorageSync(itemName, callback) {
99function getAppendContent ( productPrice , salary , percentageMode ) {
1010 if ( percentageMode ) {
1111 let percentage = getPriceInSalaryPercentage ( productPrice , salary ) ;
12- return `${ brackets . left } ${ percentage } %${ brackets . right } `
12+ return `${ percentage } %`
1313 }
1414 let timeTakenToEarn = getTimeTakenToEarn ( productPrice , salary ) ;
15- return ` ${ brackets . left } ${ timeTakenToEarn } ${ brackets . right } `
15+ return `${ timeTakenToEarn } `
1616}
1717
1818function getPriceInSalaryPercentage ( productPrice , salary ) {
@@ -274,46 +274,56 @@ function append(elementInfo, element, productPrice, settings, isPriceRange) {
274274 return ;
275275 }
276276
277- let span = document . createElement ( "span" ) ;
278- span . setAttribute ( 'id' , 'affordable' ) ;
279- desiredElement . appendChild ( span ) ;
277+ // Build badge
278+ let badge = document . createElement ( "span" ) ;
279+ badge . setAttribute ( 'id' , 'affordable' ) ;
280+ badge . className = 'affordable-badge' ;
281+
282+ let swatch = document . createElement ( "span" ) ;
283+ swatch . className = 'affordable-badge__swatch' ;
284+
285+ if ( settings . colourCodePrices ) {
286+ const percent = computeAffordabilityPercent ( productPrice , settings . salary ) ;
287+ const tier = percentToTier ( percent ) ;
288+ swatch . classList . add ( `aff-tier-${ tier } ` ) ;
289+ }
290+
291+ let text = document . createElement ( "span" ) ;
292+ text . className = 'affordable-badge__text' ;
293+ text . innerText = getAppendContent ( productPrice , settings . salary , settings . percentageMode ) ;
294+
295+ badge . appendChild ( swatch ) ;
296+ badge . appendChild ( text ) ;
297+ desiredElement . appendChild ( badge ) ;
298+
280299 if ( settings . hoverMode ) {
281300 // Hover Mode Attributes
282- span . style . display = 'none'
283- if ( settings . colourCodePrices ) {
284- addColourBasedOnPercentIntensity ( span , getPriceInSalaryPercentage ( productPrice , settings . salary ) / 100 )
285- }
286- span . innerText = " " + getAppendContent ( productPrice , settings . salary , settings . percentageMode )
301+ badge . style . display = 'none' ;
287302 element . addEventListener ( 'mouseover' , function handleMouseOver ( ) {
288- span . style . display = 'block ' ;
303+ badge . style . display = 'inline-flex ' ;
289304 } ) ;
290305 desiredElement . setAttribute ( "title" , `It will take you ${ getTimeTakenToEarn ( productPrice , settings . salary ) } to earn ${ productPrice } ` ) ;
291306 element . addEventListener ( 'mouseout' , function handleMouseOut ( ) {
292- span . style . display = 'none' ;
307+ badge . style . display = 'none' ;
293308 } ) ;
294-
295- } else {
296- // Normal Mode Attributes
297- let innerSpan = document . createElement ( "span" )
298- if ( settings . colourCodePrices ) {
299- addColourBasedOnPercentIntensity ( innerSpan , getPriceInSalaryPercentage ( productPrice , settings . salary ) / 100 )
300- }
301- innerSpan . innerText = getAppendContent ( productPrice , settings . salary , settings . percentageMode ) ;
302- span . appendChild ( innerSpan )
303- if ( ! isPriceRange ) {
304- span . setAttribute ( "style" , "display:block" ) ;
305- desiredElement . setAttribute ( "title" , `It will take you ${ getTimeTakenToEarn ( productPrice , settings . salary ) } to earn ${ productPrice } ` ) ;
306- }
309+ } else if ( ! isPriceRange ) {
310+ badge . setAttribute ( "style" , "display:inline-flex" ) ;
311+ desiredElement . setAttribute ( "title" , `It will take you ${ getTimeTakenToEarn ( productPrice , settings . salary ) } to earn ${ productPrice } ` ) ;
307312 }
308313}
309314
310- function addColourBasedOnPercentIntensity ( element , percent ) {
311- //value from 0 to 1
312- if ( percent > 1 )
313- percent = 1 ;
314- var hue = ( ( 1 - percent ) * 120 ) . toString ( 10 ) ;
315- let backgroundColor = [ "hsl(" , hue , ",100%,50%)" ] . join ( "" ) ;
316- element . style . color = backgroundColor ;
315+ function computeAffordabilityPercent ( productPrice , salary ) {
316+ let percent = productPrice / salary ;
317+ if ( percent < 0 ) percent = 0 ;
318+ if ( percent > 1 ) percent = 1 ;
319+ return percent ;
320+ }
321+
322+ function percentToTier ( percent ) {
323+ let p = percent ;
324+ if ( p < 0 ) p = 0 ;
325+ if ( p > 1 ) p = 1 ;
326+ return Math . round ( p * 10 ) ;
317327}
318328
319329function parseElementValue ( elementValue ) {
0 commit comments