]> 105106.c2e0p.group Git - movie_parser.git/blobdiff - AtomTester.html
Add a helper function for getting the super prototype of an object.
[movie_parser.git] / AtomTester.html
index 82d9be2727d03348c6799746eb4012bf8201a5c8..d0bcbf133020e5d26f5e630ddd3b8789e1edbbfb 100644 (file)
@@ -3,26 +3,81 @@
     <title>AtomTester</title>
     <script src=Atom.js></script>
     <script>
-        var atoms = [];
+        var moovAtom;
         function onFileSelect(e) {
             var file = e.target.files[0];
             var reader = new FileReader();
-            reader.onload = (function(file) {
-                return function(e) {
-                    var offset = 0;
+            output.innerHTML = '';
+            
+            function checkForMoovAtom(offset) {
+                reader.onload = function(e) {
+                    var result = e.target.result;
+                    var basicAtom = new Atom(result);
+                    if (!basicAtom)
+                        return;
+                    else if (basicAtom.type == 'moov') 
+                        readMoovAtom(offset, basicAtom.size);
+                    else
+                        checkForMoovAtom(offset + basicAtom.size);
+                };
+                var subset = file.webkitSlice(offset, offset + 16);
+                reader.readAsArrayBuffer(subset);
+            };
+            
+            function readMoovAtom(offset, length) {
+                reader.onload = function(e) { 
+                    moovAtom = Atom.create(e.target.result);
                     var output = document.getElementById('output');
-                    output.innerHTML = '';
-                    while (offset < e.target.result.byteLength) {
-                        var atom = Atom.create(e.target.result, offset);
-                        if (!atom)
-                            break;
-                        atoms.push(atom);
-                        output.appendChild(atom.toDOMNode());
-                        offset += atom.size;
-                    }
+                    output.appendChild(toDOMNode(moovAtom));
                 };
-            })(file);
-            reader.readAsArrayBuffer(file);
+                var subset = file.webkitSlice(offset, offset + length);
+                reader.readAsArrayBuffer(subset);
+            };
+            
+            checkForMoovAtom(0);
+        }
+        
+        function toDOMRepresentation(object)
+        {
+            if (object instanceof Atom)
+                return toDOMNode(object);
+            else if (object instanceof Array) {
+                var span = document.createElement('span');
+                span.appendChild(document.createTextNode('['));
+                for (var i = 0; i < object.length; ++i) {
+                    span.appendChild(toDOMRepresentation(object[i]));
+                    if (i + 1 < object.length)
+                        span.appendChild(document.createTextNode(', '));
+                }
+                span.appendChild(document.createTextNode(']'));
+                return span;
+            }
+            else if (typeof(object) == "string")
+                return document.createTextNode('"' + object + '"');
+            else if (object instanceof Date)
+                return document.createTextNode(object.toLocaleString());
+            else
+                return document.createTextNode(object.toString());
+        }
+        
+        function toDOMNode(object)
+        {
+            var output = document.createElement('dl');
+
+            for (property in object) {
+                var value = object[property];
+                if (typeof(value) == 'function')
+                    continue;
+                var div = document.createElement('div');
+                var dt = document.createElement('dt');
+                dt.innerText = property;
+                var dd = document.createElement('dd');
+                dd.appendChild(toDOMRepresentation(value));
+                div.appendChild(dt);
+                div.appendChild(dd);
+                output.appendChild(div);
+            }
+            return output;
         }
         
         function setup() {
         }
     </script>
     <style>
-        dl { border: 1px solid black; }
+        dl { border: 1px solid black; padding: 1px; }
         dt, dd { display: inline; }
         dt:after { content:": " }
         dd:after { content:"\A"; white-space:pre; }
-        dd ol { display: inline-block; margin: 0; vertical-align: top }
+        dd dl { margin: 0; vertical-align: top }
+        dd span { display: inline-block; vertical-align: top; }
+
+        dt, dd { display: inline-block; min-width: 8em; }
+        dd { margin-left: 1em; }
         
         /* make the output display as a table */
+        /*
         dl { display: table; }
         dl div { display: table-row; }
         dt, dd { display: table-cell; }
+        */
     </style>
 </head>
 <body id="atomtester" onload="setup()">