home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Demo / elearn / Chimie / interfata.swf / scripts / FSelectableListSymbol.as < prev    next >
Text File  |  2004-05-12  |  10KB  |  361 lines

  1. function FSelectableListClass()
  2. {
  3.    this.init();
  4. }
  5. FSelectableListClass.prototype = new FUIComponentClass();
  6. FSelectableListClass.prototype.init = function()
  7. {
  8.    super.init();
  9.    this.enable = true;
  10.    this.selected = new Array();
  11.    this.topDisplayed = this.numDisplayed = 0;
  12.    this.lastSelected = 0;
  13.    this.tabChildren = false;
  14.    if(this._name != undefined)
  15.    {
  16.       this.dataProvider = new DataProviderClass();
  17.       this.dataProvider.addView(this);
  18.    }
  19. };
  20. FSelectableListClass.prototype.addItemAt = function(index, label, data)
  21. {
  22.    if(index < 0 || !this.enable)
  23.    {
  24.       return undefined;
  25.    }
  26.    this.dataProvider.addItemAt(index,{label:label,data:data});
  27. };
  28. FSelectableListClass.prototype.addItem = function(label, data)
  29. {
  30.    if(!this.enable)
  31.    {
  32.       return undefined;
  33.    }
  34.    this.dataProvider.addItem({label:label,data:data});
  35. };
  36. FSelectableListClass.prototype.removeItemAt = function(index)
  37. {
  38.    this.selectHolder = this.getSelectedIndex();
  39.    var item = this.getItemAt(index);
  40.    this.dataProvider.removeItemAt(index);
  41.    return item;
  42. };
  43. FSelectableListClass.prototype.removeAll = function()
  44. {
  45.    this.dataProvider.removeAll();
  46. };
  47. FSelectableListClass.prototype.replaceItemAt = function(index, newLabel, newData)
  48. {
  49.    this.dataProvider.replaceItemAt(index,{label:newLabel,data:newData});
  50. };
  51. FSelectableListClass.prototype.sortItemsBy = function(fieldName, order)
  52. {
  53.    this.lastSelID = this.dataProvider.getItemID(this.lastSelected);
  54.    this.dataProvider.sortItemsBy(fieldName,order);
  55. };
  56. FSelectableListClass.prototype.getLength = function()
  57. {
  58.    return this.dataProvider.getLength();
  59. };
  60. FSelectableListClass.prototype.getSelectedIndex = function()
  61. {
  62.    for(var uniqueID in this.selected)
  63.    {
  64.       var tmpInd = this.selected[uniqueID].sIndex;
  65.       if(tmpInd != undefined)
  66.       {
  67.          return tmpInd;
  68.       }
  69.    }
  70. };
  71. FSelectableListClass.prototype.getSelectedItem = function()
  72. {
  73.    return this.getItemAt(this.getSelectedIndex());
  74. };
  75. FSelectableListClass.prototype.getItemAt = function(index)
  76. {
  77.    return this.dataProvider.getItemAt(index);
  78. };
  79. FSelectableListClass.prototype.getEnabled = function()
  80. {
  81.    return this.enable;
  82. };
  83. FSelectableListClass.prototype.getValue = function()
  84. {
  85.    var item = this.getSelectedItem();
  86.    return item.data != undefined ? item.data : item.label;
  87. };
  88. FSelectableListClass.prototype.setSelectedIndex = function(index, flag)
  89. {
  90.    if(index >= 0 && index < this.getLength() && this.enable)
  91.    {
  92.       this.clearSelected();
  93.       this.selectItem(index,true);
  94.       this.lastSelected = index;
  95.       this.invalidate("updateControl");
  96.       if(flag != false)
  97.       {
  98.          this.executeCallBack();
  99.       }
  100.    }
  101. };
  102. FSelectableListClass.prototype.setDataProvider = function(obj)
  103. {
  104.    this.setScrollPosition(0);
  105.    this.clearSelected();
  106.    if(obj instanceof Array)
  107.    {
  108.       this.dataProvider = new DataProviderClass();
  109.       var i = 0;
  110.       while(i < obj.length)
  111.       {
  112.          var value = typeof obj[i] != "string" ? obj[i] : {label:obj[i]};
  113.          this.dataProvider.addItem(value);
  114.          i++;
  115.       }
  116.    }
  117.    else
  118.    {
  119.       this.dataProvider = obj;
  120.    }
  121.    this.dataProvider.addView(this);
  122. };
  123. FSelectableListClass.prototype.setItemSymbol = function(linkID)
  124. {
  125.    this.tmpPos = this.getScrollPosition();
  126.    this.itemSymbol = linkID;
  127.    this.invalidate("setSize");
  128.    this.setScrollPosition(this.tmpPos);
  129. };
  130. FSelectableListClass.prototype.setEnabled = function(enabledFlag)
  131. {
  132.    this.cleanUI();
  133.    super.setEnabled(enabledFlag);
  134.    this.enable = enabledFlag;
  135.    this.boundingBox_mc.gotoAndStop(!this.enable ? "disabled" : "enabled");
  136.    var limit = Math.min(this.numDisplayed,this.getLength());
  137.    var i = 0;
  138.    while(i < limit)
  139.    {
  140.       this.container_mc["fListItem" + i + "_mc"].setEnabled(this.enable);
  141.       i++;
  142.    }
  143.    if(this.enable)
  144.    {
  145.       this.invalidate("updateControl");
  146.    }
  147. };
  148. FSelectableListClass.prototype.updateControl = function()
  149. {
  150.    var i = 0;
  151.    while(i < this.numDisplayed)
  152.    {
  153.       this.container_mc["fListItem" + i + "_mc"].drawItem(this.getItemAt(this.topDisplayed + i),this.isSelected(this.topDisplayed + i));
  154.       i++;
  155.    }
  156. };
  157. FSelectableListClass.prototype.setSize = function(w, h)
  158. {
  159.    super.setSize(w,h);
  160.    this.boundingBox_mc._xscale = this.boundingBox_mc._yscale = 100;
  161.    this.boundingBox_mc._xscale = this.width * 100 / this.boundingBox_mc._width;
  162.    this.boundingBox_mc._yscale = this.height * 100 / this.boundingBox_mc._height;
  163.    var i = 0;
  164.    while(i < this.numDisplayed)
  165.    {
  166.       this.container_mc.attachMovie(this.itemSymbol,"fListItem" + i + "_mc",10 + i,{controller:this,itemNum:i});
  167.       var item_mc = this.container_mc["fListItem" + i + "_mc"];
  168.       var offset = this.scrollOffset != undefined ? this.scrollOffset : 0;
  169.       item_mc.setSize(this.width - offset,this.itmHgt);
  170.       item_mc._y = (this.itmHgt - 2) * i;
  171.       i++;
  172.    }
  173.    this.updateControl();
  174. };
  175. FSelectableListClass.prototype.modelChanged = function(eventObj)
  176. {
  177.    var firstRow = eventObj.firstRow;
  178.    var lastRow = eventObj.lastRow;
  179.    var event = eventObj.event;
  180.    if(event == "addRows")
  181.    {
  182.       for(var i in this.selected)
  183.       {
  184.          if(this.selected[i].sIndex != undefined && this.selected[i].sIndex >= firstRow)
  185.          {
  186.             this.selected[i].sIndex += lastRow - firstRow + 1;
  187.             this.setSelectedIndex(this.selected[i].sIndex,false);
  188.          }
  189.       }
  190.    }
  191.    else if(event == "deleteRows")
  192.    {
  193.       if(firstRow == lastRow)
  194.       {
  195.          var index = firstRow;
  196.          if(this.selectHolder == index)
  197.          {
  198.             this.selectionDeleted = true;
  199.          }
  200.          if(this.topDisplayed + this.numDisplayed >= this.getLength() && this.topDisplayed > 0)
  201.          {
  202.             this.topDisplayed--;
  203.             if(this.selectionDeleted && index - 1 >= 0)
  204.             {
  205.                this.setSelectedIndex(index - 1,false);
  206.             }
  207.          }
  208.          else if(this.selectionDeleted)
  209.          {
  210.             var len = this.getLength();
  211.             if(index == len - 1 && len > 1 || index > len / 2)
  212.             {
  213.                this.setSelectedIndex(index - 1,false);
  214.             }
  215.             else
  216.             {
  217.                this.setSelectedIndex(index,false);
  218.             }
  219.          }
  220.          for(var i in this.selected)
  221.          {
  222.             if(this.selected[i].sIndex > firstRow)
  223.             {
  224.                this.selected[i].sIndex--;
  225.             }
  226.          }
  227.       }
  228.       else
  229.       {
  230.          this.clearSelected();
  231.          this.topDisplayed = 0;
  232.       }
  233.    }
  234.    else if(event == "sort")
  235.    {
  236.       var len = this.getLength();
  237.       var i = 0;
  238.       while(i < len)
  239.       {
  240.          if(this.isSelected(i))
  241.          {
  242.             var id = this.dataProvider.getItemID(i);
  243.             if(id == this.lastSelID)
  244.             {
  245.                this.lastSelected = i;
  246.             }
  247.             this.selected[String(id)].sIndex = i;
  248.          }
  249.          i++;
  250.       }
  251.    }
  252.    this.invalidate("updateControl");
  253. };
  254. FSelectableListClass.prototype.measureItmHgt = function()
  255. {
  256.    this.attachMovie(this.itemSymbol,"tmpItem_mc",0,{controller:this});
  257.    this.tmpItem_mc.drawItem({label:"Sizer: PjtTopg"},false);
  258.    this.itmHgt = this.tmpItem_mc._height;
  259.    this.tmpItem_mc.removeMovieClip();
  260. };
  261. FSelectableListClass.prototype.selectItem = function(index, selectedFlag)
  262. {
  263.    if(selectedFlag && !this.isSelected(index))
  264.    {
  265.       this.selected[String(this.dataProvider.getItemID(index))] = {sIndex:index};
  266.    }
  267.    else if(!selectedFlag)
  268.    {
  269.       delete this.selected[String(this.dataProvider.getItemID(index))];
  270.    }
  271. };
  272. FSelectableListClass.prototype.isSelected = function(index)
  273. {
  274.    return this.selected[String(this.dataProvider.getItemID(index))].sIndex != undefined;
  275. };
  276. FSelectableListClass.prototype.clearSelected = function()
  277. {
  278.    for(var uniqueID in this.selected)
  279.    {
  280.       var index = this.selected[uniqueID].sIndex;
  281.       if(index != undefined && this.topDisplayed <= index && index < this.topDisplayed + this.numDisplayed)
  282.       {
  283.          this.container_mc["fListItem" + (index - this.topDisplayed) + "_mc"].drawItem(this.getItemAt(index),false);
  284.       }
  285.    }
  286.    delete this.selected;
  287.    this.selected = new Array();
  288. };
  289. FSelectableListClass.prototype.selectionHandler = function(itemNum)
  290. {
  291.    var tmpInd = this.topDisplayed + itemNum;
  292.    if(this.getItemAt(tmpInd == undefined))
  293.    {
  294.       this.changeFlag = false;
  295.       return undefined;
  296.    }
  297.    this.changeFlag = true;
  298.    this.clearSelected();
  299.    this.selectItem(tmpInd,true);
  300.    this.container_mc["fListItem" + itemNum + "_mc"].drawItem(this.getItemAt(tmpInd),this.isSelected(tmpInd));
  301. };
  302. FSelectableListClass.prototype.moveSelBy = function(incr)
  303. {
  304.    var itmNum = this.getSelectedIndex();
  305.    var newItm = itmNum + incr;
  306.    newItm = Math.max(0,newItm);
  307.    newItm = Math.min(this.getLength() - 1,newItm);
  308.    if(newItm == itmNum)
  309.    {
  310.       return undefined;
  311.    }
  312.    if(itmNum < this.topDisplayed || itmNum >= this.topDisplayed + this.numDisplayed)
  313.    {
  314.       this.setScrollPosition(itmNum);
  315.    }
  316.    if(newItm >= this.topDisplayed + this.numDisplayed || newItm < this.topDisplayed)
  317.    {
  318.       this.setScrollPosition(this.topDisplayed + incr);
  319.    }
  320.    this.selectionHandler(newItm - this.topDisplayed);
  321. };
  322. FSelectableListClass.prototype.clickHandler = function(itmNum)
  323. {
  324.    this.focusRect.removeMovieClip();
  325.    if(!this.focused)
  326.    {
  327.       this.pressFocus();
  328.    }
  329.    this.selectionHandler(itmNum);
  330.    this.onMouseUp = this.releaseHandler;
  331. };
  332. FSelectableListClass.prototype.releaseHandler = function()
  333. {
  334.    if(this.changeFlag)
  335.    {
  336.       this.executeCallBack();
  337.    }
  338.    this.changeFlag = false;
  339.    this.onMouseUp = undefined;
  340. };
  341. FSelectableListClass.prototype.myOnSetFocus = function()
  342. {
  343.    super.myOnSetFocus();
  344.    var i = 0;
  345.    while(i < this.numDisplayed)
  346.    {
  347.       this.container_mc["fListItem" + i + "_mc"].highlight_mc.gotoAndStop("enabled");
  348.       i++;
  349.    }
  350. };
  351. FSelectableListClass.prototype.myOnKillFocus = function()
  352. {
  353.    super.myOnKillFocus();
  354.    var i = 0;
  355.    while(i < this.numDisplayed)
  356.    {
  357.       this.container_mc["fListItem" + i + "_mc"].highlight_mc.gotoAndStop("unfocused");
  358.       i++;
  359.    }
  360. };
  361.