You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
966 B

  1. module.exports = function displayBlock ({dom, content, title, order}) {
  2. const block = document.createElement('div')
  3. block.className = 'block'
  4. if (order) {
  5. block.setAttribute('data-order', order)
  6. }
  7. if (title) {
  8. const header = document.createElement('h3')
  9. header.innerHTML = title
  10. block.appendChild(header)
  11. }
  12. if (typeof content === 'string') {
  13. const div = document.createElement('div')
  14. div.innerHTML = content
  15. block.appendChild(div)
  16. } else {
  17. block.appendChild(content)
  18. }
  19. dom.appendChild(block)
  20. reorder(dom)
  21. return block
  22. }
  23. function reorder (dom) {
  24. const children = Array.from(dom.children)
  25. children.sort((child1, child2) => {
  26. let o1 = child1.hasAttribute('data-order') ? parseFloat(child1.getAttribute('data-order')) : 0
  27. let o2 = child2.hasAttribute('data-order') ? parseFloat(child2.getAttribute('data-order')) : 0
  28. return o1 - o2
  29. })
  30. children.forEach(child => dom.appendChild(child))
  31. }