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

  1. /**************************************************
  2. // scrapbook.js
  3. // Implementation file for scrapbook.xul
  4. // 
  5. // Description: 
  6. // Author: Gomita
  7. // Contributors: 
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14.  
  15. var SBstring;
  16. var SBheader;
  17. var gEditingMode;
  18.  
  19. var SBdropObserver = {};
  20. var SBdragObserver = {};
  21. var SBbuilderObserver = {};
  22.  
  23.  
  24.  
  25. function SB_init()
  26. {
  27.     SBstring = document.getElementById("ScrapBookString");
  28.     SBheader = document.getElementById("ScrapBookHeader");
  29.  
  30.     SBRDF.init();
  31.     SBstatus.init();
  32.     SBsearch.init();
  33.     SBnote.init();
  34.     SBtreeUtil.init("ScrapBookTree", false);
  35.     SB_initObservers();
  36.  
  37.     SBpref.init();
  38.     gEditingMode = document.getElementById("ScrapBookEditingMode").getAttribute("checked");
  39.  
  40.     setTimeout(function(){ SBstatus.getHttpTask(); }, 0);
  41. }
  42.  
  43.  
  44. function SB_initObservers()
  45. {
  46.     SBdragObserver = 
  47.     {
  48.         onDragStart : function(event, transferData, action)
  49.         {
  50.             var curRes = SBtree.builderView.getResourceAtIndex(SBtree.currentIndex);
  51.             transferData.data = new TransferData();
  52.             transferData.data.addDataForFlavour("moz/rdfitem", curRes.Value);
  53.             transferData.data.addDataForFlavour("text/x-moz-url", "chrome://scrapbook/content/view.xul?id=" + SBRDF.getProperty("id", curRes));
  54.         },
  55.         onDrop     : function(event, transferData, session) {},
  56.         onDragExit : function(event, session) {}
  57.     };
  58.  
  59.     SBdropObserver = 
  60.     {
  61.         getSupportedFlavours : function()
  62.         {
  63.             var flavours = new FlavourSet();
  64.             flavours.appendFlavour("moz/rdfitem");
  65.             flavours.appendFlavour("text/x-moz-url");
  66.             flavours.appendFlavour("text/html");
  67.             return flavours;
  68.         },
  69.         onDrop     : function(event, transferData, session) {},
  70.         onDragOver : function(event, flavour, session) {},
  71.         onDragExit : function(event, session) {}
  72.     };
  73.  
  74.     SBbuilderObserver = 
  75.     {
  76.         canDropOn : function(index)
  77.         {
  78.             return true;
  79.         },
  80.         canDropBeforeAfter : function(index, before)
  81.         {
  82.             return true;
  83.         },
  84.         canDrop : function(index, orient)
  85.         {
  86.             SBstatus.change("canDrop: " + index + "/" + orient);
  87.             SBdropUtil.DROP_BEFORE = -1;
  88.             SBdropUtil.DROP_ON     = 0;
  89.             SBdropUtil.DROP_AFTER  = 1;
  90.             if ( index != -1 && !SBtree.view.isContainer(index) && orient == 0 ) return false;
  91.             return true;
  92.         },
  93.         onDrop : function(row, orient)
  94.         {
  95.             try {
  96.                 var XferDataSet  = nsTransferable.get(SBdropObserver.getSupportedFlavours(), nsDragAndDrop.getDragData, true);
  97.                 var XferData     = XferDataSet.first.first;
  98.                 var XferDataType = XferData.flavour.contentType;
  99.             } catch(ex) {
  100.                 dump("*** ScrapBook Exception: Failed to get contentType of XferData.\n");
  101.             }
  102.             ( XferDataType == "moz/rdfitem" ) ? SBdropUtil.move(row, orient) : SBdropUtil.capture(XferData.data, row, orient);
  103.             SB_rebuildAllTree();
  104.         },
  105.         onToggleOpenState        : function(row)                    {},
  106.         onCycleHeader            : function(colID, header)        {},
  107.         onSelectionChanged        : function()                    {},
  108.         onCycleCell                : function(index, colID)        {},
  109.         isEditable                : function(index, colID)        {},
  110.         onSetCellText            : function(index, colID, val)    {},
  111.         onPerformAction            : function(action)                {},
  112.         onPerformActionOnRow    : function(action, index)        {},
  113.         onPerformActionOnCell    : function(action, index, colID){},
  114.     };
  115.     SBtree.builderView.addObserver(SBbuilderObserver);
  116. }
  117.  
  118.  
  119. function SB_finalize()
  120. {
  121.     SBnote.save();
  122. }
  123.  
  124.  
  125.  
  126. function SB_onKeyPressItem(aEvent)
  127. {
  128.     switch ( aEvent.keyCode )
  129.     {
  130.         case 13  : SB_open(false, false);    break;
  131.         case 46  : SB_delete();                break;
  132.         case 113 : SB_transmit("P");        break;
  133.     }
  134. }
  135.  
  136.  
  137. function SB_onClickItem(aEvent)
  138. {
  139.     if ( aEvent != null && ( aEvent.button == 0 || aEvent.button == 1 ) )
  140.     {
  141.         var obj = {};
  142.         SBtree.treeBoxObject.getCellAt(aEvent.clientX, aEvent.clientY, {}, {}, obj);
  143.         if ( !obj.value || obj.value == "twisty" ) return;
  144.     }
  145.  
  146.     switch ( aEvent.button )
  147.     {
  148.         case 0 : SB_open(aEvent.ctrlKey || aEvent.shiftKey, true); break;
  149.         case 1 : SB_open(true,  true); break;
  150.         default : break;
  151.     }
  152. }
  153.  
  154.  
  155. function SB_createPopupMenu()
  156. {
  157.     var curIdx = SBtree.currentIndex;
  158.     if ( curIdx == -1 ) return;
  159.     var isFolder = SBtree.builderView.isContainer(curIdx);
  160.     var isNote   = ( SBRDF.getProperty("type", SBtree.builderView.getResourceAtIndex(curIdx)) == "note" );
  161.     document.getElementById("ScrapBookTreePopupO").setAttribute("hidden",  isFolder);
  162.     document.getElementById("ScrapBookTreePopupT").setAttribute("hidden",  isFolder || isNote);
  163.     document.getElementById("ScrapBookTreePopupS").setAttribute("hidden",  isFolder || isNote);
  164.     document.getElementById("ScrapBookTreePopupE").setAttribute("hidden", !isNote);
  165.     document.getElementById("ScrapBookTreePopupL").setAttribute("hidden",  isFolder);
  166.     document.getElementById("ScrapBookTreePopupF").setAttribute("hidden", !isFolder);
  167.     document.getElementById("ScrapBookTreePopupF").setAttribute("default", isFolder);
  168.     document.getElementById("ScrapBookTreePopupA").setAttribute("hidden", !isFolder);
  169.     document.getElementById("ScrapBookTreePopupV").setAttribute("hidden", !isFolder);
  170.     document.getElementById("ScrapBookTreePopupZ").setAttribute("hidden", !isFolder);
  171.     document.getElementById("ScrapBookTreePopupM").setAttribute("hidden", !isFolder);
  172.     document.getElementById("ScrapBookTreePopup1").setAttribute("hidden", !isFolder);
  173.     return isFolder;
  174. }
  175.  
  176.  
  177.  
  178. function SB_open(tabbed, folderOpen)
  179. {
  180.     var curIdx = SBtree.currentIndex;
  181.     var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  182.     var myID = SBRDF.getProperty("id", curRes);
  183.     if ( !myID ) return;
  184.     if ( SBtree.view.isContainer(curIdx) )
  185.     {
  186.         if ( folderOpen == true ) SBtree.view.toggleOpenState(curIdx);
  187.         if ( SBpref.singleExpand ) SBtreeUtil.collapseOtherFolders(curIdx);
  188.         return;
  189.     }
  190.     if ( SBRDF.getProperty("type", curRes) == "note" )
  191.     {
  192.         SBnote.open(curRes, tabbed || SBpref.usetabNote );
  193.         return;
  194.     }
  195.     if ( SBpref.usetabOpen ) tabbed = true;
  196.     var myDir = SBcommon.getContentDir(myID);
  197.     var myDirPath = SBservice.IO.newFileURI(myDir).spec;
  198.     if ( gEditingMode ) {
  199.         if ( !tabbed ) {
  200.             try {
  201.                 top.document.getElementById("content").contentDocument.getElementById("ScrapBookBrowser").setAttribute("src", myDirPath + "index.html");
  202.                 return;
  203.             } catch(ex) {
  204.             }
  205.         }
  206.         SBcommon.loadURL("chrome://scrapbook/content/edit.xul?id=" + myID, tabbed);
  207.     } else {
  208.         SBcommon.loadURL(myDirPath + "index.html", tabbed);
  209.     }
  210. }
  211.  
  212.  
  213. function SB_openAllInTabs()
  214. {
  215.     var curIdx = SBtree.currentIndex;
  216.     var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  217.     SBservice.RDFC.Init(SBRDF.data, curRes);
  218.     var ResList = SBservice.RDFC.GetElements();
  219.     while ( ResList.hasMoreElements() )
  220.     {
  221.         var aRes = ResList.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  222.         if ( SBservice.RDFCU.IsContainer(SBRDF.data, aRes) ) continue;
  223.         var myID   = SBRDF.getProperty("id", aRes);
  224.         var myType = SBRDF.getProperty("type", aRes);
  225.         SBcommon.loadURL(SBcommon.getURL(myID, myType), true);
  226.     }
  227. }
  228.  
  229.  
  230. function SB_delete()
  231. {
  232.     if ( SBtree.view.selection.count > 1 ) { SB_deleteMultiple(); return; }
  233.     var curIdx = SBtree.currentIndex;
  234.     if ( curIdx == -1 ) return;
  235.     var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  236.     var parRes = SB_getParentResourceAtIndex(curIdx);
  237.     if ( !SBpref.quickDelete || SBtree.view.isContainer(curIdx) )
  238.     {
  239.         if ( !window.confirm( SBstring.getString("CONFIRM_DELETE") ) ) return;
  240.     }
  241.     if ( parRes.Value == "urn:scrapbook:search" )
  242.     {
  243.         SBRDF.removeElementFromContainer("urn:scrapbook:search", curRes);
  244.         parRes = SBRDF.findContainerRes(curRes);
  245.         if ( SBservice.RDFCU.indexOf(SBRDF.data, parRes, curRes) == -1 ) { alert("ScrapBook FATAL ERROR."); return; }
  246.     }
  247.     var rmIDs = SBRDF.deleteItemDescending(curRes, parRes);
  248.     SBRDF.flush();
  249.     for ( var i = 0; i < rmIDs.length; i++ )
  250.     {
  251.         if ( rmIDs[i].length == 14 ) SBcommon.removeDirSafety( SBcommon.getContentDir(rmIDs[i]) );
  252.     }
  253.     SBstatus.trace("Removed: " + rmIDs.length + " items");
  254.     if ( SBnote.curRes && rmIDs[0] == SBnote.curRes.Value.substring(18,32) ) SBnote.exit(false);
  255. }
  256.  
  257.  
  258. function SB_transmit(flag)
  259. {
  260.     var curRes = SBtree.builderView.getResourceAtIndex(SBtree.currentIndex);
  261.     var myID = SBRDF.getProperty("id", curRes);
  262.     if ( !myID ) return;
  263.     switch ( flag )
  264.     {
  265.         case "P"  : window.openDialog("chrome://scrapbook/content/property.xul", "", "chrome,centerscreen,modal" , myID); break;
  266.         case "M"  : window.openDialog("chrome://scrapbook/content/manage.xul", "", "chrome,centerscreen,all,resizable,dialog=no", curRes.Value); break;
  267.         case "C"  : SBcommon.loadURL("chrome://scrapbook/content/view.xul?id=" + SBRDF.getProperty("id", curRes), SBpref.usetabCombine); break;
  268.         case "S"  : SBcommon.loadURL(SBRDF.getProperty("source", curRes), SBpref.usetabSource); break;
  269.         case "L"  : SBcommon.launchDirectory(SBcommon.getContentDir(myID)); break;
  270.     }
  271. }
  272.  
  273.  
  274.  
  275.  
  276. var SBdropUtil = {
  277.  
  278.     DROP_BEFORE : 0,
  279.     DROP_ON     : 1,
  280.     DROP_AFTER  : 2,
  281.     row    : 0,
  282.     orient : 0,
  283.  
  284.     init : function(aRow, aOrient)
  285.     {
  286.         this.row = aRow;
  287.         this.orient = aOrient;
  288.     },
  289.  
  290.     move : function(aRow, aOrient)
  291.     {
  292.         this.init(aRow, aOrient);
  293.         ( SBtree.view.selection.count == 1 ) ? this.moveSingle() : this.moveMultiple();
  294.     },
  295.  
  296.     moveSingle : function()
  297.     {
  298.         var curIdx = SBtree.currentIndex;
  299.         var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  300.         var curPar = SB_getParentResourceAtIndex(curIdx);
  301.         var tarRes = SBtree.builderView.getResourceAtIndex(this.row);
  302.         var tarPar = ( this.orient == this.DROP_ON ) ? tarRes : SB_getParentResourceAtIndex(this.row);
  303.         this.moveCurrentToTarget(curRes, curPar, tarRes, tarPar);
  304.         SBRDF.flush();
  305.     },
  306.  
  307.     moveMultiple : function()
  308.     {
  309.         return;
  310.     },
  311.  
  312.     moveCurrentToTarget : function(curRes, curPar, tarRes, tarPar)
  313.     {
  314.         var curAbsIdx = SBtree.builderView.getIndexOfResource(curRes);
  315.         var curRelIdx = SBRDF.getRelativeIndex(curPar, curRes);
  316.         var tarRelIdx = SBRDF.getRelativeIndex(tarPar, tarRes);
  317.  
  318.         if ( this.orient == this.DROP_ON )
  319.         {
  320.             if ( curAbsIdx == this.row ) { SBstatus.trace("can't drop folder on itself"); return; }
  321.         }
  322.         else
  323.         {
  324.             if ( this.orient == this.DROP_AFTER ) tarRelIdx++;
  325.             if ( curPar.Value == tarPar.Value && tarRelIdx > curRelIdx ) tarRelIdx--;
  326.             if ( this.orient == this.DROP_AFTER &&
  327.                  SBtree.view.isContainer(this.row) &&
  328.                  SBtree.view.isContainerOpen(this.row) &&
  329.                  SBtree.view.isContainerEmpty(this.row) == false )
  330.             {
  331.                 if ( curAbsIdx == this.row )
  332.                 {
  333.                     SBstatus.trace("can't drop folder after open container");
  334.                     return;
  335.                 }
  336.                 SBstatus.trace("drop after open container");
  337.                 tarPar = tarRes;
  338.                 tarRes = SBtree.builderView.getResourceAtIndex(this.row + 1);
  339.                 tarRelIdx = 1;
  340.             }
  341.             if ( curPar.Value == tarPar.Value && curRelIdx == tarRelIdx ) return;
  342.         }
  343.         if ( SBtree.view.isContainer(curAbsIdx) )
  344.         {
  345.             var tmpIdx = this.row;
  346.             var tmpRes = tarRes;
  347.             while ( tmpRes.Value != SBtree.ref && tmpIdx != -1 )
  348.             {
  349.                 tmpRes = SB_getParentResourceAtIndex(tmpIdx);
  350.                 tmpIdx = SBtree.builderView.getIndexOfResource(tmpRes);
  351.                 if ( tmpRes.Value == curRes.Value ) { SBstatus.trace("can't move folder into descendant level"); return; }
  352.             }
  353.         }
  354.         SBRDF.moveItem(curRes, curPar, tarPar, tarRelIdx);
  355.     },
  356.  
  357.     capture : function(XferString, aRow, aOrient)
  358.     {
  359.         this.init(aRow, aOrient);
  360.         XferString = XferString.split("\n")[0];
  361.  
  362.         var myWindow = SBcommon.getFocusedWindow();
  363.         try {
  364.             var mySelection = myWindow.getSelection();
  365.         } catch(ex) {
  366.             alert(ex);
  367.             return;
  368.         }
  369.         var isSelected = false;
  370.         try {
  371.             isSelected = ( mySelection.anchorNode.isSameNode(mySelection.focusNode) && mySelection.anchorOffset == mySelection.focusOffset ) ? false : true;
  372.         } catch(ex) {
  373.         }
  374.         var isEntire = (XferString == top.window._content.document.location.href);
  375.  
  376.         var ResArray = ( this.row == -1 ) ? [SBtree.ref, 0] : this.getTarget();
  377.  
  378.         if ( isSelected || isEntire )
  379.         {
  380.             var targetWindow = isEntire ? top.window._content : myWindow;
  381.             top.window.SBcapture.doCaptureDocument(targetWindow, !isEntire, SBpref.captureDetail, ResArray[0], ResArray[1]);
  382.         }
  383.         else
  384.         {
  385.             if ( XferString.substring(0,7) == "http://" || XferString.substring(0,8) == "https://" )
  386.             {
  387.                 top.window.openDialog(
  388.                     "chrome://scrapbook/content/capture.xul", "", "chrome,centerscreen,all,resizable,dialog=no",
  389.                     [XferString], myWindow.location.href, SBpref.captureDetail, false, ResArray[0], ResArray[1]
  390.                 );
  391.             }
  392.             else if ( XferString.substring(0,7) == "file://" )
  393.             {
  394.                 top.window.SBcapture.doCaptureFile(XferString, "file", "file://", SBpref.captureDetail, ResArray[0], ResArray[1]);
  395.             }
  396.             else
  397.             {
  398.                 alert("ScrapBook ERROR: Unrecognizable URL.\n" + XferString);
  399.             }
  400.         }
  401.     },
  402.  
  403.     getTarget : function()
  404.     {
  405.         var tarRes = SBtree.builderView.getResourceAtIndex(this.row);
  406.         var tarPar = ( this.orient == this.DROP_ON ) ? tarRes : SB_getParentResourceAtIndex(this.row);
  407.         var tarRelIdx = SBRDF.getRelativeIndex(tarPar, tarRes);
  408.         if ( this.orient == this.DROP_AFTER ) tarRelIdx++;
  409.         if ( this.orient == this.DROP_AFTER &&
  410.              SBtree.view.isContainer(this.row) &&
  411.              SBtree.view.isContainerOpen(this.row) &&
  412.              SBtree.view.isContainerEmpty(this.row) == false )
  413.         {
  414.             SBstatus.trace("drop after open container");
  415.             tarPar = tarRes; tarRelIdx = 1;
  416.         }
  417.         return [tarPar.Value, tarRelIdx];
  418.     },
  419.  
  420. };
  421.  
  422.  
  423. function SB_getParentResourceAtIndex(aIdx)
  424. {
  425.     var parIdx = SBtree.builderView.getParentIndex(aIdx);
  426.     if ( parIdx == -1 ) {
  427.         return SBservice.RDF.GetResource(SBtree.ref);
  428.     } else {
  429.         return SBtree.builderView.getResourceAtIndex(parIdx);
  430.     }
  431. }
  432.  
  433.  
  434.  
  435. function SB_createFolder()
  436. {
  437.     var newID = SBRDF.identify(SBcommon.getTimeStamp());
  438.     var newItem = new ScrapBookItem(newID);
  439.     newItem.title = SBstring.getString("DEFAULT_FOLDER");
  440.     newItem.type = "folder";
  441.  
  442.     var tarResName, tarRelIdx, isRootPos;
  443.     try {
  444.         var curIdx = SBtree.currentIndex;
  445.         var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  446.         var curPar = SB_getParentResourceAtIndex(curIdx);
  447.         var curRelIdx = SBRDF.getRelativeIndex(curPar, curRes);
  448.         tarResName = curPar.Value;
  449.         tarRelIdx  = curRelIdx;
  450.         isRootPos  = false;
  451.     } catch(ex) {
  452.         tarResName = SBtree.ref;
  453.         tarRelIdx  = 1;
  454.         isRootPos  = true;
  455.     }
  456.     var newRes = SBRDF.addItem(newItem, tarResName, tarRelIdx);
  457.     SBtree.builder.rebuild();
  458.     SBRDF.createEmptySeq(newRes.Value);
  459.     SB_rebuildAllTree();
  460.     if ( isRootPos ) SBtree.treeBoxObject.scrollToRow(0);
  461.     SBtree.view.selection.select(SBtree.builderView.getIndexOfResource(newRes));
  462.     SBtree.focus();
  463.  
  464.     var result = {};
  465.     window.openDialog("chrome://scrapbook/content/property.xul", "", "modal,centerscreen,chrome", newItem.id, result);
  466.     if ( !result.accept )
  467.     {
  468.         SBRDF.deleteItemDescending(newRes, SBservice.RDF.GetResource(tarResName));
  469.         SBRDF.flush();
  470.         return false;
  471.     }
  472.     return true;
  473. }
  474.  
  475.  
  476. function SB_createNote()
  477. {
  478.     var tarResName, tarRelIdx, isRootPos;
  479.     try {
  480.         var curIdx = SBtree.currentIndex;
  481.         var curRes = SBtree.builderView.getResourceAtIndex(curIdx);
  482.         var curPar = SB_getParentResourceAtIndex(curIdx);
  483.         var curRelIdx = SBRDF.getRelativeIndex(curPar, curRes);
  484.         tarResName = curPar.Value;
  485.         tarRelIdx  = curRelIdx;
  486.         isRootPos  = false;
  487.     } catch(ex) {
  488.         tarResName = SBtree.ref;
  489.         tarRelIdx  = 0;
  490.         isRootPos  = true;
  491.     }
  492.     SBnote.add(tarResName, tarRelIdx);
  493.     SB_rebuildAllTree();
  494.     SBtree.view.selection.select(SBtree.builderView.getIndexOfResource(SBnote.curRes));
  495.     if ( isRootPos ) SBtree.treeBoxObject.scrollByLines(SBtree.view.rowCount);
  496. }
  497.  
  498.  
  499. function SB_rebuildAllTree()
  500. {
  501.     SBtree.builder.rebuild();
  502.  
  503.     var navList = SBservice.WM.getEnumerator("navigator:browser");
  504.     while ( navList.hasMoreElements() )
  505.     {
  506.         var nav = navList.getNext().QueryInterface(Components.interfaces.nsIDOMWindow);
  507.         try {
  508.             nav.document.getElementById("sidebar").contentDocument.getElementById("ScrapBookTree").builder.rebuild();
  509.         } catch(ex) {
  510.         }
  511.     }
  512. }
  513.  
  514.  
  515.  
  516. var SBstatus = {
  517.  
  518.     label : null,
  519.     image : null,
  520.  
  521.     init : function()
  522.     {
  523.         this.label = document.getElementById("ScrapBookStatusLabel");
  524.         this.image = document.getElementById("ScrapBookStatusImage");
  525.     },
  526.  
  527.     change : function(msg)
  528.     {
  529.         this.label.value = msg;
  530.     },
  531.  
  532.     trace : function(msg, msec)
  533.     {
  534.         this.label.value = msg;
  535.         setTimeout(function(){ if ( !SBstatus.image.hasAttribute("src") ) SBstatus.change(""); }, msec ? msec : 5000);
  536.     },
  537.  
  538.     getHttpTask : function()
  539.     {
  540.         try { var httpTask = top.window.SBcapture.httpTask; } catch(ex) { return; }
  541.         for ( var i in httpTask ) { if ( httpTask[i] > 0 ) return; }
  542.         this.reset();
  543.     },
  544.  
  545.     httpBusy : function(count, title)
  546.     {
  547.         this.label.value = (count ? "(" + count + ") " : "") + title;
  548.         this.image.setAttribute("src", "chrome://scrapbook/skin/status_busy.gif");
  549.     },
  550.  
  551.     httpComplete : function(title)
  552.     {
  553.         this.label.value = "Complete: " + title;
  554.         this.image.removeAttribute("src");
  555.     },
  556.  
  557.     reset : function()
  558.     {
  559.         this.label.value = "";
  560.         this.image.removeAttribute("src");
  561.         top.window.SBcapture.httpTask = {};
  562.     }
  563. };
  564.  
  565.  
  566.  
  567. var SBsearch = {
  568.  
  569.     type      : "",
  570.     query     : "",
  571.     re        : false,
  572.     cs        : false,
  573.     regex     : null,
  574.     count     : 0,
  575.     container : null,
  576.     xulSearch : null,
  577.  
  578.     get FORM_HISTORY()
  579.     {
  580.         try {
  581.             return Components.classes['@mozilla.org/satchel/form-history;1'].getService(Components.interfaces.nsIFormHistory);
  582.         } catch(ex) {
  583.             return null;
  584.         }
  585.     },
  586.  
  587.     init : function()
  588.     {
  589.         this.xulSearch = document.getElementById("ScrapBookSearch");
  590.         this.type = document.getElementById("ScrapBookSearch").getAttribute("searchtype");
  591.         var targetID = "ScrapBookSearch" + this.type.charAt(0).toUpperCase();
  592.         document.getElementById(targetID).setAttribute("checked", "true");
  593.         this.xulSearch.src = "chrome://scrapbook/skin/search_" + this.type + ".png";
  594.     },
  595.  
  596.     change : function(aType)
  597.     {
  598.         this.type = aType;
  599.         this.xulSearch.setAttribute("searchtype", aType);
  600.         this.xulSearch.src = "chrome://scrapbook/skin/search_" + this.type + ".png";
  601.     },
  602.  
  603.     enter : function(myInput)
  604.     {
  605.         if ( myInput.match(/^[a-z]$/i) || !myInput )
  606.         {
  607.             switch ( myInput.toLowerCase() )
  608.             {
  609.                 case "f" : this.change("fulltext"); break;
  610.                 case "t" : this.change("title"); break;
  611.                 case "c" : this.change("comment"); break;
  612.                 case "u" : this.change("source"); break;
  613.                 case "i" : this.change("id"); break;
  614.                 case "a" : this.change("all"); break;
  615.                 default  : this.exit(); break;
  616.             }
  617.             document.getElementById("ScrapBookSearchTextbox").value = "";
  618.         }
  619.         else
  620.         {
  621.             this.query = myInput;
  622.             this.re = document.getElementById("ScrapBookSearchOptionRE").getAttribute("checked");
  623.             this.cs = document.getElementById("ScrapBookSearchOptionCS").getAttribute("checked");
  624.             if ( this.FORM_HISTORY ) this.FORM_HISTORY.addEntry("ScrapBookSearchHistory", this.query);
  625.             if ( this.type == "fulltext" ) {
  626.                 this.execFT();
  627.          
  628.             } else {
  629.                 var regex1 = this.re ? this.query : this.query.replace(/([\*\+\?\.\|\[\]\{\}\^\/\$\\])/g, "\\$1");
  630.                 var regex2 = this.cs ? "m" : "mi";
  631.                 this.regex = new RegExp(regex1, regex2)
  632.                 this.exec(false);
  633.             }
  634.         }
  635.     },
  636.  
  637.     execFT : function()
  638.     {
  639.         var cache = SBcommon.getScrapBookDir().clone();
  640.         cache.append("cache.rdf");
  641.         var shouldBuild = false;
  642.         if ( !cache.exists() || cache.fileSize < 1024 * 32 ) {
  643.             shouldBuild = true;
  644.         } else {
  645.             var modTime = cache.lastModifiedTime;
  646.             if ( modTime && (new Date().getTime() - modTime) > 1000 * 60 * 60 * 24 * 5 ) shouldBuild = true;
  647.         }
  648.         var resURL   = "chrome://scrapbook/content/result.xul";
  649.         var resQuery = "?q=" + this.query + "&re=" + this.re.toString() + "&cs=" + this.cs.toString();
  650.         if ( shouldBuild ) {
  651.             this.buildFT(resURL + resQuery);
  652.         } else {
  653.             var tabbed = (top.window._content.location.href.substring(0,37) == resURL) ? false : SBpref.usetabSearch;
  654.             SBcommon.loadURL(resURL + resQuery, tabbed);
  655.         }
  656.     },
  657.  
  658.     buildFT : function(resURL)
  659.     {
  660.         window.openDialog('chrome://scrapbook/content/cache.xul','','chrome,dialog=no', resURL);
  661.     },
  662.  
  663.     exec : function(forceTitle)
  664.     {
  665.         SBRDF.clearContainer("urn:scrapbook:search");
  666.         var rootCont   = SBRDF.getContainer("urn:scrapbook:root", true);
  667.         this.container = SBRDF.getContainer("urn:scrapbook:search", true);
  668.         this.count = 0;
  669.         this.processRDFRecursively(rootCont);
  670.         var prop = ( this.type == "all" || forceTitle ) ? "title" : this.type;
  671.         document.getElementById("ScrapBookTreeItem").setAttribute("label", "rdf:" + NS_SCRAPBOOK + prop);
  672.         SBtree.setAttribute("ref", "urn:scrapbook:search");
  673.         SBtree.builder.rebuild();
  674.         SBtree.builderView.removeObserver(SBbuilderObserver);
  675.         SBheader.hidden = false;
  676.         SBheader.firstChild.value = SBstring.getFormattedString("SEARCH_RESULTS_FOUND", [this.count]);
  677.     },
  678.  
  679.     processRDFRecursively : function(aContRes)
  680.     {
  681.         var ResSet = aContRes.GetElements();
  682.         while ( ResSet.hasMoreElements() )
  683.         {
  684.             var aRes = ResSet.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  685.             var aValue = "";
  686.             if ( this.type != "all" )
  687.                 aValue = SBRDF.getProperty(this.type, aRes);
  688.             else
  689.                 aValue = [SBRDF.getProperty("title", aRes), SBRDF.getProperty("comment", aRes), SBRDF.getProperty("source", aRes), SBRDF.getProperty("id", aRes)].join("\n");
  690.             if ( SBservice.RDFCU.IsContainer(SBRDF.data, aRes) )
  691.             {
  692.                 this.processRDFRecursively( SBRDF.getContainer(aRes.Value, false) );
  693.             }
  694.             else if ( aValue && aValue.match(this.regex) )
  695.             {
  696.                 this.container.AppendElement(aRes);
  697.                 this.count++;
  698.             }
  699.         }
  700.     },
  701.  
  702.     filterByDays : function(days)
  703.     {
  704.         var ymdList = [];
  705.         var dd = new Date;
  706.         do {
  707.             var y = dd.getFullYear();
  708.             var m = dd.getMonth() + 1; if ( m < 10 ) m = "0" + m;
  709.             var d = dd.getDate();      if ( d < 10 ) d = "0" + d;
  710.             ymdList.push(y.toString() + m.toString() + d.toString());
  711.             dd.setTime( dd.getTime() - 1000 * 60 * 60 * 24 );
  712.         }
  713.         while ( --days > 0 );
  714.         var tmpType = this.type;
  715.         this.change("id");
  716.         this.regex = new RegExp("^(" + ymdList.join("|") + ")", "");
  717.         this.exec(true);
  718.         this.change(tmpType);
  719.     },
  720.  
  721.     exit : function()
  722.     {
  723.         SBheader.hidden = true;
  724.         document.getElementById("ScrapBookTreeItem").setAttribute("label", "rdf:" + NS_SCRAPBOOK + "title");
  725.         document.getElementById("ScrapBookSearchTextbox").value = "";
  726.         SBtree.setAttribute("ref", "urn:scrapbook:root");
  727.         SBtree.builder.rebuild();
  728.         SBtree.builderView.removeObserver(SBbuilderObserver);
  729.         SBtree.builderView.addObserver(SBbuilderObserver);
  730.         SBRDF.clearContainer("urn:scrapbook:search");
  731.     },
  732.  
  733.     clearFormHistory : function()
  734.     {
  735.         if ( this.FORM_HISTORY ) this.FORM_HISTORY.removeEntriesForName("ScrapBookSearchHistory");
  736.     }
  737.  
  738. };
  739.  
  740.  
  741.  
  742.  
  743. var SBsort = {
  744.  
  745.     key : "",
  746.     ascending : false,
  747.     recursive : false,
  748.  
  749.     exec : function(treeSort, aSortKey, isAscending)
  750.     {
  751.         this.key = aSortKey;
  752.         this.ascending = isAscending;
  753.         this.recursive = treeSort ? true : document.getElementById("ScrapBookSortRecursive").getAttribute("checked");
  754.         if ( treeSort ) {
  755.             var rootRes = SBservice.RDF.GetResource("urn:scrapbook:root");
  756.         } else {
  757.             var curIdx = SBtree.currentIndex;
  758.             if ( curIdx == -1 || !SBtree.view.isContainer(curIdx) ) return;
  759.             var rootRes = SBtree.builderView.getResourceAtIndex(curIdx);
  760.             if ( SBtree.view.isContainerOpen(curIdx) ) SBtree.view.toggleOpenState(curIdx);
  761.         }
  762.         this.process(rootRes);
  763.         if ( !treeSort ) SBtree.view.toggleOpenState(curIdx);
  764.         SBRDF.flush();
  765.     },
  766.  
  767.     process : function(aContRes)
  768.     {
  769.         var ResListF = [], ResListI = [], ResListN = [];
  770.         var aRDFCont = SBRDF.getContainer(aContRes.Value, false);
  771.         var ResSet = aRDFCont.GetElements();
  772.         while ( ResSet.hasMoreElements() )
  773.         {
  774.             var aRes = ResSet.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  775.             if ( SBservice.RDFCU.IsContainer(SBRDF.data, aRes) )
  776.             {
  777.                 ResListF.push(aRes);
  778.                 if ( this.recursive ) SBsort.process(aRes);
  779.             }
  780.             else
  781.             {
  782.                 ( SBRDF.getProperty("type", aRes) == "note" ? ResListN : ResListI ).push(aRes);
  783.             }
  784.         }
  785.         ResListF.sort(this.compare); if ( !this.ascending ) ResListF.reverse();
  786.         ResListI.sort(this.compare); if ( !this.ascending ) ResListI.reverse();
  787.         ResListN.sort(this.compare); if ( !this.ascending ) ResListN.reverse();
  788.         ResListF = ResListF.concat(ResListI).concat(ResListN); 
  789.         for ( var i = 0; i < ResListF.length; i++ )
  790.         {
  791.             aRDFCont.RemoveElement(ResListF[i], true);
  792.             aRDFCont.AppendElement(ResListF[i]);
  793.         }
  794.     },
  795.  
  796.     compare : function(resA, resB)
  797.     {
  798.         var a = SBRDF.getProperty(SBsort.key, resA).toUpperCase();
  799.         var b = SBRDF.getProperty(SBsort.key, resB).toUpperCase();
  800.         if ( a > b ) return 1;
  801.         if ( a < b ) return -1;
  802.         return 0;
  803.     },
  804.  
  805. };
  806.  
  807.  
  808.  
  809.  
  810. var SBpref = {
  811.  
  812.     singleExpand  : false,
  813.     captureDetail : false,
  814.     hideFavicon   : false,
  815.     quickDelete   : false,
  816.     usetabOpen    : false,
  817.     usetabSource  : false,
  818.     usetabSearch  : false,
  819.     usetabCombine : false,
  820.     usetabNote    : false,
  821.  
  822.     init : function()
  823.     {
  824.         this.captureDetail = SBcommon.getBoolPref("scrapbook.capture.detail", false);
  825.         this.singleExpand  = SBcommon.getBoolPref("scrapbook.tree.singleexpand", false);
  826.         this.hideFavicon   = SBcommon.getBoolPref("scrapbook.tree.hidefavicon", false);
  827.         this.quickDelete   = SBcommon.getBoolPref("scrapbook.tree.quickdelete", false);
  828.         this.usetabOpen    = SBcommon.getBoolPref("scrapbook.usetab.open", false);
  829.         this.usetabSource  = SBcommon.getBoolPref("scrapbook.usetab.source", false);
  830.         this.usetabSearch  = SBcommon.getBoolPref("scrapbook.usetab.search", false);
  831.         this.usetabCombine = SBcommon.getBoolPref("scrapbook.usetab.combine", false);
  832.         this.usetabNote    = SBcommon.getBoolPref("scrapbook.usetab.note", false);
  833.     },
  834. };
  835.  
  836.  
  837.