Home.vue 5.95 KB
Newer Older
1
<template>
2
  <div class="home">
3
    <div id="grid">
Kent Nielsen's avatar
Kent Nielsen committed
4
5
      <div @mousedown="resizeVertical($event, rowIndex)" class="row" v-for="(rowBlock, rowIndex) in layout" :key="rowIndex" :style="{ height: rowBlock.height + 'px' }">
        <div @mousedown="resizeHorizontal($event, rowIndex, colIndex)" class="col" v-for="(colBlock, colIndex) in rowBlock.blocks" :key="colIndex" :style="{ 'flex-grow': colBlock.width }">
Johan Degn's avatar
Johan Degn committed
6
          <component :is="colBlock.type"></component>
7
8
        </div>
      </div>
Johan Degn's avatar
Johan Degn committed
9
10
11
      <button class="add-block" @click="addBlock()">
        <img v-svg-inline class="icon" src="@/assets/collapse-plus.svg"/>
      </button>
12
    </div>
13
14
15
16
  </div>
</template>

<script>
17
18
19
20
import ActiveCourses from "@/components/blocks/ActiveCourses";
import Deadlines from "@/components/blocks/Deadlines";
import Feed from "@/components/blocks/Feed";
import Mail from "@/components/blocks/Mail";
21
import Calendar from "@/components/blocks/Calendar";
22

23
24
export default {
  name: 'Home',
25
26
27

  data() {
    return {
Johan Degn's avatar
WIP    
Johan Degn committed
28
      layout: [
29
        {
Johan Degn's avatar
Johan Degn committed
30
31
          height: 400,
          blocks: [{type: "Calendar", width: 75}, {type: "ActiveCourses", width: 25}]
32
33
        },
        {
Johan Degn's avatar
Johan Degn committed
34
35
          height: 250,
          blocks: [{type: "Deadlines", width: 100}]
36
37
        },
        {
Johan Degn's avatar
Johan Degn committed
38
39
          height: 300,
          blocks: [{type: "Feed", width: 50}, {type: "Mail", width: 25}, {type: "Mail", width: 25}]
Johan Degn's avatar
WIP    
Johan Degn committed
40
        }
41
      ],
42
      mouseDragPos: {x: 0, y: 0},
Kent Nielsen's avatar
Kent Nielsen committed
43
44
      verticalResizeObject: {index: 0},
      horizontalResizeObject: {row: 0, col: 0}
45
46
47
48
    }
  },

  methods: {
Johan Degn's avatar
Johan Degn committed
49
50
51
52
    addBlock() {
      this.layout.push({
        height: 300,
        blocks: [{ type: "ActiveCourses", width: 30 },{ type: "Feed", width: 70 }]
53
      });
54
55
    },

56
57
58
    resizeHorizontalMouseMove(event){
      const deltaX = this.mouseDragPos.x - event.x;
      this.mouseDragPos.x = event.x;
Kent Nielsen's avatar
Kent Nielsen committed
59
60
61
      const row = this.horizontalResizeObject.row;
      const col = this.horizontalResizeObject.col;

Kent Nielsen's avatar
Kent Nielsen committed
62
      // Find index of select col in html document
Kent Nielsen's avatar
Kent Nielsen committed
63
64
65
66
67
68
69
      let colIndex = col;
      for (let i = 0; i < row; i++) {
        const element = this.layout[i];
        colIndex += element.blocks.length;
      }
      const leftCol = document.getElementsByClassName("col")[colIndex - 1];
      const rigthCol = document.getElementsByClassName("col")[colIndex];
Kent Nielsen's avatar
Kent Nielsen committed
70
71
72
      
      // Total flex allowed from layout
      const totalFlex = this.layout[row].blocks[col -1].width + this.layout[row].blocks[col].width;
Kent Nielsen's avatar
Kent Nielsen committed
73
74
      const contentWidth = leftCol.clientWidth + rigthCol.clientWidth + 40;
      
Kent Nielsen's avatar
Kent Nielsen committed
75
76
77
78
79
80
81
82
83
84
85
86
87
      // Resized size
      const newLeftWidth = leftCol.clientWidth - deltaX;
      const newRightWidth = rigthCol.clientWidth + deltaX;

      // Calculating new flexGrow value
      // This is not normalized to 100"%"
      let newFlexRight = contentWidth/newRightWidth;
      let newFlexLeft = (newLeftWidth * parseFloat(newFlexRight))/newRightWidth;

      // Normalizing flewGrow to 100"%"
      const preAdjustedTotalFlex = newFlexRight+newFlexLeft;
      newFlexRight = (newFlexRight/preAdjustedTotalFlex)*totalFlex;
      newFlexLeft = (newFlexLeft/preAdjustedTotalFlex)*totalFlex;
Kent Nielsen's avatar
Kent Nielsen committed
88
      
Kent Nielsen's avatar
Kent Nielsen committed
89
90
91
      // Storing new style.
      this.layout[row].blocks[col -1].width = newFlexLeft;
      this.layout[row].blocks[col].width = newFlexRight;
92
93
    },

94
95
96
    resizeVerticalMouseMove(event){
      const deltaY = this.mouseDragPos.y - event.y;
      this.mouseDragPos.y = event.y;
Kent Nielsen's avatar
Kent Nielsen committed
97
      this.layout[this.verticalResizeObject.index].height -= deltaY
98
99
    },

Kent Nielsen's avatar
Kent Nielsen committed
100
    resizeHorizontal(event, row, col){
101
      if(event.offsetX < 0){
102
        this.mouseDragPos.x = event.x;
Kent Nielsen's avatar
Kent Nielsen committed
103
104
        this.horizontalResizeObject.row = row;
        this.horizontalResizeObject.col = col;
105
        document.addEventListener("mousemove", this.resizeHorizontalMouseMove, false);
Kent Nielsen's avatar
Kent Nielsen committed
106
        document.addEventListener("mouseup", this.eventRemover, false);
107
        document.getElementsByTagName("html")[0].classList.add("no-select");
108
      }
109
110
    },

Kent Nielsen's avatar
Kent Nielsen committed
111
112
    resizeVertical(event, index){
      if(event.offsetY > this.layout[index].height){
Kent Nielsen's avatar
Kent Nielsen committed
113
        this.mouseDragPos.y = event.y;
114
        this.verticalResizeObject.index = index; 
Kent Nielsen's avatar
Kent Nielsen committed
115
116
        document.addEventListener("mousemove", this.resizeVerticalMouseMove, false);
        document.addEventListener("mouseup", this.eventRemover, false);
117
        document.getElementsByTagName("html")[0].classList.add("no-select");
118
      }
Kent Nielsen's avatar
Kent Nielsen committed
119
120
121
122
    },

    eventRemover(){
      document.removeEventListener("mousemove", this.resizeHorizontalMouseMove, false);
Kent Nielsen's avatar
Kent Nielsen committed
123
124
      document.removeEventListener("mousemove", this.resizeVerticalMouseMove, false);
      document.removeEventListener("mouseup", this.eventRemover, false);
125
      document.getElementsByTagName("html")[0].classList.remove("no-select");
126
127
128
129
130
    }
  },

  components: {
    ActiveCourses,
131
132
    Deadlines,
    Feed,
133
134
    Mail,
    Calendar
135
  }
136
137
}
</script>
138
139

