Switch from Converse v10.1.6 to upstream (unreleased v11):
* fix poll form
This commit is contained in:
parent
51b603c894
commit
a0d5c4a368
@ -4,7 +4,7 @@
|
|||||||
import { XMLNS_POLL } from '../constants.js'
|
import { XMLNS_POLL } from '../constants.js'
|
||||||
import { tplPollForm } from '../templates/poll-form.js'
|
import { tplPollForm } from '../templates/poll-form.js'
|
||||||
import { CustomElement } from 'shared/components/element.js'
|
import { CustomElement } from 'shared/components/element.js'
|
||||||
import { converse, api } from '@converse/headless'
|
import { converse, api, parsers } from '@converse/headless'
|
||||||
import { webForm2xForm } from '@converse/headless/utils/form'
|
import { webForm2xForm } from '@converse/headless/utils/form'
|
||||||
import { __ } from 'i18n'
|
import { __ } from 'i18n'
|
||||||
import '../styles/poll-form.scss'
|
import '../styles/poll-form.scss'
|
||||||
@ -18,7 +18,6 @@ export default class MUCPollFormView extends CustomElement {
|
|||||||
return {
|
return {
|
||||||
model: { type: Object, attribute: true },
|
model: { type: Object, attribute: true },
|
||||||
modal: { type: Object, attribute: true },
|
modal: { type: Object, attribute: true },
|
||||||
form_fields: { type: Object, attribute: false },
|
|
||||||
alert_message: { type: Object, attribute: false },
|
alert_message: { type: Object, attribute: false },
|
||||||
title: { type: String, attribute: false },
|
title: { type: String, attribute: false },
|
||||||
instructions: { type: String, attribute: false }
|
instructions: { type: String, attribute: false }
|
||||||
@ -27,6 +26,8 @@ export default class MUCPollFormView extends CustomElement {
|
|||||||
|
|
||||||
_fieldTranslationMap = new Map()
|
_fieldTranslationMap = new Map()
|
||||||
|
|
||||||
|
xform = undefined
|
||||||
|
|
||||||
async initialize () {
|
async initialize () {
|
||||||
this.alert_message = undefined
|
this.alert_message = undefined
|
||||||
if (!this.model) {
|
if (!this.model) {
|
||||||
@ -36,20 +37,18 @@ export default class MUCPollFormView extends CustomElement {
|
|||||||
try {
|
try {
|
||||||
this._initFieldTranslations()
|
this._initFieldTranslations()
|
||||||
const stanza = await this._fetchPollForm()
|
const stanza = await this._fetchPollForm()
|
||||||
const query = stanza.querySelector('query')
|
const xform = parsers.parseXForm(stanza)
|
||||||
const xform = sizzle(`x[xmlns="${Strophe.NS.XFORM}"]`, query)[0]
|
|
||||||
if (!xform) {
|
if (!xform) {
|
||||||
throw Error('Missing xform in stanza')
|
throw Error('Missing xform in stanza')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xform.fields?.map(f => this._translateField(f))
|
||||||
|
this.xform = xform
|
||||||
|
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
this.title = __(LOC_poll_title) // xform.querySelector('title')?.textContent ?? ''
|
this.title = __(LOC_poll_title) // xform.querySelector('title')?.textContent ?? ''
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
this.instructions = __(LOC_poll_instructions) // xform.querySelector('instructions')?.textContent ?? ''
|
this.instructions = __(LOC_poll_instructions) // xform.querySelector('instructions')?.textContent ?? ''
|
||||||
this.form_fields = Array.from(xform.querySelectorAll('field')).map(field => {
|
|
||||||
this._translateField(field)
|
|
||||||
return u.xForm2TemplateResult(field, stanza)
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
this.alert_message = __('Error')
|
this.alert_message = __('Error')
|
||||||
@ -86,10 +85,10 @@ export default class MUCPollFormView extends CustomElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_translateField (field) {
|
_translateField (field) {
|
||||||
const v = field.getAttribute('var')
|
const v = field.var
|
||||||
const label = this._fieldTranslationMap.get(v)
|
const label = this._fieldTranslationMap.get(v)
|
||||||
if (label) {
|
if (label) {
|
||||||
field.setAttribute('label', label)
|
field.label = label
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,10 @@
|
|||||||
import { converseLocalizedHelpUrl } from '../../../shared/lib/help'
|
import { converseLocalizedHelpUrl } from '../../../shared/lib/help'
|
||||||
import { html } from 'lit'
|
import { html } from 'lit'
|
||||||
import { __ } from 'i18n'
|
import { __ } from 'i18n'
|
||||||
|
import { converse } from '@converse/headless'
|
||||||
|
|
||||||
|
const u = converse.env.utils
|
||||||
|
|
||||||
export function tplPollForm (el) {
|
export function tplPollForm (el) {
|
||||||
const i18nOk = __('Ok')
|
const i18nOk = __('Ok')
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
@ -13,10 +17,18 @@ export function tplPollForm (el) {
|
|||||||
page: 'documentation/user/streamers/polls'
|
page: 'documentation/user/streamers/polls'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let formFieldTemplates
|
||||||
|
if (el.xform) {
|
||||||
|
const fields = el.xform.fields
|
||||||
|
formFieldTemplates = fields.map(field => {
|
||||||
|
return u.xFormField2TemplateResult(field)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
${el.alert_message ? html`<div class="error">${el.alert_message}</div>` : ''}
|
${el.alert_message ? html`<div class="error">${el.alert_message}</div>` : ''}
|
||||||
${
|
${
|
||||||
el.form_fields
|
formFieldTemplates
|
||||||
? html`
|
? html`
|
||||||
<form class="converse-form" @submit=${ev => el.formSubmit(ev)}>
|
<form class="converse-form" @submit=${ev => el.formSubmit(ev)}>
|
||||||
<p class="title">
|
<p class="title">
|
||||||
@ -30,7 +42,7 @@ export function tplPollForm (el) {
|
|||||||
<p class="form-help instructions">${el.instructions}</p>
|
<p class="form-help instructions">${el.instructions}</p>
|
||||||
<div class="form-errors hidden"></div>
|
<div class="form-errors hidden"></div>
|
||||||
|
|
||||||
${el.form_fields}
|
${formFieldTemplates}
|
||||||
|
|
||||||
<fieldset class="buttons form-group">
|
<fieldset class="buttons form-group">
|
||||||
<input type="submit" class="btn btn-primary" value="${i18nOk}" />
|
<input type="submit" class="btn btn-primary" value="${i18nOk}" />
|
||||||
|
Loading…
Reference in New Issue
Block a user