content/xml/tests/books/books.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1
michael@0 2 // An inefficient, but effective bubble sort
michael@0 3 function sort(collection, key)
michael@0 4 {
michael@0 5 var i, j;
michael@0 6 var count = collection.length;
michael@0 7 var parent, child;
michael@0 8
michael@0 9 for (i = count-1; i >= 0; i--) {
michael@0 10 for (j = 1; j <= i; j++) {
michael@0 11 if (collection[j-1][key] > collection[j][key]) {
michael@0 12 // Move the item both in the local array and
michael@0 13 // in the tree
michael@0 14 child = collection[j];
michael@0 15 parent = child.parentNode;
michael@0 16
michael@0 17 collection[j] = collection[j-1];
michael@0 18 collection[j-1] = child;
michael@0 19
michael@0 20 parent.removeChild(child);
michael@0 21 parent.insertBefore(child, collection[j]);
michael@0 22 }
michael@0 23 }
michael@0 24 }
michael@0 25 }
michael@0 26
michael@0 27 // Set user properties on the nodes in the collection
michael@0 28 // based on information found in its children. For example,
michael@0 29 // make a property "Author" based on the content of the
michael@0 30 // "Author" element found in the childNode list of the node.
michael@0 31 // This makes later sorting more efficient
michael@0 32 function collectInfo(nodes, propNames)
michael@0 33 {
michael@0 34 var i, j, k;
michael@0 35 var ncount = nodes.length;
michael@0 36 var pcount = propNames.length;
michael@0 37
michael@0 38 for (i = 0; i < ncount; i++) {
michael@0 39 var node = nodes[i];
michael@0 40 var childNodes = node.childNodes;
michael@0 41 var ccount = childNodes.length;
michael@0 42
michael@0 43 for (j = 0; j < ccount; j++) {
michael@0 44 var child = childNodes[j];
michael@0 45
michael@0 46 if (child.nodeType == Node.ELEMENT_NODE) {
michael@0 47 var tagName = child.tagName;
michael@0 48
michael@0 49 for (k = 0; k < pcount; k++) {
michael@0 50 var prop = propNames[k];
michael@0 51 if (prop == tagName) {
michael@0 52 node[prop] = child.firstChild.data;
michael@0 53 }
michael@0 54 }
michael@0 55 }
michael@0 56 }
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 var enabled = true;
michael@0 61 function toggleStyleSheet()
michael@0 62 {
michael@0 63 if (enabled) {
michael@0 64 document.styleSheets[2].disabled = true;
michael@0 65 }
michael@0 66 else {
michael@0 67 document.styleSheets[2].disabled = false;
michael@0 68 }
michael@0 69
michael@0 70 enabled = !enabled;
michael@0 71 }
michael@0 72
michael@0 73 // XXX This is a workaround for a bug where
michael@0 74 // changing the disabled state of a stylesheet can't
michael@0 75 // be done in an event handler. For now, we do it
michael@0 76 // in a zero-delay timeout.
michael@0 77 function initiateToggle()
michael@0 78 {
michael@0 79 setTimeout(toggleStyleSheet, 0);
michael@0 80 }
michael@0 81
michael@0 82 var sortableProps = new Array("Author", "Title", "ISBN");
michael@0 83 var books = new Array();
michael@0 84
michael@0 85 // We uppercase the tagName as a workaround for a bug
michael@0 86 // that loses the original case of the tag.
michael@0 87 var bookset = document.getElementsByTagName("Book");
michael@0 88
michael@0 89 // We need to create a "non-live" array to operate on. Since
michael@0 90 // we'll be moving things around in this array, we can't use
michael@0 91 // the read-only, live one returned by getElementsByTagName.
michael@0 92 for (var i=0; i < bookset.length; i++) {
michael@0 93 books[i] = bookset[i];
michael@0 94 }
michael@0 95
michael@0 96 collectInfo(books, sortableProps);
michael@0 97

mercurial