home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / MediaCoder / MediaCoder2011-R8-5188.exe / htdocs / prefs / prefs.js < prev    next >
Encoding:
Text File  |  2010-02-25  |  4.4 KB  |  184 lines

  1. var curkey = null;
  2. var curnode = null;
  3. var url = document.location.href;
  4. var xmlhttp = new XMLHttpRequest;
  5.  
  6. function GetToken(str, token)
  7. {
  8.     var idx = str.indexOf(token + '=');
  9.     if (idx <= 0) return null;
  10.     var argstr = str.substring(idx + token.length + 1);
  11.     idx = argstr.indexOf('&');
  12.     return idx >=0 ? argstr.substring(0, idx) : argstr;
  13. }
  14.  
  15. function SetPrefValue(param, val)
  16. {
  17.     xmlhttp.open("GET", "/prefs/set?" + param + "=" + val, false);
  18.     xmlhttp.send(null);
  19. }
  20.  
  21. function GetPrefValue(param)
  22. {
  23.     xmlhttp.open("GET", "/prefs/get?path=" + param, false);
  24.     xmlhttp.send(null);
  25.     return xmlhttp.responseText;
  26. }
  27.  
  28. function RevertPrefValue(param)
  29. {
  30.     xmlhttp.open("GET", "/prefs/revert?path=" + param, false);
  31.     xmlhttp.send(null);
  32.     return xmlhttp.responseText;
  33. }
  34.  
  35. function getNextNode(node)
  36. {
  37.     while ((node = node.nextSibling) && node.nodeType!=1);
  38.     return node;
  39. }
  40.  
  41. function getChildNode(node)
  42. {
  43.     node = node.firstChild;
  44.     while (node && node.nodeType != 1) {
  45.         node = node.nextSibling;
  46.     } 
  47.     return node;
  48. }
  49.  
  50. function loadXML(xmlFile)
  51. {
  52.     var $xml = new XMLHttpRequest;
  53.     $xml.open('GET', xmlFile, false);
  54.     $xml.overrideMimeType('text/xml');
  55.     $xml.send(null);
  56.     var xml = $xml.responseXML;
  57.     if (!xml) {
  58.         alert("Unable to load "+xmlFile);
  59.         return null;
  60.     }
  61.     return xml;
  62. }
  63.  
  64. function transformXML(xmlDoc, xslDoc, element)
  65. {
  66.     var XSLT = new XSLTProcessor;
  67.     XSLT.importStylesheet(xslDoc);
  68.     var e = document.getElementById(element);
  69.     if (e) {
  70.         while (e.firstChild) e.removeChild(e.firstChild);
  71.         e.appendChild(XSLT.transformToFragment(xmlDoc, document));
  72.     }
  73. }
  74.  
  75. function getTransformedXML(xmlDoc, xslDoc)
  76. {
  77.     var XSLT = new XSLTProcessor;
  78.     XSLT.importStylesheet(xslDoc);
  79.     return XSLT.transformToFragment(xmlDoc, document);
  80. }
  81.  
  82. function UpdateTree(path)
  83. {
  84.     var xmlPath = "prefs.xml?text&type&value";
  85.     if (path) xmlPath += "&path=" + path;
  86.     var xmlFile = loadXML(xmlPath);
  87.     var xslTree = loadXML("prefs.xsl");
  88.     transformXML(xmlFile, xslTree, "treeview");
  89. }
  90.  
  91. function ShowValue(path)
  92. {
  93.     var xmlPath = "prefs.xml?type&value&enum&range&desc&level=1";
  94.     if (path) xmlPath += "&path=" + path;
  95.     var xmlFile = loadXML(xmlPath);
  96.     var xslFile = loadXML("value.xsl");
  97.     transformXML(xmlFile, xslFile, "valuebox");
  98.  
  99.     xslFile = loadXML("desc.xsl");
  100.     transformXML(xmlFile, xslFile, "descview");
  101.     curkey = path;
  102.  
  103.     // update value in the tree
  104.     var val = xmlFile.getElementsByTagName("value");
  105.     if (val.length > 0) {
  106.         UpdateListValue(val[0].firstChild.nodeValue);
  107.     }
  108. }
  109.  
  110. function UpdateListValue(value)
  111. {
  112.     if (curnode) {
  113.         var rownode = curnode.firstChild.firstChild;
  114.         if ((rownode = rownode.nextSibling) && (rownode = rownode.nextSibling))
  115.             rownode.setAttribute("label", value);
  116.     }
  117. }
  118.  
  119. function RevertValue()
  120. {
  121.     if (curkey) {
  122.         var val = RevertPrefValue(curkey);
  123.         ShowValue(curkey);
  124.     }
  125. }
  126.  
  127. function SaveValue(value)
  128. {
  129.     if (curkey) {
  130.         if (!value) value = "";
  131.         SetPrefValue(curkey, value);
  132.         UpdateListValue(value);
  133.     }
  134. }
  135.  
  136. function ChooseFile()
  137. {
  138.     var nsIFilePicker = Components.interfaces.nsIFilePicker;
  139.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  140.     fp.init(window, "Select a File", nsIFilePicker.modeOpen);    
  141.     var res = fp.show();
  142.     if (res == nsIFilePicker.returnOK){
  143.       var thefile = fp.file;
  144.         alert(thefile);
  145.         return thefile;
  146.     }
  147.     return null;
  148. }
  149.  
  150. function Init()
  151. {
  152.     UpdateTree(GetToken(url, "path"));
  153.     document.getElementById("topfrm").setAttribute("src", "http://www.mediacoderhq.com/info/preftop.html");
  154. }
  155.  
  156. function onTreeSelected(tree)
  157. {
  158.     var curidx = tree.currentIndex;
  159.     var items = tree.getElementsByTagName("treeitem");
  160.     var i;
  161.     for (i = 0, idx = 0; i < items.length && idx < curidx; i++, idx++) {
  162.         if (items[i].getAttribute("container") == "true") {
  163.             if (items[i].getAttribute("open") != "true") {
  164.                 // skip child nodes
  165.                 var next = items[i].nextSibling;
  166.                 if (!next) next = items[i].parentNode.parentNode.nextSibling;
  167.                 if (next) {
  168.                     // look for the next right node in items array
  169.                     for (i++; i < items.length && items[i] != next; i++);
  170.                     i--;
  171.                 }
  172.             }
  173.         }
  174.     }
  175.     if (idx == curidx) {
  176.         var key = items[i].getAttribute("uri");
  177.         var node = items[i];
  178.         while ((node = node.parentNode) && (node = node.parentNode) && node.nodeName == "treeitem") {
  179.             key = node.getAttribute("uri") + "." + key;
  180.         }
  181.         curnode = items[i];
  182.         ShowValue(key);
  183.     }
  184. }