-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help text toggle #751
Help text toggle #751
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,23 @@ class ReportGenerator { | |
return Math.round(totalScore * 100); | ||
}; | ||
|
||
const getItemRating = (value, aggregatorScored) => { | ||
if (typeof value === 'boolean') { | ||
return value ? 'good' : 'poor'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit extract good / poor / average to a file live "enum" like map const There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
// Limit the rating to average if this is a rating for Best Practices. | ||
let rating = aggregatorScored ? 'average' : 'poor'; | ||
if (value > 0.33) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extract magic numbers to a private file level const map There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not going to touch other people's stuff as part of this cl There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with sam here. If you can improve code immediately involved with what you're changing anyways, you should. Pulling these out would be helpful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If every PR is a refactor, PRs take way longer to get in and introduce errors unrelated to the PR. I'm happy to do this one because it's small. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, and there's always a limit so we can actually make progress, PRs aren't weighted down by the past, etc. But small simplifications along the way are also a good way to help pay down technical debt. People usually need to get new stuff done so pure refactor PRs don't come along that often. |
||
rating = 'average'; | ||
} | ||
if (value > 0.66) { | ||
rating = 'good'; | ||
} | ||
|
||
return rating; | ||
}; | ||
|
||
// Converts a name to a link. | ||
Handlebars.registerHelper('nameToLink', name => { | ||
return name.toLowerCase().replace(/\s/, '-'); | ||
|
@@ -79,22 +96,12 @@ class ReportGenerator { | |
return rating; | ||
}); | ||
|
||
// Converts a value to a rating string, which can be used inside the report for color styling. | ||
Handlebars.registerHelper('getItemRating', (value, aggregatorScored) => { | ||
if (typeof value === 'boolean') { | ||
return value ? 'good' : 'poor'; | ||
} | ||
|
||
// Limit the rating to average if this is a rating for Best Practices. | ||
let rating = aggregatorScored ? 'average' : 'poor'; | ||
if (value > 0.33) { | ||
rating = 'average'; | ||
} | ||
if (value > 0.66) { | ||
rating = 'good'; | ||
} | ||
// Converts a value to a rating string, which can be used inside the report | ||
// for color styling. | ||
Handlebars.registerHelper('getItemRating', getItemRating); | ||
|
||
return rating; | ||
Handlebars.registerHelper('showHelpText', value => { | ||
return getItemRating(value) === 'good' ? 'hidden' : ''; | ||
}); | ||
|
||
// Convert numbers to fixed point decimals | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,10 @@ function LighthouseReport() { | |
this.checkboxToggleView = document.querySelector('.js-toggle-view'); | ||
this.viewUserFeature = document.querySelector('.js-report-by-user-feature'); | ||
this.viewTechnology = document.querySelector('.js-report-by-technology'); | ||
this.itemDetails = document.querySelectorAll('.report-section__item-details'); | ||
|
||
this.updateView = this.updateView.bind(this); | ||
this.toggleHelpText = this.toggleHelpText.bind(this); | ||
|
||
this.addEventListeners(); | ||
} | ||
|
@@ -47,9 +49,19 @@ LighthouseReport.prototype = { | |
} | ||
}, | ||
|
||
toggleHelpText: function(e) { | ||
if (e.target.classList.contains('report-section__item-help-toggle')) { | ||
const el = e.currentTarget.parentNode.querySelector('.report-section__item-helptext'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. depending directly on the parentNode structure feels like smell, and potentially brittle code that could break with a trivial dom change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the handlers up a level to each item in the report ( |
||
el.hidden = !el.hidden; | ||
} | ||
}, | ||
|
||
addEventListeners: function() { | ||
this.printButton.addEventListener('click', this.onPrint); | ||
this.checkboxToggleView.addEventListener('change', this.updateView); | ||
Array.from(this.itemDetails).forEach(node => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...this.itemDetails].forEach should be fine, since itemDetails is an iterable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any advantage? |
||
node.addEventListener('click', this.toggleHelpText); | ||
}); | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a method on the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aggregatorScored
appears to never be usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an arg to the helper, but I don't see it used in the report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about moving this to a private method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done