Flexbox

by Trent Haynes

HTML

<div class="wrap">
  <div class="message-board">
    <p>Binary-Decimal-Hexadecimal Converter</p>
  </div>
  <div class="calculator">
    <div class="row">
      <div class="display">0</div>
    </div>

    <div class="row">
      <div class="description" title="Convert from which number-system? "><label>From</label></div>
      <div title="Convert FROM binary number-system ..." class="system from btn bin"
           data-system-string="bin" data-system-number="2">Bin</div>
      <div title="Convert FROM decimal number-system ..." class="system from btn dec"
           data-system-string="dec" data-system-number="10">Dec</div>
      <div title="Convert FROM hexadecimal number-system ..." class="system from btn hex"
           data-system-string="hex" data-system-number="16">Hex</div>
      <div class="system btn del reset" title="Reset converter-app">
        <i class="fa fa-refresh" aria-hidden="true"></i></div>
    </div>

    <div class="numbers">
      <div class="row">
        <div class="num btn bin dec hex" data-number-value="0">0</div>
        <div class="num btn bin dec hex" data-number-value="1">1</div>
        <div class="num btn dec hex" data-number-value="2">2</div>
        <div class="num btn dec hex" data-number-value="3">3</div>
        <div class="num btn dec hex" data-number-value="4">4</div>
        <div class="num btn dec hex" data-number-value="5">5</div>
        <div class="num btn dec hex" data-number-value="6">6</div>
        <div class="num btn dec hex" data-number-value="7">7</div>
      </div>
      <div class="row">
        <div class="num btn dec hex" data-number-value="8">8</div>
        <div class="num btn dec hex" data-number-value="9">9</div>
        <div class="num btn hex" data-number-value="A">A</div>
        <div class="num btn hex" data-number-value="B">B</div>
        <div class="num btn hex" data-number-value="C">C</div>
        <div class="num btn hex" data-number-value="D">D</div>
        <div class="num btn hex"...

CSS

.wrap {
  max-width: 480px;
  margin: 20px auto;
  font-family: helvetica, arial, sans-serif;
}

.calculator {
  background-color: #cdcdcd;
  padding: 10px 20px;
  border-radius: 16px;
  border: 1px solid #000;
  border-right: 2px solid #000;
  border-bottom: 2px solid #000;
}

.display {
  width: 100%;
  text-align: right;
  background: #efefef;
  background: linear-gradient(to right, #c5c5c5, #f4f4f4);
  padding: 10px 15px;
  border-radius: 6px;
  border: 1px solid #000;
  border-top: 2px solid #000;
  border-left: 2px solid #000;
  font-family: "courier new", sans-serif;
  font-size: 1.5em;
}

.row {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
  
  align-items: center;
}

.description {
  font-size: 1.5em;
  padding-right: 2.0833%;
  min-width: 12.4998%;
}

.btn {
  border-radius: 12px;
  border: 1px solid #000;
  width: 4.16666%;
  height: 4.16666%;
  text-align: center;
  line-height: 20px;
  padding: 2.0833333%;
  margin: 1.0416%;
  border-right: 2px solid #000;
  border-bottom: 2px solid #000;
  position: relative;
  text-shadow: 1px 1px 1px #323232;
}

label {
  text-shadow: 2px 2px 2px #969696;
}

.system {
  width: 25%;
  background-color: #898989;
  color: white;
  font-weight: bold;
  font-size: 2em;
}

.num {
  background-color: #efefef;
}

.btn:hover {
  opacity: 0.5;
}

.btn:active {
  top: 2px;
  border-right: 2px solid transparent;
  border-bottom: 2px solid transparent;
}

.del {
  color: orange;
  font-size: 1.5em;
}

.not-selectable {
  opacity: 0.2;
  pointer-events: none;
}

.selected, .reset:active {
  border-top: 2px solid #000;
  border-left: 2px solid #000;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  color: #898989;
  background-color: white;
}

.message-board {
  font-weight: bold;
  font-family: georgia;
  font-size: 2em;
  text-align:...

JavaScript

var sc = {};

sc.btns = document.querySelectorAll('.btn');
sc.from = document.querySelectorAll('.from');
sc.to = document.querySelectorAll('.to');
sc.nums = document.querySelectorAll('.num');
sc.delete = document.querySelector('.del'); 
sc.display = document.querySelector('.display');

sc.currentSystem = 'dec';
sc.targetSystemInt;
sc.targetSystemString;

sc.btns = Array.prototype.slice.call(sc.btns);
sc.from = Array.prototype.slice.call(sc.from);
sc.to = Array.prototype.slice.call(sc.to);
sc.nums = Array.prototype.slice.call(sc.nums);

sc.changeClasses = function (element, toRemove, toAdd) {
  element.classList.remove(toRemove);
  element.classList.add(toAdd);
}

sc.setSystem = function(setSystem) {

  sc.nums.forEach(function(num) {
    if (num.classList.contains(setSystem)) {
      sc.changeClasses(num, 'not-selectable', 'selectable');
    } else {
      sc.changeClasses(num, 'selectable', 'not-selectable');
    }
  });

  sc.setState = function(setOfElements, addOrRemove, cssClass) {
    setOfElements.forEach(function(element) {

      if (addOrRemove === 'add') {
        element.classList.add(cssClass);
      }

      if (addOrRemove === 'remove') {
        element.classList.remove(cssClass);
      }
    });
  }    

  sc.from.forEach(function(btn) {

    if (btn.classList.contains(setSystem)) {
      btn.classList.add('selected');
    } else {
      btn.classList.remove('selected');
    }
  });

  sc.to.forEach(function(btn) {
    var btnSystem = btn.getAttribute('data-system-string');

    if (btnSystem === sc.currentSystem) {
      btn.classList.add('not-selectable');
    } else {
      btn.classList.remove('not-selectable');
    }

  });
}

sc.from.forEach(function(element) {

  element.addEventListener('click', function(e) {
    sc.currentSystem =
      e.currentTarget.getAttribute('data-system-string');
    sc.setSystem(sc.currentSystem);
  });
});

sc.to.forEach(function(element) {

  element.addEventListener('click', function(e) {
    var toConvert =...