1 of 1 found this page helpful

SectionUI: Transform the UI of sections on a page (tab and accordion styles)

    Please discuss this template in this forum thread.

    Introduction

    This template is designed to change the UI of sections contained on the page.  Presently, both "tab" and "accordion" styles are supported (similar to the Tab/Accordion template), but others are possible in the future if desired.

    The key concept here (and where this is different from others) is that the sections are entered and edited directly on this page (in fact, the tab sections are editable right inside the tabs.)  So this is not dependent on pulling content from other pages, or complex template calls.  As the name indicates, it takes sections on this page and changes the way they're presented.

    History

    Version Date Authors Description
    1.00 24-Sept-2009 neilw, rberinger  Initial release
    1.10 11-Nov-2009 neilw, rberinger
    • Added "accordion" style
    • Improved FOUC performance
    • Updated docs to require 9.08
    1.1.1 12-Nov-2009 neilw Slight tweaks to accordion "options" behavior
    1.1.2 12-Nov-2009 neilw Minor code cleanup
    1.1.3 8-Feb-2010 rberinger Move resource files to script.mindtouch.com
    1.1.4 22-March-2010 neilw Fixed a critical typo that crept in somehow
    1.1.5 7-Dec-2011 neilw Support SectionUI of top-level headings
    1.2.0 8-Dec-2011 neilw Cleaned up stylesheets and fixed editor behavior
    1.2.1 17-May-2012 neilw Fixed support of top-level headings in deuce lighty-blue

    Known bugs/to do

    • Behavior of the "edit" buttons on non-active tabs is not quite right when event:'mouseover' is used.  This is a thorny problem, so for now it'll stay as-is.
    • Edit button behavior on "accordion" style is not cosmetically perfect, but it works fine.

    Requirements

    • This template requires MindTouch version 9.08 (Minneopa) or later

    How do I install it?

    NOTE: You must have UNSAFECONTENT permissions to install this template successfully.

    These instructions will help you install the template'SectionUI' on your wiki quickly and correctly.

    Perform the following steps:

    1. Copy the template source code:
      1. If the code in the "HTML Source" area below is not already visible and selected, click the "View" button to display and select it.
      2. Copy it to your clipboard (CTRL-C).
    2. Create a new template on your wiki:
      1. Select the Tools->Templates menu item on your wiki
      2. Click "New Page" button. This will open the editor on a new page with the title "Template:Page Title".
      3. Replace "Page Title" with the name of the template.
    3. Paste in the source code:
      1. In the editor, click the "View->Source" menu item to view the HTML source of the page.
      2. Select all text (CTRL-A).
      3. Paste in the template source code from the clipboard (CTRL-V).
    4. Click "Save" button.

    HTML Source for Template:SectionUI

    A quick note about the examples on this page

    For all the examples on this page, the code is shown before the working example.  The code is shown with the syntax extension, and looks like this:

    SectionUI{
        style: "tab", 
        maxLength: 20
    }

    This means that the actual code on the page should be enclosed in a DekiScript block.  If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above. 

    How do I use it?

    The basic approach here is very simple.  Call this template directly under a section heading.  The UI transformation will then be applied to all headings inside that section, that are one heading lower.  In other words, if I call this template right under a Heading 1, then all the Heading 2s inside that section will be transformed.

    Arguments

    Create the list of arguments according to the table below.

    Name Type Default Description
    style str? "tab" Style of UI.  At present, "tab" and "accordion" are the only valid options.
    allowEdits bool? true Grant the ability to users with write access to directly edit sections within the tab panels.
    showOriginal bool? false Provide a link to reload the page and display in its original form.
    maxLength num?  0 Max text length for the tab name after it will be truncated, and appended with "...".  0 -> no limit
    maxHeight num? "0" Max tab container height, above which the region will be scrolled.  0 -> no limit
    textSize str? "1em" Text size of tab titles.  Any valid text size css: i.e. 1em, 10px, 20%, etc.
    options map? {} Various options specific to each style (see below)

    Style Options

    Following are the available settings for the "options" argument, for each style type.  Each option is a map element.

    "Tab"
    Name Description
    collapsible Set to true to allow an already selected tab to become unselected again upon reselection.
    event valid option is mouseover (example: event: 'mouseover')
    selected Zero-based index of the tab to be selected on initialization. To set all tabs to unselected pass -1 as value.

     

     

    "Accordion"

    Complete details on the "accordion" style may be found on the jQuery UI page.  Below are the most useful options for use with this template:

    Name Description
    active Zero-based index of the section to be opened on initialization.  Set to false to begin with none open (requires collapsible=true)
    autoheight set to true to maintain a fixed height for the accordion, equal to the size of the largest section.
    collapsible Set to true to allow an already selected tab to become unselected again upon reselection.
    event valid option is mouseover (example: event: 'mouseover')

     

    Examples

    Style="tab"

    In a DekiScript block, put this code directly under a heading:

    sectionUI{
     maxHeight:200,
     maxLength:15,
     textsize:".8em",
     showOriginal:true
    }
    
    Top heading (level 4)
    Explanation of this example

    In this case, the template is called right under "Top Heading (level 3)", which as indicated is a Heading level 3.  The tabs are created from all the level 4 headings inside this section.  To see what the original page looks like, click the "show original" link in the upper right corner of the tab.

    Note that each tab can be edited: this is the same as editing the section on the page.  If you edit a tab and add a new heading, then when you save the tabs will be updated with the new structure.

    Simple Text

    This is some simple content for a tab.

    A very long section that will scroll

    this

    section

    is

    very

    very

    long,

    but

    we're

    going

    to

    scroll

    it

    so

    the

    tab

    region

    doesn't

    get

    too

    big

    A table!
    this is
    a table
    isn't

    it?

       

    Style="accordion"

    Here's the same example, this time rendered as a fixed-height accordion.  In a DekiScript block, put this code directly under a heading:

    sectionUI{
     style:"accordion",
     maxHeight:200,
     textsize:"1.25em",
     showOriginal:true,
     options:{ autoheight:true }
    }
    
    Top heading (level 3)
    Explanation of this example

    In this case, the template is called right under "Top Heading (level 3)", which as indicated is a Heading level 3.  The tabs are created from all the level 4 headings inside this section.  To see what the original page looks like, click the "show original" link in the upper right corner of the tab.

    Note that each tab can be edited: this is the same as editing the section on the page.  If you edit a tab and add a new heading, then when you save the tabs will be updated with the new structure.

    Simple Text

    This is some simple content for a tab.

    A very long section that will scroll

    this

    section

    is

    very

    very

    long,

    but

    we're

    going

    to

    scroll

    it

    so

    the

    tab

    region

    doesn't

    get

    too

    big

    A table!
    this is
    a table
    isn't

    it?

       

    Reference(s)

    Credits/Special Thanks

    Special Thanks to the jQuery and jQuery-ui teams for their wonderful framework.
    Thanks goes to crb for challenging neilw to collapse the headers in a page.

    Template Source Code

    For your viewing pleasure only.  Please use the installation instructions above to install the template.

    // SectionUI, by neilw and rberinger, 2009
    //
    // Version History
    //     1.0.0    24-Sept-2009    neilw, rberinger        First release
    //     1.1.0    ??-????-2009    neilw, rberinger        Added support for "Accordion" style
    //     1.1.1    12-Nov-2009     neilw                   Slight tweak for accordion options
    //     1.1.2    12-Nov-2009     neilw                   A bit of code cleanup
    //     1.1.3    8-Feb-2010      rberinger               Moved resource files to script.mindtouch.com
    //     1.1.4    22-March-2010   neilw                   Fixed typo that crept in somehow
    //     1.1.5    7-Dec-2011      neilw                   Properly handle level 1 headings
    //     1.2.0    8-Dec-2011      neilw                   Cleaned up stylesheets and fixed editing
    //     1.2.1    17-May-2012     neilw                   Handle level 1 headings in Deuce lighty-blue
    //
    // Usage:
    //    SectionUI {
    //        style: str?,            (default "tab")  Style of UI for sections.  Valid options: "tab", "accordion".
    //        allowEdits: bool?,      (default true)   Allow editing of subsections if user has permission
    //        showOriginal: bool?,    (default false)  Provide link to reload page with original version of page
    //        maxLength: num?,        (default 0)      Maximum length of tab title after which title will be truncated.  "0" -> no limit
    //        maxHeight: str?,        (default "0")    Maximum height of a tab section, after which it will be scrolled.  "0" -> no limit
    //        textSize: str?,         (default "1em")  Size of text in tab titles
    //        options: map?           (default {})     Options specific to each style
    //    }
    
    // Parameters
    var errors = [];
    
    // Style
    var style = $0 ?? $style ?? "tab";
    let style = string.tolower(style);
    var allowable_styles = [ "tab", "accordion" ];
    if (!list.contains(allowable_styles, style)) {
        let errors ..= [ "invalid style '"..style.."': supported styles are "..
            string.join(allowable_styles, ",") ];
        let style = "tab";
    }
    // allowEdits
    var allow_edits = $1 ?? $allowEdits ?? true;
    if (allow_edits is not bool) {
        let errors ..= [ "allowEdits parameter must be true or false" ];
        let allow_edits = true;
    }
    // showOriginal
    var show_original = $2 ?? $showOriginal ?? false;
    // maxLength
    var max_length = $3 ?? $maxLength ?? 0;
    if (max_length is not num || max_length < 0) {
        let errors ..= [ "maxLength parameter must be a non-negative number" ];
        let max_length = 0;
    }
    // maxHeight
    var max_height = $4 ?? $maxHeight ?? 0;
    // textSize
    var text_size = $5 ?? $textSize ?? '';
    // options
    var options = $6 ?? $options;
    if (options is not nil && options is not map) {
        let errors ..= [ "options parameter must be a map" ];
        let options = nil;
    }
    if (style == "tab") {
        let options = options ?? { collapsible:true };
        if (options.event == "mouseover")            // bad to use "mouseover" with "collapsible"
            let options ..= { collapsible:false };
    }
    else if (style == "accordion") {
        let options = options ?? {};
        if (options.autoHeight == nil) let options ..= { autoHeight:false };
        if (options.active == nil) let options ..= { active:false };
    }
    
    if ( __request.args.SectionUI == false)
        <p><a href=(page.uri & { sectionUI:true })> "Show transformed page" </a></p>;
    else {
    <html>
    <head>
    <link type="text/css" href="http://scripts.mindtouch.com/res/jqueryui/ui.core.css" rel="stylesheet" />
    <link type="text/css" href="http://scripts.mindtouch.com/res/jqueryui/ui.theme.css" rel="stylesheet" />
    style == "tab" ?
        <link type="text/css" href="http://scripts.mindtouch.com/res/jqueryui/ui.tabs.css" rel="stylesheet" /> :
        <link type="text/css" href="http://scripts.mindtouch.com/res/jqueryui/ui.accordion.css" rel="stylesheet" />;
    
    <script type="text/javascript" src="http://scripts.mindtouch.com/res/jqueryui/ui.core.js.txt"></script>
    style == "tab" ?
        <script type="text/javascript" src="http://scripts.mindtouch.com/res/jqueryui/ui.tabs.js.txt"></script> :
        <script type="text/javascript" src="http://scripts.mindtouch.com/res/jqueryui/ui.accordion.js.txt"></script>;
    
    // Hide entire page content frame until SectionUI does its thing.  Optimal if at most 1 SectionUI is used on a page.
    <style type="text/css"> " .pageContentFrame { display:none; } " </style>
    
    // Call the main SectionUI function
    <script type="text/javascript"> "
    Deki.$(document).ready(function($) {
        var id = "..json.emit(@id)..";
        var $section = $('div#'+id).parent();
        $('div#'+id).remove();
        transform_sections($, id, $section,
            "..json.emit(style)..",
            "..json.emit(max_length)..",
            "..json.emit(max_height)..",
            "..json.emit(text_size)..",
            "..json.emit(allow_edits)..",
            "..json.emit(options)..");
    
        $('div.pageContentFrame').show();
    });
    " </script>
    // Main SectionUI function.  This does all the work in mangling the HTML to enable the transformation
    <script type="text/javascript"> "
    function transform_sections($, id, $section, style, max_length, max_height, font_size, editable, options) {
    // Figure out what header level we're at (should be simpler way to do this???)
        var isTop = $section.attr('id') == 'pageText';
        var $outer = $section.find(':header').eq(0);
        if ($outer.length == 0) return;
        var inner = 'h' + ((isTop?0:1) + Number($outer.get(0).tagName.substr(1,1)));
        if (isTop) $outer = $outer.parent().prev();
    // Process options as necessary
        if (font_size != '') font_size = 'font-size:'+font_size;
    // First wrap entire top level section in a div
        var top_id = id + 'top';
        $outer.nextAll().wrapAll('<div id=\"'+top_id+'\"></div>');
    // If 'tab', create a UL for tab names
        if (style == 'tab') $section.find('div#'+top_id).prepend('<ul></ul>');
        var $ul = $section.find('ul:first');
    // Now process each of the subsections
        $section.find(inner).each(function() {
            $(this).hide();
            var $div = $(this).parent();
        // clean up extra vertical whitespace
            $div.css({ 'padding-top':'5px', 'padding-bottom':'10px' });
            $(this).nextAll(':first').css({'margin-top':'0px','padding-top':'0px'});
            $(this).nextAll(':last').css({'margin-bottom':'0px','padding-bottom':'0px'});
        // set max_height of each section
            if (max_height)
                $div.css({'max-height':max_height, 'overflow':'auto'});
        // truncate title text if necessary
            var title_text = $(this).text();
            if (max_length && title_text.length > max_length) title_text = title_text.substr(0,max_length) + '...';
        // Process the section title according to style
            var hdr_link = '<a style=\"'+font_size+';text-decoration:none;\" href=\"#'+$div.attr('id')+'\" title=\"'+title_text+'\">' + title_text +
                '<span class=\"icon\" style=\"visibility:hidden; margin-left:10px\">' +
                '<img class=\"sectionedit\" alt=\"Edit section\" src=\"/skins/common/icons/icon-trans.gif\" style=\"cursor:pointer\"/></span></a>';
            switch(style) {
                case 'tab':        // add tab title to <ul>
                    $ul.append('<li style=\"list-style-type:none\">' + hdr_link + '</li>');
                    break;
                case 'accordion':  // insert title before <div>
                    $div.before('<div class=\"accordion-header\">' + hdr_link + '</div>');                 break;
                default: break;
            }           
        // Add pop-up 'edit' icon, if enabled
            if (editable && $(this).find('div.editIcon').length) {
                var $header = (style == 'tab' ? $ul.find('li:last') :
                                                $section.find('div.accordion-header:last'));
                if (console) console.log('setting hover for edit icon');
                $header.hover(
                    function() {
                        if (!Deki.Editor || (!Deki.Editor.IsLoading && !Deki.Editor.IsStarted))
                             $(this).find('span').css('visibility','visible');
                    },
                    function() { $(this).find('span:last').css('visibility','hidden'); }
                );
                var $tab = $header.find('a:first');
                var activeClass = (style == 'tab' ? 'ui-tabs-selected' : 'ui-state-active');
                $header.find('img:last').click(function() {
                    if (!$header.hasClass(activeClass)) $tab.click();
                    $div.find(inner+' a:last').click();
                    return(false);
                });
            }
        });
    // Call appropriate styler
        switch(style) {
            case 'tab':
                $('#'+top_id).tabs(options);
                break;
            case 'accordion':
                options.header = 'div.accordion-header';
                $('#'+top_id).accordion(options);
                break;
            default: break;
        }
    }
    " </script>;
    </head>
    <body>
    <div id=(@id) />;            // Marker
    <div id=(@id.."original")    // "Show Original" link
        style=("width:100%;text-align:right;font-size:.7em;visibility:"..(show_original?"visible":"hidden"))>
        <a href=(page.uri&{SectionUI:false})> (show_original ? "show original" : ".") </a>
    </div>;
    if (#errors) {
        <p style="color:red">
            "ERRORS:"; <br />;
            foreach (var e in errors) { e; <br />; }
        </p>
    }
    </body>
    </html>
    }

    Disclaimers

    Was this page helpful?
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by