this.root.innerHTML = opts.content || ''
{ val }
this.mixin(fieldMixin)
this.autocomplete = AUTOCOMPLETE[this.item.fname]
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
{ val }
this.mixin(fieldMixin)
normalize(value) {
var ua = window.navigator.userAgent.toLowerCase()
if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) return value
value = value.replace(/[A-Za-z0-9-]/g, function (s) {
return String.fromCharCode(s.charCodeAt(0) - 65248)
})
var matched = value.match(/^(\d{3})-?(\d{4})$/)
return matched ? matched[1] + '-' + matched[2] : value
}
blur(e) {
this.val = this.normalize(e.target.value)
e.target.classList.add('focused')
}
this.mixin(fieldMixin)
{ h[val] }
this.mixin(fieldMixin, choiceMixin)
{ h[val] }
this.mixin(fieldMixin, choiceMixin)
edit(e) {
this.val = e.target.value
this.toggleOther(this.val == this.othervalue)
}
{ show_values(val) }
this.mixin(fieldMixin, choiceMixin)
this.val = []
show_values(vals) {
var h = this.h
return vals.map(function (v) { return h[v] }).join(',')
}
edit(e) {
var value = e.target.value
this.val = this.val.filter(function (v) { return v != value })
if (e.target.checked) this.val.push(value)
this.toggleOther(this.val.indexOf(this.othervalue) >= 0)
}
var AUTOCOMPLETE = {
tel: 'tel',
company_name: 'organization',
name_1: 'family-name',
name_2: 'given-name'
}
var fieldMixin = {
init: function () {
this.item = this.opts.item
this.val = this.item.val || ''
this.otherRequired = false
},
isRequired: function () {
return this.item.frequired == 'true' || this.otherRequired
},
edit: function (e) {
this.val = e.target.value
}
}
var choiceMixin = {
init: function () {
var item = this.opts.item
var h = {}
this.kvs = String(item.foptions || '').split(/\r?\n/).filter(Boolean).map(function (s) {
var sa = s.split(/:/, 2)
h[sa[0]] = sa[1]
return { k: sa[0], v: sa[1] }
})
this.h = h
var other = item.ftagoption_other_answer || []
this.othervalue = other[0] || ''
this.related_answer = other[1] || ''
this.related_required = other[2] || ''
this.brs = item.ftagoptionbr || []
},
brstyle: function (k) {
return this.brs.indexOf(k) >= 0 ? 'display:block;' : ''
},
toggleOther: function (selected) {
relatedOther(this.related_answer, selected, this.related_required)
}
}
var relatedOther = function (elid, active, required) {
var target = elid && window.refs && window.refs[elid]
var el = elid && document.getElementById(elid)
if (!target || !el) return
target.otherRequired = active && required == 'required'
if (!active) {
target.val = ''
if (target.item.frequired == 'true') {
el.style.backgroundColor = 'red'
}
}
el.readOnly = !active
if (active) {
el.removeAttribute('tabindex')
} else {
el.setAttribute('tabindex', -1)
}
target.root.classList.toggle('grayout', !active)
target.update()
}
var self = this
self.isvalid = false
self.sending = false
self.state = opts.state
function syncValues() {
opts.items.forEach(function (item) {
var tag = self.refs[item.fname]
var el = document.getElementById(item.fname)
if (!tag || !el || typeof tag.val != 'string' || el.type == 'hidden') return
if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName)) tag.val = el.value
})
}
function onRoute() {
var tag = location.hash.replace(/^#/, '')
if (tag == 'confirm') syncValues()
var validated = self.refs.form.checkValidity()
if (tag == 'confirm' && !validated) {
location.hash = ''
} else {
self.state = tag || 'init'
riot.update()
self.isvalid = self.state == 'init' && self.refs.form.checkValidity()
self.update()
}
try { leadmountoption() } catch (err) {}
}
self.on('mount', function () {
window.refs = self.refs
if (location.hash) history.replaceState(null, '', location.pathname + location.search)
var labels = document.querySelectorAll('[data-reated-other]')
for (var i = 0; i < labels.length; i++) {
var elid = labels[i].getAttribute('data-reated-other')
if (!elid) continue
labels[i].parentNode.style.marginBottom = 0
var container = document.querySelector('[ref="' + elid + '"]')
if (container) container.classList.add('related_other')
relatedOther(elid, false)
}
window.addEventListener('hashchange', onRoute)
self.refs.form.addEventListener('animationstart', function (e) {
if (e.animationName == 'lead-autofill') { self.checkAll(); self.update() }
})
try { leadmountoption() } catch (err) {}
})
checkAll(e) {
syncValues()
self.isvalid = self.refs.form.checkValidity()
var agreement = document.getElementById('agreement')
if (agreement && agreement.checked) agreement.parentElement.style.border = ''
}
checkItems(e) {
var invalid = document.querySelectorAll(':required:invalid')
if (!invalid.length) {
if (location.hash == '#confirm') onRoute()
return
}
invalid[0].focus()
var agreement = document.getElementById('agreement')
if (agreement) agreement.parentElement.style.border = agreement.checked ? '' : '1px solid red'
}
var SEND_ERRORS = {
nonce: '一定時間が経過したため送信できませんでした。もう一度「この内容で送信」を押してください。',
invalid: '送信できませんでした。ページを再読み込みして、もう一度お試しください。',
article: '記事が見つかりませんでした。ページを再読み込みして、もう一度お試しください。',
server: '送信に失敗しました。時間をおいて、もう一度お試しください。'
}
self.postlead = function (e) {
e.preventDefault()
if (self.sending) return
self.sending = true
var body = new URLSearchParams(new FormData(self.refs.form))
body.append('nonce', window.leadNonce || '')
fetch(opts.action, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: body.toString()
})
.then(function (response) { return response.text() })
.then(function (text) {
var obj = null
try { obj = JSON.parse(text) } catch (err) {}
if (obj && obj.ok && obj.r) {
var moved = false
var go = function () {
if (moved) return
moved = true
window.location = obj.r
}
window.dataLayer = window.dataLayer || []
window.dataLayer.push({
'event': 'generate_lead',
'lead_source': opts.lead_source,
'lead_code': opts.code,
'eventCallback': go,
'eventTimeout': 2000
})
setTimeout(go, 2500)
return
}
var type = (obj && obj.error) || 'server'
if (type == 'closed' || type == 'permission') {
window.location = opts.thanks
return
}
if (window.fetchLeadNonce) window.fetchLeadNonce()
alert(SEND_ERRORS[type] || SEND_ERRORS.server)
self.sending = false
})
.catch(function (error) {
console.error('Error:', error)
alert(SEND_ERRORS.server)
self.sending = false
})
}