// a generic tool for doing list selection, passing elements between two divs 'left' and 'right' // the styling needs to be cleaned up a bit, making some options more configurable and others less function listSelector(left, right, globalName){ var name; var wrapper; var leftDiv, rightDiv, leftTitle, rightTitle, leftColours, rightColours; var items; var styleTags, boxStyle, itemStyle, rowStyle; var leftValidator = null; var rightValidator = null; this.name = globalName; this.leftColours = []; this.rightColours = []; this.rowStyle = "height:1.2em; margin-top:1px; width:100%;"; this.styleTags = { 'height' : '100%', }; this.boxStyle = { 'position' : 'absolute', 'width' : '100%', 'height' : '100%', 'line-height' : '1.2em', 'text-align' : 'left' }; this.boxBackdropStyle = { 'position' : 'absolute', 'width' : '40%', 'height' : '90%', 'background' : '#FFF', 'border' : 'solid 1px', 'overflow' : 'hidden' } this.itemStyle = { 'margin-top' : '1px', 'height':'1.2em', 'width':'100%' }; this.headerStyle = { 'width' : '50%', 'position' : 'absolute', 'text-align' : 'center', 'font-size' : '120%', 'font-weight' : 'bold' }; this.highlightStyle = { 'background-color' : '#25BEFF' }; this.unhighlightStyle = { 'background' : 'none' }; this.drawLeftColumn = function(){ this.leftBackdrop = $('
'); this.leftBackdrop.css(this.boxBackdropStyle); this.leftBackdrop.css({ 'top' : '5%', 'left' : '5%', }); this.leftBackdrop.appendTo(this.wrapper); this.leftHighlight = $('
'); this.leftHighlight.css(this.boxStyle); this.leftHighlight.css({'overflow':'auto'}); this.leftHighlight.appendTo(this.leftBackdrop); this.leftDiv = $('
'); this.leftDiv.css(this.boxStyle); this.leftDiv.appendTo(this.leftHighlight); if(this.leftTitle != undefined){ var pos = this.leftDiv.position(); var caption = $('
' + this.leftTitle + '
'); caption.appendTo(this.wrapper); caption.css(this.headerStyle); caption.css({ 'left' : pos.left + 'px', 'top' : (pos.top - 15) + 'px' }); } // if any highlights were added, place them in the column now var m = 0; for(var n in this.leftColours){ while(m < n){ d = $('
'); d.css(this.itemStyle); this.leftHighlight.append(d); m++; } d = $('
'); d.css(this.itemStyle); this.leftHighlight.append(d); m = 1.0 * n + 1; } } this.drawRightColumn = function(){ this.rightBackdrop = $('
'); this.rightBackdrop.css(this.boxBackdropStyle); this.rightBackdrop.css({ 'top' : '5%', 'left' : '55%', }); this.rightBackdrop.appendTo(this.wrapper); this.rightHighlight = $('
'); this.rightHighlight.css(this.boxStyle); this.rightHighlight.css({'overflow':'auto'}); this.rightHighlight.appendTo(this.rightBackdrop); this.rightDiv = $('
'); this.rightDiv.css(this.boxStyle); this.rightDiv.appendTo(this.rightHighlight); if(this.rightTitle != undefined){ var pos = this.rightDiv.position(); var caption = $('
' + this.rightTitle + '
'); caption.appendTo(this.wrapper); caption.css(this.headerStyle); caption.css({ 'left' : '50%', 'top' : (pos.top - 15) + 'px' }); } // if any highlights were added, place them in the column now var m = 0, d; for(var n in this.rightColours){ while(m < n){ d = $('
'); d.css(this.itemStyle); this.rightHighlight.append(d); m++; } d = $('
'); d.css(this.itemStyle); this.rightHighlight.append(d); m = 1.0 * n + 1; } } this.draw = function(){ this.wrapper = $('
'); this.wrapper.css(this.styleTags); // create the columns this.drawLeftColumn(); this.drawRightColumn(); // build our list of selectable items in each column this.items = []; for(id in left){ this.items[id] = $('
' + left[id] + '
'); eval(this.name + '.items[' + id + '].click(function(){' + this.name + '.switch(' + id + ');});'); eval(this.name + '.items[' + id + '].mouseenter(function(){' + this.name + '.highlight(' + id + ', true); document.body.style.cursor="pointer";});'); eval(this.name + '.items[' + id + '].mouseleave(function(){' + this.name + '.highlight(' + id + ', false); document.body.style.cursor="default";});'); this.items[id].css(this.itemStyle); this.items[id].appendTo(this.leftDiv); this.items[id].data('column', 'left'); } for(id in right){ this.items[id] = $('
' + right[id] + '
'); eval(this.name + '.items[' + id + '].click(function(){' + this.name + '.switch(' + id + ');});'); eval(this.name + '.items[' + id + '].mouseenter(function(){' + this.name + '.highlight(' + id + ', true); document.body.style.cursor="pointer";});'); eval(this.name + '.items[' + id + '].mouseleave(function(){' + this.name + '.highlight(' + id + ', false); document.body.style.cursor="default";});'); this.items[id].css(this.itemStyle); this.items[id].appendTo(this.rightDiv); this.items[id].data('column', 'right'); } return this.wrapper; } this.highlight = function(itemId, on){ if(on){ this.items[itemId].css(this.highlightStyle); }else{ this.items[itemId].css(this.unhighlightStyle); } } this.switch = function(itemId){ item = this.items[itemId]; if(item.data('column') == 'left'){ if(this.rightValidator == null || this.rightValidator(itemId)){ item.data('column', 'right'); this.items[itemId].appendTo(this.rightDiv); } }else{ if(this.leftValidator == null || this.leftValidator(itemId)){ item.data('column', 'left'); this.items[itemId].appendTo(this.leftDiv); } } this.highlight(itemId, false); } this.setStyle = function(newTags){ for(var n in newTags){ this.styleTags[n] = newTags[n]; } } this.getLeftList = function(){ return this.getList('left'); } this.getRightList = function(){ return this.getList('right'); } this.getList = function(side){ var returnval = []; var n = 0; for(id in this.items){ if(this.items[id].data('column') == side){ returnval[n] = id; n++; } } return returnval; } }