Revision 647147 of Template:MakeColumnsForDL

  • Revision slug: Template:MakeColumnsForDL
  • Revision title: Template:MakeColumnsForDL
  • Revision id: 647147
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Rewritten to avoid cases of splitting in the middle of the "<dt>" tag

Revision Content

<% // Outputs a two-column view for the input HTML, which must be a
// list. // // Parameters: // // $0 The HTML containing a
to turn into a two column menu. var html = $0; var len = html.length; // Remove the surrounding
- we will do our own in each column. html = html.replace(/]*>|<\/dl>$/g, ""); if (len) { var items = html.split("
<%-col1%>
<%-col2%>
<% } else { %>

There are no subpages at this time.

<% } %>

Revision Source

<%
// Outputs a two-column view for the input HTML, which must be a <dl>
// list.
//
// Parameters:
//
//  $0  The HTML containing a <dl> to turn into a two column menu.

var html = $0;
var len = html.length;

// Remove the surrounding <dl> - we will do our own in each column.

html = html.replace(/<dl[^>]*>|<\/dl>$/g, "");

if (len) {
    var items = html.split("<dt ");
    items.shift();  // The first item is empty here since the string should start with "<dt"
    var sum = 0;
    var lengths = items.map(function(v) {
        sum += v.length;
        return v.length;
    });
        
    var n = 0;
    var ts = 0;
    
    // Figure out how many items go in the first column. If there are only
    // two items in the list, this is always 1, to be sure both columns
    // have content.
    
    if (items.length == 2) {
        n = 1;
    } else {
        while (ts < sum/2) {
            ts += lengths[n++];
        }
    }
    
    var col1 = "<dt " + items.slice(0, n).join("<dt ");
    var col2 = "<dt " + items.slice(n, items.length).join("<dt ");
    
    %>
    <div class="row topicpage-table">
    <div class="section"><dl><%-col1%></dl></div>
    <div class="section"><dl><%-col2%></dl></div>
    </div>
    <%
} else {
    %><p><strong>There are no subpages at this time.</strong></p><%
}
%>