-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathhelpers.js
410 lines (363 loc) · 12.2 KB
/
helpers.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import Adapt from 'core/js/adapt';
const defaultAriaLevels = {
'_menu': 1,
'_menuGroup': 2,
'_menuItem': 2,
'_page': 1,
'_article': 2,
'_block': 3,
'_component': 4,
'_componentItem': 5,
'_notify': 1
};
const helpers = {
lowercase(text) {
return text.toLowerCase();
},
capitalise(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
},
inc(index) {
return index + 1;
},
dec(index) {
return index - 1;
},
odd (index) {
return (index + 1) % 2 === 0 ? 'even' : 'odd';
},
equals(value, text, block) {
return helpers.compare.call(this, value, '==', text, block);
},
compare(value, operator, text, block) {
// Comparison operators
switch (operator) {
case '===':
if (value === text) return block.fn(this);
break;
case '=': case '==':
// eslint-disable-next-line eqeqeq
if (value == text) return block.fn(this);
break;
case '>=':
if (value >= text) return block.fn(this);
break;
case '<=':
if (value <= text) return block.fn(this);
break;
case '>':
if (value > text) return block.fn(this);
break;
case '<':
if (value < text) return block.fn(this);
break;
}
return block.inverse(this);
},
math(lvalue, operator, rvalue, options) {
// Mathematical operators
lvalue = parseFloat(lvalue);
rvalue = parseFloat(rvalue);
switch (operator) {
case '+': return lvalue + rvalue;
case '-': return lvalue - rvalue;
case '*': return lvalue * rvalue;
case '/': return lvalue / rvalue;
case '%': return lvalue % rvalue;
}
},
/**
* Equivalent to:
* if (conditionA || conditionB)
* @example
* {{#any displayTitle body instruction}}
* <div class='component__header {{_component}}__header'></div>
* {{/any}}
*/
any(...args) {
const specified = args.slice(0, -1);
const block = args.slice(-1)[0];
return specified.some(Boolean) ? (block.fn ? block.fn(this) : true) : (block.inverse ? block.inverse(this) : false);
},
/**
* Equivalent to:
* if (conditionA && conditionB)
* @example
* {{#all displayTitle body instruction}}
* <div class='component__header {{_component}}__header'></div>
* {{/all}}
*/
all(...args) {
const specified = args.slice(0, -1);
const block = args.slice(-1)[0];
return specified.every(Boolean) ? (block.fn ? block.fn(this) : true) : (block.inverse ? block.inverse(this) : false);
},
/**
* Equivalent to:
* if (!conditionA && !conditionB)
* @example
* {{#none displayTitle body instruction}}
* <div class='component__header {{_component}}__header'></div>
* {{/none}}
*/
none(...args) {
const specified = args.slice(0, -1);
const block = args.slice(-1)[0];
return !specified.some(Boolean) ? (block.fn ? block.fn(this) : true) : (block.inverse ? block.inverse(this) : false);
},
/**
* Allow JSON to be a template i.e. you can use handlebars {{expressions}} within your JSON
*/
compile(template, context) {
if (!template) {
return '';
}
if (template instanceof Object) template = template.toString();
let data = this;
if (context) {
// choose between a passed argument context or the default handlebars helper context
data = (!context.data || !context.data.root ? context : context.data.root);
}
return Handlebars.compile(template)(data);
},
/**
* Allow JSON to be a template and accessible text
*/
compile_a11y_text(template, context) {
Adapt.a11y.log.deprecated('a11y_text is no longer required. https://tink.uk/understanding-screen-reader-interaction-modes/');
return helpers.compile.call(this, template, context);
},
/**
* Allow JSON to be a template and normalized text
*/
compile_a11y_normalize(template, context) {
if (!template) {
return '';
}
if (template instanceof Object) template = template.toString();
return Handlebars.helpers.a11y_normalize.call(this, helpers.compile.call(this, template, context));
},
/**
* Remove all html tags except styling tags
*/
compile_a11y_remove_breaks(template, context) {
if (!template) {
return '';
}
return Handlebars.helpers.a11y_remove_breaks.call(this, helpers.compile.call(this, template, context));
},
/**
* makes the _globals object in course.json available to a template
*/
import_globals(context) {
if (context.data.root._globals) {
return '';
}
context.data.root._globals = Adapt.course.get('_globals');
return '';
},
/**
* makes the Adapt module data available to a template
*/
import_adapt(context) {
if (context.data.root.Adapt) {
return;
}
const adapt = context.data.root.Adapt = {};
let i, l, name;
const directImport = ['config', 'course'];
for (i = 0, l = directImport.length; i < l; i++) {
name = directImport[i];
// convert the model to a json object and add to the current context
adapt[name] = Adapt[name].toJSON();
}
const indexedImport = ['contentObjects', 'articles', 'blocks', 'components'];
for (i = 0, l = indexedImport.length; i < l; i++) {
name = indexedImport[i];
// convert the collection of models to an array of json objects
const importArray = Adapt[name].toJSON();
// convert the array of json models to an object indexed by id
const importIndex = {};
for (let i1 = 0, l1 = importArray.length; i1 < l1; i1++) {
const item = importArray[i1];
importIndex[item._id] = item;
}
// add the indexed object to the current context
adapt[name] = importIndex;
}
return '';
},
/**
* Allow components to fetch their component description.
*
* Creates an aria label using the `a11y_aria_label` helper containing
* the component description specified in the
* `_globals._component[componentName].ariaRegion`. This value is defined
* in the `properties.schema:globals.ariaRegion`.
*
* @param {string} [override]
* @returns {string}
*/
component_description(override, context) {
if (!this._isA11yComponentDescriptionEnabled) {
return;
}
const isNotDefined = (!this._globals._components || !this._globals._components['_' + this._component]);
if (isNotDefined) {
return;
}
const hasOverride = (arguments.length > 1);
let description;
if (hasOverride) {
description = override;
description = helpers.compile(description, context);
} else {
description = this._globals._components['_' + this._component].ariaRegion;
description = helpers.compile(description, override);
}
if (!description) {
return;
}
return new Handlebars.SafeString('<div class="aria-label">' + description + '</div>');
},
a11y_text(text) {
Adapt.a11y.log.deprecated('a11y_text is no longer required. https://tink.uk/understanding-screen-reader-interaction-modes/');
return text;
},
/**
* Handlebars helper for `Adapt.a11y.normalize(htmls)`.
*
* @param {string} htmls Any htmls.
* @returns {string}
*/
a11y_normalize(htmls) {
return Adapt.a11y.normalize.apply(Adapt.a11y, arguments);
},
/**
* Handlebars helper for `Adapt.a11y.removeBreaks(htmls)`.
*
* @param {string} htmls Any htmls.
* @returns {string}
*/
a11y_remove_breaks(htmls) {
return Adapt.a11y.removeBreaks.apply(Adapt.a11y, arguments);
},
/**
* Creates a div styled with tiny, transparent text.
* It it absolutely positioned.
* The text is not visibly readable but is read by screen readers.
*
* @param {string} htmls
* @returns {string}
*/
a11y_aria_label(htmls) {
let values = Array.prototype.slice.call(arguments, 0, -1);
values = values.filter(Boolean);
return new Handlebars.SafeString('<div class="aria-label">' + values.join(' ') + '</div>');
},
/**
* Creates a div styled with tiny, transparent text.
* It it relatively positioned.
* The text is not visibly readable but is read by screen readers.
*
* @param {string} htmls Aria label texts.
* @returns {string}
*/
a11y_aria_label_relative(htmls) {
let values = Array.prototype.slice.call(arguments, 0, -1);
values = values.filter(Boolean);
return new Handlebars.SafeString('<div class="aria-label relative">' + values.join(' ') + '</div>');
},
/**
* Creates a div styled with tiny, transparent text and `role"=img"`.
* It is used for representing an image to a screen reader user in an
* order which cannot be represented in the DOM in a way that achieves the
* styling objectives.
* It it absolutely positioned.
* The text is not visibly readable but is read by screen readers.
*
* @param {string} texts Aria label texts.
* @returns {string}
*/
a11y_aria_image(texts) {
let values = Array.prototype.slice.call(arguments, 0, -1);
values = values.filter(Boolean);
return new Handlebars.SafeString('<div class="aria-label" role="img" aria-label="' + values.join(' ') + '"></div>');
},
/**
* Returns an `a` tag which when receiving focus causes the focus to wrap
* to the top of the readable document.
*
* @returns {string}
*/
a11y_wrap_focus() {
const cfg = Adapt.config.get('_accessibility');
if (cfg._isPopupWrapFocusEnabled === false) return '';
return new Handlebars.SafeString('<a class="a11y-focusguard a11y-ignore a11y-ignore-focus" role="presentation"> </a>');
},
/**
* Creates the attributes for a subject heading text. `role="heading"` and
* `aria-level="#"`. It will use the `_ariaLevel` attribute from the current
* context if specified, a number if given as the `levelOrType` parameter,
* or a name from the configured aria levels hash.
*
* @param {number|string} levelOrType
* @returns {string}
*/
a11y_attrs_heading(levelOrType) {
// get the global configuration from config.json
const cfg = Adapt.config.get('_accessibility');
// default level to use if nothing overrides it
let level = 1;
// first check to see if the Handlebars context has an override
if (this._ariaLevel) {
levelOrType = this._ariaLevel;
}
if (isNaN(levelOrType) === false) {
// if a number is passed just use this
level = levelOrType;
} else if (_.isString(levelOrType)) {
// if a string is passed check if it is defined in global configuration
cfg._ariaLevels = cfg._ariaLevels || defaultAriaLevels;
if (cfg._ariaLevels && cfg._ariaLevels['_' + levelOrType] !== undefined) {
level = cfg._ariaLevels['_' + levelOrType];
}
}
return new Handlebars.SafeString(' role="heading" aria-level="' + level + '" ');
},
a11y_attrs_tabbable() {
Adapt.a11y.log.deprecated('a11y_attrs_tabbable should not be used. tabbable elements should be natively tabbable.');
return new Handlebars.SafeString(' role="region" tabindex="0" ');
},
/**
* Produce display text with alternative screen reader version.
* @param {string} visible Text that will be displayed on screen
* @param {string} alternatives Text that will be read by the screen reader (instead of what's displayed on screen)
* @example {{a11y_alt_text '$5bn' 'five billion dollars'}} or {{a11y_alt_text 'Here are some bits to read' 'There are' _items.length 'items to read'}}
*/
a11y_alt_text(visible, alternatives) {
let values = Array.prototype.slice.call(arguments, 1, -1);
values = values.filter(Boolean);
return new Handlebars.SafeString('<span aria-hidden="true">' + visible + '</span><span class="aria-label">' + values.join(' ') + '</span>');
}
};
// Compatibility references
Object.assign(helpers, {
if_value_equals() {
Adapt.a11y.log.deprecated('if_value_equals, use equals instead.');
return helpers.equals.apply(this, arguments);
},
numbers() {
Adapt.a11y.log.deprecated('numbers, use inc instead.');
return helpers.inc.apply(this, arguments);
},
lowerCase() {
Adapt.a11y.log.deprecated('lowerCase, use lowercase instead.');
return helpers.lowercase.apply(this, arguments);
}
});
for (let name in helpers) {
if (!helpers.hasOwnProperty(name)) continue;
Handlebars.registerHelper(name, helpers[name]);
}
export default helpers;