title-footer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /********************************************************
  2. * *
  3. * Javascript for the Title-Footer plugin for Reveal.js *
  4. * *
  5. * Author: Igor Leturia *
  6. * *
  7. * License: GPL v3 *
  8. * http://www.gnu.org/copyleft/gpl.html *
  9. * *
  10. ********************************************************/
  11. /* Title-Footer object and properties declaration with default values */
  12. var title_footer=
  13. {
  14. title: '',
  15. background:'rgba(0,0,0,0.1)',
  16. };
  17. /* Function to obtain all child elements with any of the indicated tags (from http://www.quirksmode.org/dom/getElementsByTagNames.html) */
  18. title_footer.getElementsByTagNames=function(list,obj)
  19. {
  20. if (!obj)
  21. {
  22. var obj=document;
  23. };
  24. var tagNames=list.split(',');
  25. var resultArray=new Array();
  26. for (var i=0;i<tagNames.length;i++)
  27. {
  28. var tags=obj.getElementsByTagName(tagNames[i]);
  29. for (var j=0;j<tags.length;j++)
  30. {
  31. resultArray.push(tags[j]);
  32. };
  33. };
  34. var testNode=resultArray[0];
  35. if (!testNode)
  36. {
  37. return [];
  38. };
  39. if (testNode.sourceIndex)
  40. {
  41. resultArray.sort(
  42. function (a,b)
  43. {
  44. return a.sourceIndex-b.sourceIndex;
  45. }
  46. );
  47. }
  48. else if (testNode.compareDocumentPosition)
  49. {
  50. resultArray.sort(
  51. function (a,b)
  52. {
  53. return 3-(a.compareDocumentPosition(b)&6);
  54. }
  55. );
  56. };
  57. return resultArray;
  58. };
  59. /* Method to initialize the Title-Footer footer */
  60. title_footer.initialize=function(title,background)
  61. {
  62. // Link to the Title-Footer CSS
  63. var link=document.createElement("link");
  64. link.href="plugin/title-footer/title-footer.css";
  65. link.type="text/css";
  66. link.rel="stylesheet";
  67. document.getElementsByTagName("head")[0].appendChild(link);
  68. // Initialize properties according to parameters
  69. this.background=background || 'rgba(0,0,0,0.1)';
  70. var title=title || '';
  71. if (title!='')
  72. {
  73. this.title=title;
  74. }
  75. else
  76. {
  77. var first_section=document.getElementsByTagName('section')[0];
  78. if (first_section.getElementsByTagName('section').length>0)
  79. {
  80. first_section=first_section.getElementsByTagName('section')[0];
  81. };
  82. var title_elements=this.getElementsByTagNames('h1,h2,h3',first_section);
  83. if (title_elements.length>0)
  84. {
  85. this.title=title_elements[0].textContent;
  86. for (var title_elements_index=1;title_elements_index<title_elements.length;title_elements_index++)
  87. {
  88. this.title=this.title+' - '+title_elements[title_elements_index].textContent;
  89. };
  90. };
  91. };
  92. // Create the Title-Footer footer
  93. var title_footer=document.createElement('footer');
  94. title_footer.setAttribute('id','title-footer');
  95. title_footer.setAttribute('style','background:'+this.background);
  96. var title_footer_p=document.createElement('p');
  97. title_footer.appendChild(title_footer_p);
  98. var a_element=document.createElement('a');
  99. a_element.setAttribute('href','#/0');
  100. a_element.appendChild(document.createTextNode(this.title));
  101. title_footer_p.appendChild(a_element);
  102. var div_class_reveal=document.querySelectorAll('.reveal')[0];
  103. div_class_reveal.appendChild(title_footer);
  104. };