Fixed bbode element toc for webpages

- At the moment webpages can contain empty headings if the textarea
"Title" 
is left empty in the webpage creator
(https://hostname/webpages/username)
This commit is contained in:
Einer von Vielen 2015-01-18 14:21:26 +01:00
parent 6657712714
commit 123ca1d403
2 changed files with 32 additions and 25 deletions

View File

@ -13,7 +13,10 @@
* or implied. See the License for the specific language governing permissions and limitations * or implied. See the License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
* The original script was modified to work within the red#martrix (added var pathname). * The original script was modified to work within the red#martrix
* - added var pathname
* - added var textHeading: Accept heading with text only
* Why? At the moment webpages can contain empty title using h3
*/ */
(function ($) { (function ($) {
@ -54,30 +57,34 @@
return elem.is(selector) ? index : undefined; return elem.is(selector) ? index : undefined;
})[0]; })[0];
if (level > currentLevel) { // Accept heading with text only
// If the heading is at a deeper level than where we are, start a new nested var textHeading = elem.text();
// list, but only if we already have some list items in the parent. If we do if(textHeading != '') {
// not, that means that we're skipping levels, so we can just add new list items if (level > currentLevel) {
// at the current level. // If the heading is at a deeper level than where we are, start a new nested
// In the upside-down stack, unshift = push, and stack[0] = the top. // list, but only if we already have some list items in the parent. If we do
var parentItem = stack[0].children("li:last")[0]; // not, that means that we're skipping levels, so we can just add new list items
if (parentItem) { // at the current level.
stack.unshift($("<" + listTag + "/>").appendTo(parentItem)); // In the upside-down stack, unshift = push, and stack[0] = the top.
var parentItem = stack[0].children("li:last")[0];
if (parentItem) {
stack.unshift($("<" + listTag + "/>").appendTo(parentItem));
}
} else {
// Truncate the stack to the current level by chopping off the 'top' of the
// stack. We also need to preserve at least one element in the stack - that is
// the containing element.
stack.splice(0, Math.min(currentLevel - level, Math.max(stack.length - 1, 0)));
} }
} else { // the variable pathname was added to the original script.
// Truncate the stack to the current level by chopping off the 'top' of the var pathname = window.location.pathname;
// stack. We also need to preserve at least one element in the stack - that is // Add the list item
// the containing element. $("<li/>").appendTo(stack[0]).append(
stack.splice(0, Math.min(currentLevel - level, Math.max(stack.length - 1, 0))); $("<a/>").text(elem.text()).attr("href", pathname + "#" + elem.attr("id"))
} );
// the variable pathname was added to the original script.
var pathname = window.location.pathname;
// Add the list item
$("<li/>").appendTo(stack[0]).append(
$("<a/>").text(elem.text()).attr("href", pathname + "#" + elem.attr("id"))
);
currentLevel = level; currentLevel = level;
}
}); });
}); });
}, old = $.fn.toc; }, old = $.fn.toc;

View File

@ -1125,7 +1125,7 @@ $(document).ready(function() {
$(".autotime").timeago(); $(".autotime").timeago();
$("#toc").toc({content: "div.page-body", headings: "h1,h2,h3,h4"}); $("#toc").toc({content: "body", headings: "h1,h2,h3,h4"});
}); });