function flex2html(element, json) { let carousel = carousel_struc() let result = '' if(json['type'] === 'flex') { json = json['contents'] if(json['type'] === 'bubble') { result = bubble_object(json) carousel = carousel.replace('', result) } else if(json['type'] === 'carousel') { json['contents'].forEach((obj, index) => { let result = bubble_object(obj) result = result.replace('', '') result = result.replace('', '') carousel = carousel.replace('', result + '') }) } } document.getElementById(element).innerHTML += carousel return carousel } function bubble_object(json) { let { hero, header, body, footer } = json let hero_object = hero_struc(json) let header_object = header_struc(json) let body_object = body_struc(json) let footer_object = footer_struc(json) let bubble = bubble_struc(json) let box = '' for(let key in hero){ if(hero.hasOwnProperty(key)) { if(key === 'type' && hero[key] === 'box') { box = box_object(hero) let box_inner = box_recursive(box, hero['contents']) box = box_inner } else { box = convert_object(hero) } } } hero_object = hero_object.replace('', box) box = '' for(let key in header){ if(header.hasOwnProperty(key)) { if(key === 'type' && header[key] === 'box') { box = box_object(header) let box_inner = box_recursive(box, header['contents']) box = box_inner } } } header_object = header_object.replace('', box) box = '' for(let key in body){ if(body.hasOwnProperty(key)) { if(key === 'type' && body[key] === 'box') { box = box_object(body) let box_inner = box_recursive(box, body['contents']) box = box_inner } } } body_object = body_object.replace('', box) box = '' for(let key in footer){ if(footer.hasOwnProperty(key)) { if(key === 'type' && footer[key] === 'box') { box = box_object(footer) let box_inner = box_recursive(box, footer['contents']) box = box_inner } } } footer_object = footer_object.replace('', box) bubble = bubble.replace('', hero_object) bubble = bubble.replace('', header_object) bubble = bubble.replace('', body_object) bubble = bubble.replace('', footer_object) return bubble } function box_recursive(parent_box, json) { let result = [] json.forEach((obj, index) => { let temp if(obj['type'] === 'box') { let temp2 = box_object(obj) temp = box_recursive(temp2, obj['contents']) } else if(obj['type'] === 'text' && obj['contents'] && obj['contents'].length > 0 ) { let temp2 = convert_object(obj) temp = box_recursive(temp2, obj['contents']) } else { temp = convert_object(obj) } result[index] = temp }) json.forEach((obj, index) => { result[index] = result[index].replace('', '') parent_box = parent_box.replace('', result[index] + '') }) return parent_box } function convert_object(json) { switch(json['type']) { case 'image': object = image_object(json) break; case 'icon': object = icon_object(json) break; case 'text': object = text_object(json) break; case 'span': object = span_object(json) break; case 'button': object = button_object(json) break; case 'filler': object = filler_object(json) break; case 'spacer': object = spacer_object(json) break; case 'separator': object = separator_object(json) break; default: object = null } return object } function box_object(json) { let style = '' let {layout, position, flex, spacing, margin, width, height, backgroundColor, borderColor, borderWidth, cornerRadius, justifyContent, alignItems, offsetTop, offsetBottom, offsetStart, offsetEnd, paddingAll, paddingTop, paddingBottom, paddingStart, paddingEnd, background} = json if(layout === 'baseline') { layout1 = 'hr' layout2 = 'bl' } else if(layout === 'horizontal') { layout1 = 'hr' layout2 = '' } else if(layout === 'vertical') { layout1 = 'vr' layout2 = '' } fl = '' if(flex > 3) { style += `-webkit-box-flex:${flex};flex-grow:${flex};` } else { fl = (flex >= 0) ? `fl${flex}` : '' } exabs = (position === 'absolute') ? 'ExAbs' : '' if(spacing && spacing.indexOf("px") >= 0) { spc = '' } else { spc = (spacing) ? 'spc' + upperalldigit(spacing) : '' } if(margin && (margin.indexOf("px") >= 0 || margin.indexOf("%") >= 0)) { style += `margin-top:${margin};` exmgn = '' } else { exmgn = (margin) ? 'ExMgnT' + upperalldigit(margin) : '' } if(width && width !== '') { style += `width:${width}; max-width:${width};` } if(height && height !== '') { style += `height:${height};` } if(backgroundColor) { style += `background-color:${backgroundColor} !important;` } if(borderColor) { style += `border-color:${borderColor} !important;` } if(borderWidth && (borderWidth.indexOf("px") >= 0 || borderWidth.indexOf("%") >= 0)) { style += `border-width:${borderWidth};` ExBdr = '' } else { switch(borderWidth) { case 'none': ExBdr = 'ExBdrWdtNone' break; case 'light': ExBdr = 'ExBdrWdtLgh' break; case 'normal': ExBdr = 'ExBdrWdtNml' break; case 'medium': ExBdr = 'ExBdrWdtMdm' break; case 'semi-bold': ExBdr = 'ExBdrWdtSbd' break; case 'bold': ExBdr = 'ExBdrWdtBld' break; default: ExBdr = '' // code block } } if(cornerRadius && (cornerRadius.indexOf("px") >= 0 || cornerRadius.indexOf("%") >= 0)) { style += `border-radius:${cornerRadius};` ExBdrRad = '' } else { ExBdrRad = (cornerRadius) ? 'ExBdrRad' + upperalldigit(cornerRadius) : '' } jfc = '' if(justifyContent && justifyContent !== '') { switch(justifyContent) { case 'center': jfc = 'itms-jfcC' break; case 'flex-start': jfc = 'itms-jfcS' break; case 'flex-end': jfc = 'itms-jfcE' break; case 'space-between': jfc = 'itms-jfcSB' break; case 'space-around': jfc = 'itms-jfcSA' break; case 'space-evenly': jfc = 'itms-jfcSE' break; default: jfc = '' // code block } } alg = '' if(alignItems && alignItems !== '') { switch(alignItems) { case 'center': alg = 'itms-algC' break; case 'flex-start': alg = 'itms-algS' break; case 'flex-end': alg = 'itms-algE' break; default: alg = '' // code block } } if(offsetTop && (offsetTop.indexOf("px") >= 0 || offsetTop.indexOf("%") >= 0)) { style += `top:${offsetTop};` ext = '' } else { ext = (offsetTop) ? 'ExT' + upperalldigit(offsetTop) : '' } if(offsetBottom && (offsetBottom.indexOf("px") >= 0 || offsetBottom.indexOf("%") >= 0)) { style += `bottom:${offsetBottom};` exb = '' } else { exb = (offsetBottom) ? 'ExB' + upperalldigit(offsetBottom) : '' } if(offsetStart && (offsetStart.indexOf("px") >= 0 ||offsetStart.indexOf("%") >= 0)) { style += `left:${offsetStart};` exl = '' } else { exl = (offsetStart) ? 'ExL' + upperalldigit(offsetStart) : '' } if(offsetEnd && (offsetEnd.indexOf("px") >= 0 || offsetEnd.indexOf("%") >= 0)) { style += `right:${offsetEnd};` exr = '' } else { exr = (offsetEnd) ? 'ExR' + upperalldigit(offsetEnd) : '' } if(paddingAll && (paddingAll.indexOf("px") >= 0 || paddingAll.indexOf("%") >= 0)) { style += `padding:${paddingAll};` ExPadA = '' } else { ExPadA = (paddingAll) ? 'ExPadA' + upperalldigit(paddingAll) : '' } if(paddingTop && (paddingTop.indexOf("px") >= 0 ||paddingTop.indexOf("%") >= 0)) { style += `padding-top:${paddingTop};` ExPadT = '' } else { ExPadT = (paddingTop) ? 'ExPadT' + upperalldigit(paddingTop) : '' } if(paddingBottom && (paddingBottom.indexOf("px") >= 0 || paddingBottom.indexOf("%")>=0)) { style += `padding-bottom:${paddingBottom};` ExPadB = '' } else { ExPadB = (paddingBottom) ? 'ExPadB' + upperalldigit(paddingBottom) : '' } if(paddingStart && (paddingStart.indexOf("px") >= 0 || paddingStart.indexOf("%") >= 0)) { style += `padding-left:${paddingStart};` ExPadL = '' } else { ExPadL = (paddingStart) ? 'ExPadL' + upperalldigit(paddingStart) : '' } if(paddingEnd && (paddingEnd.indexOf("px") >= 0 || paddingEnd.indexOf("%") >= 0)) { style += `padding-right:${paddingEnd};` ExPadR = '' } else { ExPadR = (paddingEnd) ? 'ExPadR' + upperalldigit(paddingEnd) : '' } if(background && background.type === 'linearGradient') { centerPosition = (background.centerPosition) ? background.centerPosition : '50%' if(background.centerColor) { style += `background: linear-gradient(${background.angle}, ${background.startColor} 0%, ${background.centerColor} ${centerPosition}, ${background.endColor} 100%);` } else { style += `background: linear-gradient(${background.angle}, ${background.startColor} 0%, ${background.endColor} 100%);` } } return `
` } function button_object(json) { style2 = '' style3 = '' let {flex, margin, position, height, style, color, gravity, adjustMode, offsetTop, offsetBottom, offsetStart, offsetEnd, action} = json fl = '' if(flex > 3) { style2 += `-webkit-box-flex:${flex};flex-grow:${flex};` } else { fl = (flex >= 0) ? `fl${flex}` : '' } exabs = (position === 'absolute') ? 'ExAbs' : '' if(margin && (margin.indexOf("px") >= 0||margin.indexOf("%") >= 0)) { style2 += `margin-top:${margin};` exmgn = '' } else { exmgn = (margin) ? 'ExMgnT' + upperalldigit(margin) : '' } height = (!height || height === '' || height === 'md') ? '' : 'Ex' + upperalldigit(height) grv = (gravity === 'bottom' || gravity === 'center') ? 'grv' + upper1digit(gravity) : ''; ExBtn = 'ExBtnL' if(style && style !== '') { switch(style) { case 'link': ExBtn = 'ExBtnL' break; case 'primary': ExBtn = 'ExBtn1' break; case 'secondary': ExBtn = 'ExBtn2' break; default: ExBtn = 'ExBtnL' // code block } } if(color) { style3 += `background-color:${color} !important;` } if(offsetTop && (offsetTop.indexOf("px") >= 0 || offsetTop.indexOf("%") >= 0)) { style2 += `top:${offsetTop};` ext = '' } else { ext = (offsetTop) ? 'ExT' + upperalldigit(offsetTop) : '' } if(offsetBottom && (offsetBottom.indexOf("px") >= 0||offsetBottom.indexOf("%") >= 0)) { style2 += `bottom:${offsetBottom};` exb = '' } else { exb = (offsetBottom) ? 'ExB' + upperalldigit(offsetBottom) : '' } if(offsetStart && (offsetStart.indexOf("px") >= 0 ||offsetStart.indexOf("%") >= 0)) { style2 += `left:${offsetStart};` exl = '' } else { exl = (offsetStart) ? 'ExL' + upperalldigit(offsetStart) : '' } if(offsetEnd && (offsetEnd.indexOf("px") >= 0 || offsetEnd.indexOf("%") >= 0)) { style2 += `right:${offsetEnd};` exr = '' } else { exr = (offsetEnd) ? 'ExR' + upperalldigit(offsetEnd) : '' } action = (!action) ? {'type':'none'} : action if(action.type === 'uri') { return `` } else if(action.type === 'message') { return `` } else if(action.type === 'postback') { return `` } else { return `` } } function filler_object(json) { let style = '' let {flex} = json fl = '' if(flex > 3) { style += `-webkit-box-flex:${flex};flex-grow:${flex};` } else { fl = (flex >= 0) ? `fl${flex}` : '' } return `` } function icon_object(json) { let style2 = '' let {size, aspectRatio, url, position, margin, offsetTop, offsetBottom, offsetStart, offsetEnd} = json let styleimg = `background-image:url('${url}');` size = (!size || size === '') ? 'md' : size if(size.indexOf("px") >= 0 || size.indexOf("%") >= 0) { style2 += `font-size:${size};` size = '' } else { size = 'Ex' + upperalldigit(size) } if(!aspectRatio || aspectRatio === '') { styleimg += `width:1em;` } else { ratio = ratio[0]/ratio[1] styleimg += `width:${ratio}em;` } exabs = (position === 'absolute') ? 'ExAbs' : '' if(margin && (margin.indexOf("px") >= 0 ||margin.indexOf("%") >= 0)) { style2 += `margin-top:${margin};` exmgn = '' } else { exmgn = (margin) ? 'ExMgnT' + upperalldigit(margin) : '' } if(offsetTop && (offsetTop.indexOf("px") >= 0||offsetTop.indexOf("%") >= 0)) { style2 += `top:${offsetTop};` ext = '' } else { ext = (offsetTop) ? 'ExT' + upperalldigit(offsetTop) : '' } if(offsetBottom && (offsetBottom.indexOf("px") >= 0 || offsetBottom.indexOf("%") >= 0)) { style2 += `bottom:${offsetBottom};` exb = '' } else { exb = (offsetBottom) ? 'ExB' + upperalldigit(offsetBottom) : '' } if(offsetStart && (offsetStart.indexOf("px") >= 0||offsetStart.indexOf("%") >= 0)) { style2 += `left:${offsetStart};` exl = '' } else { exl = (offsetStart) ? 'ExL' + upperalldigit(offsetStart) : '' } if(offsetEnd && (offsetEnd.indexOf("px") >= 0 || offsetEnd.indexOf("%") >= 0)) { style2 += `right:${offsetEnd};` exr = '' } else { exr = (offsetEnd) ? 'ExR' + upperalldigit(offsetEnd) : '' } return `${text}