-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathclickToHighlight.js
More file actions
62 lines (57 loc) 路 2.39 KB
/
Copy pathclickToHighlight.js
File metadata and controls
62 lines (57 loc) 路 2.39 KB
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
/**
* DataFormJS [clickToHighlight] Plugin.
*
* This allows a user to easily see where they are on
* wide table rows or mobile devices or highlight other
* elements under a list.
*
* This file depends on CSS, example:
* .highlight {background-color:yellow; }
*/
/* Validates with both [jshint] and [eslint] */
/* global app */
/* jshint strict: true */
/* eslint-env browser */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint strict: ["error", "function"] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
(function () {
'use strict';
function toggleHighlight(e) {
// Do not change highlight when a link or input is clicked. The reason is
// because when changing pages the row quickly flashes the highlight before
// the page changes. Since the user is clicking to another page rather
// than highlighting a row the style change is not desired. Additionally
// if the user is editing a row they will likely not want to trigger a
// highlight at the same time.
var nodeName = e.target.nodeName;
if (nodeName === 'A' || nodeName === 'INPUT' || nodeName === 'SELECT' || nodeName === 'TEXTAREA' || nodeName === 'BUTTON') {
return;
}
/* jshint validthis:true */
this.classList.toggle('highlight');
}
var clickToHighlight = {
onRendered: function (rootElement) {
var elements = (rootElement || document).querySelectorAll('.click-to-highlight');
Array.prototype.forEach.call(elements, function(el) {
var n, m;
if (el.tagName === 'TABLE' && el.tBodies.length === 1) {
var rows = el.tBodies[0].rows;
for (n = 0, m = rows.length; n < m; n++) {
rows[n].style.cursor = 'pointer';
rows[n].addEventListener('click', toggleHighlight);
}
} else {
for (n = 0, m = el.children.length; n < m; n++) {
el.children[n].style.cursor = 'pointer';
el.children[n].addEventListener('click', toggleHighlight);
}
}
});
}
};
// Add Plugin to DataFormsJS
app.addPlugin('clickToHighlight', clickToHighlight);
})();