2009-07-19 18:41:42 +02:00
|
|
|
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2008-08-28 16:48:45 +02:00
|
|
|
/* custom extensions to the DOM api */
|
2009-07-28 20:36:06 +02:00
|
|
|
Element.addMethods({
|
|
|
|
addInterface: function(element, objectInterface) {
|
|
|
|
element = $(element);
|
|
|
|
Object.extend(element, objectInterface);
|
|
|
|
if (element.bind)
|
|
|
|
element.bind();
|
|
|
|
},
|
2010-08-23 22:58:41 +02:00
|
|
|
|
|
|
|
allTextContent: function(element) {
|
|
|
|
var content = "";
|
|
|
|
for (var i = 0; i < element.childNodes.length; i++) {
|
|
|
|
var node = $(element.childNodes[i]);
|
|
|
|
if (node.nodeType == Node.TEXT_NODE) {
|
|
|
|
content += node.nodeValue;
|
|
|
|
}
|
|
|
|
else if (node.nodeType == Node.ELEMENT_NODE) {
|
|
|
|
content += Element.allTextContent(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
childNodesWithTag: function(element, tagName) {
|
|
|
|
element = $(element);
|
|
|
|
|
|
|
|
var matchingNodes = new Array();
|
2010-08-03 20:53:34 +02:00
|
|
|
tagName = tagName.toUpperCase();
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
while (currentElement
|
|
|
|
&& currentElement.tagName != tagName) {
|
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
var i = 0;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
while (currentElement && currentElement.tagName) {
|
|
|
|
offset += currentElement.offsetTop;
|
|
|
|
currentElement = currentElement.parentNode;
|
|
|
|
i++;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
return offset;
|
|
|
|
},
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
var str = element.tagName;
|
|
|
|
if (id)
|
|
|
|
str += "; id = " + id;
|
|
|
|
if (nclass)
|
|
|
|
str += "; class = " + nclass;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
if (additionalInfo)
|
|
|
|
str += "; " + additionalInfo;
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
if (additionalKeys)
|
|
|
|
for (var i = 0; i < additionalKeys.length; i++) {
|
2010-02-11 03:36:43 +01:00
|
|
|
var value = element.readAttribute(additionalKeys[i]);
|
2009-07-28 20:36:06 +02:00
|
|
|
if (value)
|
|
|
|
str += "; " + additionalKeys[i] + " = " + value;
|
|
|
|
}
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
log (str);
|
|
|
|
},
|
|
|
|
|
|
|
|
getSelectedNodes: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
|
|
|
|
if (!element.selectedElements)
|
|
|
|
element.selectedElements = new Array();
|
|
|
|
|
|
|
|
return element.selectedElements;
|
|
|
|
},
|
|
|
|
|
|
|
|
getSelectedNodesId: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
|
2010-08-26 16:14:10 +02:00
|
|
|
var selArray = null;
|
|
|
|
if (element.selectedIds) {
|
|
|
|
selArray = element.selectedIds;
|
|
|
|
}
|
2010-10-11 22:02:24 +02:00
|
|
|
else {
|
|
|
|
selArray = [];
|
|
|
|
if (element.selectedElements) {
|
|
|
|
for (var i = 0; i < element.selectedElements.length; i++) {
|
|
|
|
var node = element.selectedElements[i];
|
|
|
|
selArray.push(node.getAttribute("id"));
|
|
|
|
}
|
2009-07-28 20:36:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selArray;
|
|
|
|
},
|
|
|
|
|
|
|
|
onContextMenu: function(element, event) {
|
|
|
|
element = $(element);
|
|
|
|
Event.stop(event);
|
|
|
|
if (document.currentPopupMenu)
|
|
|
|
hideMenu(document.currentPopupMenu);
|
2007-05-28 18:12:06 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
var popup = element.sogoContextMenu;
|
|
|
|
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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
var leftDiff = (window.width()
|
|
|
|
- (menuLeft + popup.offsetWidth));
|
|
|
|
if (leftDiff < 0)
|
|
|
|
menuLeft -= popup.offsetWidth;
|
|
|
|
|
|
|
|
var isVisible = true;
|
|
|
|
if (popup.prepareVisibility)
|
|
|
|
isVisible = popup.prepareVisibility();
|
|
|
|
|
|
|
|
if (isVisible) {
|
|
|
|
popup.setStyle( { top: menuTop + "px",
|
|
|
|
left: menuLeft + "px",
|
|
|
|
visibility: "visible" } );
|
2009-06-02 21:45:07 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
document.currentPopupMenu = popup;
|
|
|
|
document.body.observe("click", onBodyClickMenuHandler);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log ("Warning: not showing the contextual menu " + element.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
var parent = element.up();
|
2010-08-26 16:14:10 +02:00
|
|
|
if (!parent.selectedElements || !parent.selectedIds) {
|
2009-07-28 20:36:06 +02:00
|
|
|
// Selected nodes are kept in a array at the
|
|
|
|
// container level.
|
|
|
|
parent.selectedElements = new Array();
|
2010-05-27 16:41:59 +02:00
|
|
|
parent.selectedIds = new Array();
|
|
|
|
}
|
2009-07-28 20:36:06 +02:00
|
|
|
for (var i = 0; i < parent.selectedElements.length; i++)
|
|
|
|
if (parent.selectedElements[i] == element) return;
|
2010-08-26 16:14:10 +02:00
|
|
|
for (var i = 0; i < parent.selectedIds.length; i++)
|
|
|
|
if (parent.selectedIds[i] == element.id) return;
|
2009-07-28 20:36:06 +02:00
|
|
|
parent.selectedElements.push(element); // use index instead ?
|
2010-05-27 16:41:59 +02:00
|
|
|
parent.selectedIds.push(element.id);
|
2009-07-28 20:36:06 +02:00
|
|
|
},
|
2010-05-27 16:41:59 +02:00
|
|
|
|
2009-07-28 20:36:06 +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++;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-07-30 22:10:00 +02:00
|
|
|
selectAll: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
if (element.tagName == 'UL')
|
|
|
|
rows = element.getElementsByTagName('LI');
|
|
|
|
else
|
|
|
|
rows = element.select('TBODY TR');
|
|
|
|
for (var i = 0; i < rows.length; i++)
|
|
|
|
if (rows[i].nodeType == 1)
|
|
|
|
$(rows[i]).selectElement();
|
2010-08-26 16:14:10 +02:00
|
|
|
},
|
2010-07-30 22:10:00 +02:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
deselect: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
element.removeClassName('_selected');
|
2010-11-03 21:23:49 +01:00
|
|
|
var parent = element.parentNode;
|
|
|
|
if (parent) {
|
|
|
|
if (parent.selectedElements)
|
|
|
|
parent.selectedElements = parent.selectedElements.without(element);
|
|
|
|
if (parent.selectedIds)
|
|
|
|
parent.selectedIds = parent.selectedIds.without(element.id);
|
2010-05-27 16:41:59 +02:00
|
|
|
}
|
2009-07-28 20:36:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
deselectAll: function(element) {
|
|
|
|
element = $(element);
|
2010-10-13 19:45:01 +02:00
|
|
|
if (element.tagName == 'TABLE') {
|
2010-12-03 23:09:18 +01:00
|
|
|
var tbody = element.tBodies[0];
|
2010-10-13 19:45:01 +02:00
|
|
|
if (tbody)
|
|
|
|
element = tbody;
|
|
|
|
}
|
2010-08-26 16:14:10 +02:00
|
|
|
var s = element.select("._selected");
|
|
|
|
for (var i = 0; i < s.length; i++)
|
|
|
|
s[i].removeClassName("_selected");
|
|
|
|
element.selectedElements = null;
|
|
|
|
element.selectedIds = null;
|
2010-05-27 16:41:59 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshSelectionByIds: function(element) {
|
|
|
|
element = $(element);
|
|
|
|
if (element.selectedIds) {
|
|
|
|
for (var i = 0; i < element.selectedIds.length; i++) {
|
|
|
|
var e = element.down('#'+element.selectedIds[i]);
|
2010-12-03 23:09:18 +01:00
|
|
|
if (e) {
|
|
|
|
if (!e.hasClassName('_selected'))
|
|
|
|
e.addClassName('_selected');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log ("refreshSelectionByIds Error: " + element.tagName
|
|
|
|
+ " select by ID " + element.selectedIds[i]
|
|
|
|
+ " not found (" + element.childNodes.length + " children)");
|
2011-01-21 22:46:27 +01:00
|
|
|
//element.selectedIds.splice(i,1);
|
2010-12-03 23:09:18 +01:00
|
|
|
}
|
2010-05-27 16:41:59 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-28 20:36:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setCaretTo: function(element, pos) {
|
|
|
|
element = $(element);
|
|
|
|
if (element.setSelectionRange) { // For Mozilla and Safari
|
|
|
|
element.focus();
|
|
|
|
element.setSelectionRange(pos, pos);
|
|
|
|
}
|
|
|
|
else if (element.createTextRange) { // For IE
|
|
|
|
var range = element.createTextRange();
|
|
|
|
range.move("character", pos);
|
|
|
|
range.select();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
i++;
|
|
|
|
});
|
|
|
|
return values.join(",");
|
|
|
|
},
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
setCheckBoxListValues: function(element, checkboxName, values) {
|
|
|
|
element = $(element);
|
|
|
|
var v = values.split(',');
|
|
|
|
var i = 1;
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
Form.getInputs(element, 'checkbox', checkboxName).each(function(input) {
|
2008-01-14 14:57:28 +01:00
|
|
|
|
2009-07-28 20:36:06 +02:00
|
|
|
if ($(v).indexOf(i+"") != -1)
|
|
|
|
input.checked = 1;
|
|
|
|
i++;
|
|
|
|
});
|
|
|
|
}
|
2010-07-07 17:37:29 +02:00
|
|
|
});
|