projects.js 4.6 KB

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