home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 August / PCpro_2005_08.ISO / files / firefox / ScrapBook.xpi / chrome / scrapbook.jar / content / scrapnote.js < prev    next >
Encoding:
JavaScript  |  2005-06-04  |  4.8 KB  |  165 lines

  1. /**************************************************
  2. // scrapnote.js
  3. // Implementation file for scrapbook.xul, note.xul
  4. // 
  5. // Description: 
  6. // Author: Gomita
  7. // Contributors: 
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14. var SBnote = {
  15.  
  16.     editXUL : null,
  17.     curRes  : null,
  18.     curFile : null,
  19.     toSave  : false,
  20.     sidebar : true,
  21.     lock    : false,
  22.  
  23.     dropListener : function() { SBnote.change(true); },
  24.  
  25.     init : function()
  26.     {
  27.         this.editXUL = document.getElementById("ScrapNoteTextbox");
  28.         this.editXUL.removeEventListener("dragdrop", this.dropListener, true);
  29.         this.editXUL.addEventListener("dragdrop",    this.dropListener, true);
  30.     },
  31.  
  32.     add : function(tarResName, tarRelIdx)
  33.     {
  34.         if ( this.lock ) return;
  35.         this.lock = true;
  36.         setTimeout(function(){ SBnote.lock = false; }, 1000);
  37.         this.save();
  38.         var newID = SBRDF.identify(SBcommon.getTimeStamp());
  39.         var newSBitem = new ScrapBookItem(newID);
  40.         newSBitem.type  = "note";
  41.         newSBitem.chars = "UTF-8";
  42.         this.curRes = SBRDF.addItem(newSBitem, tarResName, tarRelIdx);
  43.         this.curFile = SBcommon.getContentDir(SBRDF.getProperty("id", this.curRes)).clone();
  44.         this.curFile.append("index.html");
  45.         SBcommon.writeFile(this.curFile, "", "UTF-8");
  46.         SBpref.usetabNote ? this.open(this.curRes, true) : this.edit(this.curRes);
  47.     },
  48.  
  49.     edit : function(aRes)
  50.     {
  51.         this.save();
  52.         this.curRes = aRes;
  53.         this.toSave = false;
  54.         if ( this.sidebar )
  55.         {
  56.             document.getElementById("ScrapBookSplitter").hidden = false;
  57.             document.getElementById("ScrapNote").hidden = false;
  58.         }
  59.         this.curFile = SBcommon.getContentDir(SBRDF.getProperty("id", this.curRes)).clone();
  60.         this.curFile.append("index.html");
  61.         var myContent = SBcommon.readFile(this.curFile);
  62.         myContent = SBcommon.convertStringToUTF8(myContent);
  63.         this.editXUL.value = myContent;
  64.         this.editXUL.focus();
  65.         document.getElementById("ScrapNoteLabel").value = SBRDF.getProperty("title", this.curRes);
  66.         if ( !this.sidebar )
  67.         {
  68.             var myIcon = SBcommon.getDefaultIcon("note");
  69.             document.getElementById("ScrapNoteImage").setAttribute("src", myIcon);
  70.             if ( !document.getElementById("ScrapNoteBrowser").hidden ) SNpreview.show();
  71.             var myBrowser = SBservice.WM.getMostRecentWindow("navigator:browser").getBrowser();
  72.             if ( myBrowser.selectedBrowser.contentWindow.gID == gID )
  73.             {
  74.                 myBrowser.selectedTab.label = SBRDF.getProperty("title", this.curRes);
  75.                 myBrowser.selectedTab.setAttribute("image", myIcon);
  76.             }
  77.         }
  78.     },
  79.  
  80.     save : function()
  81.     {
  82.         if ( !this.toSave ) return;
  83.         if ( !SBRDF.getProperty("id", this.curRes) ) return;
  84.         SBcommon.writeFile(this.curFile, this.editXUL.value, "UTF-8");
  85.         this.updateResource();
  86.         if ( this.sidebar ) SBstatus.trace("Saving... " + SBRDF.getProperty("title", this.curRes), 1000);
  87.         this.change(false);
  88.     },
  89.  
  90.     updateResource : function()
  91.     {
  92.         var myTitle = this.editXUL.value.split("\n")[0].replace(/\t/g, " ");
  93.         if ( myTitle.length > 50 ) myTitle = myTitle.substring(0,50) + "...";
  94.         SBRDF.updateItem(this.curRes, "title", myTitle);
  95.         SBRDF.flush();
  96.     },
  97.  
  98.     exit : function()
  99.     {
  100.         this.save();
  101.         this.curRes  = null;
  102.         this.curFile = null;
  103.         this.change(false);
  104.         if ( this.sidebar ) {
  105.             document.getElementById("ScrapBookSplitter").hidden = true;
  106.             document.getElementById("ScrapNote").hidden = true;
  107.         }
  108.     },
  109.  
  110.     open : function(aRes, tabbed)
  111.     {
  112.         if ( top.document.getElementById("content").contentWindow.SBnote )
  113.         {
  114.             top.document.getElementById("content").contentWindow.SBnote.edit(aRes);
  115.         }
  116.         else
  117.         {
  118.             if ( tabbed ) {
  119.                 if ( top.document.getElementById("content").contentWindow.location.href == "about:blank" ) tabbed = false;
  120.                 SBcommon.loadURL("chrome://scrapbook/content/note.xul?id=" + SBRDF.getProperty("id", aRes), tabbed);
  121.             } else {
  122.                 SBnote.edit(aRes);
  123.             }
  124.         }
  125.     },
  126.  
  127.     expand : function()
  128.     {
  129.         this.open(this.curRes, true);
  130.         this.exit();
  131.     },
  132.  
  133.     change : function(aBool)
  134.     {
  135.         this.toSave = aBool;
  136.         if ( !this.sidebar ) document.getElementById("ScrapNoteToolbarS").disabled = !aBool;
  137.     },
  138.  
  139. };
  140.  
  141.  
  142. function SN_insertTab(aEvent)
  143. {
  144.     if ( ( aEvent.keyCode != aEvent.DOM_VK_TAB) || aEvent.ctrlKey || aEvent.altKey || aEvent.shiftKey ) return;
  145.     aEvent.preventDefault();
  146.  
  147.     var command = "cmd_insertText";
  148.     try {
  149.         var controller = document.commandDispatcher.getControllerForCommand(command);
  150.         if (controller && controller.isCommandEnabled(command))
  151.         {
  152.             controller = controller.QueryInterface(Components.interfaces.nsICommandController);
  153.             var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
  154.             params = params.createInstance(Components.interfaces.nsICommandParams);
  155.             params.setStringValue("state_data", "\t");
  156.             controller.doCommandWithParams(command, params);
  157.         }
  158.     }
  159.     catch(ex) {
  160.         dump("*** ScrapBook Exception: Failed to execute cmd_insertText.\n");
  161.     }
  162. }
  163.  
  164.  
  165.