1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xml/tests/books/books.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 + 1.5 +// An inefficient, but effective bubble sort 1.6 +function sort(collection, key) 1.7 +{ 1.8 + var i, j; 1.9 + var count = collection.length; 1.10 + var parent, child; 1.11 + 1.12 + for (i = count-1; i >= 0; i--) { 1.13 + for (j = 1; j <= i; j++) { 1.14 + if (collection[j-1][key] > collection[j][key]) { 1.15 + // Move the item both in the local array and 1.16 + // in the tree 1.17 + child = collection[j]; 1.18 + parent = child.parentNode; 1.19 + 1.20 + collection[j] = collection[j-1]; 1.21 + collection[j-1] = child; 1.22 + 1.23 + parent.removeChild(child); 1.24 + parent.insertBefore(child, collection[j]); 1.25 + } 1.26 + } 1.27 + } 1.28 +} 1.29 + 1.30 +// Set user properties on the nodes in the collection 1.31 +// based on information found in its children. For example, 1.32 +// make a property "Author" based on the content of the 1.33 +// "Author" element found in the childNode list of the node. 1.34 +// This makes later sorting more efficient 1.35 +function collectInfo(nodes, propNames) 1.36 +{ 1.37 + var i, j, k; 1.38 + var ncount = nodes.length; 1.39 + var pcount = propNames.length; 1.40 + 1.41 + for (i = 0; i < ncount; i++) { 1.42 + var node = nodes[i]; 1.43 + var childNodes = node.childNodes; 1.44 + var ccount = childNodes.length; 1.45 + 1.46 + for (j = 0; j < ccount; j++) { 1.47 + var child = childNodes[j]; 1.48 + 1.49 + if (child.nodeType == Node.ELEMENT_NODE) { 1.50 + var tagName = child.tagName; 1.51 + 1.52 + for (k = 0; k < pcount; k++) { 1.53 + var prop = propNames[k]; 1.54 + if (prop == tagName) { 1.55 + node[prop] = child.firstChild.data; 1.56 + } 1.57 + } 1.58 + } 1.59 + } 1.60 + } 1.61 +} 1.62 + 1.63 +var enabled = true; 1.64 +function toggleStyleSheet() 1.65 +{ 1.66 + if (enabled) { 1.67 + document.styleSheets[2].disabled = true; 1.68 + } 1.69 + else { 1.70 + document.styleSheets[2].disabled = false; 1.71 + } 1.72 + 1.73 + enabled = !enabled; 1.74 +} 1.75 + 1.76 +// XXX This is a workaround for a bug where 1.77 +// changing the disabled state of a stylesheet can't 1.78 +// be done in an event handler. For now, we do it 1.79 +// in a zero-delay timeout. 1.80 +function initiateToggle() 1.81 +{ 1.82 + setTimeout(toggleStyleSheet, 0); 1.83 +} 1.84 + 1.85 +var sortableProps = new Array("Author", "Title", "ISBN"); 1.86 +var books = new Array(); 1.87 + 1.88 +// We uppercase the tagName as a workaround for a bug 1.89 +// that loses the original case of the tag. 1.90 +var bookset = document.getElementsByTagName("Book"); 1.91 + 1.92 +// We need to create a "non-live" array to operate on. Since 1.93 +// we'll be moving things around in this array, we can't use 1.94 +// the read-only, live one returned by getElementsByTagName. 1.95 +for (var i=0; i < bookset.length; i++) { 1.96 + books[i] = bookset[i]; 1.97 +} 1.98 + 1.99 +collectInfo(books, sortableProps); 1.100 +