2008-08-28 16:48:45 +02:00
|
|
|
/* -*- Mode: java; tab-width: 2; c-tab-always-indent: t; indent-tabs-mode: t; c-basic-offset: 2 -*- */
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
/* custom extensions to the DOM api */
|
|
|
|
Element.addMethods(
|
|
|
|
{
|
|
|
|
addInterface: function(element, objectInterface) {
|
|
|
|
element = $(element);
|
|
|
|
Object.extend(element, objectInterface);
|
|
|
|
if (element.bind)
|
|
|
|
element.bind();
|
|
|
|
},
|
|
|
|
|
|
|
|
childNodesWithTag: function(element, tagName) {
|
|
|
|
element = $(element);
|
|
|
|
|
|
|
|
var matchingNodes = new Array();
|
|
|
|
var tagName = tagName.toUpperCase();
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
for (var i = 0; i < element.childNodes.length; i++) {
|
|
|
|
var childNode = $(element.childNodes[i]);
|
|
|
|
if (Object.isElement(childNode)
|
|
|
|
&& childNode.tagName
|
|
|
|
&& childNode.tagName.toUpperCase() == tagName)
|
|
|
|
matchingNodes.push(childNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchingNodes;
|
|
|
|
},
|
|
|
|
|
|
|
|
getParentWithTagName: function(element, tagName) {
|
|
|
|
element = $(element);
|
|
|
|
var currentElement = element;
|
|
|
|
tagName = tagName.toUpperCase();
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
while (currentElement
|
|
|
|
&& currentElement.tagName != tagName) {
|
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
return currentElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
cascadeLeftOffset: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
var currentElement = element;
|
|
|
|
|
|
|
|
var offset = 0;
|
|
|
|
while (currentElement) {
|
|
|
|
offset += currentElement.offsetLeft;
|
|
|
|
currentElement = $(currentElement).getParentWithTagName("div");
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
},
|
|
|
|
|
|
|
|
cascadeTopOffset: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
var currentElement = element;
|
|
|
|
var offset = 0;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
var i = 0;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
while (currentElement && currentElement.tagName) {
|
|
|
|
offset += currentElement.offsetTop;
|
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
i++;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
return offset;
|
|
|
|
},
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
dump: function(element, additionalInfo, additionalKeys) {
|
|
|
|
element = $(element);
|
|
|
|
var id = element.getAttribute("id");
|
|
|
|
var nclass = element.getAttribute("class");
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
var str = element.tagName;
|
|
|
|
if (id)
|
|
|
|
str += "; id = " + id;
|
|
|
|
if (nclass)
|
|
|
|
str += "; class = " + nclass;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
if (additionalInfo)
|
|
|
|
str += "; " + additionalInfo;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
if (additionalKeys)
|
|
|
|
for (var i = 0; i < additionalKeys.length; i++) {
|
|
|
|
var value = element.getAttribute(additionalKeys[i]);
|
|
|
|
if (value)
|
|
|
|
str += "; " + additionalKeys[i] + " = " + value;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
log (str);
|
|
|
|
},
|
2006-11-01 21:28:35 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
getSelectedNodes: function(element) {
|
|
|
|
element = $(element);
|
2009-02-27 20:42:44 +01:00
|
|
|
|
|
|
|
if (!element.selectedElements)
|
|
|
|
element.selectedElements = new Array();
|
|
|
|
|
|
|
|
return element.selectedElements;
|
2008-08-28 16:48:45 +02:00
|
|
|
},
|
2006-11-01 21:28:35 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
getSelectedNodesId: function(element) {
|
|
|
|
element = $(element);
|
2009-02-27 20:42:44 +01:00
|
|
|
|
|
|
|
var selArray = new Array();
|
|
|
|
if (element.selectedElements) {
|
|
|
|
for (var i = 0; i < element.selectedElements.length; i++) {
|
|
|
|
var node = element.selectedElements[i];
|
|
|
|
selArray.push(node.getAttribute("id"));
|
|
|
|
}
|
2008-08-28 16:48:45 +02:00
|
|
|
}
|
2009-02-27 20:42:44 +01:00
|
|
|
else
|
|
|
|
element.selectedElements = new Array();
|
2008-08-28 16:48:45 +02:00
|
|
|
|
|
|
|
return selArray;
|
|
|
|
},
|
|
|
|
|
|
|
|
onContextMenu: function(element, event) {
|
|
|
|
element = $(element);
|
|
|
|
|
|
|
|
if (document.currentPopupMenu)
|
|
|
|
hideMenu(document.currentPopupMenu);
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-02-27 20:42:44 +01:00
|
|
|
var popup = element.sogoContextMenu;
|
2008-08-28 16:48:45 +02:00
|
|
|
var menuTop = Event.pointerY(event);
|
|
|
|
var menuLeft = Event.pointerX(event);
|
|
|
|
var heightDiff = (window.height()
|
|
|
|
- (menuTop + popup.offsetHeight));
|
|
|
|
if (heightDiff < 0)
|
|
|
|
menuTop += heightDiff;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
var leftDiff = (window.width()
|
|
|
|
- (menuLeft + popup.offsetWidth));
|
|
|
|
if (leftDiff < 0)
|
|
|
|
menuLeft -= popup.offsetWidth;
|
2007-06-02 00:35:36 +02:00
|
|
|
|
2009-06-02 21:45:07 +02:00
|
|
|
var isVisible = true;
|
2008-08-28 16:48:45 +02:00
|
|
|
if (popup.prepareVisibility)
|
2009-06-02 21:45:07 +02:00
|
|
|
isVisible = popup.prepareVisibility();
|
|
|
|
|
|
|
|
if (isVisible) {
|
|
|
|
popup.setStyle( { top: menuTop + "px",
|
|
|
|
left: menuLeft + "px",
|
|
|
|
visibility: "visible" } );
|
|
|
|
|
|
|
|
document.currentPopupMenu = popup;
|
|
|
|
document.body.observe("click", onBodyClickMenuHandler);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log ("Warning: not showing the contextual menu " + element.id);
|
2008-08-28 16:48:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
attachMenu: function(element, menuName) {
|
|
|
|
element = $(element);
|
|
|
|
element.sogoContextMenu = $(menuName);
|
|
|
|
element.observe("contextmenu",
|
|
|
|
element.onContextMenu.bindAsEventListener(element));
|
|
|
|
},
|
|
|
|
|
|
|
|
selectElement: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
element.addClassName('_selected');
|
2009-02-27 20:42:44 +01:00
|
|
|
|
|
|
|
var parent = element.up();
|
|
|
|
if (!parent.selectedElements)
|
|
|
|
// Selected nodes are kept in a array at the
|
|
|
|
// container level.
|
|
|
|
parent.selectedElements = new Array();
|
|
|
|
for (var i = 0; i < parent.selectedElements.length; i++)
|
|
|
|
if (parent.selectedElements[i] == element) return;
|
|
|
|
parent.selectedElements.push(element); // use index instead ?
|
2008-08-28 16:48:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
selectRange: function(element, startIndex, endIndex) {
|
|
|
|
element = $(element);
|
|
|
|
var s;
|
|
|
|
var e;
|
|
|
|
var rows;
|
|
|
|
|
|
|
|
if (startIndex > endIndex) {
|
|
|
|
s = endIndex;
|
|
|
|
e = startIndex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s = startIndex;
|
|
|
|
e = endIndex;
|
|
|
|
}
|
|
|
|
if (element.tagName == 'UL')
|
|
|
|
rows = element.getElementsByTagName('LI');
|
|
|
|
else
|
|
|
|
rows = element.getElementsByTagName('TR');
|
|
|
|
while (s <= e) {
|
|
|
|
if (rows[s].nodeType == 1)
|
|
|
|
$(rows[s]).selectElement();
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deselect: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
element.removeClassName('_selected');
|
2009-02-27 20:42:44 +01:00
|
|
|
|
|
|
|
var parent = element.up();
|
2009-03-17 20:48:19 +01:00
|
|
|
if (parent && parent.selectedElements)
|
2009-02-27 20:42:44 +01:00
|
|
|
parent.selectedElements = parent.selectedElements.without(element);
|
|
|
|
},
|
2008-08-28 16:48:45 +02:00
|
|
|
|
|
|
|
deselectAll: function(element) {
|
|
|
|
element = $(element);
|
2009-02-27 20:42:44 +01:00
|
|
|
if (element.selectedElements) {
|
|
|
|
for (var i = 0; i < element.selectedElements.length; i++)
|
|
|
|
element.selectedElements[i].removeClassName('_selected');
|
|
|
|
element.selectedElements = null;
|
2008-08-28 16:48:45 +02:00
|
|
|
}
|
2009-02-27 20:42:44 +01:00
|
|
|
},
|
2008-08-28 16:48:45 +02:00
|
|
|
|
2009-06-16 22:29:25 +02:00
|
|
|
setCaretTo: function(element, pos) {
|
|
|
|
element = $(element);
|
|
|
|
if (element.setSelectionRange) { // For Mozilla and Safari
|
|
|
|
element.focus();
|
2008-08-28 16:48:45 +02:00
|
|
|
element.setSelectionRange(pos, pos);
|
|
|
|
}
|
|
|
|
else if (element.createTextRange) { // For IE
|
2009-06-16 22:29:25 +02:00
|
|
|
var range = element.createTextRange();
|
2008-08-28 16:48:45 +02:00
|
|
|
range.move("character", pos);
|
|
|
|
range.select();
|
2009-06-16 22:29:25 +02:00
|
|
|
}
|
2008-08-28 16:48:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
selectText: function(element, start, end) {
|
|
|
|
element = $(element);
|
|
|
|
if (element.setSelectionRange) { // For Mozilla and Safari
|
|
|
|
element.setSelectionRange(start, end);
|
|
|
|
}
|
|
|
|
else if (element.createTextRange) { // For IE
|
|
|
|
var textRange = element.createTextRange();
|
|
|
|
textRange.moveStart("character", start);
|
|
|
|
textRange.moveEnd("character", end-element.value.length);
|
|
|
|
textRange.select();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
element.select();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getRadioValue: function(element, radioName) {
|
|
|
|
element = $(element);
|
|
|
|
var radioValue;
|
|
|
|
Form.getInputs(element, 'radio', radioName).each(function(input) {
|
|
|
|
if (input.checked)
|
|
|
|
radioValue = input.value;
|
|
|
|
});
|
|
|
|
return radioValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
setRadioValue: function(element, radioName, value) {
|
|
|
|
element = $(element);
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
Form.getInputs(element, 'radio', radioName).each(function(input) {
|
|
|
|
if (i == value)
|
|
|
|
input.checked = 1;
|
|
|
|
i++;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getCheckBoxListValues: function(element, checkboxName) {
|
|
|
|
element = $(element);
|
|
|
|
var values = new Array();
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
Form.getInputs(element, 'checkbox', checkboxName).each(function(input) {
|
|
|
|
if (input.checked)
|
|
|
|
values.push(i+1);
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
i++;
|
|
|
|
});
|
|
|
|
return values.join(",");
|
|
|
|
},
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
setCheckBoxListValues: function(element, checkboxName, values) {
|
|
|
|
element = $(element);
|
|
|
|
var v = values.split(',');
|
|
|
|
var i = 1;
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
Form.getInputs(element, 'checkbox', checkboxName).each(function(input) {
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
if ($(v).indexOf(i+"") != -1)
|
|
|
|
input.checked = 1;
|
|
|
|
i++;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|