Browse Source

Added model binding and some visual aides

Bryan Allred 12 years ago
parent
commit
bdef20de24
5 changed files with 93 additions and 9 deletions
  1. 20 4
      partials/intro.html
  2. 25 1
      scripts/app.js
  3. 33 1
      scripts/controllers.js
  4. 3 3
      scripts/utilities.js
  5. 12 0
      styles/hexmeh.css

+ 20 - 4
partials/intro.html

@ -1,7 +1,23 @@
1
<h2>Welcome!</h2>
2
3
<div>
1
<div class="center">
4 2
    <label for="generated-binary">Generated binary</label>
5
    <input type="text" id="generated-binary" ng-model="binary" />
3
    <select name="generated-length" ng-model="binaryLength">
4
        <option ng:repeat="length in allowedLengths">{{length}}</option>
5
    </select>
6
    <label for="generated-length">bits long</label><br />
7
    
8
    <input type="text" id="generated-binary" ng-model="binary" disabled="disabled" class="center" style="width: 80%;" /><br />
9
    
6 10
    <button class="button blue" ng-click="generate()">Generate Binary</button>
11
    <button class="button red" ng-click="solve()">Translate to Hex</button>
12
</div>
13
14
<div class="center" ng-show="displayResults()">
15
    <div ng:repeat="part in parts" style="margin: 2em;" fadey="1000">
16
        <span class="part" fadey="1500">{{part.binary}}</span>
17
        <span fadey="1800">is now</span>
18
        <span class="part" fadey="2100">{{part.hex}}</span>
19
    </div>
20
    
21
    <label for="solved-hex">Hex</label><br />
22
    <input type="text" id="solved-hex" ng-model="hex" disabled="disabled" class="center" style="width: 80%;" /><br />
7 23
</div>

+ 25 - 1
scripts/app.js

@ -13,4 +13,28 @@ angular.module("hexmeh", [])
13 13
            templateUrl: "partials/intro.html"
14 14
        });
15 15
        //.otherwise({ redirectTo: "/" });
16
    }]);
16
    }])
17
    .directive('fadey', function() {
18
        return {
19
            restrict: 'A',
20
            link: function(scope, elm, attrs) {
21
                var duration = parseInt(attrs.fadey);
22
                
23
                if (isNaN(duration)) {
24
                    duration = 500;
25
                }
26
                
27
                elm = jQuery(elm);
28
                elm.hide();
29
                elm.fadeIn(duration)
30
                
31
                scope.destroy = function(complete) {
32
                    elm.fadeOut(duration, function() {
33
                        if (complete) {
34
                            complete.apply(scope);
35
                        }
36
                    });
37
                };
38
            }
39
        };
40
    });;

+ 33 - 1
scripts/controllers.js

@ -1,11 +1,43 @@
1 1
'use strict';
2 2
3 3
function MainController($scope, $routeParams) {
4
  $scope.allowedLengths = [4, 8, 16, 32, 64, 128, 256];
5
  $scope.binaryLength = 64;
4 6
  $scope.binary = generateBinary($scope.binaryLength);
5
  $scope.binaryLength = 128;
7
  $scope.parts = [];
8
  $scope.hex = "";
9
  
10
  $scope.displayResults = function () {
11
      return $scope.hex.length > 0;
12
  };
6 13
  
7 14
  $scope.generate = function () {
8 15
      $scope.binary = generateBinary($scope.binaryLength);
16
      $scope.parts = [];
17
      $scope.hex = "";
18
      
19
      return false;
20
  };
21
  
22
  $scope.solve = function () {
23
      $scope.parts = [];
24
      $scope.hex = "";
25
      
26
      var original = $scope.binary;
27
      
28
      while (original.length > 0) {
29
          // Find the 16 bytes we need.
30
          var part = original.substr(-4);
31
          var character = binaryToHex(part);
32
          
33
          // Divvy this stuff up.
34
          $scope.parts.push({ "binary": part, "hex": character });
35
          $scope.hex = character + $scope.hex;
36
          
37
          // Remove the last four.
38
          original = original.substr(0, original.length - 4);
39
      }
40
      
9 41
      return false;
10 42
  };
11 43
}

+ 3 - 3
scripts/utilities.js

@ -25,7 +25,7 @@ function padLeft(number, length) {
25 25
    }
26 26
27 27
    return str;
28
};
28
}
29 29
30 30
function binaryToHex(binary) {
31 31
    var character = null;
@ -41,7 +41,7 @@ function binaryToHex(binary) {
41 41
    }
42 42
43 43
    return character;
44
};
44
}
45 45
46 46
function generateBinary(length) {
47 47
    var binary = '';
@ -57,4 +57,4 @@ function generateBinary(length) {
57 57
    }
58 58
59 59
    return binary;
60
};
60
}

+ 12 - 0
styles/hexmeh.css

@ -44,6 +44,10 @@ textarea, select, input, button {
44 44
    border-radius: 4px;
45 45
}
46 46
47
.center {
48
    text-align: center;
49
}
50
47 51
.right {
48 52
    float: right;
49 53
}
@ -67,6 +71,14 @@ div[role="main"] {
67 71
    margin: 3em 2em;
68 72
}
69 73
74
.part {
75
    padding: 0.35em 1em;
76
    border: 1px dotted #000;
77
    border-radius: 4px;
78
    font-weight: bold;
79
    background-color: #eee;
80
}
81
70 82
/**
71 83
 * Buttons
72 84
 */