Skip to content

Instantly share code, notes, and snippets.

@diego-lipinski-de-castro
Created August 17, 2017 22:27
Show Gist options
  • Save diego-lipinski-de-castro/7053ddd15ebc54909cbded84bfb7392b to your computer and use it in GitHub Desktop.
Save diego-lipinski-de-castro/7053ddd15ebc54909cbded84bfb7392b to your computer and use it in GitHub Desktop.
Binário
div#app
.columns
.column.is-offset-one-quarter
template(v-for="type in typeConversions")
.field
label.label {{ type.name }}
.control
input.input(v-model="type.result" type="text" disabled)
hr
.field
.control
input.input(v-model="input" type="text" placeholder="Input" v-bind:disabled="selectedType.length <= 0")
.select
select(v-model="selectedType")
option(value="nulled" disabled) Select input base type
template(v-for="type in typeConversions")
option(:value="type.value") {{ type.name }}
hr
button.button.is-primary(@click="calculate()") Calculate
button.button.is-primary(@click="reset()" style="float: right") Reset
new Vue({
el: "#app",
data() {
return {
first: "",
last: "",
input: "",
typeConversions: [
{ value: "decimal", base: 10, name: "Decimal", result: "" },
{ value: "hexadecimal", base: 16, name: "Hexadecimal", result: "" },
{ value: "octadecimal", base: 8, name: "Octadecimal", result: "" },
{ value: "binario", base: 2, name: "Binário", result: "" }
],
selectedType: ""
};
},
created() {
self = this;
},
methods: {
calculate: () => {
let typeItem = $.grep(self.typeConversions, type => {
return type.value == self.selectedType;
});
let newDecimal = self.toDecimal(self.input, typeItem[0].base);
self.typeConversions.forEach(item => {
item.result = self.converion(newDecimal, item.base);
});
},
toDecimal: (value, base) => {
return parseInt(value, base);
},
converion: (value, newBase) => {
return self.toDecimal(value).toString(newBase);
},
reset: () => {
self.input = "";
self.selectedType = "";
self.typeConversions.forEach(item => {
item.result = "";
});
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
html, body
height 100%
body
display flex
align-items center
justify-content center
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.2/css/bulma.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment