home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 February / Chip_2004-02_cd1.bin / chplus / jak_psat_web / behavior / dragdrop.htc < prev    next >
Text File  |  2003-04-06  |  7KB  |  324 lines

  1.  
  2. <public:property name='dragColor' />
  3.  
  4. <public:attach event=oncontentready onevent="init();" />
  5.  
  6. <script>
  7. var tbody, headRow;
  8. var bDragMode = false;
  9. var objDragItem;
  10. var arrHitTest = new Array();
  11. var iArrayHit = false;
  12. var ColumnCount = null;
  13.  
  14. //
  15. // Init function.. Fills out variables with data
  16. // loaded with oncontentready.
  17. //
  18. function init() 
  19. {
  20.   var i;
  21.  
  22.     // get TBODY - take the first TBODY for the table
  23.     tbody = element.tBodies(0);
  24.     if (!tbody) return;
  25.  
  26.     //Get THEAD  
  27.     var click = element.tHead;
  28.     if (!click)  return;
  29.  
  30.     
  31.   headRow = click.children[0];
  32.   
  33.   if (headRow.tagName != "TR") return;
  34.  
  35.  
  36.   headRow.runtimeStyle.cursor = "hand";
  37.     
  38.   ColumnCount = headRow.children.length;
  39.  
  40.   for(i=0; i < ColumnCount ; i++)
  41.   {
  42.     arrHitTest[i] = new Array();
  43.   }
  44.  
  45.   
  46.   var cx=0;
  47.   var cy=0;
  48.   var c;
  49.   defaultTitleColor = headRow.children[0].currentStyle.backgroundColor;     
  50.  
  51.   for (i=0; i<ColumnCount ; i++) {
  52.  
  53.     var clickCell = headRow.children[i];
  54.     clickCell.selectIndex = i;
  55.     c = clickCell.offsetParent;
  56.  
  57.  
  58.     if(cx == 0 && cy == 0 )
  59.     {
  60.         while (c.offsetParent != null) {
  61.                   cy += c.offsetTop;
  62.                   cx += c.offsetLeft;
  63.                   c = c.offsetParent;
  64.         }
  65.     }
  66.  
  67.     arrHitTest[i][0] = cx + clickCell.offsetLeft;
  68.     arrHitTest[i][1] = cy + clickCell.offsetTop;
  69.     arrHitTest[i][2] = clickCell;
  70.     arrHitTest[i][3] = cx + clickCell.offsetLeft + eval(clickCell.width);
  71.  
  72.     clickCell.attachEvent("onmousedown",onMouseDown);
  73.   } 
  74.  
  75.   element.document.attachEvent("onmousemove",onMouseMove);
  76.   element.document.attachEvent("onmouseup",onMouseUp);
  77.   element.document.attachEvent("onselectstart",onSelect);
  78. }
  79.  
  80.  
  81. function InitHeader()
  82. {
  83.   var cx=0;
  84.   var cy=0;
  85.   var c;
  86.      
  87.   for (i=0; i<ColumnCount ; i++) {
  88.  
  89.     var clickCell = headRow.children[i];
  90.     clickCell.selectIndex = i;
  91.     c = clickCell.offsetParent;
  92.  
  93.     if(cx == 0 && cy == 0 )
  94.     {
  95.         while (c.offsetParent != null) {
  96.                   cy += c.offsetTop;
  97.                   cx += c.offsetLeft;
  98.                   c = c.offsetParent;
  99.         }
  100.     }
  101.  
  102.     arrHitTest[i][0] = cx + clickCell.offsetLeft;
  103.     arrHitTest[i][1] = cy + clickCell.offsetTop;
  104.     arrHitTest[i][2] = clickCell;
  105.     arrHitTest[i][3] = cx + clickCell.offsetLeft + eval(clickCell.width);
  106.   } 
  107. }
  108.  
  109. function onSelect()
  110. {
  111.     //disable selection
  112.     return false;
  113. }
  114.  
  115. function ChangeHeader(iChange)
  116. {
  117.     for(var y = 0; y < arrHitTest.length; y++)
  118.     {
  119.     if (arrHitTest[y][2].currentStyle.backgroundColor == dragColor)
  120.         arrHitTest[y][2].style.backgroundColor = defaultTitleColor;
  121.     }
  122.  
  123.     if(iChange == "-1") return; 
  124.  
  125.     arrHitTest[iChange][2].style.backgroundColor = dragColor;
  126. }
  127.  
  128. function onMouseUp(e)
  129. {
  130.     if(!bDragMode)    return;
  131.     bDragMode = false;
  132.  
  133.     var iSelected = objDragItem.selectIndex;
  134.     
  135.     objDragItem.removeNode(true);
  136.     objDragItem = null;
  137.  
  138.     ChangeHeader(-1);
  139.  
  140.     if( (iArrayHit - 1) < 0 || iSelected < 0) return;    // default faliure
  141.  
  142.     CopyRow(iSelected, (iArrayHit - 1) );
  143.  
  144.     // Reset our variables
  145.     iSelected = 0;
  146.     iArrayHit = -1;
  147. }
  148.  
  149. function onMouseDown(e)
  150. {
  151.     bDragMode     = true;
  152.     var src     = e.srcElement;
  153.     var c     = e.srcElement;
  154.  
  155.     while (src.tagName != "TD") 
  156.         src = src.parentElement;
  157.  
  158.     // Create our header on the fly
  159.     objDragItem = document.createElement("DIV");
  160.     objDragItem.innerHTML        = src.innerHTML;
  161.     objDragItem.style.height    = src.currentStyle.height;
  162.     objDragItem.style.width     = src.currentStyle.width;
  163.     objDragItem.style.background     = src.currentStyle.backgroundColor;
  164.     objDragItem.style.fontColor    = src.currentStyle.fontColor;
  165.     objDragItem.style.position     = "absolute";
  166.     objDragItem.selectIndex        = src.selectIndex;
  167.     while (c.offsetParent != null) 
  168.         {
  169.         objDragItem.style.y += c.offsetTop;
  170.         objDragItem.style.x += c.offsetLeft;
  171.         c = c.offsetParent;
  172.     }
  173.      objDragItem.style.borderStyle    = "outset";
  174.     objDragItem.style.display    = "none";
  175.  
  176.     src.insertBefore(objDragItem);
  177. }
  178.  
  179. function onMouseMove(e)
  180. {
  181.     if(!bDragMode || !objDragItem) return;    // If we aren't dragging or our object
  182.                                 // is null, we return
  183.  
  184.     // Hardcoded value for height difference
  185.     var midWObj = objDragItem.style.posWidth / 2;
  186.     var midHObj = 12;
  187.  
  188.     // Save mouse's position in the document
  189.      var intTop = e.clientY + element.document.body.scrollTop;
  190.      var intLeft = e.clientX + element.document.body.scrollLeft;
  191.  
  192.  
  193.     var cx=0,cy=0;
  194.     var elCurrent = objDragItem.offsetParent;
  195.                while (elCurrent.offsetParent != null) {
  196.                   cx += elCurrent.offsetTop;
  197.                   cy += elCurrent.offsetLeft;
  198.                   elCurrent = elCurrent.offsetParent;
  199.                }
  200.  
  201.  
  202.       objDragItem.style.pixelTop  = intTop  - cx - midHObj;
  203.       objDragItem.style.pixelLeft = intLeft - cy - midWObj;
  204.  
  205.  
  206.     if(objDragItem.style.display == "none") objDragItem.style.display = "";
  207.  
  208.     iArrayHit = CheckHit(intTop , intLeft , e);
  209.  
  210.     e.cancelBubble = false;
  211.     e.returnValue = false;
  212. }
  213.  
  214. function CheckHit(x,y,e)
  215. {
  216.     midWObj = objDragItem.style.posWidth / 2;
  217.     midHObj = 12;
  218.  
  219.     if( ((x) > (arrHitTest[0][1] + 20) ) || ( (x) < (arrHitTest[0][1]) ) )
  220.     {
  221.         ChangeHeader(-1);
  222.         return -1;
  223.     }
  224.  
  225.     for(var i=0; i < ColumnCount; i++)
  226.     {
  227.         if( (y) > (arrHitTest[i][0]) && (y) < (arrHitTest[i][3] )) //+ 100))
  228.         {
  229.             ChangeHeader(i);
  230.             return i + 1;
  231.         }
  232.     }
  233.     return -1;
  234. }
  235.  
  236. //
  237. // Copy from row to row.. Does the Header also.
  238. //
  239. function CopyRow(from, to)
  240. {    
  241.     if(from == to) return;
  242.  
  243.  
  244.     var origfrom = from;
  245.     var origto = to;
  246.     var iDiff = 0;
  247.  
  248.     if( from > to )
  249.     {
  250.  
  251.         iDiff = from - to;
  252.  
  253.         var saveObj     = headRow.children[from].innerHTML;
  254.         var saveWidth     = headRow.children[from].width;
  255.  
  256.         for(var i = 0 ; i < iDiff; i++)
  257.         {
  258.             headRow.children[from].innerHTML = headRow.children[from - 1].innerHTML;
  259.             headRow.children[from].width = headRow.children[from - 1].width;
  260.             from--;
  261.         }
  262.         headRow.children[to].innerHTML     = saveObj;
  263.         headRow.children[to].width = saveWidth;
  264.         
  265.     }
  266.     else
  267.     {
  268.  
  269.         iDiff = to - from;
  270.  
  271.         var saveObj = headRow.children[from].innerHTML;
  272.         var saveWidth     = headRow.children[from].width;
  273.  
  274.         for(var i = 0 ; i < iDiff; i++)
  275.         {
  276.             headRow.children[from].innerHTML = headRow.children[from + 1].innerHTML;
  277.             headRow.children[from].width = headRow.children[from + 1].width;
  278.             from++;
  279.         }
  280.  
  281.         headRow.children[to].innerHTML     = saveObj;
  282.         headRow.children[to].width = saveWidth;
  283.     }
  284.  
  285.  
  286.  
  287.     for(var i = 0 ; i < headRow.children.length; i++)
  288.             headRow.children[i].selectIndex = i;
  289.  
  290.  
  291.  
  292.     InitHeader();
  293.     for ( var iRowInsert = 0 ; iRowInsert < tbody.rows.length; iRowInsert++ )
  294.     {
  295.         from = origfrom;
  296.         to = origto;
  297.         if( from > to )
  298.         {
  299.             iDiff = from - to;
  300.             var saveObj = tbody.children[iRowInsert].children[from].innerHTML
  301.             for(var i = 0 ; i < iDiff; i++)
  302.             {
  303.                 tbody.children[iRowInsert].children[from].innerHTML = tbody.children[iRowInsert].children[from - 1].innerHTML;
  304.                 from--;
  305.             }
  306.             tbody.children[iRowInsert].children[to].innerHTML = saveObj;
  307.  
  308.         }
  309.         else
  310.         {
  311.             iDiff = to - from;
  312.             var saveObj = tbody.children[iRowInsert].children[from].innerHTML
  313.             for(var i = 0 ; i < iDiff; i++)
  314.             {
  315.                 tbody.children[iRowInsert].children[from].innerHTML = tbody.children[iRowInsert].children[from + 1].innerHTML;
  316.                 from++;
  317.             }
  318.             tbody.children[iRowInsert].children[to].innerHTML = saveObj;
  319.         }
  320.     }
  321. }
  322.  
  323. </script>
  324.