/*
* Objeto para armazenamento de itens da dropdown
*/
function DropDownMenuItem(onClickScript, text, myValue, altText) {
this.onClickScript = onClickScript;
this.text = text;
this.altText = altText;
this.style = "";
this.myValue = myValue;
this.mouseOutClass = "popupLineMouseOut";
this.mouseOverClass = "popupLineMouseOver";
}
function DropDownMenu(dropDownId, parentComponent) {
// Componente pai
this.parentComponent = parentComponent;
// Flag - indica se o browser é o Internet Explorer
this.IE = false;
if(document.all) {
this.IE = true;
}
// Indica se é para forçar absolute position no Dropdown
this.forceAbsolutePosition = false;
// Classe responsável pelo dropdown
this.menuClass = "popupMenu";
// Classe responsável pelo scroll
this.scrollPanelClass = "popupMenuScroll";
this.scrollPanel = null;
// Classe responsável pela div que armazena os itens do dropdown
this.itemsPanelClass = "popupMenuItems";
this.itemsPanel = null;
this.menuDiv = null;
this.id = dropDownId;
// Largura do scrollbar
this.scrollBarWidth = 13;
// Altura dos itens
this.itemHeight = 16;
this.itemPanelMinTop = 0;
if(this.parentComponent) {
// Larguda do dropdown
this.width = parseInt(this.parentComponent.style.width);
// Altura do dropdown
this.height = parseInt(this.parentComponent.style.height);
} else {
// Larguda do dropdown
this.width = 0;
// Altura do dropdown
this.height = 0;
}
// Posicao absoluta do topo do dropdown
this.top = 0;
// Posicao absoluta da esquerda do dropdown
this.left = 0;
// Cor da borda externa do dropdown
this.externalBordersColor = '#A7A7A7';
// Intervalo de tempo do scroll
this.timeInterval = 50;
// Flag - indica se as bordas externas do dropdown devem ser desenhadas
this.drawExternalBorders = true;
// Imagem de background
this.backImage = null;
// Style z-index para o Div em que vai ser desenhado o dropdown
this.zIndex = 0;
// Array que armazena itens da drop-down list
this.dropDownMenuItems = new Array();
/*
* Adiciona item da drop-down list
*/
function addDropDownMenuItem(dropDownMenuItem) {
this.dropDownMenuItems.push(dropDownMenuItem);
}
this.addDropDownMenuItem = addDropDownMenuItem;
function destroy() {
var myDiv = document.getElementById(this.id);
if(myDiv) {
document.body.removeChild(myDiv);
}
}
this.destroy = destroy;
/*
* Retorna true se menu está ativo
*/
function isActive() {
return document.getElementById(this.id) ? true:false;
}
this.isActive = isActive;
/*
* Desenha drop-down list
*/
function draw() {
if(this.dropDownMenuItems.length == 0) {
// Se não tem itens, nao desenha dropdown
return;
}
// Cria menu
this.menuDiv = document.createElement("div");
this.menuDiv.className = this.menuClass;
this.menuDiv.id = this.id;
this.menuDiv.name = this.id;
if(this.parentComponent && !this.forceAbsolutePosition) {
this.menuDiv.style.position = 'relative';
} else {
this.menuDiv.style.top = this.top;
this.menuDiv.style.left = this.left;
this.menuDiv.style.position = 'absolute';
}
try {
this.menuDiv.style.width = this.width;
}
catch(e) {}
try {
this.menuDiv.style.height = this.height;
}
catch(e) {}
try {
this.menuDiv.style.zIndex = this.zIndex;
}
catch(e) {}
// Cria div abaixo do painel esquerdo para mander o background
this.divBack = document.createElement("div");
this.menuDiv.appendChild(this.divBack);
this.divBack.style.height = this.height;
this.divBack.style.position = 'absolute';
this.divBack.style.top = 0;
this.divBack.style.backgroundColor = 'white';
this.divBack.style.backgroundRepeat = 'repeat-x';
if(this.backImage != null) {
this.divBack.style.backgroundImage = "url('"+this.backImage+"')";
}
// Cria painel esquerdo (itens)
this.itemsPanel = document.createElement("div");
this.itemsPanel.id = this.id + "ItemsDiv";
this.menuDiv.appendChild(this.itemsPanel);
this.itemsPanel.className = this.itemsPanelClass;
this.itemsPanel.style.position = 'absolute';
this.itemsPanel.style.clip='rect(0px auto '+this.height+'px auto)';
this.itemsPanel.style.top = 0;
this.itemsPanel.style.left = +1;
// Verifica necessidade de scroll
var hasScroll = false;
if((this.dropDownMenuItems.length * this.itemHeight) > parseInt(this.height)) {
// TEM SCROLL
hasScroll = true;
this.itemsPanel.style.width = parseInt(this.width) - this.scrollBarWidth;
this.divBack.style.width = parseInt(this.width) - this.scrollBarWidth;
if(this.drawExternalBorders) {
this.divBack.style.borderLeft = '1px solid '+this.externalBordersColor;
this.divBack.style.borderBottom = '1px solid '+this.externalBordersColor;
}
// Cria panel direito (scroll)
this.drawScroll();
}
else {
// NAO TEM SCROLL
hasScroll = false;
if(this.drawExternalBorders) {
this.divBack.style.borderLeft = '1px solid '+this.externalBordersColor
this.divBack.style.borderRight = '1px solid '+this.externalBordersColor
this.divBack.style.borderBottom = '1px solid '+this.externalBordersColor
}
if(this.IE) {
this.itemsPanel.style.width = parseInt(this.width) - 2;
this.divBack.style.width = this.width;
}
else {
this.itemsPanel.style.width = parseInt(this.width);
this.divBack.style.width = this.width-3;
}
/*
Esta alteração estava sumindo com a borda da direita no IE
if(document.all) {
this.menuDiv.style.width = parseInt(this.menuDiv.style.width) - 2;
}
*/
}
// Insere itens
var htmlItems = "";
var sbHtmlItems = new StringBuffer();
for(var i = 0; i < this.dropDownMenuItems.length; i++) {
var dropDownMenuItem = this.dropDownMenuItems[i];
var titleText = "";
if(dropDownMenuItem.altText) {
titleText = dropDownMenuItem.altText;
}
else {
titleText = dropDownMenuItem.text;
}
sbHtmlItems.append("
");
}
this.itemsPanel.innerHTML = sbHtmlItems.toString();
if(this.parentComponent) {
this.parentComponent.appendChild(this.menuDiv);
} else {
document.body.appendChild(this.menuDiv);
}
if(hasScroll) {
this.initScroll();
}
document.onmouseup = null;
}
this.draw = draw;
/*
* Desenha barra de rolagem
*/
function drawScroll() {
this.scrollPanel = document.createElement("div");
this.menuDiv.appendChild(this.scrollPanel);
this.scrollPanel.className = this.scrollPanelClass;
this.scrollPanel.style.width = this.scrollBarWidth;
this.scrollPanel.style.height = this.height;
this.scrollPanel.id = this.id+"ScrollPanel";
if(this.drawExternalBorders) {
this.scrollPanel.style.borderRight = '1px solid '+this.externalBordersColor
this.scrollPanel.style.borderBottom = '1px solid '+this.externalBordersColor
}
var scrollHTML = "";
scrollHTML += "
";
scrollHTML += " |
";
scrollHTML += " |
";
scrollHTML += "";
scrollHTML += " ";
scrollHTML += " |
";
scrollHTML += " |
";
scrollHTML += " |
";
scrollHTML += "
";
scrollHTML += "
";
this.scrollPanel.innerHTML = scrollHTML;
}
this.drawScroll = drawScroll;
/*
* SCROLL DRAG
*/
function startScrollDrag(evt, sender) {
this.isScrolling = true;
//document.body.style.overflow = "hidden";
posBeginDivScroll = parseInt(sender.style.top);
posBeginMouse = 0;
if(this.IE) {
posBeginMouse = event.clientY;
}
else {
posBeginMouse = evt.pageY
}
var minMax = getScrollBarScrollableAreaSize(sender.objRef);
var totalSize = minMax[1] - minMax[0];
var minTop = -5;
var maxTop = minTop + totalSize;
document.onmousemove = function(evt) {
scrollPosYOffset = 10;
posY = 0;
if(sender.objRef.IE) {
posY = event.clientY;
}
else {
posY = evt.pageY
}
var topo = posBeginDivScroll + posY - posBeginMouse;
var minMax = getScrollBarScrollableAreaSize(sender.objRef);
// Nao permite passar dos limites do scroll
if(topo < minTop) { topo = minTop;}
if(topo > maxTop) { topo = maxTop;}
// Atualiza posicao do botao de scroll
sender.style.top = topo;
var progress = (topo + 5) / totalSize;
var topContent = ((sender.objRef.itemsPanel.offsetHeight - sender.objRef.height) * progress);
sender.objRef.itemsPanel.style.top = 0 - topContent;
sender.objRef.itemsPanel.style.clip = 'rect(' + topContent + 'px auto '+ (topContent + sender.objRef.height)+'px auto)';
return false; // Retorna false para IE nao selecionar a imagem
}
document.onmouseup = this.stopScrollDrag;
}
this.startScrollDrag = startScrollDrag;
function stopScrollDrag() {
this.isScrolling = false;
document.onmousemove = null;
document.onmouseup = null;
//document.body.style.overflow = "auto";
}
this.stopScrollDrag = stopScrollDrag;
/*
* SCROLL UP
*/
function startScrollUp(senderId) {
this.isScrolling = true;
var objSender = document.getElementById(senderId);
doScrollUp(senderId);
}
this.startScrollUp = startScrollUp;
function doScrollUp(senderId) {
var objSender = document.getElementById(senderId);
if(objSender.objRef.isScrolling) {
var topBottom = objSender.objRef.getRectTopBottom(objSender.objRef.itemsPanel.style.clip);
if(Math.abs(parseInt(topBottom[0])) <= 0) {
return;
}
var move = 0;
if(parseInt(topBottom[0]) < objSender.objRef.itemHeight) {
move = Math.abs(parseInt(topBottom[0]));
}
else {
move = objSender.objRef.itemHeight;
}
objSender.objRef.doScroll(senderId, move);
var t = setTimeout("document.getElementById('"+senderId+"').objRef.doScrollUp('"+senderId+"');", objSender.objRef.timeInterval);
}
}
this.doScrollUp = doScrollUp;
function stopScrollUp() {
this.isScrolling = false;
}
this.stopScrollUp = stopScrollUp;
/*
* SCROLL DOWN
*/
function startScrollDown(senderId) {
this.isScrolling = true;
var objSender = document.getElementById(senderId);
doScrollDown(senderId);
}
this.startScrollDown = startScrollDown;
function doScrollDown(senderId) {
var objSender = document.getElementById(senderId);
if(objSender.objRef.isScrolling) {
if(objSender.objRef.dropDownMenuItems.length * objSender.objRef.itemHeight <= Math.abs(parseInt(objSender.objRef.itemsPanel.style.top)) + objSender.objRef.height) {
return;
}
objSender.objRef.doScroll(senderId, objSender.objRef.itemHeight * -1);
var t = setTimeout("document.getElementById('"+senderId+"').objRef.doScrollDown('"+senderId+"');", objSender.objRef.timeInterval);
}
}
this.doScrollDown = doScrollDown;
function stopScrollDown() {
this.isScrolling = false;
}
this.stopScrollDown = stopScrollDown;
/*
* SCROLL COMMONS
*/
function doScroll(senderId, pos) {
var objSender = document.getElementById(senderId);
objSender.objRef.itemsPanel.style.top = parseInt(objSender.objRef.itemsPanel.style.top) + pos;
objSender.objRef.itemsPanel.style.clip = 'rect(' + Math.abs(parseInt(objSender.objRef.itemsPanel.style.top)) + 'px auto '+(Math.abs(parseInt(objSender.objRef.itemsPanel.style.top)) + objSender.objRef.height)+'px auto)';
var scrollBtn = document.getElementById(objSender.objRef.id+"ScrollBtn");
var minMax = getScrollBarScrollableAreaSize(objSender.objRef);
// Calcula posição do botao de rolagem e o posiciona
var progress = (Math.abs(parseInt(objSender.objRef.itemsPanel.style.top)) / ((objSender.objRef.dropDownMenuItems.length * objSender.objRef.itemHeight)-objSender.objRef.height));
var totalSize = minMax[1] - minMax[0];
var posicao = (totalSize * progress);
var adjustSize = -5;
scrollBtn.style.top = adjustSize + (posicao>totalSize?totalSize:(totalSize * progress));
}
this.doScroll = doScroll;
function initScroll() {
document.getElementById(this.id+"ScrollBtnUp").objRef = this;
document.getElementById(this.id+"ScrollBtn").objRef = this;
document.getElementById(this.id+"ScrollBtnDown").objRef = this;
}
this.initScroll = initScroll;
/*
* Responsável pelo cálculo da area de scroll para drag and drop do botao de scroll
*
* Retorno: Array com: Index 0 => posicao minima / Index 1 => posicao maxima
*/
function getScrollBarScrollableAreaSize(objRef) {
var maxUp = findPosY(document.getElementById(objRef.id+"ScrollTop"));
var maxDown = findPosY(document.getElementById(objRef.id+"ScrollBottom"));
var scrollBtn = document.getElementById(objRef.id+"ScrollBtn");
maxDown = maxDown - parseInt(scrollBtn.height) + 5;
return [maxUp, maxDown];
}
this.getScrollBarScrollableAreaSize = getScrollBarScrollableAreaSize;
function getRectTopBottom(rectStr) {
var rectValues = rectStr.split(/\s|rect|\(|\)|px/);
if(document.all) {
return [rectValues[0], rectValues[2]];
} else {
return [rectValues[2], rectValues[7]];
}
}
this.getRectTopBottom = getRectTopBottom;
}