/
AppDemo9Id.vue
152 lines (131 loc) · 5.17 KB
/
AppDemo9Id.vue
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
<template>
<app-demo :path="DEFAULT_PATH" :name="$options.name" class="app-demo__card-9">
<pre class="app-demo__code keadaan"></pre>
<input type="number" class="app-demo__form input1" min="0" />
<select class="app-demo__form operator">
<option value="+">+</option>
<option value="-">−</option>
<option value="*">×</option>
<option value="/">÷</option>
</select>
<input type="number" class="app-demo__form input2" min="0" />
<div class="hasil"></div>
<div ref="script9" class="script"></div>
</app-demo>
</template>
<script lang="ts">
import onetime from 'onetime'
import ExtendableCreateReactivity1 from './ExtendableCreateReactivity1'
export default ExtendableCreateReactivity1.extend({
name: 'AppDemo9Id',
mounted() {
const interval = setInterval(() => {
const script9 = this.$refs?.script9 as Element
if (script9) {
this.init(script9)
clearInterval(interval)
}
}, 50)
},
methods: {
init: onetime(function (script9: Element) {
const newScript = document.createElement('script')
const inlineScript = document.createTextNode(`if (!window.keadaan9) {
// Kita meletakkan kode di dalam ekspresi fungsi yang dipanggil secara langsung untuk mencegah mengotori variabel global
// Kita juga mengganti fungsi panah menjadi fungsi anonim karena fungsi panah akan diserialisasi oleh Nuxt.
window.keadaan9 = (function () {
const OPERATOR = {
TAMBAH: '+',
KURANG: '-',
KALI: '*',
BAGI: '/'
}
const keadaan = {
hasil: 0,
operator: OPERATOR.TAMBAH,
input1: 0,
input2: 0
}
function mulai() {
// Kita harus memberi awalan selektor sesuai dengan kelas akar komponen
// Hal tersebut mencegah scrip dieksekusi untuk semua demo
const tampilanKeadaan = document.querySelector(
'.app-demo__card-9 .keadaan'
)
const tampilanHasil = document.querySelector(
'.app-demo__card-9 .hasil'
)
const tampilanInput1 = document.querySelector(
'.app-demo__card-9 .input1'
)
const tampilanInput2 = document.querySelector(
'.app-demo__card-9 .input2'
)
const tampilanOperator = document.querySelector(
'.app-demo__card-9 .operator'
)
function mutakhirkanTampilan() {
tampilanKeadaan.innerText = JSON.stringify(keadaan, null, 2)
tampilanHasil.innerText = keadaan.hasil.toString()
tampilanInput1.value = keadaan.input1.toString()
tampilanInput2.value = keadaan.input2.toString()
tampilanOperator.value = keadaan.operator
}
function kalkulasiHasil() {
switch (keadaan.operator) {
case OPERATOR.TAMBAH:
keadaan.hasil = keadaan.input1 + keadaan.input2
break
case OPERATOR.KURANG:
keadaan.hasil = keadaan.input1 - keadaan.input2
break
case OPERATOR.KALI:
keadaan.hasil = keadaan.input1 * keadaan.input2
break
case OPERATOR.BAGI:
keadaan.hasil = keadaan.input1 / keadaan.input2
break
default:
break
}
}
mutakhirkanTampilan()
tampilanInput1.addEventListener('input', function (peristiwa) {
const targetInput1 = peristiwa.target
keadaan.input1 = parseInt(targetInput1.value)
})
tampilanInput2.addEventListener('input', function (peristiwa) {
const targetInput2 = peristiwa.target
keadaan.input2 = parseInt(targetInput2.value)
})
tampilanOperator.addEventListener('change', function (peristiwa) {
const targetOperator = peristiwa.target
const selectedOperator = targetOperator.selectedOptions[0].value
keadaan.operator = selectedOperator
})
let input1 = keadaan['input1']
Object.defineProperty(keadaan, 'input1', {
enumerable: true,
configurable: true,
get: function pengambilReaktif() {
return input1
},
set: function pengaturReaktif(nilaiBaru) {
input1 = nilaiBaru
kalkulasiHasil()
mutakhirkanTampilan()
}
})
}
// Kita harus menjalankan fungsi karena skrip ini dieksekusi di dalam komponen vue
// document.addEventListener('DOMContentLoaded', mulai)
mulai()
return keadaan
})()
}`)
newScript.appendChild(inlineScript)
script9.appendChild(newScript)
})
}
})
</script>