<style scoped lang="scss">
140
  $gap: $default-margin;
141
142
143
144
145
146
147
148

  #grid {
    width: 100%;
    padding: $gap;
  }

  .row {
    display: flex;
Johan Degn's avatar
Johan Degn committed
149
    margin-bottom: $gap;
Kent Nielsen's avatar
Kent Nielsen committed
150
    position: relative;
Kent Nielsen's avatar
Kent Nielsen committed
151
    min-height: 150px
Kent Nielsen's avatar
Kent Nielsen committed
152
153
  }

Kent Nielsen's avatar
Kent Nielsen committed
154
  // Must be outside ".row" or else after affect children
Kent Nielsen's avatar
Kent Nielsen committed
155
156
  .row::after{
        content: " ";
Kent Nielsen's avatar
Kent Nielsen committed
157
        background-color: rgba(0, 0, 0, 0);
Kent Nielsen's avatar
Kent Nielsen committed
158
159
160
161
162
        position: absolute;
        bottom: - $gap;
        width: 100%;
        height: $gap;
        cursor: n-resize;
163
  }
164

Johan Degn's avatar
Johan Degn committed
165
166
  .col {
    flex-basis: 0; // a basis of 0px. very important!
167
    background-color: $light-green-theme-color;
Johan Degn's avatar
Johan Degn committed
168
    border-radius: 4px;
169
    border: 2px solid $theme-color;
Kent Nielsen's avatar
Kent Nielsen committed
170
    position: relative;
171

Johan Degn's avatar
Johan Degn committed
172
173
    &:not(:first-child) {
      margin-left: $gap / 2;
Kent Nielsen's avatar
Kent Nielsen committed
174
      
Kent Nielsen's avatar
Kent Nielsen committed
175
176
      &::before{
        content: " ";
Kent Nielsen's avatar
Kent Nielsen committed
177
        background-color: rgba(0, 0, 0, 0);
Kent Nielsen's avatar
Kent Nielsen committed
178
179
180
181
182
183
        position: absolute;
        left: - $gap - 2px;
        width: $gap;
        height: 100%;
        cursor: w-resize;
      }
184
    }
Kent Nielsen's avatar
Kent Nielsen committed
185

Johan Degn's avatar
Johan Degn committed
186
187
    &:not(:last-child) {
      margin-right: $gap / 2;
188
189
    }
  }
Johan Degn's avatar
Johan Degn committed
190
  
191
192
193
194
195
196
197
198
  .add-block {
    width: 50px;
    height: 50px;
    border: none;
    background: none;
    padding: 0;

    svg {
199
      fill: #777f80;
200
201
202
203
204
205
    }

    &:hover, &:focus {
      cursor: pointer;

      svg {
206
        fill: $darker-theme-color;
207
208
209
      }
    }
  }
210
</style>