-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathApp.vue
More file actions
199 lines (178 loc) · 5.06 KB
/
Copy pathApp.vue
File metadata and controls
199 lines (178 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<template>
<main id="app">
<transition name="fade">
<template v-if="preloading">
<Preload />
</template>
</transition>
<Topbar />
<Actionbar
:gradient="currentGradient"
:palette="showingPalette"
:showModal="showModal"
:closeModals="closeModals"
:downloadGradient="downloadGradient"
:updateDirection="updateDirection"
@togglePalette="togglePalette" />
<List
:gradients="gradients"
:direction="currentDirection"
:palette="showingPalette"
:updateGradient="updateGradient" />
<Display
:gradient="currentGradient"
:direction="currentDirection"
:closeModals="closeModals"
:updateDirection="updateDirection"
:showModal="showModal"
@updatedIndex="updateIndex"
@closePalette="closePalette"
@togglePalette="togglePalette" />
<GradientModal
:show="showingGradientModal"
:closeModals="closeModals" />
<CodeModal
:gradient="currentGradient"
:direction="currentDirection"
:show="showingCodeModal"
:closeModals="closeModals" />
</main>
</template>
<script>
import Preload from './components/Preload';
import Topbar from './components/Topbar';
import Actionbar from './components/Actionbar';
import Display from './components/Display';
import List from './components/List';
import GradientModal from './components/modals/GradientModal';
import CodeModal from './components/modals/CodeModal';
import Download from './services/gradientDownloader';
import Favicon from './services/faviconUpdater';
import Gradients from '../gradients.json';
const gradients = [...Gradients].reverse();
const WHITESPACE_RE = /\s/g;
let faviconTimer = null;
export default {
name: 'app',
data() {
return {
index: {},
preloading: true,
directionIndex: 2,
currentDirection: 'to right',
directions: ['to left', 'to bottom', 'to right', 'to top'],
currentGradient: {
name: null,
colors: ['#ffffff', '#ffffff'],
},
gradients: [],
showingPalette: false,
showingGradientModal: false,
showingCodeModal: false,
};
},
components: {
Preload,
Topbar,
Display,
Actionbar,
List,
GradientModal,
CodeModal,
},
methods: {
showModal(type) {
if (type === 'gradient') this.showingGradientModal = true;
if (type === 'code') this.showingCodeModal = true;
},
closeModals() {
this.showingGradientModal = false;
this.showingCodeModal = false;
},
updateGradient(name) {
const id = this.gradients.findIndex(gradient => gradient.name.replace(/\s/g, '') === name.replace(/\s/g, ''));
this.index = id;
if (this.showingPalette) this.showingPalette = false;
},
closePalette() {
this.showingPalette = false;
},
togglePalette() {
this.showingPalette = !this.showingPalette;
},
updateIndex(direction) {
if (direction === 'up') {
const updatedIndex = this.index + 1;
this.index = (updatedIndex > this.gradients.length - 1) ? 0 : updatedIndex;
} else if (direction === 'down') {
const updatedIndex = this.index - 1;
this.index = (updatedIndex < 0) ? this.gradients.length - 1 : updatedIndex;
}
this.showingPalette = false;
},
updateDirection(dir) {
/* eslint-disable max-len */
const currentIndex = this.directionIndex;
if (dir === 'up') {
let newIndex = currentIndex + 1;
newIndex = (newIndex >= this.directions.length) ? 0 : newIndex;
this.directionIndex = newIndex;
} else if (dir === 'down') {
let newIndex = currentIndex - 1;
newIndex = (newIndex < 0) ? this.directions.length - 1 : newIndex;
this.directionIndex = newIndex;
}
},
downloadGradient() {
Download(
this.currentDirection,
this.currentGradient.name,
...this.currentGradient.colors,
);
},
updateFavicon() {
clearTimeout(faviconTimer);
faviconTimer = setTimeout(() => {
Favicon(this.currentDirection, ...this.currentGradient.colors);
}, 150);
},
fetchGradients() {
this.gradients = gradients;
},
setCurrentGradient() {
if (window.location.hash) {
const gradientName = window.location.hash.substring(1);
this.updateGradient(gradientName);
} else {
const randomId = Math.floor(Math.random() * this.gradients.length);
this.index = randomId;
}
},
fadePreloader() {
const app = this;
setTimeout(() => {
app.preloading = false;
}, 2000);
},
boot() {
this.fetchGradients();
this.setCurrentGradient();
this.fadePreloader();
},
},
watch: {
index(val) {
this.currentGradient = this.gradients[val];
window.location.hash = this.currentGradient.name.replace(WHITESPACE_RE, '');
this.updateFavicon();
},
directionIndex(id) {
this.currentDirection = this.directions[id];
this.updateFavicon();
},
},
mounted() {
this.boot();
},
};
</script>