|
| 1 | +import { ConversationalForm } from 'conversational-form'; |
| 2 | +import { |
| 3 | + TweenLite, |
| 4 | +} from 'gsap/TweenMax'; |
| 5 | +// import { Power3 } from 'gsap/EasePack'; |
| 6 | +import loadcss from 'loadcss'; |
| 7 | +import Tracking from './tracking'; |
| 8 | +import 'reset-css'; |
| 9 | +import './scss/main.scss'; |
| 10 | + |
| 11 | +const animTime = 0.8; |
| 12 | +const themes = [ |
| 13 | + 'conversational-form-dark.min.css', |
| 14 | + 'conversational-form.min.css', |
| 15 | + 'conversational-form-purple.min.css', |
| 16 | + 'conversational-form-irisblue.min.css', |
| 17 | + 'conversational-form-red.min.css', |
| 18 | + 'conversational-form-green.min.css', |
| 19 | +]; |
| 20 | +let currentTheme = themes[0]; |
| 21 | + |
| 22 | +function preloadFormImages() { |
| 23 | + const images = [].slice.call(document.querySelectorAll('form *[cf-image]')); |
| 24 | + // eslint-disable-next-line no-return-assign |
| 25 | + images.map(el => (new Image()).src = el.getAttribute('cf-image')); |
| 26 | +} |
| 27 | + |
| 28 | +function loadThemes() { |
| 29 | + console.log('loadThemes'); |
| 30 | + |
| 31 | + loadcss(themes.map(t => `./${t}`), (links) => { |
| 32 | + links.forEach(link => console.log(link)); |
| 33 | + |
| 34 | + const stylesheets = [].slice.call(document.styleSheets); |
| 35 | + stylesheets.map((s, i) => { |
| 36 | + // console.log(i, s); |
| 37 | + if (i > 1) document.styleSheets[i].disabled = true; |
| 38 | + return s; |
| 39 | + }); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function changeTheme(themeName) { |
| 44 | + console.log(currentTheme); |
| 45 | + if (currentTheme === themeName) return; |
| 46 | + |
| 47 | + console.log('changeTheme', themeName); |
| 48 | + const stylesheets = [].slice.call(document.styleSheets); |
| 49 | + stylesheets.map((s, i) => { |
| 50 | + if (i > 0 && document.styleSheets[i].disabled === false) { |
| 51 | + document.styleSheets[i].disabled = true; |
| 52 | + } else if (document.styleSheets[i].href.indexOf(themeName) > -1) { |
| 53 | + document.styleSheets[i].disabled = false; |
| 54 | + currentTheme = themeName; |
| 55 | + } |
| 56 | + console.log('theme', i, document.styleSheets[i].href, document.styleSheets[i].disabled); |
| 57 | + |
| 58 | + return s; |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +window.changeTheme = changeTheme; |
| 63 | + |
| 64 | +function animateIn() { |
| 65 | + console.log('animateIn'); |
| 66 | + const headerEl = document.querySelector('header'); |
| 67 | + const headlineEl = document.querySelector('h1'); |
| 68 | + const cfEl = document.querySelector('.cf'); |
| 69 | + const aboutEl = document.querySelector('.about'); |
| 70 | + |
| 71 | + if ( |
| 72 | + window.innerHeight < 780 |
| 73 | + ) { |
| 74 | + TweenLite.set( |
| 75 | + headlineEl, |
| 76 | + { |
| 77 | + css: { |
| 78 | + scale: 0.68, |
| 79 | + 'margin-top': 60, |
| 80 | + 'margin-bottom': 60, |
| 81 | + }, |
| 82 | + }, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + TweenLite.set(headerEl, { y: 10 }); |
| 87 | + TweenLite.to( |
| 88 | + headerEl, |
| 89 | + animTime / 2, |
| 90 | + { |
| 91 | + opacity: 1, |
| 92 | + y: 0, |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + TweenLite.set(headlineEl, { y: 20 }); |
| 97 | + TweenLite.to( |
| 98 | + headlineEl, |
| 99 | + animTime, |
| 100 | + { |
| 101 | + opacity: 1, |
| 102 | + y: 0, |
| 103 | + // onComplete: scaleDownHeader(headlineEl), |
| 104 | + delay: animTime / 2, |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + TweenLite.set(cfEl, { y: 40 }); |
| 109 | + TweenLite.to( |
| 110 | + cfEl, |
| 111 | + animTime * 1.2, |
| 112 | + { |
| 113 | + opacity: 1, |
| 114 | + y: 0, |
| 115 | + delay: animTime, |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + setTimeout( |
| 120 | + () => { |
| 121 | + window.ConversationalForm.start(); |
| 122 | + }, |
| 123 | + animTime * 0.6 * 1000, |
| 124 | + ); |
| 125 | + |
| 126 | + TweenLite.to( |
| 127 | + aboutEl, |
| 128 | + animTime * 3, |
| 129 | + { |
| 130 | + opacity: 1, |
| 131 | + delay: animTime * 2, |
| 132 | + }, |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function init() { |
| 137 | + loadThemes(); |
| 138 | + preloadFormImages(); |
| 139 | + |
| 140 | + Tracking.registerAllExternalLinks(); |
| 141 | + |
| 142 | + const wrapperEl = document.querySelector('.wrapper'); |
| 143 | + const formEl = document.querySelector('.form'); |
| 144 | + const cfEl = document.querySelector('.cf'); |
| 145 | + |
| 146 | + // eslint-disable-next-line no-unused-vars |
| 147 | + const cfInstance = new ConversationalForm({ |
| 148 | + formEl, |
| 149 | + context: cfEl, |
| 150 | + loadExternalStyleSheet: false, |
| 151 | + preventAutoFocus: true, |
| 152 | + preventAutoStart: true, |
| 153 | + submitCallback: () => { |
| 154 | + const formDataSerialized = cfInstance.getFormData(true); |
| 155 | + console.log('done', formDataSerialized); |
| 156 | + if ( |
| 157 | + formDataSerialized.getstarted |
| 158 | + && formDataSerialized.getstarted.indexOf('no') === -1 |
| 159 | + ) { |
| 160 | + cfInstance.addRobotChatResponse('Ok. Thank you for trying out Conversational Form.'); |
| 161 | + } |
| 162 | + }, |
| 163 | + flowStepCallback: (dto, success) => { |
| 164 | + console.log('fsc', dto.tag.name, dto.tag.value); |
| 165 | + |
| 166 | + Tracking.event('conversational form example', dto.tag.name, dto.tag.value); |
| 167 | + |
| 168 | + if (dto.tag.name === 'theme') changeTheme(dto.tag.value[0]); |
| 169 | + |
| 170 | + setTimeout(() => { |
| 171 | + if ( |
| 172 | + dto.tag.name !== 'changeThemeAgain' |
| 173 | + || ( |
| 174 | + dto.tag.name === 'changeThemeAgain' |
| 175 | + && dto.tag.value[0] !== 'yes' |
| 176 | + ) |
| 177 | + ) { |
| 178 | + success(); |
| 179 | + } |
| 180 | + |
| 181 | + if ( |
| 182 | + dto.tag.name === 'changeThemeAgain' |
| 183 | + && dto.tag.value[0] === 'yes' |
| 184 | + ) { |
| 185 | + window.ConversationalForm.remapTagsAndStartFrom(3); |
| 186 | + } |
| 187 | + }, 0); |
| 188 | + }, |
| 189 | + }); |
| 190 | + |
| 191 | + TweenLite.to( |
| 192 | + wrapperEl, |
| 193 | + 1, |
| 194 | + { |
| 195 | + opacity: 1, |
| 196 | + // ease: Power1, |
| 197 | + delay: animTime / 2, |
| 198 | + onComplete: animateIn, |
| 199 | + }, |
| 200 | + ); |
| 201 | +} |
| 202 | + |
| 203 | +document.addEventListener('DOMContentLoaded', init); |
0 commit comments