tabs.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. define([], function () {
  2. return function () {
  3. var self = this
  4. var tabs = document.createElement("ul")
  5. tabs.classList.add("tabs")
  6. var container = document.createElement("div")
  7. function gotoTab(li) {
  8. for (var i = 0; i < tabs.children.length; i++)
  9. tabs.children[i].classList.remove("visible")
  10. while (container.firstChild)
  11. container.removeChild(container.firstChild)
  12. li.classList.add("visible")
  13. var tab = document.createElement("div")
  14. tab.classList.add("tab")
  15. container.appendChild(tab)
  16. li.child.render(tab)
  17. }
  18. function switchTab() {
  19. gotoTab(this)
  20. return false
  21. }
  22. self.add = function (title, d) {
  23. var li = document.createElement("li")
  24. li.textContent = title
  25. li.onclick = switchTab
  26. li.child = d
  27. tabs.appendChild(li)
  28. var anyVisible = false
  29. for (var i = 0; i < tabs.children.length; i++)
  30. if (tabs.children[i].classList.contains("visible")) {
  31. anyVisible = true
  32. break
  33. }
  34. if (!anyVisible)
  35. gotoTab(li)
  36. }
  37. self.render = function (el) {
  38. el.appendChild(tabs)
  39. el.appendChild(container)
  40. }
  41. return self
  42. }
  43. })