projects.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import qs from 'querystring'
  2. import { REST } from 'api/rest-axios'
  3. export default {
  4. namespaced: true,
  5. // initial state
  6. state: {
  7. projects: []
  8. },
  9. // getters
  10. getters: {
  11. getGridPos: (state) => (index) => {
  12. let popped = state.projects[index].grid.pop()
  13. console.log('getGridPos: popped', popped)
  14. return popped
  15. }
  16. },
  17. // mutations
  18. mutations: {
  19. setProjects (state, projects) {
  20. // record the retrieved data
  21. state.projects = projects
  22. // randomly define building sizes
  23. let totalW = 0
  24. for (var i = 0; i < state.projects.length; i++) {
  25. let w = Math.round(21 + Math.random() * 15)
  26. totalW += w
  27. state.projects[i].size = {
  28. x: w,
  29. y: Math.round(100 + Math.random() * 250),
  30. z: Math.round(10 + Math.random() * 30)
  31. }
  32. }
  33. // positioning buildings on x regarding the widths
  34. // & setting up the window sizing
  35. // & setting up the content grid
  36. let margin = 5
  37. totalW += margin * (state.projects.length - 1)
  38. let wall, a
  39. let grid
  40. // fro each PROJECTS
  41. for (var j = 0; j < state.projects.length; j++) {
  42. // X POS
  43. let x = -1 * totalW / 2// + state.projects[0].size.x / 2
  44. for (var k = 0; k < j; k++) {
  45. x += state.projects[k].size.x
  46. }
  47. x += margin * j
  48. x += state.projects[j].size.x / 2
  49. state.projects[j].position = {
  50. x: x,
  51. y: -1 * state.projects[j].size.y / 2 + 10 + Math.random() * 30, // -10 + Math.random() * this.size.y / 2
  52. z: -10 + Math.random() * 10
  53. }
  54. // WINDOWS
  55. wall = {
  56. wallW: 0.001,
  57. // dig windows on face and back
  58. winW: 2 + Math.random() * 2,
  59. winH: 4 + Math.random() * 4,
  60. margin: 2,
  61. nbrWinX: 0,
  62. paddingX: 0,
  63. nbrWinY: 0,
  64. paddingY: 0,
  65. nbrWinZ: 0,
  66. paddingZ: 0
  67. }
  68. // removing windows on X until padding is enough
  69. a = 0
  70. wall.nbrWinX = Math.floor((state.projects[j].size.x - 2 * wall.margin) / wall.winW)
  71. while (wall.paddingX < 0.4) {
  72. wall.nbrWinX -= a
  73. wall.paddingX = (state.projects[j].size.x - 2 * wall.margin - wall.winW * wall.nbrWinX) / (wall.nbrWinX - 1)
  74. a++
  75. }
  76. // removing windows on Y until padding is enough
  77. a = 0
  78. wall.nbrWinY = Math.floor((state.projects[j].size.y - 2 * wall.margin) / wall.winH)
  79. while (wall.paddingY < 0.4) {
  80. wall.nbrWinY -= a
  81. wall.paddingY = (state.projects[j].size.y - 2 * wall.margin - wall.winH * wall.nbrWinY) / (wall.nbrWinY - 1)
  82. a++
  83. }
  84. state.projects[j].wall = { ...wall }
  85. // CONTENTS GRID
  86. a = 0
  87. wall.nbrWinZ = Math.floor((state.projects[j].size.z - 2 * wall.margin) / wall.winW)
  88. while (wall.paddingZ < 0.4) {
  89. wall.nbrWinZ -= a
  90. wall.paddingZ = (state.projects[j].size.z - 2 * wall.margin - wall.winW * wall.nbrWinZ) / (wall.nbrWinZ - 1)
  91. a++
  92. }
  93. grid = []
  94. for (var l = 0; l < wall.nbrWinZ * 2; l++) { // cols
  95. for (var m = 0; m < wall.nbrWinY * 2; m++) { // rows
  96. grid.push({
  97. z: margin + wall.winW / 2 * l,
  98. y: margin + wall.winH / 2 * m
  99. })
  100. }
  101. }
  102. // shuffle the grid
  103. for (let n = grid.length - 1; n > 0; n--) {
  104. const o = Math.floor(Math.random() * n)
  105. const temp = grid[n]
  106. grid[n] = grid[o]
  107. grid[o] = temp
  108. }
  109. state.projects[j].grid = [ ...grid ]
  110. }
  111. }
  112. },
  113. // actions
  114. actions: {
  115. // async get authors
  116. getProjects ({ dispatch, commit, state }) {
  117. let params = {
  118. _sort: `weight:ASC`
  119. }
  120. let q = qs.stringify(params)
  121. REST.get(`projects?` + q, {})
  122. .then(({ data }) => {
  123. console.log('projects getProjects REST: data', data)
  124. commit('setProjects', data)
  125. })
  126. .catch((error) => {
  127. console.warn('Issue with getProjects', error)
  128. Promise.reject(error)
  129. })
  130. }
  131. }
  132. }