|
| 1 | +/** |
| 2 | + * Matrix Question |
| 3 | + * ---------------------------------------------------- |
| 4 | + * Description: |
| 5 | + * Matrix component extension of Question. |
| 6 | + * |
| 7 | + * Supports editable legend/hint, row labels, column labels, |
| 8 | + * and mode switching confirmation in the editor. |
| 9 | + **/ |
| 10 | + |
| 11 | +const utilities = require('./utilities'); |
| 12 | +const mergeObjects = utilities.mergeObjects; |
| 13 | +const Question = require('./question'); |
| 14 | +const EditableElement = require('./editable_components').EditableElement; |
| 15 | + |
| 16 | +const SELECTOR_HINT = "fieldset > .govuk-hint"; |
| 17 | +const SELECTOR_LABEL = "legend > :first-child"; |
| 18 | +const SELECTOR_ROW_LABEL = "[data-fb-matrix-row-label]"; |
| 19 | +const SELECTOR_COLUMN_LABEL = "[data-fb-matrix-column-label]"; |
| 20 | +const SELECTOR_MODE = "[data-fb-matrix-mode]"; |
| 21 | +const SELECTOR_CELL_INPUTS = "tbody input"; |
| 22 | + |
| 23 | + |
| 24 | +class MatrixQuestion extends Question { |
| 25 | + constructor($node, config) { |
| 26 | + super($node, mergeObjects({ |
| 27 | + selectorLabel: SELECTOR_LABEL, |
| 28 | + selectorHint: SELECTOR_HINT |
| 29 | + }, config)); |
| 30 | + |
| 31 | + $node.addClass("MatrixQuestion"); |
| 32 | + |
| 33 | + this._rowLabelElements = []; |
| 34 | + this._columnLabelElements = []; |
| 35 | + |
| 36 | + this.initialiseMatrixLabelEditing(config); |
| 37 | + this.initialiseModeSwitchConfirmation(config); |
| 38 | + } |
| 39 | + |
| 40 | + initialiseMatrixLabelEditing(config) { |
| 41 | + const question = this; |
| 42 | + |
| 43 | + this.$node.find(SELECTOR_ROW_LABEL).each(function(index) { |
| 44 | + const editable = createEditableLabel($(this), config); |
| 45 | + question._rowLabelElements.push(editable); |
| 46 | + $(this).on("blur.matrix-row-label", function() { |
| 47 | + question.syncRowLabel(index, editable.content, $(this).data("fb-matrix-row-id")); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + this.$node.find(SELECTOR_COLUMN_LABEL).each(function(index) { |
| 52 | + const editable = createEditableLabel($(this), config); |
| 53 | + question._columnLabelElements.push(editable); |
| 54 | + $(this).on("blur.matrix-column-label", function() { |
| 55 | + question.syncColumnLabel(index, editable.content, $(this).data("fb-matrix-column-id")); |
| 56 | + }); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + initialiseModeSwitchConfirmation(config) { |
| 61 | + const question = this; |
| 62 | + const warningText = config.text && config.text.matrix && config.text.matrix.modeSwitchWarning; |
| 63 | + |
| 64 | + this.$node.find(SELECTOR_MODE).on("change.matrix-mode", function() { |
| 65 | + const $field = $(this); |
| 66 | + const previousValue = question.data.mode || $field.data("fb-matrix-mode-original") || $field.val(); |
| 67 | + const nextValue = $field.val(); |
| 68 | + |
| 69 | + if (previousValue === nextValue) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (question.hasMatrixCellInput() && warningText && !window.confirm(warningText)) { |
| 74 | + $field.val(previousValue); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + question.data.mode = nextValue; |
| 79 | + $field.data("fb-matrix-mode-original", nextValue); |
| 80 | + question.editable.emitSaveRequired(); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + hasMatrixCellInput() { |
| 85 | + let hasInput = false; |
| 86 | + |
| 87 | + this.$node.find(SELECTOR_CELL_INPUTS).each(function() { |
| 88 | + const $input = $(this); |
| 89 | + const type = ($input.attr("type") || "").toLowerCase(); |
| 90 | + |
| 91 | + if ((type === "radio" || type === "checkbox") && $input.prop("checked")) { |
| 92 | + hasInput = true; |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + if (type === "number" && $input.val() !== "") { |
| 97 | + hasInput = true; |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | + }); |
| 103 | + |
| 104 | + return hasInput; |
| 105 | + } |
| 106 | + |
| 107 | + syncRowLabel(index, label, rowId) { |
| 108 | + const row = findAxisItem(this.data.rows, rowId, index); |
| 109 | + if (!row) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + row.label = label; |
| 114 | + this.editable.emitSaveRequired(); |
| 115 | + } |
| 116 | + |
| 117 | + syncColumnLabel(index, label, columnId) { |
| 118 | + const column = findAxisItem(this.data.columns, columnId, index); |
| 119 | + if (!column) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + column.label = label; |
| 124 | + this.editable.emitSaveRequired(); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +function createEditableLabel($node, config) { |
| 130 | + const existing = $node.data("instance"); |
| 131 | + if (existing) { |
| 132 | + return existing; |
| 133 | + } |
| 134 | + |
| 135 | + return new EditableElement($node, config); |
| 136 | +} |
| 137 | + |
| 138 | +function findAxisItem(collection, id, index) { |
| 139 | + if (!Array.isArray(collection)) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + if (id) { |
| 144 | + const match = collection.find(function(item) { |
| 145 | + return item.id === id; |
| 146 | + }); |
| 147 | + |
| 148 | + if (match) { |
| 149 | + return match; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return collection[index] || null; |
| 154 | +} |
| 155 | + |
| 156 | +module.exports = MatrixQuestion; |
0 commit comments