home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / sb-file-panel.js < prev    next >
Encoding:
JavaScript  |  2001-03-21  |  16.5 KB  |  545 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *    Ben Goodger
  22.  */
  23.  
  24. /*
  25.   Script for the file properties window
  26. */
  27.  
  28. function FileProperties()
  29. {
  30.   var tree = document.getElementById('fileTree');
  31.  
  32.   if (tree.selectedItems.length >= 1) {
  33.   // don't bother showing properties on bookmark separators
  34.   var type = tree.selectedItems[0].getAttribute('type');
  35.     if (type != "http://home.netscape.com/NC-rdf#BookmarkSeparator") {
  36.     var props = window.open("chrome://communicator/content/bookmarks/bm-props.xul",
  37.                                 "BookmarkProperties", "chrome,menubar,resizable");
  38.     props.BookmarkURL = tree.selectedItems[0].getAttribute("id");
  39.   }
  40.   } else {
  41.     dump("nothing selected!\n");
  42.   }
  43. }
  44.  
  45. function OpenSearch(tabName)
  46. {
  47.   window.openDialog("resource:/res/samples/search.xul", "SearchWindow", "dialog=no,close,chrome,resizable", tabName);
  48. }
  49.  
  50. function OpenURL(event, node)
  51. {
  52.     // clear any single-click/edit timeouts
  53.     if (timerID != null)
  54.     {
  55.         gEditNode = null;
  56.         clearTimeout(timerID);
  57.         timerID = null;
  58.     }
  59.  
  60.     if (node.getAttribute('container') == "true")
  61.     {
  62.         return(false);
  63.     }
  64.  
  65.     var url = node.getAttribute('id');
  66.  
  67.     // Ignore "NC:" urls.
  68.     if (url.substring(0, 3) == "NC:")
  69.     {
  70.         return(false);
  71.     }
  72.  
  73.   try
  74.   {
  75.     // add support for IE favorites under Win32, and NetPositive URLs under BeOS
  76.     if (url.indexOf("file://") == 0)
  77.     {
  78.       var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  79.       if (rdf)   rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  80.       if (rdf)
  81.       {
  82.         var fileSys = rdf.GetDataSource("rdf:files");
  83.         if (fileSys)
  84.         {
  85.           var src = rdf.GetResource(url, true);
  86.           var prop = rdf.GetResource("http://home.netscape.com/NC-rdf#URL", true);
  87.           var target = fileSys.GetTarget(src, prop, true);
  88.           if (target) target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
  89.           if (target) target = target.Value;
  90.           if (target) url = target;
  91.  
  92.         }
  93.       }
  94.     }
  95.   }
  96.   catch(ex)
  97.   {
  98.   }
  99.  
  100.     if(top.isEditor != undefined) {
  101.         if(top.editorShell) {
  102.             var ext = getFileExtension(url);
  103.             // look at the extension of the file to see what should be done
  104.             // with it. Note that this is a typically windowsey approach. will
  105.             // rdf:files hold a reference to the actual file type? (Mac?)
  106.             switch(ext) {
  107.             // XXX Crude, but it will do for now.
  108.             case "gif":
  109.             case "jpeg":
  110.             case "jpg":
  111.             case "png":
  112.                 // just insert the image
  113.                 EditorAutoInsertImage(url);
  114.                 break;
  115.             case "htm":
  116.             case "html":
  117.             case "xul":
  118.                 toEditor(url);
  119.                 break;
  120.             case "js":
  121.                 EditorInsertJSFile(url);
  122.                 break;
  123.             case "css":
  124.                 EditorInsertCSSFile(url);
  125.                 break;
  126.             default:
  127.                 // load the file in the editor
  128.                 dump("Editor Message: Weirdo File Format\n");
  129.                 toEditor(url);
  130.                 break;
  131.             }
  132.         }
  133.     } else {
  134.         window.open(url,'bookmarks');
  135.     }
  136.  
  137.     dump("OpenURL(" + url + ")\n");
  138.  
  139.     return(true);
  140. }
  141.  
  142. // returns the extension of a specified file URL
  143. function getFileExtension(url)
  144. {
  145.     return url.substring(url.lastIndexOf(".")+1,url.length).toLowerCase();
  146. }
  147.  
  148. var htmlInput = null;
  149. var saveNode = null;
  150. var newValue = "";
  151. var timerID = null;
  152. var gEditNode = null;
  153.  
  154. function DoSingleClick(event, node)
  155. {
  156.   var type = node.parentNode.parentNode.getAttribute('type');
  157.   var selected = node.parentNode.parentNode.getAttribute('selected');
  158.  
  159.   if (gEditNode == node) {
  160.     // Only start an inline edit if it is the second consecutive click
  161.     // on the same node that is not already editing or a separator.
  162.     if (!htmlInput &&
  163.         type != "http://home.netscape.com/NC-rdf#BookmarkSeparator") {
  164.       // Edit node if we don't get a double-click in less than 1/2 second
  165.       timerID = setTimeout("OpenEditNode()", 500);
  166.     }
  167.   } else {
  168.     if (htmlInput) {
  169.       // Clicked during an edit
  170.       // Save the changes and move on
  171.       CloseEditNode(true);
  172.     }
  173.     gEditNode = node;
  174.   }
  175.   return false;
  176. }
  177.  
  178. function OpenEditNode()
  179. {
  180.     dump("OpenEditNode entered.\n");
  181.  
  182.     // clear any single-click/edit timeouts
  183.     if (timerID != null)
  184.     {
  185.         clearTimeout(timerID);
  186.         timerID = null;
  187.     }
  188.  
  189.     // XXX uncomment the following line to replace the whole input row we do this
  190.     // (and, therefore, only allow editing on the name column) until we can
  191.     // arbitrarily change the content model (bugs prevent this at the moment)
  192.     gEditNode = gEditNode.parentNode;
  193.  
  194.     var name = gEditNode.parentNode.getAttribute("Name");
  195.     dump("Single click on '" + name + "'\n");
  196.  
  197.     var theParent = gEditNode.parentNode;
  198.     dump("Parent node is a " + theParent.nodeName + "\n\n");
  199.  
  200.     saveNode = gEditNode;
  201.  
  202.     // unselect all nodes!
  203.     var select_list = document.getElementsByAttribute("selected", "true");
  204.     dump("# of Nodes selected: " + select_list.length + "\n\n");
  205.     for (var nodeIndex=0; nodeIndex<select_list.length; nodeIndex++)
  206.     {
  207.         var node = select_list[nodeIndex];
  208.         if (node)
  209.         {
  210.           dump("Unselecting node "+node.getAttribute("Name") + "\n");
  211.           node.removeAttribute("selected");
  212.         }
  213.     }
  214.  
  215.     // XXX for now, just remove the child from the parent
  216.     // optimally, we'd like to not remove the child-parent relationship
  217.     // and instead just set a "display: none;" attribute on the child node
  218.  
  219. //    gEditNode.setAttribute("style", "display: none;");
  220. //    dump("gEditNode hidden.\n");
  221.     theParent.removeChild(gEditNode);
  222.     gEditNode = null;
  223.     dump("gEditNode removed.\n");
  224.  
  225.     // create the html:input node
  226.     htmlInput = document.createElementNS("http://www.w3.org/1999/xhtml", "html:input");
  227.     htmlInput.setAttribute("value", name);
  228.     htmlInput.setAttribute("onkeypress", "return EditNodeKeyPress(event)");
  229.  
  230.     theParent.appendChild(htmlInput);
  231.     dump("html:input node added.\n");
  232.  
  233.     htmlInput.focus();
  234.  
  235.     dump("OpenEditNode done.\n");
  236.     return(true);
  237. }
  238.  
  239. function CloseEditNode(saveChangeFlag)
  240. {
  241.     dump("CloseEditNode entered.\n");
  242.  
  243.     if (htmlInput)
  244.     {
  245.         if (saveChangeFlag)
  246.         {
  247.             newValue = htmlInput.value;
  248.         }
  249.         dump("  Got html input: "+newValue+" \n");
  250.  
  251.         var theParent = htmlInput.parentNode;
  252.         theParent.removeChild(htmlInput);
  253.         theParent.appendChild(saveNode);
  254.         dump("  child node appended.\n");
  255.  
  256.         if (saveNode && saveChangeFlag)
  257.         {
  258.             var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  259.             RDF = RDF.QueryInterface(Components.interfaces.nsIRDFService);
  260.             var Bookmarks = RDF.GetDataSource("rdf:bookmarks");
  261.             dump("Got bookmarks datasource.\n");
  262.  
  263.             // XXX once we support multi-column editing, get the property
  264.             // from the column in the content model
  265.             var propertyName = "http://home.netscape.com/NC-rdf#Name";
  266.             var propertyNode = RDF.GetResource(propertyName, true);
  267.             dump("  replacing value of property '" + propertyName + "'\n");
  268.  
  269.             // get the URI
  270.             var theNode = saveNode;
  271.             var bookmarkURL = "";
  272.             while(true)
  273.             {
  274.                 var tag = theNode.nodeName;
  275.                 if (tag == "treeitem")
  276.                 {
  277.                     bookmarkURL = theNode.getAttribute("id");
  278.                     break;
  279.                 }
  280.                 theNode = theNode.parentNode;
  281.             }
  282.             dump("  uri is '" + bookmarkURL + "'\n");
  283.  
  284.             if (bookmarkURL == "")    return(false);
  285.             var bookmarkNode = RDF.GetResource(bookmarkURL, true);
  286.  
  287.  
  288.             dump("  newValue = '" + newValue + "'\n");
  289.             newValue = (newValue != "") ? RDF.GetLiteral(newValue) : null;
  290.  
  291.             var oldValue = Bookmarks.GetTarget(bookmarkNode, propertyNode, true);
  292.             if (oldValue)
  293.             {
  294.                 oldValue = oldValue.QueryInterface(Components.interfaces.nsIRDFLiteral);
  295.                 dump("  oldValue = '" + oldValue + "'\n");
  296.             }
  297.  
  298.             if (oldValue != newValue)
  299.             {
  300.                 if (oldValue && !newValue)
  301.                 {
  302.                     Bookmarks.Unassert(bookmarkNode, propertyNode, oldValue);
  303.                     dump("  Unassert used.\n");
  304.                 }
  305.                 else if (!oldValue && newValue)
  306.                 {
  307.                     Bookmarks.Assert(bookmarkNode, propertyNode, newValue, true);
  308.                     dump("  Assert used.\n");
  309.                 }
  310.                 else if (oldValue && newValue)
  311.                 {
  312.                     Bookmarks.Change(bookmarkNode, propertyNode, oldValue, newValue);
  313.                     dump("  Change used.\n");
  314.                 }
  315.  
  316.                 dump("re-writing bookmarks.html\n");
  317.                 var remote = Bookmarks.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  318.                 remote.Flush();
  319.             }
  320.  
  321.             newValue = "";
  322.             saveNode = null;
  323.         }
  324.         else dump("saveNode was null?\n");
  325.         htmlInput = null;
  326.     }
  327.     else dump("htmlInput was null?\n");
  328.  
  329.     dump("CloseEditNode done.\n");
  330. }
  331.  
  332. function EditNodeKeyPress(event)
  333. {
  334.     if (event.keyCode == 27)
  335.     {
  336.         CloseEditNode(false);
  337.         return(false);
  338.     }
  339.     else if (event.keyCode == 13 || event.keyCode == 10)
  340.     {
  341.         CloseEditNode(true);
  342.         return(false);
  343.     }
  344.     return(true);
  345. }
  346.  
  347. function doSort(sortColName)
  348. {
  349.   var node = document.getElementById(sortColName);
  350.   // determine column resource to sort on
  351.   var sortResource = node.getAttribute('resource');
  352.   if (!node) return(false);
  353.  
  354.   var sortDirection="ascending";
  355.   var isSortActive = node.getAttribute('sortActive');
  356.   if (isSortActive == "true") {
  357.     var currentDirection = node.getAttribute('sortDirection');
  358.     if (currentDirection == "ascending")
  359.       sortDirection = "descending";
  360.     else if (currentDirection == "descending")
  361.       sortDirection = "natural";
  362.     else
  363.       sortDirection = "ascending";
  364.   }
  365.  
  366.   // get RDF Core service
  367.   var rdfCore = XPAppCoresManager.Find("RDFCore");
  368.   if (!rdfCore) {
  369.     rdfCore = new RDFCore();
  370.     if (!rdfCore) {
  371.       return(false);
  372.     }
  373.     rdfCore.Init("RDFCore");
  374. //    XPAppCoresManager.Add(rdfCore);
  375.   }
  376.   // sort!!!
  377.   rdfCore.doSort(node, sortResource, sortDirection);
  378.   return(false);
  379. }
  380.  
  381.  
  382. function fillContextMenu(name,node)
  383. {
  384.     if (!name)    return(false);
  385.     var popupNode = document.getElementById(name);
  386.     if (!popupNode)    return(false);
  387.  
  388.     var url = GetFileURL(node);
  389.     var ext = getFileExtension(url);
  390.  
  391.     // remove the menu node (which tosses all of its kids);
  392.     // do this in case any old command nodes are hanging around
  393.     var menuNode = popupNode.childNodes[0];
  394.     popupNode.removeChild(menuNode);
  395.  
  396.     // create a new menu node
  397.     menuNode = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menu");
  398.     popupNode.appendChild(menuNode);
  399.  
  400.     dump("mwa");
  401.     if(ext == "gif")
  402.     {
  403.         // note: deleted all the doContextCmd stuff from bookmarks.
  404.         menuItem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem");
  405.         menuItem.setAttribute("label","Insert Image");
  406.         // menuItem.setAttribute("onaction","AutoInsertImage(\'" + url + "\')");
  407.         parent.appendChild(menuItem);
  408.     }
  409.  
  410.     return(true);
  411. }
  412.  
  413. function isImageFile(parent,url)
  414. {
  415. }
  416.  
  417. function GetFileURL(node)
  418. {
  419.     var url = node.getAttribute('id');
  420.  
  421.     // Ignore "NC:" urls.
  422.     if (url.substring(0, 3) == "NC:")
  423.     {
  424.         return(false);
  425.     }
  426.  
  427.   try
  428.   {
  429.     // add support for IE favorites under Win32, and NetPositive URLs under BeOS
  430.     if (url.indexOf("file://") == 0)
  431.     {
  432.       var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  433.       if (rdf)   rdf = rdf.QueryInterface(Components.interfaces.nsIRDFService);
  434.       if (rdf)
  435.       {
  436.         var fileSys = rdf.GetDataSource("rdf:files");
  437.         if (fileSys)
  438.         {
  439.           var src = rdf.GetResource(url, true);
  440.           var prop = rdf.GetResource("http://home.netscape.com/NC-rdf#URL", true);
  441.           var target = fileSys.GetTarget(src, prop, true);
  442.           if (target) target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
  443.           if (target) target = target.Value;
  444.           if (target) url = target;
  445.  
  446.         }
  447.       }
  448.     }
  449.   }
  450.   catch(ex)
  451.   {
  452.   }
  453.     return url;
  454. }
  455.  
  456.  
  457.  
  458. function doContextCmd(cmdName)
  459. {
  460.   dump("doContextCmd start: cmd='" + cmdName + "'\n");
  461.  
  462.   var treeNode = document.getElementById("bookmarksTree");
  463.   if (!treeNode)    return(false);
  464.   var db = treeNode.database;
  465.   if (!db)    return(false);
  466.  
  467.   var compositeDB = db.QueryInterface(Components.interfaces.nsIRDFDataSource);
  468.   if (!compositeDB)    return(false);
  469.  
  470.   var isupports = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  471.   if (!isupports)    return(false);
  472.   var rdf = isupports.QueryInterface(Components.interfaces.nsIRDFService);
  473.   if (!rdf)    return(false);
  474.  
  475.   // need a resource for the command
  476.   var cmdResource = rdf.GetResource(cmdName);
  477.   if (!cmdResource)        return(false);
  478.   cmdResource = cmdResource.QueryInterface(Components.interfaces.nsIRDFResource);
  479.   if (!cmdResource)        return(false);
  480.  
  481.   var select_list = treeNode.getElementsByAttribute("selected", "true");
  482.   if (select_list.length < 1)    return(false);
  483.  
  484.   dump("# of Nodes selected: " + select_list.length + "\n\n");
  485.  
  486.   // set up selection nsISupportsArray
  487.   var selectionInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  488.   var selectionArray = selectionInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  489.  
  490.   // set up arguments nsISupportsArray
  491.   var argumentsInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  492.   var argumentsArray = argumentsInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  493.  
  494.   // get argument (parent)
  495.   var parentArc = rdf.GetResource("http://home.netscape.com/NC-rdf#parent");
  496.   if (!parentArc)        return(false);
  497.  
  498.   for (var nodeIndex=0; nodeIndex<select_list.length; nodeIndex++)
  499.   {
  500.     var node = select_list[nodeIndex];
  501.     if (!node)    break;
  502.     var uri = node.getAttribute("ref");
  503.     if ((uri) || (uri == ""))
  504.     {
  505.       uri = node.getAttribute("id");
  506.     }
  507.     if (!uri)    return(false);
  508.  
  509.     var rdfNode = rdf.GetResource(uri);
  510.     if (!rdfNode)    break;
  511.  
  512.     // add node into selection array
  513.     selectionArray.AppendElement(rdfNode);
  514.  
  515.     // get the parent's URI
  516.     var parentURI="";
  517.     var theParent = node;
  518.     while (theParent)
  519.     {
  520.       theParent = theParent.parentNode;
  521.  
  522.       parentURI = theParent.getAttribute("ref");
  523.       if ((!parentURI) || (parentURI == ""))
  524.       {
  525.         parentURI = theParent.getAttribute("id");
  526.       }
  527.       if (parentURI != "")  break;
  528.     }
  529.     if (parentURI == "")    return(false);
  530.  
  531.     var parentNode = rdf.GetResource(parentURI, true);
  532.     if (!parentNode)  return(false);
  533.  
  534.     // add parent arc and node into arguments array
  535.     argumentsArray.AppendElement(parentArc);
  536.     argumentsArray.AppendElement(parentNode);
  537.   }
  538.  
  539.   // do the command
  540.   compositeDB.DoCommand( selectionArray, cmdResource, argumentsArray );
  541.  
  542.   dump("doContextCmd ends.\n\n");
  543.   return(true);
  544. }
  545.