[10672] | 1 | /********************************************************************** |
---|
| 2 | * (C) British Crown Copyright 2006-17 Met Office. |
---|
| 3 | * |
---|
| 4 | * This file is part of FCM. |
---|
| 5 | * |
---|
| 6 | * FCM is free software: you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * FCM is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | * |
---|
| 19 | **********************************************************************/ |
---|
| 20 | |
---|
| 21 | FCM = {}; |
---|
| 22 | $(function() { |
---|
| 23 | var TITLE_COLLAPSE = "collapse"; |
---|
| 24 | var TITLE_EXPAND = "expand"; |
---|
| 25 | var IS_MAIN = true; |
---|
| 26 | |
---|
| 27 | // Toggle a collapse/expand image. |
---|
| 28 | function collapse_expand_icon_toggle(anchor) { |
---|
| 29 | var icon = $("span", anchor); |
---|
| 30 | if (icon.attr("title") == TITLE_EXPAND) { |
---|
| 31 | icon.attr("title", TITLE_COLLAPSE); |
---|
| 32 | icon.removeClass("glyphicon-triangle-right"); |
---|
| 33 | icon.addClass("glyphicon-triangle-bottom"); |
---|
| 34 | anchor.siblings().filter("ul").show(); |
---|
| 35 | } |
---|
| 36 | else { // if (icon.attr("title") == "collapse") |
---|
| 37 | icon.attr("title", TITLE_EXPAND); |
---|
| 38 | icon.removeClass("glyphicon-triangle-bottom"); |
---|
| 39 | icon.addClass("glyphicon-triangle-right"); |
---|
| 40 | anchor.siblings().filter("ul").hide(); |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | // Add collapse/expand anchor to a ul tree. |
---|
| 45 | function ul_collapse_expand(ul, is_main) { |
---|
| 46 | var nodes = $("li", ul); |
---|
| 47 | nodes.each(function(i) { |
---|
| 48 | var li = $(this); |
---|
| 49 | var li_anchor = li.children().first(); |
---|
| 50 | if (!li_anchor.is("a")) { |
---|
| 51 | return; |
---|
| 52 | } |
---|
| 53 | var li_ul = $("> ul", li); |
---|
| 54 | li_ul.hide(); |
---|
| 55 | var icon = $("<span/>", {"title": TITLE_EXPAND, "aria-hidden": "true"}); |
---|
| 56 | icon.addClass("glyphicon"); |
---|
| 57 | icon.addClass("glyphicon-triangle-right"); |
---|
| 58 | icon.css("cursor", "pointer"); |
---|
| 59 | icon.css("opacity", 0.5); |
---|
| 60 | var anchor = $("<a/>").append(icon); |
---|
| 61 | li.prepend(" "); |
---|
| 62 | li.prepend(anchor); |
---|
| 63 | if (is_main) { |
---|
| 64 | anchor.click(function() { |
---|
| 65 | var href = li_anchor.attr("href"); |
---|
| 66 | $.get( |
---|
| 67 | href, |
---|
| 68 | function(data) { |
---|
| 69 | collapse_expand_icon_toggle(anchor); |
---|
| 70 | anchor.unbind("click"); |
---|
| 71 | if (content_gen(li, data, href)) { |
---|
| 72 | ul_collapse_expand(li.children().filter("ul")); |
---|
| 73 | anchor.click(function() { |
---|
| 74 | collapse_expand_icon_toggle(anchor); |
---|
| 75 | }); |
---|
| 76 | } |
---|
| 77 | else { |
---|
| 78 | icon.css("opacity", 0.1); |
---|
| 79 | } |
---|
| 80 | }, |
---|
| 81 | "html" |
---|
| 82 | ) |
---|
| 83 | .error(function(b) { |
---|
| 84 | alert(b); |
---|
| 85 | anchor.unbind("click"); |
---|
| 86 | icon.css("opacity", 0.1); |
---|
| 87 | }); |
---|
| 88 | }); |
---|
| 89 | } |
---|
| 90 | else if (li_ul.length) { |
---|
| 91 | anchor.click(function() { |
---|
| 92 | collapse_expand_icon_toggle(anchor); |
---|
| 93 | }); |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | icon.css("opacity", 0.1); |
---|
| 97 | } |
---|
| 98 | }); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // Generate table of content of a document. |
---|
| 102 | function content_gen(root, d, d_href) { |
---|
| 103 | if (d == null) { |
---|
| 104 | d = document; |
---|
| 105 | } |
---|
| 106 | var CONTENT_INDEX_OF = {"h2": 1, "h3": 2, "h4": 3, "h5": 4, "h6": 5}; |
---|
| 107 | var stack = []; |
---|
| 108 | var done_something = false; |
---|
| 109 | var headings = $("h2, h3, h4, h5, h6", $(d)); |
---|
| 110 | headings.each(function(i) { |
---|
| 111 | if (this.id == null || this.id == "") { |
---|
| 112 | return; |
---|
| 113 | } |
---|
| 114 | var tag_name = this.tagName.toLowerCase(); |
---|
| 115 | // Add to table of content |
---|
| 116 | while (CONTENT_INDEX_OF[tag_name] < stack.length) { |
---|
| 117 | stack.shift(); |
---|
| 118 | } |
---|
| 119 | while (stack.length < CONTENT_INDEX_OF[tag_name]) { |
---|
| 120 | var node = stack.length == 0 ? root : $("> :last-child", stack[0]); |
---|
| 121 | stack.unshift($("<ul/>").appendTo(node).addClass("list-unstyled")); |
---|
| 122 | } |
---|
| 123 | var href = "#" + this.id; |
---|
| 124 | if (d_href) { |
---|
| 125 | href = d_href + href; |
---|
| 126 | } |
---|
| 127 | var padding = ""; |
---|
| 128 | for (var i = 0; i < stack.length; i++) { |
---|
| 129 | padding += " "; |
---|
| 130 | } |
---|
| 131 | stack[0].append($("<li/>").html(padding).append( |
---|
| 132 | $("<a/>", {"href": href}).html($(this).text()) |
---|
| 133 | )); |
---|
| 134 | |
---|
| 135 | // Add a section link as well |
---|
| 136 | if (d == document) { |
---|
| 137 | var section_link_anchor = $("<a/>", {"href": "#" + this.id}); |
---|
| 138 | section_link_anchor.addClass("sectionlink"); |
---|
| 139 | section_link_anchor.append("\xb6"); |
---|
| 140 | $(this).append(section_link_anchor); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | done_something = true; |
---|
| 144 | }); |
---|
| 145 | return done_something; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | var NODE; |
---|
| 149 | |
---|
| 150 | // Top page table of content |
---|
| 151 | NODE = $(".fcm-top-content"); |
---|
| 152 | if (NODE) { |
---|
| 153 | ul_collapse_expand(NODE, IS_MAIN); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // Table of content |
---|
| 157 | NODE = $(".fcm-page-content"); |
---|
| 158 | if (NODE) { |
---|
| 159 | if (content_gen(NODE)) { |
---|
| 160 | ul_collapse_expand(NODE); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // Display version information |
---|
| 165 | NODE = $(".fcm-version"); |
---|
| 166 | if (NODE) { |
---|
| 167 | NODE.text("FCM " + FCM.VERSION); |
---|
| 168 | } |
---|
| 169 | }); |
---|