govuk_publishing_components 21.63.2 → 21.63.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/govuk_publishing_components/components/govspeak.js +17 -15
- data/app/assets/javascripts/govuk_publishing_components/lib/govspeak/barchart-enhancement.js +18 -11
- data/app/assets/javascripts/govuk_publishing_components/lib/govspeak/magna-charta.js +423 -0
- data/app/views/govuk_publishing_components/components/_radio.html.erb +3 -1
- data/lib/govuk_publishing_components/version.rb +1 -1
- metadata +3 -3
- data/app/assets/javascripts/govuk_publishing_components/vendor/magna-charta.min.js +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dfe72210f443a0c8d4f94c41fc1996bffa3fe47b772ecc7851c4c223612d4c5
|
4
|
+
data.tar.gz: 299ba77eba106b9d4ba23257ff645fefa8b9a6ea5e5aa010f9f564a821d0fa73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9a85dda00e7198025eff0c30434c57ccf72e10840d81d32f8990ad9cb08d6989735eb387cfad56d09486e56d848662d27df14c14686dd47e24f5e444da2f02e
|
7
|
+
data.tar.gz: 0b95e6218d7772692270f7257545fe62a30608e9627f8f7b527e621f11a1f70d319af5b07a066c1fdbe5618fbfed1d580e4793f29f8b99a425bbe14de7a39f67
|
@@ -2,25 +2,27 @@ window.GOVUK = window.GOVUK || {}
|
|
2
2
|
window.GOVUK.Modules = window.GOVUK.Modules || {};
|
3
3
|
|
4
4
|
(function (Modules) {
|
5
|
-
|
5
|
+
function Govspeak () { }
|
6
6
|
|
7
|
-
|
8
|
-
this
|
9
|
-
if (!$element.hasClass('disable-youtube')) {
|
10
|
-
this.embedYoutube($element)
|
11
|
-
}
|
7
|
+
Govspeak.prototype.start = function ($module) {
|
8
|
+
this.$module = $module[0]
|
12
9
|
|
13
|
-
|
10
|
+
if (this.$module.className.indexOf('disable-youtube') === -1) {
|
11
|
+
this.embedYoutube()
|
14
12
|
}
|
15
13
|
|
16
|
-
this.
|
17
|
-
|
18
|
-
enhancement.init()
|
19
|
-
}
|
14
|
+
this.createBarcharts()
|
15
|
+
}
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
17
|
+
Govspeak.prototype.embedYoutube = function () {
|
18
|
+
var enhancement = new window.GOVUK.GovspeakYoutubeLinkEnhancement(this.$module)
|
19
|
+
enhancement.init()
|
25
20
|
}
|
21
|
+
|
22
|
+
Govspeak.prototype.createBarcharts = function () {
|
23
|
+
var enhancement = new window.GOVUK.GovspeakBarchartEnhancement(this.$module)
|
24
|
+
enhancement.init()
|
25
|
+
}
|
26
|
+
|
27
|
+
Modules.Govspeak = Govspeak
|
26
28
|
})(window.GOVUK.Modules)
|
data/app/assets/javascripts/govuk_publishing_components/lib/govspeak/barchart-enhancement.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
// = require govuk_publishing_components/
|
1
|
+
// = require govuk_publishing_components/lib/govspeak/magna-charta
|
2
2
|
|
3
3
|
window.GOVUK = window.GOVUK || {};
|
4
4
|
|
@@ -10,16 +10,23 @@ window.GOVUK = window.GOVUK || {};
|
|
10
10
|
}
|
11
11
|
|
12
12
|
BarchartEnhancement.prototype.init = function () {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
var
|
20
|
-
|
21
|
-
|
22
|
-
|
13
|
+
var $barchartsOriginal = this.$element.querySelectorAll('.js-barchart-table')
|
14
|
+
var $barcharts = []
|
15
|
+
|
16
|
+
for (var j = 0; j < $barchartsOriginal.length; j++) {
|
17
|
+
// this prevents the code from initialising more than once
|
18
|
+
// we'd use :not selectors for $barchartsOriginal but for lack of IE8 support
|
19
|
+
var styles = $barchartsOriginal[j].className
|
20
|
+
if (styles.indexOf('mc-chart') === -1 && styles.indexOf('js-barchart-table-init') === -1) {
|
21
|
+
$barcharts.push($barchartsOriginal[j])
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
for (var i = 0; i < $barcharts.length; i++) {
|
26
|
+
var $table = $barcharts[i]
|
27
|
+
new GOVUK.Modules.MagnaCharta().start($table, { toggleText: 'Change between chart and table' })
|
28
|
+
$table.className = $table.className + ' js-barchart-table-init'
|
29
|
+
}
|
23
30
|
}
|
24
31
|
|
25
32
|
GOVUK.GovspeakBarchartEnhancement = BarchartEnhancement
|
@@ -0,0 +1,423 @@
|
|
1
|
+
//= require govuk/vendor/polyfills/Element/prototype/classList.js
|
2
|
+
// This is a non-jQuery version of Magna Charta: https://github.com/alphagov/magna-charta
|
3
|
+
window.GOVUK = window.GOVUK || {}
|
4
|
+
window.GOVUK.Modules = window.GOVUK.Modules || {};
|
5
|
+
|
6
|
+
(function (Modules) {
|
7
|
+
function MagnaCharta () { }
|
8
|
+
|
9
|
+
MagnaCharta.prototype.start = function ($module, options) {
|
10
|
+
this.$module = $module[0]
|
11
|
+
this.options = {
|
12
|
+
outOf: 65,
|
13
|
+
applyOnInit: true,
|
14
|
+
toggleText: 'Toggle between chart and table',
|
15
|
+
autoOutdent: false,
|
16
|
+
outdentAll: false,
|
17
|
+
toggleAfter: false, // BOOL set TRUE to append the toggle link
|
18
|
+
returnReference: false // for testing purposes
|
19
|
+
}
|
20
|
+
|
21
|
+
for (var k in options) this.options[k] = options[k]
|
22
|
+
this.detectIEVersion()
|
23
|
+
|
24
|
+
// Magna Charta doesn't work in IE7 or less - so plain tables are shown to those browsers
|
25
|
+
this.ENABLED = !(this.ie && this.ie < 8)
|
26
|
+
|
27
|
+
// store a reference to the table in the object
|
28
|
+
this.$table = $module
|
29
|
+
|
30
|
+
// lets make what will become the new graph
|
31
|
+
this.$graph = document.createElement('div')
|
32
|
+
|
33
|
+
// set the graph to aria-hidden, which isn't changed at any point
|
34
|
+
// the graph is totally inaccessible, so we let screen readers navigate the table
|
35
|
+
// and ignore the graph entirely
|
36
|
+
this.$graph.setAttribute('aria-hidden', 'true')
|
37
|
+
|
38
|
+
// copy over classes from the table, and add the extra one
|
39
|
+
this.$graph.setAttribute('class', this.$table.className)
|
40
|
+
this.$graph.classList.add('mc-chart')
|
41
|
+
|
42
|
+
// set the stacked option based on
|
43
|
+
// giving the table a class of mc-stacked
|
44
|
+
this.options.stacked = this.$table.classList.contains('mc-stacked')
|
45
|
+
|
46
|
+
// set the negative option based on
|
47
|
+
// giving the table a class of mc-negative
|
48
|
+
this.options.negative = this.$table.classList.contains('mc-negative')
|
49
|
+
|
50
|
+
// true if it's a 'multiple' table
|
51
|
+
// this means multiple bars per rows, but not stacked.
|
52
|
+
var moreThanTwoTDs = this.$table.querySelectorAll('tbody tr')[0].querySelectorAll('td').length > 2
|
53
|
+
this.options.multiple = !this.options.stacked && (this.$table.classList.contains('mc-multiple') || moreThanTwoTDs)
|
54
|
+
|
55
|
+
// set the outdent options
|
56
|
+
// which can be set via classes or overriden by setting the value to true
|
57
|
+
// in the initial options object that's passed in
|
58
|
+
this.options.autoOutdent = this.options.autoOutdent || this.$table.classList.contains('mc-auto-outdent')
|
59
|
+
|
60
|
+
this.options.outdentAll = this.options.outdentAll || this.$table.classList.contains('mc-outdented')
|
61
|
+
|
62
|
+
// add a mc-multiple class if it is
|
63
|
+
if (this.options.multiple) {
|
64
|
+
this.$graph.classList.add('mc-multiple')
|
65
|
+
}
|
66
|
+
|
67
|
+
this.options.hasCaption = !!this.$table.querySelectorAll('caption').length
|
68
|
+
|
69
|
+
if (this.ENABLED) {
|
70
|
+
this.apply()
|
71
|
+
// if applyOnInit is false, toggle immediately to show the table and hide the graph
|
72
|
+
if (!this.options.applyOnInit) {
|
73
|
+
this.toggleLink.click()
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
if (this.options.returnReference) {
|
78
|
+
return this
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
MagnaCharta.prototype.detectIEVersion = function () {
|
83
|
+
// detect IE version: James Padolsey, https://gist.github.com/527683
|
84
|
+
this.ie = (function () {
|
85
|
+
var undef
|
86
|
+
var v = 3
|
87
|
+
var div = document.createElement('div')
|
88
|
+
var all = div.getElementsByTagName('i')
|
89
|
+
|
90
|
+
do {
|
91
|
+
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->'
|
92
|
+
} while (v < 10 && all[0])
|
93
|
+
|
94
|
+
return (v > 4) ? v : undef
|
95
|
+
})()
|
96
|
+
}
|
97
|
+
|
98
|
+
MagnaCharta.prototype.apply = function () {
|
99
|
+
if (this.ENABLED) {
|
100
|
+
this.constructChart()
|
101
|
+
this.addClassesToHeader()
|
102
|
+
this.applyWidths()
|
103
|
+
this.insert()
|
104
|
+
this.$table.classList.add('visually-hidden')
|
105
|
+
this.applyOutdent()
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
// methods for constructing the chart
|
110
|
+
MagnaCharta.prototype.construct = {}
|
111
|
+
|
112
|
+
// constructs the header
|
113
|
+
MagnaCharta.prototype.construct.thead = function () {
|
114
|
+
var thead = document.createElement('div')
|
115
|
+
thead.classList.add('mc-thead')
|
116
|
+
|
117
|
+
var tr = document.createElement('div')
|
118
|
+
tr.classList.add('mc-tr')
|
119
|
+
|
120
|
+
var output = ''
|
121
|
+
var allTheTHs = this.$table.querySelectorAll('th')
|
122
|
+
|
123
|
+
for (var i = 0; i < allTheTHs.length; i++) {
|
124
|
+
output += '<div class="mc-th">'
|
125
|
+
output += allTheTHs[i].innerHTML
|
126
|
+
output += '</div>'
|
127
|
+
}
|
128
|
+
|
129
|
+
tr.innerHTML = output
|
130
|
+
thead.appendChild(tr)
|
131
|
+
|
132
|
+
return thead
|
133
|
+
}
|
134
|
+
|
135
|
+
MagnaCharta.prototype.construct.tbody = function () {
|
136
|
+
var tbody = document.createElement('div')
|
137
|
+
tbody.classList.add('mc-tbody')
|
138
|
+
|
139
|
+
var allTheTbodyTrs = this.$table.querySelectorAll('tbody tr')
|
140
|
+
|
141
|
+
for (var i = 0; i < allTheTbodyTrs.length; i++) {
|
142
|
+
var tr = document.createElement('div')
|
143
|
+
tr.classList.add('mc-tr')
|
144
|
+
|
145
|
+
var cellsOutput = ''
|
146
|
+
var allTheTds = allTheTbodyTrs[i].querySelectorAll('td')
|
147
|
+
|
148
|
+
for (var j = 0; j < allTheTds.length; j++) {
|
149
|
+
cellsOutput += '<div class="mc-td">'
|
150
|
+
cellsOutput += allTheTds[j].innerHTML
|
151
|
+
cellsOutput += '</div>'
|
152
|
+
}
|
153
|
+
tr.innerHTML = cellsOutput
|
154
|
+
tbody.appendChild(tr)
|
155
|
+
}
|
156
|
+
|
157
|
+
return tbody
|
158
|
+
}
|
159
|
+
|
160
|
+
MagnaCharta.prototype.construct.caption = function () {
|
161
|
+
var cap = this.$table.querySelector('caption')
|
162
|
+
return cap.cloneNode(true)
|
163
|
+
}
|
164
|
+
|
165
|
+
// construct a link to allow the user to toggle between chart and table
|
166
|
+
MagnaCharta.prototype.construct.toggleLink = function (toggleText) {
|
167
|
+
var link = document.createElement('a')
|
168
|
+
link.setAttribute('href', '#')
|
169
|
+
link.classList.add('mc-toggle-link')
|
170
|
+
link.innerHTML = toggleText
|
171
|
+
link.setAttribute('aria-hidden', 'true')
|
172
|
+
|
173
|
+
return link
|
174
|
+
}
|
175
|
+
|
176
|
+
// toggles between showing the table and showing the chart
|
177
|
+
MagnaCharta.prototype.addToggleClick = function () {
|
178
|
+
var that = this
|
179
|
+
|
180
|
+
this.toggleLink.addEventListener('click', function (e) {
|
181
|
+
e.preventDefault()
|
182
|
+
that.$graph.classList.toggle('visually-hidden')
|
183
|
+
that.$table.classList.toggle('visually-hidden')
|
184
|
+
})
|
185
|
+
}
|
186
|
+
|
187
|
+
MagnaCharta.prototype.constructChart = function () {
|
188
|
+
// turn every element in the table into divs with appropriate classes
|
189
|
+
// call them and define this as scope so it's easier to
|
190
|
+
// get at options and properties
|
191
|
+
var thead = this.construct.thead.call(this)
|
192
|
+
var tbody = this.construct.tbody.call(this)
|
193
|
+
this.toggleLink = this.construct.toggleLink(this.options.toggleText)
|
194
|
+
this.addToggleClick(this.toggleLink)
|
195
|
+
|
196
|
+
if (this.options.hasCaption) {
|
197
|
+
var caption = this.construct.caption.call(this)
|
198
|
+
this.$graph.appendChild(caption)
|
199
|
+
}
|
200
|
+
|
201
|
+
if (this.options.toggleAfter) {
|
202
|
+
this.$table.insertAdjacentElement('afterend', this.toggleLink)
|
203
|
+
} else {
|
204
|
+
this.$table.insertAdjacentElement('beforebegin', this.toggleLink)
|
205
|
+
}
|
206
|
+
|
207
|
+
this.$graph.appendChild(thead)
|
208
|
+
this.$graph.appendChild(tbody)
|
209
|
+
}
|
210
|
+
|
211
|
+
// some handy utility methods
|
212
|
+
MagnaCharta.prototype.utils = {
|
213
|
+
isFloat: function (val) {
|
214
|
+
return !isNaN(parseFloat(val))
|
215
|
+
},
|
216
|
+
stripValue: function (val) {
|
217
|
+
var re = new RegExp('\\,|£|%|[a-z]', 'gi')
|
218
|
+
return val.replace(re, '')
|
219
|
+
},
|
220
|
+
returnMax: function (values) {
|
221
|
+
var max = 0
|
222
|
+
for (var i = 0; i < values.length; i++) {
|
223
|
+
if (values[i] > max) { max = values[i] }
|
224
|
+
}
|
225
|
+
return max
|
226
|
+
},
|
227
|
+
isNegative: function (value) {
|
228
|
+
return (value < 0)
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
MagnaCharta.prototype.addClassesToHeader = function () {
|
233
|
+
var headerCells = this.$graph.querySelectorAll('.mc-th')
|
234
|
+
var looplength = headerCells.length
|
235
|
+
|
236
|
+
if (this.options.stacked) {
|
237
|
+
var last = looplength - 1
|
238
|
+
headerCells[last].classList.add('mc-stacked-header', 'mc-header-total')
|
239
|
+
looplength -= 1
|
240
|
+
}
|
241
|
+
|
242
|
+
// we deliberately don't apply this to the first cell
|
243
|
+
for (var i = 1; i < looplength; i++) {
|
244
|
+
headerCells[i].classList.add('mc-key-header')
|
245
|
+
if (!headerCells[i].classList.contains('mc-stacked-header')) {
|
246
|
+
headerCells[i].classList.add('mc-key-' + i)
|
247
|
+
}
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
MagnaCharta.prototype.calculateMaxWidth = function () {
|
252
|
+
// store the cell values in here so we can figure out the maximum value later
|
253
|
+
var values = []
|
254
|
+
|
255
|
+
// var to store the maximum negative value (used only for negative charts)
|
256
|
+
var maxNegativeValue = 0
|
257
|
+
|
258
|
+
// loop through every tr in the table
|
259
|
+
var trs = this.$graph.querySelectorAll('.mc-tr')
|
260
|
+
for (var i = 0; i < trs.length; i++) {
|
261
|
+
var $this = trs[i]
|
262
|
+
|
263
|
+
// the first td is going to be the key, so ignore it
|
264
|
+
// we'd use $this.querySelectorAll('.mc-td:not(:first-child)') but for IE8
|
265
|
+
var $bodyCellsOriginal = $this.querySelectorAll('.mc-td')
|
266
|
+
var $bodyCells = []
|
267
|
+
for (var k = 1; k < $bodyCellsOriginal.length; k++) {
|
268
|
+
$bodyCells.push($bodyCellsOriginal[k])
|
269
|
+
}
|
270
|
+
|
271
|
+
var bodyCellsLength = $bodyCells.length
|
272
|
+
|
273
|
+
// might be the row containing th elements, so we need to check
|
274
|
+
if (bodyCellsLength) {
|
275
|
+
// if it's stacked, the last column is a totals
|
276
|
+
// so we don't want that in our calculations
|
277
|
+
if (this.options.stacked) {
|
278
|
+
$bodyCells[bodyCellsLength - 1].classList.add('mc-stacked-total')
|
279
|
+
bodyCellsLength -= 1
|
280
|
+
}
|
281
|
+
|
282
|
+
// first td in each row is key
|
283
|
+
var firstCell = $this.querySelector('.mc-td')
|
284
|
+
if (firstCell) {
|
285
|
+
firstCell.classList.add('mc-key-cell')
|
286
|
+
}
|
287
|
+
|
288
|
+
// store the total value of the bar cells in a row
|
289
|
+
// for anything but stacked, this is just the value of one <td>
|
290
|
+
var cellsTotalValue = 0
|
291
|
+
|
292
|
+
for (var j = 0; j < bodyCellsLength; j++) {
|
293
|
+
var $cell = $bodyCells[j]
|
294
|
+
$cell.classList.add('mc-bar-cell')
|
295
|
+
$cell.classList.add('mc-bar-' + (j + 1))
|
296
|
+
var cellVal = this.utils.stripValue($cell.innerText)
|
297
|
+
|
298
|
+
if (this.utils.isFloat(cellVal)) {
|
299
|
+
var parsedVal = parseFloat(cellVal, 10)
|
300
|
+
var absParsedVal = Math.abs(parsedVal)
|
301
|
+
if (parsedVal === 0) {
|
302
|
+
$cell.classList.add('mc-bar-zero')
|
303
|
+
}
|
304
|
+
|
305
|
+
if (this.options.negative) {
|
306
|
+
if (this.utils.isNegative(parsedVal)) {
|
307
|
+
$cell.classList.add('mc-bar-negative')
|
308
|
+
if (absParsedVal > maxNegativeValue) {
|
309
|
+
maxNegativeValue = absParsedVal
|
310
|
+
}
|
311
|
+
} else {
|
312
|
+
$cell.classList.add('mc-bar-positive')
|
313
|
+
}
|
314
|
+
}
|
315
|
+
// now we are done with our negative calculations
|
316
|
+
// set parsedVal to absParsedVal
|
317
|
+
parsedVal = absParsedVal
|
318
|
+
|
319
|
+
if (!this.options.stacked) {
|
320
|
+
cellsTotalValue = parsedVal
|
321
|
+
values.push(parsedVal)
|
322
|
+
} else {
|
323
|
+
cellsTotalValue += parsedVal
|
324
|
+
}
|
325
|
+
}
|
326
|
+
}
|
327
|
+
}
|
328
|
+
|
329
|
+
// if stacked, we need to push the total value of the row to the values array
|
330
|
+
if (this.options.stacked) { values.push(cellsTotalValue) }
|
331
|
+
}
|
332
|
+
|
333
|
+
var resp = {}
|
334
|
+
resp.max = parseFloat(this.utils.returnMax(values), 10)
|
335
|
+
resp.single = parseFloat(this.options.outOf / resp.max, 10)
|
336
|
+
|
337
|
+
if (this.options.negative) {
|
338
|
+
resp.marginLeft = parseFloat(maxNegativeValue, 10) * resp.single
|
339
|
+
resp.maxNegative = parseFloat(maxNegativeValue, 10)
|
340
|
+
}
|
341
|
+
|
342
|
+
return resp
|
343
|
+
}
|
344
|
+
|
345
|
+
MagnaCharta.prototype.applyWidths = function () {
|
346
|
+
this.dimensions = this.calculateMaxWidth()
|
347
|
+
var trs = this.$graph.querySelectorAll('.mc-tr')
|
348
|
+
|
349
|
+
for (var i = 0; i < trs.length; i++) {
|
350
|
+
var cells = trs[i].querySelectorAll('.mc-bar-cell')
|
351
|
+
|
352
|
+
for (var j = 0; j < cells.length; j++) {
|
353
|
+
var $cell = cells[j]
|
354
|
+
var parsedCellVal = parseFloat(this.utils.stripValue($cell.innerText), 10)
|
355
|
+
var parsedVal = parsedCellVal * this.dimensions.single
|
356
|
+
var absParsedCellVal = Math.abs(parsedCellVal)
|
357
|
+
var absParsedVal = Math.abs(parsedVal)
|
358
|
+
|
359
|
+
// apply the left margin to the positive bars
|
360
|
+
if (this.options.negative) {
|
361
|
+
if ($cell.classList.contains('mc-bar-positive')) {
|
362
|
+
$cell.style.marginLeft = this.dimensions.marginLeft + '%'
|
363
|
+
} else {
|
364
|
+
// if its negative but not the maximum negative
|
365
|
+
// we need to give it enough margin to push it further right to align
|
366
|
+
if (absParsedCellVal < this.dimensions.maxNegative) {
|
367
|
+
// left margin needs to be (largestNegVal - thisNegVal) * single
|
368
|
+
var leftMarg = (this.dimensions.maxNegative - absParsedCellVal) * this.dimensions.single
|
369
|
+
$cell.style.marginLeft = leftMarg + '%'
|
370
|
+
}
|
371
|
+
}
|
372
|
+
}
|
373
|
+
|
374
|
+
// wrap the cell value in a span tag
|
375
|
+
$cell.innerHTML = '<span>' + $cell.innerHTML + '</span>'
|
376
|
+
$cell.style.width = absParsedVal + '%'
|
377
|
+
}
|
378
|
+
}
|
379
|
+
}
|
380
|
+
|
381
|
+
MagnaCharta.prototype.insert = function () {
|
382
|
+
this.$table.insertAdjacentElement('afterend', this.$graph)
|
383
|
+
}
|
384
|
+
|
385
|
+
MagnaCharta.prototype.applyOutdent = function () {
|
386
|
+
/*
|
387
|
+
* this figures out if a cell needs an outdent and applies it
|
388
|
+
* it needs an outdent if the width of the text is greater than the width of the bar
|
389
|
+
* if this is the case, wrap the value in a span, and use absolute positioning
|
390
|
+
* to push it out (the bar is styled to be relative)
|
391
|
+
* unfortunately this has to be done once the chart has been inserted
|
392
|
+
*/
|
393
|
+
var cells = this.$graph.querySelectorAll('.mc-bar-cell')
|
394
|
+
|
395
|
+
for (var i = 0; i < cells.length; i++) {
|
396
|
+
var $cell = cells[i]
|
397
|
+
var cellVal = parseFloat(this.utils.stripValue($cell.innerText), 10)
|
398
|
+
var $cellSpan = $cell.querySelector('span')
|
399
|
+
var spanWidth = parseFloat(window.getComputedStyle($cellSpan, null).width.replace('px', '')) + 10 // +10 just for extra padding
|
400
|
+
var cellWidth = parseFloat(window.getComputedStyle($cell, null).width.replace('px', ''))
|
401
|
+
|
402
|
+
if (!this.options.stacked) {
|
403
|
+
// if it's 0, it is effectively outdented
|
404
|
+
if (cellVal === 0) { $cell.classList.add('mc-bar-outdented') }
|
405
|
+
|
406
|
+
if ((this.options.autoOutdent && spanWidth > cellWidth) || this.options.outdentAll) {
|
407
|
+
$cell.classList.add('mc-bar-outdented')
|
408
|
+
$cellSpan.style.marginLeft = '100%'
|
409
|
+
$cellSpan.style.display = 'inline-block'
|
410
|
+
} else {
|
411
|
+
$cell.classList.add('mc-bar-indented')
|
412
|
+
}
|
413
|
+
} else {
|
414
|
+
// if it's a stacked graph
|
415
|
+
if (spanWidth > cellWidth && cellVal > 0) {
|
416
|
+
$cell.classList.add('mc-value-overflow')
|
417
|
+
}
|
418
|
+
}
|
419
|
+
}
|
420
|
+
}
|
421
|
+
|
422
|
+
Modules.MagnaCharta = MagnaCharta
|
423
|
+
})(window.GOVUK.Modules)
|
@@ -53,7 +53,9 @@
|
|
53
53
|
<%= tag.h1 heading, class: "gem-c-radio__heading-text govuk-fieldset__heading" %>
|
54
54
|
<% end %>
|
55
55
|
<% else %>
|
56
|
-
<%= tag.legend
|
56
|
+
<%= tag.legend class: legend_classes do %>
|
57
|
+
<%= tag.h2 heading, class: "govuk-fieldset__heading" %>
|
58
|
+
<% end %>
|
57
59
|
<% end %>
|
58
60
|
<% end %>
|
59
61
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_publishing_components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 21.63.
|
4
|
+
version: 21.63.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: govuk_app_config
|
@@ -419,6 +419,7 @@ files:
|
|
419
419
|
- app/assets/javascripts/govuk_publishing_components/lib/cookie-functions.js
|
420
420
|
- app/assets/javascripts/govuk_publishing_components/lib/current-location.js
|
421
421
|
- app/assets/javascripts/govuk_publishing_components/lib/govspeak/barchart-enhancement.js
|
422
|
+
- app/assets/javascripts/govuk_publishing_components/lib/govspeak/magna-charta.js
|
422
423
|
- app/assets/javascripts/govuk_publishing_components/lib/govspeak/youtube-link-enhancement.js
|
423
424
|
- app/assets/javascripts/govuk_publishing_components/lib/header-navigation.js
|
424
425
|
- app/assets/javascripts/govuk_publishing_components/lib/primary-links.js
|
@@ -428,7 +429,6 @@ files:
|
|
428
429
|
- app/assets/javascripts/govuk_publishing_components/modules.js
|
429
430
|
- app/assets/javascripts/govuk_publishing_components/vendor/html5shiv-printshiv.js
|
430
431
|
- app/assets/javascripts/govuk_publishing_components/vendor/json2.js
|
431
|
-
- app/assets/javascripts/govuk_publishing_components/vendor/magna-charta.min.js
|
432
432
|
- app/assets/javascripts/govuk_publishing_components/vendor/modernizr.js
|
433
433
|
- app/assets/javascripts/govuk_publishing_components/vendor/polyfills/closest.js
|
434
434
|
- app/assets/javascripts/govuk_publishing_components/vendor/polyfills/indexOf.js
|
@@ -1,4 +0,0 @@
|
|
1
|
-
/*! Magna Charta - v3.0.0 - 2012-12-04
|
2
|
-
* https://github.com/alphagov/magna-charta
|
3
|
-
*/
|
4
|
-
(function(e){var t=function(){this.init=function(t,n){var r={outOf:65,applyOnInit:!0,toggleText:"Toggle between chart and table",autoOutdent:!1,outdentAll:!1};this.options=e.extend({},r,n);var i=function(){var e,t=3,n=document.createElement("div"),r=n.getElementsByTagName("i");do n.innerHTML="<!--[if gt IE "+ ++t+"]><i></i><![endif]-->";while(t<10&&r[0]);return t>4?t:e}();return this.ENABLED=!(i&&i<8),this.$table=t,this.$graph=e("<div/>").attr("aria-hidden","true"),this.$graph.attr("class",this.$table.attr("class")).addClass("mc-chart"),this.options.stacked=this.$table.hasClass("mc-stacked"),this.options.negative=this.$table.hasClass("mc-negative"),this.options.multiple=!this.options.stacked&&(this.$table.hasClass("mc-multiple")||this.$table.find("tbody tr").first().find("td").length>2),this.options.autoOutdent=this.options.autoOutdent||this.$table.hasClass("mc-auto-outdent"),this.options.outdentAll=this.options.outdentAll||this.$table.hasClass("mc-outdented"),this.options.multiple&&this.$graph.addClass("mc-multiple"),this.options.hasCaption=!!this.$table.find("caption").length,this.ENABLED&&(this.apply(),this.options.applyOnInit||this.toggle()),this}};t.prototype.construct={},t.prototype.construct.thead=function(){var t=e("<div />",{"class":"mc-thead"}),n=e("<div />",{"class":"mc-tr"}),r="";return this.$table.find("th").each(function(t,n){r+='<div class="mc-th">',r+=e(n).html(),r+="</div>"}),n.append(r),t.append(n),t},t.prototype.construct.tbody=function(){var t=e("<div />",{"class":"mc-tbody"}),n="";return this.$table.find("tbody tr").each(function(n,r){var i=e("<div />",{"class":"mc-tr"}),s="";e(r).find("td").each(function(t,n){s+='<div class="mc-td">',s+=e(n).html(),s+="</div>"}),i.append(s),t.append(i)}),t},t.prototype.construct.caption=function(){var e=this.$table.find("caption");return e.clone()},t.prototype.construct.toggleLink=function(){var t=this;return e("<a />",{href:"#","class":"mc-toggle-link",text:this.options.toggleText}).on("click",function(e){t.toggle(e)})},t.prototype.constructChart=function(){var e=this.construct.thead.call(this),t=this.construct.tbody.call(this),n=this.construct.toggleLink.call(this);if(this.options.hasCaption){var r=this.construct.caption.call(this);this.$graph.append(r)}this.$table.before(n),this.$graph.append(e),this.$graph.append(t)},t.prototype.apply=function(){this.ENABLED&&(this.constructChart(),this.addClassesToHeader(),this.calculateMaxWidth(),this.applyWidths(),this.insert(),this.$table.addClass("visually-hidden"),this.applyOutdent())},t.prototype.toggle=function(e){this.$graph.toggle(),this.$table.toggleClass("visually-hidden"),e&&e.preventDefault()},t.prototype.utils={isFloat:function(e){return!isNaN(parseFloat(e))},stripValue:function(e){var t=new RegExp("\\,|£|%|[a-z]","gi");return e.replace(t,"")},returnMax:function(e){var t=0;for(var n=0;n<e.length;n++)e[n]>t&&(t=e[n]);return t},isNegative:function(e){return e<0}},t.prototype.addClassesToHeader=function(){var t=this.$graph.find(".mc-th").filter(":not(:first)");this.options.stacked&&(t.last().addClass("mc-stacked-header mc-header-total"),t=t.filter(":not(:last)")),t.addClass("mc-key-header").filter(":not(.mc-stacked-header)").each(function(t,n){e(n).addClass("mc-key-"+(t+1))})},t.prototype.calculateMaxWidth=function(){var t=this,n=[],r=0;this.$graph.find(".mc-tr").each(function(i,s){var o=e(s),u=o.find(".mc-td:not(:first)");if(t.options.stacked){var a=u.last().addClass("mc-stacked-total");u=u.filter(":not(:last)")}o.find(".mc-td:first").addClass("mc-key-cell");var f=0;u.each(function(i,s){var o=e(s).addClass("mc-bar-cell").addClass("mc-bar-"+(i+1)),u=t.utils.stripValue(o.text());if(t.utils.isFloat(u)){var a=parseFloat(u,10),l=Math.abs(a);a===0&&o.addClass("mc-bar-zero"),t.options.negative&&(t.utils.isNegative(a)?(o.addClass("mc-bar-negative"),l>r&&(r=l)):o.addClass("mc-bar-positive")),a=l,t.options.stacked?f+=a:(f=a,n.push(a))}}),t.options.stacked&&n.push(f)});var i={};return i.max=parseFloat(t.utils.returnMax(n),10),i.single=parseFloat(this.options.outOf/i.max,10),this.options.negative&&(i.marginLeft=parseFloat(r,10)*i.single,i.maxNegative=parseFloat(r,10)),i},t.prototype.applyWidths=function(){this.dimensions=this.calculateMaxWidth();var t=this;this.$graph.find(".mc-tr").each(function(n,r){var i=e(r),s=i.find(".mc-bar-cell:not(.mc-bar-zero)").length;i.find(".mc-bar-cell").each(function(n,r){var i=e(r),s=parseFloat(t.utils.stripValue(i.text()),10),o=s*t.dimensions.single,u=Math.abs(s),a=Math.abs(o);if(t.options.negative)if(i.hasClass("mc-bar-positive"))i.css("margin-left",t.dimensions.marginLeft+"%");else if(u<t.dimensions.maxNegative){var f=(t.dimensions.maxNegative-u)*t.dimensions.single;i.css("margin-left",f+"%")}i.wrapInner("<span />"),i.css("width",a+"%")})})},t.prototype.insert=function(){this.$table.after(this.$graph)},t.prototype.applyOutdent=function(){var t=this,n=this.$graph.find(".mc-bar-cell");this.$graph.find(".mc-bar-cell").each(function(n,r){var i=e(r),s=parseFloat(t.utils.stripValue(i.text()),10),o=i.children("span"),u=o.width()+10,a=i.width(),f=parseFloat(i[0].style.width,10),l=i.height();t.options.stacked?u>a&&s>0&&i.addClass("mc-value-overflow"):(s===0&&i.addClass("mc-bar-outdented"),t.options.autoOutdent&&u>a||t.options.outdentAll?(i.addClass("mc-bar-outdented"),o.css({"margin-left":"100%",display:"inline-block"})):i.addClass("mc-bar-indented"))})},e.magnaCharta=function(e,n){return(new t).init(e,n)}})(jQuery);
|