PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Documentation/Help/scripts/ElementCollection.js

#
JavaScript | 91 lines | 71 code | 14 blank | 6 comment | 39 complexity | 9a0f0ad4e0149ada894265fd76f45f54 MD5 | raw file
Possible License(s): MIT
  1. // a collection of elements whoose style can be changed based on the values of attributes
  2. function ElementCollection(parentId) {
  3. // get the tabbed region
  4. this.parent = document.getElementById(parentId);
  5. // get the children
  6. this.elements = new Array();
  7. if (this.parent.tagName == 'TABLE') {
  8. // special handling for tables
  9. var bodies = this.parent.tBodies;
  10. for(i=0; i<bodies.length; i++) {
  11. var rows = bodies[i].rows;
  12. for(j=0; j<rows.length; j++) {
  13. if (rows[j].nodeType == 1) this.elements.push(rows[j]);
  14. }
  15. }
  16. // this.elements = this.parent.tBodies[0].rows;
  17. } else {
  18. // all other cases
  19. var nodes = this.parent.childNodes;
  20. for(i=0; i<nodes.length; i++) {
  21. if (nodes[i].nodeType == 1) this.elements.push(nodes[i]);
  22. }
  23. }
  24. }
  25. ElementCollection.prototype.process = function(processFunction) {
  26. for(var i=0; i<this.elements.length; i++) {
  27. var element = this.elements[i];
  28. processFunction(element);
  29. }
  30. }
  31. ElementCollection.prototype.changeStyle = function(attributeName, attributeValue, styleName, styleValue) {
  32. for(var i=0; i<this.elements.length; i++) {
  33. var element = this.elements[i];
  34. var value = element.getAttribute(attributeName);
  35. if (value != null) {
  36. if (value == attributeValue) {
  37. element.style[styleName] = styleValue;
  38. }
  39. }
  40. }
  41. }
  42. ElementCollection.prototype.toggleStyle = function(attributeName, attributeValue, styleName, trueStyleValue, falseStyleValue) {
  43. for(var i=0; i<this.elements.length; i++) {
  44. var element = this.elements[i];
  45. if (element.nodeType != 1) continue;
  46. var value = element.getAttribute(attributeName);
  47. if (value == null) continue;
  48. if (value == attributeValue) {
  49. element.style[styleName] = trueStyleValue;
  50. } else {
  51. element.style[styleName] = falseStyleValue;
  52. }
  53. }
  54. }
  55. ElementCollection.prototype.toggleClass = function(attributeName, attributeValue, trueClass, falseClass) {
  56. for(var i=0; i<this.elements.length; i++) {
  57. var element = this.elements[i];
  58. if (element.nodeType != 1) continue;
  59. var value = element.getAttribute(attributeName);
  60. if (value == null) continue;
  61. if (value == attributeValue) {
  62. element.className = trueClass;
  63. } else {
  64. element.className = falseClass;
  65. }
  66. }
  67. }
  68. function useShowAttribute(element) {
  69. if (element == null) return;
  70. var value = element.getAttribute("show");
  71. if (value == null) return;
  72. if (value == "true") {
  73. element.style["display"] = "block";
  74. } else {
  75. element.style["display"] = "none";
  76. }
  77. }