Parcourir la Source

Added model binding and some visual aides

Bryan Allred 12 ans auparavant
Parent
Commettre
bdef20de24
5 fichiers modifiés avec 93 ajouts et 9 suppressions
  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
<h2>Welcome!</h2>
2
3
<div>
1
<div class="center">
4
    <label for="generated-binary">Generated binary</label>
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
    <button class="button blue" ng-click="generate()">Generate Binary</button>
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
</div>
23
</div>

+ 25 - 1
scripts/app.js

13
            templateUrl: "partials/intro.html"
13
            templateUrl: "partials/intro.html"
14
        });
14
        });
15
        //.otherwise({ redirectTo: "/" });
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
'use strict';
1
'use strict';
2
2
3
function MainController($scope, $routeParams) {
3
function MainController($scope, $routeParams) {
4
  $scope.allowedLengths = [4, 8, 16, 32, 64, 128, 256];
5
  $scope.binaryLength = 64;
4
  $scope.binary = generateBinary($scope.binaryLength);
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
  $scope.generate = function () {
14
  $scope.generate = function () {
8
      $scope.binary = generateBinary($scope.binaryLength);
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
      return false;
41
      return false;
10
  };
42
  };
11
}
43
}

+ 3 - 3
scripts/utilities.js

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

+ 12 - 0
styles/hexmeh.css

44
    border-radius: 4px;
44
    border-radius: 4px;
45
}
45
}
46
46
47
.center {
48
    text-align: center;
49
}
50
47
.right {
51
.right {
48
    float: right;
52
    float: right;
49
}
53
}
67
    margin: 3em 2em;
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
 * Buttons
83
 * Buttons
72
 */
84
 */