katerinka28

Untitled

Dec 11th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import service from './service'
  2. const Poll = function () {
  3.   this.data = []
  4.   this.initial = document.getElementById('poll')
  5.   console.log(this.initial)
  6. }
  7. Poll.prototype.init = async function () {
  8.   await this.getData().then(() => {
  9.     this.__proto__.createQuestion(this.data.root, this.initial)
  10.   })
  11. }
  12. Poll.prototype.getData = async function () {
  13.   let {
  14.     data
  15.   } = (await service().get('./json.html'))
  16.   this.data = data
  17. }
  18. Poll.prototype.createNode = function (obj, parent, className) {
  19.   let nodeEl = document.createElement('div')
  20.  
  21.   nodeEl.classList.add(className)
  22.   let text = document.createElement('p')
  23.   text.innerText = obj.title
  24.   text.classList.add('text')
  25.   if (obj.kind) {
  26.     nodeEl.classList.add(`${className}--${obj.kind}`)
  27.   }
  28.   text.classList.add(obj.type)
  29.   nodeEl.appendChild(text)
  30.  
  31.   parent.appendChild(nodeEl)
  32.  
  33.   return nodeEl
  34. }
  35. Poll.prototype.createQuestion = function (question, parent = questionList.initial) {
  36.  
  37.   let nodeEl = this.createNode(question, parent, 'poll__question')
  38.   parent.appendChild(nodeEl)
  39.   console.log(parent)
  40.   if ("question" == question.type) {
  41.     this.addChildren(question, nodeEl)
  42.   }
  43.   if (parent == questionList.initial) return
  44.   this.addArrow(nodeEl)
  45. }
  46.  
  47. Poll.prototype.addChildren = function ({
  48.   variants
  49. }, parent) {
  50.   let wrap = document.createElement('div')
  51.   wrap.classList.add('poll__wrap')
  52.   wrap.style.gridTemplateColumns = `repeat(${variants.length}, 1fr)`
  53.   variants.forEach(child => {
  54.     let nodeEl = this.createNode(child, wrap, 'poll__answer')
  55.     this.addArrow(nodeEl)
  56.     this.createQuestion(questionList.data[child.ref], nodeEl)
  57.   })
  58.   parent.appendChild(wrap)
  59. }
  60.  
  61.  
  62.  
  63. Poll.prototype.addArrow = function (parent) {
  64.   let path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  65.   path.setAttribute('d', 'M25,25 L50,75 L75,25 Z')
  66.   path.setAttribute('stroke', 'black')
  67.   path.setAttribute('stroke-width', '10px')
  68.   path.setAttribute('fill', 'black')
  69.  
  70.   let rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
  71.   rect.setAttribute('width', '25px')
  72.   rect.setAttribute('height', '50px')
  73.   rect.setAttribute('fill', 'black')
  74.   rect.setAttribute('x', 38)
  75.   rect.setAttribute('y', 0)
  76.  
  77.   let arrow = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  78.   arrow.setAttribute('width', '20px')
  79.   arrow.setAttribute('height', '40px')
  80.   arrow.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
  81.   arrow.appendChild(path)
  82.   arrow.appendChild(rect)
  83.   arrow.setAttribute('viewBox', '0, 0, 100, 100')
  84.   parent.prepend(arrow)
  85. }
  86.  
  87. let questionList
  88.  
  89. document.addEventListener("DOMContentLoaded", () => {
  90.   questionList = new Poll()
  91.   questionList.init()
  92. })
Advertisement
Add Comment
Please, Sign In to add comment