|
| 1 | +import { Controller } from '@hotwired/stimulus' |
| 2 | + |
| 3 | +export default class extends Controller { |
| 4 | + static targets = ['editors', 'editor', 'forms', 'form', 'noTeamDisclaimer', 'noEditorDisclaimer'] |
| 5 | + static values = { |
| 6 | + editors: Array, |
| 7 | + targetUseCase: String |
| 8 | + } |
| 9 | + |
| 10 | + chooseTeam (event) { |
| 11 | + this._hideForms() |
| 12 | + |
| 13 | + switch (event.target.value) { |
| 14 | + case 'internal': |
| 15 | + this._hideEditors() |
| 16 | + this._hideEditorForms() |
| 17 | + if (this.hasTargetUseCaseValue) { |
| 18 | + this._hideOtherUseCaseForms() |
| 19 | + } |
| 20 | + this._showFormsBlock() |
| 21 | + break |
| 22 | + case 'editor': |
| 23 | + this._showEditors() |
| 24 | + break |
| 25 | + case 'none': |
| 26 | + this._hideEditors() |
| 27 | + this._showNoTeamDisclaimer() |
| 28 | + break |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + chooseEditor (event) { |
| 33 | + const editor = event.target.value |
| 34 | + |
| 35 | + this.formTargets.forEach((form) => { |
| 36 | + const formTags = this._getFormTags(form) |
| 37 | + |
| 38 | + if (formTags.includes(editor)) { |
| 39 | + form.classList.remove('fr-hidden') |
| 40 | + } else { |
| 41 | + form.classList.add('fr-hidden') |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + if (this.hasTargetUseCaseValue) { |
| 46 | + this._hideOtherUseCaseForms() |
| 47 | + } |
| 48 | + |
| 49 | + if (this._noForm()) { |
| 50 | + this._showNoEditorDisclaimer() |
| 51 | + } else { |
| 52 | + this._showFormsBlock() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + _getFormTags (form) { |
| 57 | + return JSON.parse(form.getAttribute('data-choose-authorization-request-form-tags')) |
| 58 | + } |
| 59 | + |
| 60 | + _showEditors () { |
| 61 | + this.editorsTarget.classList.remove('fr-hidden') |
| 62 | + } |
| 63 | + |
| 64 | + _hideEditors () { |
| 65 | + this.editorsTarget.classList.add('fr-hidden') |
| 66 | + this.editorTargets.forEach((editor) => { |
| 67 | + editor.checked = false |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + _hideEditorForms () { |
| 72 | + this._showAllForms() |
| 73 | + |
| 74 | + this.editorsValue.forEach((editor) => { |
| 75 | + this.formTargets.forEach((form) => { |
| 76 | + const formTags = this._getFormTags(form) |
| 77 | + |
| 78 | + if (formTags.includes(editor)) { |
| 79 | + form.classList.add('fr-hidden') |
| 80 | + } |
| 81 | + }) |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + _showFormsBlock () { |
| 86 | + this.formsTarget.classList.remove('fr-hidden') |
| 87 | + } |
| 88 | + |
| 89 | + _showAllForms () { |
| 90 | + this.formTargets.forEach((form) => { |
| 91 | + form.classList.remove('fr-hidden') |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + _hideForms () { |
| 96 | + this.formTargets.forEach((form) => { |
| 97 | + form.classList.add('fr-hidden') |
| 98 | + }) |
| 99 | + this.formsTarget.classList.add('fr-hidden') |
| 100 | + this.noTeamDisclaimerTarget.classList.add('fr-hidden') |
| 101 | + this.noEditorDisclaimerTarget.classList.add('fr-hidden') |
| 102 | + } |
| 103 | + |
| 104 | + _showNoTeamDisclaimer () { |
| 105 | + this.noTeamDisclaimerTarget.classList.remove('fr-hidden') |
| 106 | + } |
| 107 | + |
| 108 | + _showNoEditorDisclaimer () { |
| 109 | + this.noEditorDisclaimerTarget.classList.remove('fr-hidden') |
| 110 | + this.formsTarget.classList.add('fr-hidden') |
| 111 | + } |
| 112 | + |
| 113 | + _noForm () { |
| 114 | + return this.formTargets.every((form) => { |
| 115 | + return form.classList.contains('fr-hidden') |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + _hideOtherUseCaseForms () { |
| 120 | + this.formTargets.forEach((form) => { |
| 121 | + const formTags = this._getFormTags(form) |
| 122 | + |
| 123 | + if (!formTags.includes(this.targetUseCaseValue) && !formTags.includes('default')) { |
| 124 | + form.classList.add('fr-hidden') |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
0 commit comments