expand.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. define(["options", "localization", "jquery"], function (options, i18n, $) {
  2. var selectors = {
  3. /* Selectors for the nodes that will contain an expand/collapse button. */
  4. "expand_buttons": [
  5. /* Table caption */
  6. "table > caption:not(:empty)",
  7. /* Article title */
  8. ".topic > .title",
  9. /* Section title. Exclude task labels generated by DITA-OT (EXM-37958 & EXM-38501).*/
  10. ".sectiontitle:not(.tasklabel)",
  11. /* Index terms groups */
  12. ".wh_term_group > .wh_first_letter"
  13. ],
  14. /* WH-1613 - permalink selector */
  15. "permalinks": [
  16. ".dt[id]",
  17. ".section[id] .sectiontitle",
  18. ".title.topictitle2[id]",
  19. "table[id] .tablecap"
  20. ]
  21. };
  22. var expandInitialState = options.get("webhelp.topic.collapsible.elements.initial.state");
  23. /**
  24. * Add expand-collapse support.
  25. */
  26. $(document).ready(function () {
  27. /* Add the expand/collapse buttons. */
  28. selectors.expand_buttons.forEach(
  29. function (selector) {
  30. var matchedNodes = $(document).find(selector);
  31. // Add the expand/collapse support only if the title node has visible siblings.
  32. var visibleSiblings = matchedNodes.siblings(':not(:hidden)');
  33. if (visibleSiblings.length > 0) {
  34. // Add the element with expand/collapse capabilities
  35. matchedNodes.prepend(
  36. $("<span>", {
  37. "class": "wh_expand_btn expanded",
  38. "role": "button",
  39. "aria-expanded" : "true",
  40. "tabindex" : 0,
  41. "aria-label" : i18n.getLocalization("collapse")
  42. })
  43. );
  44. markHiddenSiblingsAsNotExpandable(matchedNodes);
  45. }
  46. }
  47. );
  48. /*
  49. * WH-1613
  50. * Add the permalink icons
  51. */
  52. selectors.permalinks.forEach(
  53. function (selector) {
  54. var matchedNodes = $(document).find(selector);
  55. // Add the element for the permalink action
  56. matchedNodes.append("<span class='permalink'/>");
  57. }
  58. );
  59. /*
  60. * Slide down when click on a letter from the indexterms bar
  61. * */
  62. $('.wh-letters a').click(function (e) {
  63. var id = $(this).attr('href').replace("#", "");
  64. e.preventDefault();
  65. history.replaceState({}, '', e.target.href);
  66. if ($("[id='" + id + "']").length > 0) {
  67. $('html, body').animate({scrollTop: $("[id='" + id + "']").offset().top}, 1000);
  68. }
  69. });
  70. /*
  71. * WH-1613
  72. * Permalink action
  73. * */
  74. $('span.permalink').click(function (e) {
  75. var id = $(this).closest('[id]').attr('id');
  76. var hash = '#' + id;
  77. e.preventDefault();
  78. history.replaceState({}, '', hash);
  79. $('html, body').animate({scrollTop: $("[id='" + id + "']").offset().top}, 1000);
  80. });
  81. /* Expand / collapse subtopic sections */
  82. function toggleSubtopics(state) {
  83. var siblings = $(this).parent().siblings(':not(.wh_not_expandable)');
  84. if (state !== undefined) {
  85. // Will expand-collapse the siblings of the parent node, excepting the ones that were marked otherwise
  86. if (state == 'collapsed') {
  87. siblings.slideUp(0);
  88. $('.webhelp_expand_collapse_sections').attr('data-next-state', 'expanded').attr('title', i18n.getLocalization('expandSections'));
  89. $(this).removeClass('expanded');
  90. $(this).attr('aria-expanded', false);
  91. $(this).attr('aria-label', i18n.getLocalization('expand'));
  92. } else {
  93. siblings.slideDown(0);
  94. $('.webhelp_expand_collapse_sections').attr('data-next-state', 'collapsed').attr('title', i18n.getLocalization('collapseSections'));
  95. $(this).addClass('expanded');
  96. $(this).attr('aria-expanded', true);
  97. $(this).attr('aria-label', i18n.getLocalization("collapse"));
  98. }
  99. } else {
  100. // Change the button state
  101. $(this).toggleClass("expanded");
  102. var isExpanded = $(this).hasClass("expanded");
  103. $(this).attr('aria-expanded', isExpanded);
  104. if (isExpanded) {
  105. $(this).attr('aria-label', i18n.getLocalization("collapse"));
  106. } else {
  107. $(this).attr('aria-label', i18n.getLocalization('expand'));
  108. }
  109. var parent = $(this).parent();
  110. var tagName = parent.prop("tagName");
  111. // Will expand-collapse the siblings of the parent node, excepting the ones that were marked otherwise
  112. if (tagName == "CAPTION" || parent.hasClass('wh_first_letter')) {
  113. // The table does not have display:block, so it will not slide.
  114. // In this case we'll just hide it
  115. siblings.toggle();
  116. } else {
  117. siblings.slideToggle("1000");
  118. }
  119. }
  120. }
  121. /*
  122. * WH-235
  123. * Sets the initial state of collapsible elements
  124. */
  125. $.each($(document).find('.wh_expand_btn'), function () {
  126. toggleSubtopics.call(this, expandInitialState);
  127. });
  128. /*
  129. * Toggle the subtopic sections
  130. */
  131. $('.webhelp_expand_collapse_sections').click(function () {
  132. var state = $('.webhelp_expand_collapse_sections').attr('data-next-state');
  133. $.each($(document).find('.wh_expand_btn'), function () {
  134. toggleSubtopics.call(this, state);
  135. });
  136. return false;
  137. });
  138. /*
  139. * WH-1750 - Handle topic TOC expand/collapse actions
  140. */
  141. $('.wh_topic_toc a').click(function () {
  142. var currentNode = $(this).attr("href");
  143. var contentNode = $(currentNode);
  144. if(contentNode.length){
  145. $.each(contentNode.parents(), function () {
  146. if ($(this).children(".title").length) {
  147. toggleSubtopics.call($(this).children('.title').find('.wh_expand_btn'),'expanded');
  148. }
  149. });
  150. toggleSubtopics.call(contentNode.children('.title').find('.wh_expand_btn'),'expanded');
  151. }
  152. });
  153. /* Expand / collapse support for the marked content */
  154. var expandWidgets = $(document).find('.wh_expand_btn');
  155. expandWidgets.click(function (event) {
  156. toggleSubtopics.call(this);
  157. return false;
  158. });
  159. /* Toggle expand/collapse on enter and space */
  160. expandWidgets.keypress(function( event ) {
  161. // Enter & Spacebar events
  162. if ( event.which === 13 || event.which === 32) {
  163. event.preventDefault();
  164. toggleSubtopics.call(this);
  165. }
  166. });
  167. });
  168. /**
  169. * Marks the hidden siblings of the matched nodes as being not expandable.
  170. *
  171. * @param nodes The matched nodes.
  172. */
  173. function markHiddenSiblingsAsNotExpandable(nodes) {
  174. var siblings = nodes.siblings(":hidden");
  175. siblings.addClass("wh_not_expandable");
  176. }
  177. });