/
SpeedLimitModal.vue
138 lines (136 loc) · 3.23 KB
/
SpeedLimitModal.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
<template>
<v-dialog
v-model="dialog"
scrollable
content-class="rounded-form"
max-width="500px"
:fullscreen="isPhone"
>
<v-card>
<v-card-title class="pa-0">
<v-toolbar-title class="ma-4 primarytext--text">
<h3>Limit {{ mode }}</h3>
</v-toolbar-title>
</v-card-title>
<v-card-text>
<v-container>
<v-row>
<v-col>
<v-text-field
v-model="limit"
autofocus
clearable
label="Speed Limit"
:prepend-inner-icon="mdiSpeedometer"
suffix="KB/s"
@focus="$event.target.select()"
@keydown.enter="setLimit"
/>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-divider />
<v-card-actions class="justify-end">
<v-btn
class="accent white--text elevation-0 px-4"
@click="setLimit"
>
Save
</v-btn>
<v-btn
class="error white--text elevation-0 px-4"
@click="close"
>
Cancel
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
import { mdiSpeedometer, mdiClose } from '@mdi/js'
import { Modal, FullScreenModal } from '@/mixins'
import qbit from '@/services/qbit'
export default {
name: 'SpeedLimitModal',
mixins: [Modal, FullScreenModal],
props: {
mode: String,
hash: String
},
data() {
return {
limit: '',
mdiSpeedometer, mdiClose
}
},
computed: {
...mapGetters(['getTorrent']),
torrent() {
return this.getTorrent(this.hash)
},
isPhone() {
return this.$vuetify.breakpoint.xsOnly
}
},
async created() {
switch (this.mode) {
case 'download':
if (this.isGlobal()) {
const limit = await qbit.getGlobalDownloadLimit()
this.limit = this.formatLimit(limit)
} else {
this.limit = this.formatLimit(this.torrent?.dl_limit)
}
break
case 'upload':
if (this.isGlobal()) {
const limit = await qbit.getGlobalUploadLimit()
this.limit = this.formatLimit(limit)
} else {
this.limit = this.formatLimit(this.torrent?.up_limit)
}
break
default:
break
}
},
methods: {
setLimit() {
switch (this.mode) {
case 'download':
if (this.isGlobal()) {
qbit.setGlobalDownloadLimit(this.exportLimit())
} else {
qbit.setDownloadLimit([this.hash], this.exportLimit())
}
break
case 'upload':
if (this.isGlobal()) {
qbit.setGlobalUploadLimit(this.exportLimit())
} else {
qbit.setUploadLimit([this.hash], this.exportLimit())
}
break
default:
break
}
this.close()
},
isGlobal() {
return this.torrent ? false : true
},
formatLimit(limit) {
return limit > 0 ? limit / 1024 : '∞'
},
exportLimit() {
return this.limit > 0 ? this.limit * 1024 : NaN
},
close() {
this.dialog = false
}
}
}
</script>