home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 85 / CD-ROM 85 / CD-ROM 85.iso / med2000 / js / list.js < prev    next >
Encoding:
JavaScript  |  2001-04-19  |  7.3 KB  |  219 lines

  1. // List Object
  2. // generic selection widget built primarily to be incorporated into other List-based widgets (MenuList, ScrollList, SelectList)
  3. // 19990410
  4.  
  5. // Copyright (C) 1999 Dan Steinman
  6. // Distributed under the terms of the GNU Library General Public License
  7. // Available at http://www.dansteinman.com/dynapi/
  8.  
  9. // Thanks to: Knut Dale <Knut.S.Dale@eto.ericsson.se>
  10.  
  11. function List(x,y,width,itemH) {
  12.     this.name = "List"+(List.count++)
  13.     this.x = x
  14.     this.y = y
  15.     this.w = width
  16.     if (arguments.length==4) {
  17.         this.itemH = itemH
  18.         this.itemHset = true
  19.         this.h = -1
  20.     }
  21.     else {
  22.         this.itemH = null
  23.         this.itemHset = false
  24.         this.h = (is.ns)? -1 : 1000
  25.     }
  26.  
  27.     this.itemSpacing = 1
  28.     this.fontname = 'New Times Roman'
  29.     this.fontsize = 8
  30.     this.visibility = 'inherit'
  31.  
  32.     this.overOpen = false
  33.     this.menulist = null
  34.     this.indent = 5
  35.     
  36.     this.color = new Object()
  37.     this.color.textNormal = '#000000'
  38.     this.color.textSelected = '#FFFFFF'
  39.     this.color.bgNormal = '#E6E6E6'
  40.     this.color.bgSelected = '#0000A0'
  41.     this.color.bgRollover = '#D1D1D1'
  42.     this.color.bg = '#C0C0C0'
  43.  
  44.     this.allowDeselect = false
  45.     this.multiSelect = false
  46.     this.preSelect = null
  47.  
  48.     this.items = new Array()
  49.     this.selectedIndex = null
  50.     this.obj = this.name + "ListObject"
  51.     eval(this.obj + "=this")
  52.  
  53.     this.add = ListAdd
  54.     this.build = ListBuild
  55.     this.activate = ListActivate
  56.     this.over = ListOver
  57.     this.out = ListOut
  58.     this.down = ListDown
  59.     this.select = ListSelect
  60.     this.setCols = ListSetCols
  61.     this.image = new Object()
  62.     this.setImage = ListSetImage
  63.     this.deselect = ListDeselect
  64.     this.onSelect = new Function()
  65. }
  66. function ListSetCols() {
  67.     this.cols = arguments
  68.     this.multiCol = true
  69. }
  70. function ListSetImage(image0,image1,width,height) {
  71.     this.image.image0 = new Image()
  72.     this.image.image0.src = image0
  73.     this.image.image1 = new Image()
  74.     this.image.image1.src = image1
  75.     this.image.w = width
  76.     this.image.h = height
  77. }
  78. function ListAdd(value) {
  79.     var i = this.items.length
  80.     this.items[i] = new Array()
  81.     this.items[i].selected = false
  82.     this.items[i].value = value
  83.     if (arguments.length>2) {
  84.         this.items[i].textNormal = this.items[i].textSelected = '<table border=0 cellpadding=0 cellspacing=0><tr>'
  85.         this.items[i].text = new Array()
  86.         for (var j=1;j<arguments.length;j++) {
  87.             this.items[i].text[j-1] = ''+arguments[j]
  88.             this.items[i].textNormal += '<td width='+this.cols[j-1]+'><div class="'+this.name+'TextNormal">'+arguments[j]+'</div></td>'
  89.             this.items[i].textSelected += '<td width='+this.cols[j-1]+'><div class="'+this.name+'TextSelected">'+arguments[j]+'</div></td>'
  90.         }
  91.         this.items[i].textNormal += '</tr></table>'
  92.         this.items[i].textSelected += '</tr></table>'
  93.     }
  94.     else {
  95.         this.items[i].text = arguments[1]
  96.         this.items[i].textNormal = '<div class="'+this.name+'TextNormal">'+arguments[1]+'</div>'
  97.         this.items[i].textSelected = '<div class="'+this.name+'TextSelected">'+arguments[1]+'</div>'
  98.     }
  99.     if (this.itemH) {
  100.         this.h += this.itemH+this.itemSpacing
  101.         this.items[i].y = i*this.itemH+i*this.itemSpacing
  102.     }
  103.     else this.items[i].y = 0
  104. }
  105. function ListBuild() {
  106.     this.css = ''
  107.     this.css += css(this.name+'List',this.x,this.y,this.w,this.h,this.color.bg,(this.itemHset)?this.visibility:'hidden')
  108.     for (var i=0;i<this.items.length;i++) {
  109.         this.css += css(this.name+'ListItem'+i,0,this.items[i].y,this.w,this.itemH,this.color.bgNormal)
  110.         if (this.items[i].hasImage) this.css += css(this.name+'ListItemImgLyr'+i,this.w-this.image.w,this.items[i].y)
  111.         this.css += css(this.name+'ListItemC'+i,0,this.items[i].y,this.w,this.itemH)
  112.     }
  113.     this.css += '.'+this.name+'TextNormal {font-family:"'+this.fontname+'"; font-size:'+this.fontsize+'pt; color:'+this.color.textNormal+'; background-color:transparent; margin-left:'+this.indent+'px;}\n'+
  114.     '.'+this.name+'TextSelected {font-family:"'+this.fontname+'"; font-size:'+this.fontsize+'pt; color:'+this.color.textSelected+'; background-color:transparent; margin-left:'+this.indent+'px;}\n'
  115.  
  116.     this.div = '<div id="'+this.name+'List">\n'
  117.     for (var i=0;i<this.items.length;i++) {
  118.         this.div += '<div id="'+this.name+'ListItem'+i+'">'+this.items[i].textNormal+'</div>\n'
  119.         if (this.items[i].hasImage) this.div += '<div id="'+this.name+'ListItemImgLyr'+i+'"><img name="'+this.name+'ListItemImg'+i+'" src="'+this.image.image0.src+'" width='+this.image.w+' height='+this.image.h+'></div>\n'
  120.         this.div += '<div id="'+this.name+'ListItemC'+i+'"></div>\n'
  121.     }
  122.     this.div += '</div>'
  123. }
  124. function ListActivate() {
  125.     if (is.ie) this.h -= 1001
  126.     this.lyr = new DynLayer(this.name+'List')
  127.     this.lyr.clipInit()
  128.  
  129.     for (var i=0;i<this.items.length;i++) {
  130.         this.items[i].lyr = new DynLayer(this.name+'ListItem'+i)
  131.         this.items[i].lyr.setbg = DynLayerSetbg
  132.         this.items[i].lyre = new DynLayer(this.name+'ListItemC'+i)
  133.         if (is.ns) this.items[i].lyre.event.captureEvents(Event.MOUSEDOWN)
  134.         this.items[i].lyre.event.onmouseover = new Function(this.obj+'.over('+i+'); return false;')
  135.         this.items[i].lyre.event.onmouseout = new Function(this.obj+'.out('+i+'); return false;')
  136.         this.items[i].lyre.event.onmousedown = new Function(this.obj+'.down('+i+'); return false;')
  137.         if (!this.itemHset) {
  138.             this.itemH = (is.ns)? this.items[0].lyr.doc.height : this.items[0].lyr.event.offsetHeight
  139.             this.items[i].lyr.moveTo(null,i*this.itemH+this.itemSpacing*i)
  140.             this.items[i].lyre.moveTo(null,i*this.itemH+this.itemSpacing*i)
  141.             if (is.ns) {
  142.                 this.items[i].lyr.clipInit()
  143.                 this.items[i].lyr.clipTo(0,this.w,this.itemH,0)
  144.                 this.items[i].lyre.clipInit()
  145.                 this.items[i].lyre.clipTo(0,this.w,this.itemH,0)
  146.             }
  147.             this.h += this.itemH+this.itemSpacing
  148.         }
  149.         if (this.items[i].hasImage) {
  150.             this.items[i].imagelyr = new DynLayer(this.name+'ListItemImgLyr'+i)
  151.         }
  152.     }
  153.     if (!this.itemHset) {
  154.         this.lyr.clipTo(0,this.w,this.h,0)
  155.         if (is.ie) this.lyr.css.height = this.h
  156.     }
  157.     if (this.preSelect!=null) this.select(this.preSelect)
  158.     this.lyr.css.visibility = this.visibility
  159. }
  160. function ListOver(i) {
  161.     if (!this.items[i].selected) {
  162.        this.items[i].lyr.setbg(this.color.bgRollover)
  163.     }
  164.     if (this.overOpen && i!=this.selectedIndex) {
  165.         this.menulist.hideMenu()
  166.         this.deselect(this.selectedIndex)
  167.         if (this.items[i].hasChild) this.select(i)
  168.     }
  169. }
  170. function ListOut(i) {
  171.     if (!this.items[i].selected) this.items[i].lyr.setbg(this.color.bgNormal)
  172. }
  173. function ListDown(i) {
  174.     if (!this.items[i].selected) {
  175.         if (!this.multiSelect && this.selectedIndex!=null) this.deselect(this.selectedIndex)
  176.         this.select(i)        
  177.     }
  178.     else {
  179.         if (this.multiSelect || this.allowDeselect) {
  180.             this.menulist.hideMenu()
  181.             this.deselect(i)
  182.         }
  183.     }
  184. }
  185. function ListSelect(i) {
  186.     if (this.items[i]!=null) {    
  187.         this.selectedIndex = i
  188.         this.value = this.items[i].value
  189.         if (this.items[i].hasImage) this.items[i].imagelyr.doc.images[this.name+'ListItemImg'+i].src = this.image.image1.src
  190.         this.items[i].lyr.setbg(this.color.bgSelected)
  191.         this.items[i].lyr.write(this.items[i].textSelected)
  192.         this.items[i].selected = true
  193.         this.onSelect()
  194.     }
  195. }
  196.  
  197. function ListDeselect(i) {
  198.     if (this.items[i]!=null) {
  199.        if (this.items[i].selected) {
  200.         if (this.items[i].hasImage) this.items[i].imagelyr.doc.images[this.name+'ListItemImg'+i].src = this.image.image0.src
  201.         this.items[i].lyr.setbg(this.color.bgNormal)
  202.         this.items[i].lyr.write(this.items[i].textNormal)
  203.         this.items[i].selected = false
  204.         if (!this.multiSelect) this.selectedIndex = null
  205.        }
  206.     }
  207. }
  208. function ListRedirect() {
  209.     location.href = this.value
  210. }
  211. List.count = 0
  212.  
  213. // Dynlayer setbg() required
  214. function DynLayerSetbg(color) {
  215.     if (is.ns) this.doc.bgColor = color
  216.     else if (is.ie) this.css.backgroundColor = color
  217. }
  218.  
  219.