home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / fieldMapImport.js < prev    next >
Text File  |  2001-02-14  |  14KB  |  513 lines

  1. var importService = 0;
  2. var    fieldMap = null;
  3. var transferType = null;
  4. var recordNum = 0;
  5. var amAtEnd = false;
  6. var addInterface = null;
  7. var dialogResult = null;
  8. var dragStart = false;
  9. var dragData = null;
  10.  
  11. function OnLoadFieldMapImport()
  12. {
  13.     // top.bundle = srGetStrBundle("chrome://messenger/locale/importMsgs.properties");
  14.     top.importService = Components.classes["@mozilla.org/import/import-service;1"].getService();
  15.     top.importService = top.importService.QueryInterface(Components.interfaces.nsIImportService);
  16.     top.transferType = "moz/fieldmap";
  17.     top.recordNum = 0;
  18.     
  19.     // We need a field map object...
  20.     // assume we have one passed in? or just make one?
  21.     if (window.arguments && window.arguments[0]) {
  22.         top.fieldMap = window.arguments[0].fieldMap;
  23.         top.addInterface = window.arguments[0].addInterface;
  24.         top.dialogResult = window.arguments[0].result;
  25.     }
  26.     if (top.fieldMap == null) {
  27.         top.fieldMap = top.importService.CreateNewFieldMap();    
  28.         top.fieldMap.DefaultFieldMap( top.fieldMap.numMozFields);
  29.     }
  30.     
  31.     doSetOKCancel( FieldImportOKButton, 0);
  32.  
  33.     ListFields();
  34.     OnNextRecord();
  35. }
  36.  
  37. function SetDivText(id, text)
  38. {
  39.     var div = document.getElementById(id);
  40.     
  41.     if ( div )
  42.     {
  43.         if ( div.childNodes.length == 0 )
  44.         {
  45.             var textNode = document.createTextNode(text);
  46.             div.appendChild(textNode);                               
  47.         }
  48.         else if ( div.childNodes.length == 1 )
  49.             div.childNodes[0].nodeValue = text;
  50.     }
  51. }
  52.  
  53.  
  54. function FieldSelectionChanged()
  55. {
  56.     var tree = document.getElementById('fieldList');
  57.     if ( tree && tree.selectedItems && (tree.selectedItems.length == 1) )
  58.     {
  59.     }
  60. }
  61.  
  62.  
  63. function IndexInMap( index)
  64. {
  65.     var count = top.fieldMap.mapSize;
  66.     var    i;
  67.     for (i = 0; i < count; i++) {
  68.         if (top.fieldMap.GetFieldMap( i) == index)
  69.             return( true);
  70.     }
  71.  
  72.     return( false);
  73. }
  74.  
  75. function ListFields() {
  76.     if (top.fieldMap == null)
  77.         return;
  78.         
  79.     body = document.getElementById("fieldBody");
  80.     count = top.fieldMap.mapSize;
  81.     var index;
  82.     var    i;
  83.     for (i = 0; i < count; i++) {
  84.         index = top.fieldMap.GetFieldMap( i);
  85.         AddFieldToList( body, top.fieldMap.GetFieldDescription( index), index, top.fieldMap.GetFieldActive( i));
  86.     }
  87.  
  88.     count = top.fieldMap.numMozFields;
  89.     for (i = 0; i < count; i++) {
  90.         if (!IndexInMap( i))
  91.             AddFieldToList( body, top.fieldMap.GetFieldDescription( i), i, false);
  92.     }
  93. }
  94.  
  95.  
  96. function CreateField( name, index, on, cBoxIndex)
  97. {
  98.     var item = document.createElement('treeitem');
  99.     var row = document.createElement('treerow');
  100.     var cell = document.createElement('treecell');
  101.     cell.setAttribute('value', name);
  102.     item.setAttribute('field-index', index);
  103.     
  104.     var cCell = document.createElement( 'treecell');
  105.     var cBox = document.createElement( 'checkbox');
  106.     if (on == true)
  107.         cBox.setAttribute( 'checked', "true");
  108.     cBox.setAttribute( 'value', name);
  109.  
  110.     cCell.appendChild( cBox);
  111.     cCell.setAttribute( 'allowevents', "true");    
  112.  
  113.     row.appendChild( cCell);
  114.     /* row.appendChild(cell); */
  115.  
  116.     cell = document.createElement( 'treecell');
  117.     cell.setAttribute( "class", "importsampledata");
  118.     cell.setAttribute( 'value', " ");
  119.     cell.setAttribute( 'noDrag', "true");    
  120.         
  121.     row.appendChild( cell);
  122.  
  123.     item.appendChild(row);
  124.  
  125.     return( item);
  126. }
  127.  
  128. function AddFieldToList(body, name, index, on)
  129. {
  130.     var item = CreateField( name, index, on, body.childNodes.length);
  131.     body.appendChild(item);
  132. }
  133.  
  134. function BeginDrag( event)
  135. {
  136.     top.dragStart = false;
  137.  
  138.     var tree = document.getElementById("fieldList");
  139.     if ( event.target == tree ) {
  140.         return( true);                    // continue propagating the event
  141.     }
  142.  
  143.     if (!tree) {
  144.         return( false);
  145.     }
  146.  
  147.     var dragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  148.     if ( dragService ) dragService = dragService.QueryInterface(Components.interfaces.nsIDragService);
  149.     if ( !dragService )    {
  150.         return(false);
  151.     }
  152.  
  153.     var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance();
  154.     if ( trans ) trans = trans.QueryInterface(Components.interfaces.nsITransferable);
  155.     if ( !trans ) {
  156.         return(false);
  157.     }
  158.  
  159.     var genData = Components.classes["@mozilla.org/supports-wstring;1"].createInstance();
  160.     if ( genData ) genData = genData.QueryInterface(Components.interfaces.nsISupportsWString);
  161.     if (!genData) {
  162.         return(false);
  163.     }
  164.  
  165.     // trans.addDataFlavor( "text/unicode");
  166.     trans.addDataFlavor( top.transferType);
  167.  
  168.     // the index is on the <treeitem> which is two levels above the <treecell> which is
  169.     // the target of the event.
  170.     if (event.target.getAttribute( 'noDrag') == "true") {
  171.         return( false);
  172.     }
  173.  
  174.     var index = event.target.parentNode.parentNode.getAttribute("field-index");
  175.     if (!index)
  176.         index = event.target.parentNode.parentNode.parentNode.getAttribute( "field-index");
  177.     if (!index)
  178.         return( false);
  179.  
  180.     var indexStr = ("" + index);
  181.     genData.data = indexStr;
  182.     
  183.     // trans.setTransferData ( "text/unicode", genData, indexStr.length * 2);
  184.     trans.setTransferData ( top.transferType, genData, indexStr.length * 2);
  185.  
  186.     var transArray = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  187.     if ( transArray ) transArray = transArray.QueryInterface(Components.interfaces.nsISupportsArray);
  188.     if ( !transArray )    {
  189.         return(false);
  190.     }
  191.  
  192.     // put it into the transferable as an |nsISupports|
  193.     var genTrans = trans.QueryInterface(Components.interfaces.nsISupports);
  194.     transArray.AppendElement(genTrans);
  195.     
  196.     var nsIDragService = Components.interfaces.nsIDragService;
  197.     top.dragStart = true;
  198.  
  199.     dragService.invokeDragSession ( event.target, transArray, null, nsIDragService.DRAGDROP_ACTION_MOVE);
  200.     
  201.  
  202.     return( false);  // don't propagate the event if a drag has begun
  203. }
  204.  
  205.  
  206. function SetRow( row, dstIndex, dstBox, dstField)
  207. {
  208.     row.setAttribute( 'field-index', dstIndex);
  209.     if (dstBox == true) {
  210.         row.firstChild.firstChild.firstChild.checked = true;
  211.     }
  212.     else {
  213.         row.firstChild.firstChild.firstChild.checked = false;
  214.     }
  215.  
  216.     /* row.firstChild.childNodes[1].setAttribute( "value", dstField); */
  217.  
  218.     row.firstChild.firstChild.firstChild.setAttribute( 'value', dstField);
  219. }
  220.  
  221.  
  222. function AssignRow( toRow, fromRow)
  223. {
  224.     /*
  225.     SetRow( toRow,    fromRow.getAttribute( 'field-index'), 
  226.                     fromRow.firstChild.firstChild.firstChild.checked,
  227.                     fromRow.firstChild.childNodes[1].getAttribute( "value"));
  228.     */
  229.     SetRow( toRow,    fromRow.getAttribute( 'field-index'), 
  230.                     fromRow.firstChild.firstChild.firstChild.checked,
  231.                     fromRow.firstChild.firstChild.firstChild.getAttribute( "value"));
  232. }
  233.  
  234.  
  235. function FindRowFromIndex( body, index)
  236. {
  237.     for (var i = 0; i < body.childNodes.length; i++) {
  238.         if (body.childNodes[i].getAttribute( 'field-index') == index)
  239.             return( i);
  240.     }
  241.  
  242.     return( -1);
  243. }
  244.  
  245. function FindRowFromItem( body, item)
  246. {
  247.     for (var i = 0; i < body.childNodes.length; i++) {
  248.         if (body.childNodes[i] == item)
  249.             return( i);
  250.     }
  251.  
  252.     return( -1);
  253. }
  254.  
  255. function DropOnTree( event)
  256. {
  257.  
  258.     var treeRoot = document.getElementById("fieldList");
  259.     if (!treeRoot)    return(false);
  260.  
  261.     // target is the <treecell>, and the <treeitem> is two levels above
  262.     var treeItem = event.target.parentNode.parentNode;
  263.     if (!treeItem)    return(false);
  264.  
  265.     // get drop hint attributes
  266.     var dropBefore = treeItem.getAttribute("dd-droplocation");
  267.     var dropOn = treeItem.getAttribute("dd-dropon");
  268.  
  269.     // calculate drop action
  270.     var dropAction;
  271.     if (dropBefore == "true")    dropAction = "before";
  272.     else if (dropOn == "true")    dropAction = "on";
  273.     else                dropAction = "after";
  274.  
  275.     dump( "DropAction: " + dropAction + "\n");
  276.  
  277.     // calculate parent container node
  278.     /* bookmarks.js uses this, not sure what it's for???
  279.     var containerItem = treeItem;
  280.     if (dropAction != "on")
  281.         containerItem = treeItem.parentNode.parentNode;
  282.     */
  283.  
  284.     var dragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  285.     if ( dragService ) dragService = dragService.QueryInterface(Components.interfaces.nsIDragService);
  286.     if ( !dragService )    return(false);
  287.     
  288.     var dragSession = dragService.getCurrentSession();
  289.     if ( !dragSession )    return(false);
  290.  
  291.     var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance();
  292.     if ( trans ) trans = trans.QueryInterface(Components.interfaces.nsITransferable);
  293.     if ( !trans )        return(false);
  294.     trans.addDataFlavor( top.transferType);
  295.     // trans.addDataFlavor( "text/unicode");
  296.  
  297.     var body = document.getElementById( "fieldBody");
  298.     if (!body)
  299.         return( false);
  300.  
  301.     for ( var i = 0; i < dragSession.numDropItems; ++i )
  302.     {
  303.         dragSession.getData ( trans, i );
  304.         var dataObj = new Object();
  305.         var bestFlavor = new Object();
  306.         var len = new Object();
  307.         try {
  308.             trans.getAnyTransferData( bestFlavor, dataObj, len);
  309.             if ( dataObj )    {
  310.                 dataObj = dataObj.value.QueryInterface(Components.interfaces.nsISupportsWString);
  311.             }
  312.             if ( !dataObj )    {
  313.                 continue;
  314.             }
  315.  
  316.             var fIndex = parseInt( dataObj.data);
  317.             
  318.             dump( "Source row: " + fIndex + "\n");
  319.  
  320.             // so now what, move the given row to the new position!
  321.             // find the source row index
  322.             var srcRow = FindRowFromIndex( body, fIndex);
  323.             if (srcRow < 0) {
  324.                 dump( "*** Error finding source row\n");
  325.                 continue;
  326.             }
  327.             var dstRow = FindRowFromItem( body, treeItem);
  328.             if (dstRow < 0) {
  329.                 dump( "*** Error finding destination row\n");
  330.                 continue;
  331.             }
  332.  
  333.             // always do before unless we can't
  334.             if (dropAction == "on" || dropAction == "after") {
  335.                 dstRow++;
  336.                 if (dstRow >= body.childNodes.length) {
  337.                     if (srcRow == (body.childNodes.length - 1))
  338.                         continue;
  339.                     dstRow = -1;
  340.                 }
  341.             }
  342.             
  343.             var maxIndex = body.childNodes.length - 1;
  344.             var dstBox = body.childNodes[srcRow].firstChild.firstChild.firstChild.checked;
  345.             var dstField = body.childNodes[srcRow].firstChild.firstChild.firstChild.getAttribute( 'value');
  346.             var dstIndex = body.childNodes[srcRow].getAttribute( 'field-index');
  347.             
  348.             dump( "FieldDrag from " + srcRow + " to " + dstRow + "\n");
  349.  
  350.             if (dstRow < 0) {
  351.                 // remove the row and append it to the end!
  352.                 // Move srcRow to the end!                
  353.                 while (srcRow < maxIndex) {
  354.                     AssignRow( body.childNodes[srcRow], body.childNodes[srcRow + 1]);
  355.                     srcRow++;
  356.                 }
  357.                 
  358.                 SetRow( body.childNodes[maxIndex], dstIndex, dstBox, dstField);
  359.             }
  360.             else {
  361.                 if (dstRow == srcRow)
  362.                     continue;
  363.                 if (srcRow < dstRow)
  364.                     dstRow--;
  365.                 if (dstRow == srcRow)
  366.                     continue;
  367.  
  368.                 if (dstRow < srcRow) {
  369.                     // move dstRow down to srcRow
  370.                     while (dstRow < srcRow) {
  371.                         AssignRow( body.childNodes[srcRow], body.childNodes[srcRow - 1]);
  372.                         srcRow--;
  373.                     }
  374.                 }
  375.                 else {
  376.                     // move dstRow up to srcRow
  377.                     while (srcRow < dstRow) {
  378.                         AssignRow( body.childNodes[srcRow], body.childNodes[srcRow + 1]);
  379.                         srcRow++;
  380.                     }
  381.                 }
  382.                 SetRow( body.childNodes[dstRow], dstIndex, dstBox, dstField);
  383.             }
  384.  
  385.         }
  386.         catch( ex) {
  387.             dump( "Caught drag exception in DropOnTree\n");
  388.             dump( ex);
  389.             dump( "\n");
  390.         }
  391.     }
  392.  
  393.     return(false);
  394. }
  395.  
  396.  
  397. function DragOverTree( event)
  398. {
  399.     if (!top.dragStart)
  400.         return( false);
  401.  
  402.     var validFlavor = false;
  403.     var dragSession = null;
  404.     var retVal = true;
  405.  
  406.     var dragService = Components.classes["@mozilla.org/widget/dragservice;1"].getService();
  407.     if ( dragService ) dragService = dragService.QueryInterface(Components.interfaces.nsIDragService);
  408.     if ( !dragService )    return(false);
  409.  
  410.     dragSession = dragService.getCurrentSession();
  411.     if ( !dragSession )    return(false);
  412.  
  413.     if ( dragSession.isDataFlavorSupported( top.transferType) )    validFlavor = true;
  414.     // if ( dragSession.isDataFlavorSupported( "text/unicode") )    validFlavor = true;
  415.     
  416.     if (event.target == document.getElementById( "fieldBody")) return( false);
  417.  
  418.     // touch the attribute on the rowgroup to trigger the repaint with the drop feedback.
  419.     if ( validFlavor )
  420.     {
  421.         //XXX this is really slow and likes to refresh N times per second.
  422.         var rowGroup = event.target.parentNode.parentNode;
  423.         rowGroup.setAttribute ( "dd-triggerrepaint", 0 );
  424.         dragSession.canDrop = true;
  425.         // necessary??
  426.         retVal = false; // do not propagate message
  427.     }
  428.     return(retVal);
  429. }
  430.  
  431. function ShowSampleData( data)
  432. {
  433.     var fBody = document.getElementById( "fieldBody");
  434.     var fields = data.split( "\n");
  435.     for (var i = 0; i < fBody.childNodes.length; i++) {
  436.         if (i < fields.length) {
  437.             // fBody.childNodes[i].firstChild.childNodes[2].setAttribute( 'value', fields[i]);
  438.             fBody.childNodes[i].firstChild.childNodes[1].setAttribute( 'value', fields[i]);
  439.         }
  440.         else {
  441.             // fBody.childNodes[i].firstChild.childNodes[2].setAttribute( 'value', " ");
  442.             fBody.childNodes[i].firstChild.childNodes[1].setAttribute( 'value', " ");
  443.         }
  444.     }
  445.  
  446. }
  447.  
  448. function FetchSampleData()
  449. {
  450.     if (!top.addInterface)
  451.         return( false);
  452.     
  453.     var num = top.recordNum - 1;
  454.     if (num < 0)
  455.         num = 0;
  456.     var data = top.addInterface.GetData( "sampleData-"+num);
  457.     var result = false;
  458.     if (data != null) {
  459.         data = data.QueryInterface( Components.interfaces.nsISupportsWString);
  460.         if (data != null) {
  461.             ShowSampleData( data.data);    
  462.             result = true;
  463.         }
  464.     }
  465.     
  466.     return( result);
  467. }
  468.  
  469. function OnPreviousRecord()
  470. {
  471.     if (top.recordNum <= 1)
  472.         return;
  473.     top.recordNum--;
  474.     top.amAtEnd = false;
  475.     if (FetchSampleData()) {
  476.         document.getElementById('recordNumber').setAttribute('value', ("" + top.recordNum));        
  477.     }
  478. }
  479.  
  480. function OnNextRecord()
  481. {
  482.     if (top.amAtEnd)
  483.         return;
  484.     top.recordNum++;
  485.     if (!FetchSampleData()) {
  486.         top.amAtEnd = true;
  487.         top.recordNum--;
  488.     }
  489.     else
  490.         document.getElementById('recordNumber').setAttribute('value', ("" + top.recordNum));        
  491. }
  492.  
  493. function FieldImportOKButton()
  494. {
  495.     var body = document.getElementById( "fieldBody");
  496.     var max = body.childNodes.length;
  497.     var fIndex;
  498.     var on;
  499.     for (var i = 0; i < max; i++) {
  500.         fIndex = body.childNodes[i].getAttribute( 'field-index');
  501.         on = body.childNodes[i].firstChild.firstChild.firstChild.checked;
  502.         top.fieldMap.SetFieldMap( i, fIndex);
  503.         if (on == true)
  504.             top.fieldMap.SetFieldActive( i, true);
  505.         else
  506.             top.fieldMap.SetFieldActive( i, false);
  507.     }
  508.  
  509.     top.dialogResult.ok = true;
  510.  
  511.     return true;
  512. }
  513.