index.html 4.86 KB
Newer Older
nextime's avatar
nextime committed
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<!DOCTYPE html>
<html>
   <head>
      <title>Decimal Calculator Client</title>
      <style>
         body {
            font-family: Segoe UI,Tahoma,Arial,Verdana,sans-serif;
         }

         #calcdisplay {
            color: #333;
            background-color: #fff;
            padding: 0.2em;
            min-height: 3em;
            text-align: right;
         }

         #calc {
            margin: 60px;
            background-color: #028ec9;
            padding: 1em;
            border-radius: 0.5em;
         }

         #calc td {
            height: 100%;
         }

         #calc button {
            width: 100%;
            height: 100%;
            min-height: 3em;
            min-width: 3em;
         }
      </style>

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
      <script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>

      <script type="text/javascript">
         var sess = null;
         var clearFirst = false;

         $(document).ready(function()
         {
            var wsuri;
            if (window.location.protocol === "file:") {
               wsuri = "ws://localhost:9000";
            } else {
               wsuri = "ws://" + window.location.hostname + ":9000";
            }

            // connect to WAMP server
            ab.connect(wsuri,

               // WAMP session was established
               function (session) {

                  sess = session;
                  console.log("Connected to " + wsuri);

                  sess.prefix("calculator", "http://example.com/simple/calculator#");
               },

               // WAMP session is gone
               function (code, reason) {

                  sess = null;

                  if (code == ab.CONNECTION_UNSUPPORTED) {
                     window.location = "http://autobahn.ws/unsupportedbrowser";
                  } else {
                     alert(reason);
                  }
               }
            );
         });

         function key_digit(d) {

            var s = $('#calcdisplay').html();

            if (clearFirst) {
               if (d == ".") {
                  s = "0.";
               } else {
                  s = d;
               }
               clearFirst = false;
            } else {
               if (d == ".") {
                  if (s.indexOf(".") == -1) {
                     s += ".";
                  }
               }
               else {
                  if (s == "0") {
                     s = d;
                  }
                  else {
                     s += d;
                  }
               }
            }
            $('#calcdisplay').html(s);
         }

         function key_op(op) {

            arg = {op: op};
            if (op != "C") {
               arg.num = $('#calcdisplay').html();
            }

            sess.call("calculator:calc", arg).then(function (res) { $('#calcdisplay').html(res); },
                                                   function (res) { alert("Error (" + res + ")"); });

            clearFirst = true;
         }
     </script>
   </head>
   <body>
      <h1>Decimal Calculator Client</h1>
      <noscript>
         <span style="color: #f00; font-weight: bold;">
            You need to turn on JavaScript.
         </span>
      </noscript>
      <table id="calc">
         <tr>
            <td id="calcdisplay" colspan="5">0</td>
         </tr>
         <tr><td style="height: 0.5em;" colspan="5"></td></tr>
         <tr>
            <td><button onclick='key_digit("7");'>7</button></td>
            <td><button onclick='key_digit("8");'>8</button></td>
            <td><button onclick='key_digit("9");'>9</button></td>
            <td><button onclick='key_op("/");'>/</button></td>
            <td rowspan="2"><button onclick='key_op("C");'>C</button></td>
         </tr>
         <tr>
            <td><button onclick='key_digit("4");'>4</button></td>
            <td><button onclick='key_digit("5");'>5</button></td>
            <td><button onclick='key_digit("6");'>6</button></td>
            <td><button onclick='key_op("*");'>*</button></td>
         </tr>
         <tr>
            <td><button onclick='key_digit("1");'>1</button></td>
            <td><button onclick='key_digit("2");'>2</button></td>
            <td><button onclick='key_digit("3");'>3</button></td>
            <td><button onclick='key_op("-");'>-</button></td>
            <td rowspan="2"><button onclick='key_op("=");'>=</button></td>
         </tr>
         <tr>
            <td colspan="2"><button onclick='key_digit("0");'>0</button></td>
            <td><button onclick='key_digit(".");'>.</button></td>
            <td><button onclick='key_op("+");'>+</button></td>
         </tr>
      </table>
   </body>
 </html>