-
Notifications
You must be signed in to change notification settings - Fork 882
/
AssessorPresenter.js
378 lines (328 loc) · 10.3 KB
/
AssessorPresenter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* eslint-disable tree-shaking/no-side-effects-in-initialization */
import { __ } from "@wordpress/i18n";
import { difference, forEach, isNumber, isObject, isUndefined } from "lodash-es";
import { assessmentPresenterResult as template } from "../../snippetPreview/templates.js";
import scoreToRating from "../interpreters/scoreToRating.js";
import createConfig from "../../config/presenter.js";
/**
* Represents the AssessorPresenter.
*/
class AssessorPresenter {
/**
* Constructs the AssessorPresenter.
*
* @param {Object} args A list of arguments to use in the presenter.
* @param {object} args.targets The HTML elements to render the output to.
* @param {string} args.targets.output The HTML element to render the individual ratings out to.
* @param {string} args.targets.overall The HTML element to render the overall rating out to.
* @param {string} args.keyword The keyword to use for checking, when calculating the overall rating.
* @param {SEOAssessor} args.assessor The Assessor object to retrieve assessment results from.
*
* @constructor
*/
constructor( args ) {
this.keyword = args.keyword;
this.assessor = args.assessor;
this.output = args.targets.output;
this.overall = args.targets.overall || "overallScore";
this.presenterConfig = createConfig();
this._disableMarkerButtons = false;
this._activeMarker = false;
}
/**
* Sets the keyword.
*
* @param {string} keyword The keyword to use.
* @returns {void}
*/
setKeyword( keyword ) {
this.keyword = keyword;
}
/**
* Checks whether a specific property exists in the presenter configuration.
*
* @param {string} property The property name to search for.
* @returns {boolean} Whether or not the property exists.
*/
configHasProperty( property ) {
return this.presenterConfig.hasOwnProperty( property );
}
/**
* Gets a fully formatted indicator object that can be used.
*
* @param {string} rating The rating to use.
* @returns {Object} An object containing the class, the screen reader text, and the full text.
*/
getIndicator( rating ) {
return {
className: this.getIndicatorColorClass( rating ),
screenReaderText: this.getIndicatorScreenReaderText( rating ),
fullText: this.getIndicatorFullText( rating ),
screenReaderReadabilityText: this.getIndicatorScreenReaderReadabilityText( rating ),
};
}
/**
* Gets the indicator color class from the presenter configuration, if it exists.
*
* @param {string} rating The rating to check against the config.
* @returns {string} String containing the CSS class to be used.
*/
getIndicatorColorClass( rating ) {
if ( ! this.configHasProperty( rating ) ) {
return "";
}
return this.presenterConfig[ rating ].className;
}
/**
* Get the indicator screen reader text from the presenter configuration, if it exists.
*
* @param {string} rating The rating to check against the config.
* @returns {string} Translated string containing the screen reader text to be used.
*/
getIndicatorScreenReaderText( rating ) {
if ( ! this.configHasProperty( rating ) ) {
return "";
}
return this.presenterConfig[ rating ].screenReaderText;
}
/**
* Get the indicator screen reader readability text from the presenter configuration, if it exists.
*
* @param {string} rating The rating to check against the config.
* @returns {string} Translated string containing the screen reader readability text to be used.
*/
getIndicatorScreenReaderReadabilityText( rating ) {
if ( ! this.configHasProperty( rating ) ) {
return "";
}
return this.presenterConfig[ rating ].screenReaderReadabilityText;
}
/**
* Get the indicator full text from the presenter configuration, if it exists.
*
* @param {string} rating The rating to check against the config.
* @returns {string} Translated string containing the full text to be used.
*/
getIndicatorFullText( rating ) {
if ( ! this.configHasProperty( rating ) ) {
return "";
}
return this.presenterConfig[ rating ].fullText;
}
/**
* Adds a rating based on the numeric score.
*
* @param {Object} result Object based on the Assessment result. Requires a score property to work.
* @returns {Object} The Assessment result object with the rating added.
*/
resultToRating( result ) {
if ( ! isObject( result ) ) {
return "";
}
result.rating = scoreToRating( result.score );
return result;
}
/**
* Takes the individual assessment results, sorts and rates them.
*
* @returns {Object} Object containing all the individual ratings.
*/
getIndividualRatings() {
const ratings = {};
const validResults = this.sort( this.assessor.getValidResults() );
const mappedResults = validResults.map( this.resultToRating );
forEach( mappedResults, function( item, key ) {
ratings[ key ] = this.addRating( item );
}.bind( this ) );
return ratings;
}
/**
* Excludes items from the results that are present in the exclude array.
*
* @param {Array} results Array containing the items to filter through.
* @param {Array} exclude Array of results to exclude.
* @returns {Array} Array containing items that remain after exclusion.
*/
excludeFromResults( results, exclude ) {
return difference( results, exclude );
}
/**
* Sorts results based on their score property and always places items considered to be unsortable, at the top.
*
* @param {Array} results Array containing the results that need to be sorted.
* @returns {Array} Array containing the sorted results.
*/
sort( results ) {
const unsortables = this.getUndefinedScores( results );
const sortables = this.excludeFromResults( results, unsortables );
sortables.sort( function( a, b ) {
return a.score - b.score;
} );
return unsortables.concat( sortables );
}
/**
* Returns a subset of results that have an undefined score or a score set to zero.
*
* @param {Array} results The results to filter through.
* @returns {Array} A subset of results containing items with an undefined score or where the score is zero.
*/
getUndefinedScores( results ) {
return results.filter( function( result ) {
return isUndefined( result.score ) || result.score === 0;
} );
}
/**
* Creates a rating object based on the item that is being passed.
*
* @param {AssessmentResult} item The item to check and create a rating object from.
* @returns {Object} Object containing a parsed item, including a colored indicator.
*/
addRating( item ) {
const indicator = this.getIndicator( item.rating );
indicator.text = item.text;
indicator.identifier = item.getIdentifier();
if ( item.hasMarker() ) {
indicator.marker = item.getMarker();
}
return indicator;
}
/**
* Calculates the overall rating score based on the overall score.
*
* @param {Number} overallScore The overall score to use in the calculation.
* @returns {Object} The rating based on the score.
*/
getOverallRating( overallScore ) {
let rating = 0;
if ( this.keyword === "" ) {
return this.resultToRating( { score: rating } );
}
if ( isNumber( overallScore ) ) {
rating = ( overallScore / 10 );
}
return this.resultToRating( { score: rating } );
}
/**
* Mark with a given marker. This will set the active marker to the correct value.
*
* @param {string} identifier The identifier for the assessment/marker.
* @param {Function} marker The marker function.
* @returns {void}
*/
markAssessment( identifier, marker ) {
if ( this._activeMarker === identifier ) {
this.removeAllMarks();
this._activeMarker = false;
} else {
marker();
this._activeMarker = identifier;
}
this.render();
}
/**
* Disables the currently active marker in the UI.
*
* @returns {void}
*/
disableMarker() {
this._activeMarker = false;
this.render();
}
/**
* Disables the marker buttons.
*
* @returns {void}
*/
disableMarkerButtons() {
this._disableMarkerButtons = true;
this.render();
}
/**
* Enables the marker buttons.
*
* @returns {void}
*/
enableMarkerButtons() {
this._disableMarkerButtons = false;
this.render();
}
/**
* Adds an event listener for the marker button
*
* @param {string} identifier The identifier for the assessment the marker belongs to.
* @param {Function} marker The marker function that can mark the assessment in the text.
* @returns {void}
*/
addMarkerEventHandler( identifier, marker ) {
const container = document.getElementById( this.output );
const markButton = container.getElementsByClassName( "js-assessment-results__mark-" + identifier )[ 0 ];
markButton.addEventListener( "click", this.markAssessment.bind( this, identifier, marker ) );
}
/**
* Renders out both the individual and the overall ratings.
*
* @returns {void}
*/
render() {
this.renderIndividualRatings();
this.renderOverallRating();
}
/**
* Adds event handlers to the mark buttons
*
* @param {Array} scores The list of rendered scores.
*
* @returns {void}
*/
bindMarkButtons( scores ) {
// Make sure the button works for every score with a marker.
forEach( scores, function( score ) {
if ( score.hasOwnProperty( "marker" ) ) {
this.addMarkerEventHandler( score.identifier, score.marker );
}
}.bind( this ) );
}
/**
* Removes all marks currently on the text
*
* @returns {void}
*/
removeAllMarks() {
const marker = this.assessor.getSpecificMarker();
marker( this.assessor.getPaper(), [] );
}
/**
* Renders out the individual ratings.
*
* @returns {void}
*/
renderIndividualRatings() {
const outputTarget = document.getElementById( this.output );
const scores = this.getIndividualRatings();
outputTarget.innerHTML = template( {
scores: scores,
i18n: {
disabledMarkText: __( "Marks are disabled in current view", "wordpress-seo" ),
markInText: __( "Mark this result in the text", "wordpress-seo" ),
removeMarksInText: __( "Remove marks in the text", "wordpress-seo" ),
},
activeMarker: this._activeMarker,
markerButtonsDisabled: this._disableMarkerButtons,
} );
this.bindMarkButtons( scores );
}
/**
* Renders out the overall rating.
*
* @returns {void}
*/
renderOverallRating() {
const overallRating = this.getOverallRating( this.assessor.calculateOverallScore() );
const overallRatingElement = document.getElementById( this.overall );
if ( ! overallRatingElement ) {
return;
}
overallRatingElement.className = "overallScore " + this.getIndicatorColorClass( overallRating.rating );
}
}
export default AssessorPresenter;