-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02-ComputedSocks.html
129 lines (89 loc) · 2.81 KB
/
02-ComputedSocks.html
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
<html>
<head>
<script src="https://unpkg.com/vue"></script>
<link href="./assets/style.css" rel="stylesheet">
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet">
<title>Computed Socks</title>
</head>
<body>
<div id="app">
<div class="nav-bar"><h1>Computed Socks</h1></div>
<div class="product">
<div class="product-image">
<img :src="image">
</div>
<div class="product-info">
<!-- Computed Properties -->
<h1>{{ title }}</h1>
<div>
<p v-if="inventory > 10">In Stock</p>
<p v-else-if="inventory">Almost sold out</p>
<p v-else>Out of Stock</p>
</div>
<button
v-on:click="cart++; inventory--"
:disabled="!inventory"
:class="{'add-to-cart': true, disabledButton: inventory ? false : true}"
>
Add to Cart
</button>
<div class="cart">
{{ cart }}
<i class="fas fa-shopping-cart"></i>
</div>
<!-- Loops -->
<div v-for="(variant, index) in variants.filter(x => !x.soldOut)"
:key="variant.id"
class="color-box"
:class="{active: selectedVariantIndex === index}"
:style="{backgroundColor: variant.color}"
@mouseover="setSelectedVariant(variant, index)"
></div>
</div>
<!-- Can use Math, Date directly inside the templates -->
<small>Rendered on: {{ new Date().toGMTString() }} <br><br></small>
</div>
</div>
<a href="01-Socks.html" class="prev">
<i class="fas fa-arrow-left"></i> moving back
</a>
<a href="03-ComponentSocks.html" class="next">
moving on <i class="fas fa-arrow-right"></i>
</a>
</body>
</html>
<script type="text/javascript">
Vue.createApp({
data() {
return {
product: 'Socks',
selectedVariantIndex: 0,
variants: [
{id: 1, color: 'green'},
{id: 2, color: 'blue'},
{id: 3, color: 'red', soldOut: true},
],
inventory: 3,
cart: 0
};
},
methods: {
setSelectedVariant(variant, index) {
console.log('hover', index, variant);
this.selectedVariantIndex = index;
}
},
computed: {
title() {
// Return values are cached
return this.product.toUpperCase();
},
image() {
const color = this.variants[this.selectedVariantIndex].color;
return `./assets/socks-${color}.jpg`;
}
}
}).mount('#app');
</script>
<!-- livereload -->
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>