Difference between revisions of "User:Aarnott/Lego Bin 14"

From Dungeons and Dragons Wiki
Jump to: navigation, search
m
m (Error Handling)
Line 5: Line 5:
 
Working prototype for user boxes below:
 
Working prototype for user boxes below:
  
 +
/**
 +
  * Find the user navbox, which is identified by the id #user_navbox,
 +
  * and append an [Expand] link to the header. The [Expand] link will
 +
  * load the navbox contents dynamically when the user clicks it.
 +
  *
 +
  */
 
  jQuery(document).ready(function($) {
 
  jQuery(document).ready(function($) {
 
     var $expandSpan = $("<span>");
 
     var $expandSpan = $("<span>");
 
     $expandSpan.addClass("mw-collapsible-toggle");
 
     $expandSpan.addClass("mw-collapsible-toggle");
 
   
 
   
     var $expandLink = $("<a>",
+
     var $expandLink = $("&lt;a&gt;",
 
         {
 
         {
 
             text: '[Expand]',
 
             text: '[Expand]',
Line 18: Line 24:
 
         }
 
         }
 
     );
 
     );
 
 
     $expandSpan.append($expandLink);
 
     $expandSpan.append($expandLink);
 
   
 
   
Line 25: Line 30:
 
   
 
   
 
  function loadUserNavboxContents() {
 
  function loadUserNavboxContents() {
     var $loadingSpinnerRow = $("<tr>");
+
    //Attach the spinner icon while the content loads
     var $loadingSpinnerCol = $("<td>");
+
     var $loadingSpinnerRow = $("&lt;tr&gt;");
 +
     var $loadingSpinnerCol = $("&lt;td&gt;");
 
     $loadingSpinnerCol.attr('colSpan', "" + 3 + "");
 
     $loadingSpinnerCol.attr('colSpan', "" + 3 + "");
 
     $loadingSpinnerCol.attr('style', 'text-align:left; vertical-align:middle;');
 
     $loadingSpinnerCol.attr('style', 'text-align:left; vertical-align:middle;');
 
   
 
   
     var $loadingSpinnerImage = $("<img>");
+
     var $loadingSpinnerImage = $("&lt;img&gt;");
 
     $loadingSpinnerImage.attr('src', 'http://dnd-wiki.org/w/images/4/44/Spinner.gif');
 
     $loadingSpinnerImage.attr('src', 'http://dnd-wiki.org/w/images/4/44/Spinner.gif');
 
   
 
   
Line 38: Line 44:
 
     $("#user_navbox").append($loadingSpinnerRow);
 
     $("#user_navbox").append($loadingSpinnerRow);
 
   
 
   
 +
    //The user navbox has an html data attribute called "data-author", which
 +
    //is used to store the author's name. This author name corresponds directly
 +
    //to a url for the author's fully opened navbox.
 
     var author = $("#user_navbox").attr('data-author');
 
     var author = $("#user_navbox").attr('data-author');
+
     var navboxPath = 'User:Aarnott/Lego_Bin_14/Navbox/' + author;
     $.get('/wiki/User:Aarnott/Lego_Bin_14/Navbox/'+author,
+
    var navboxUrl = '/wiki/' + navboxPath;
         function(data) {
+
    $.get(navboxUrl)
 +
         .done(function(data) {
 +
            //The user_navbox with the full content replaces the loader navbox
 
             var $html = $(data);
 
             var $html = $(data);
             $("#user_navbox").replaceWith($html.find("#user_navbox"));
+
             var navbox = $html.find("#user_navbox");
 +
            if(navbox.length > 0) {
 +
                $("#user_navbox").replaceWith(navbox[0]);
 +
            } else {
 +
                //Detach the spinner
 +
                $loadingSpinnerRow.detach();
 +
                //Error condition -- no navbox found
 +
                var $noNavboxRow = $("&lt;tr&gt;");
 +
                var $noNavboxCol = $("&lt;td&gt;");
 +
                $noNavboxCol.attr('colSpan', "" + 3 + "");
 +
                $noNavboxCol.append("No user navbox data was found. If this is your navbox, check"
 +
                    + " &lt;a href='"+navboxUrl+"'&gt;"+navboxPath+"&lt;a&gt; to see if it is"
 +
                    + " configured correctly. You can also message one of the"
 +
                    + "&lt;a href='/w/index.php?title=Special%3AListUsers&username=&group=sysop&limit=50'&gt;Administrators&lt;a&gt;"
 +
                    + " if you have any questions.");
 +
                $noNavboxRow.append($noNavboxCol);
 +
                $("#user_navbox").append($noNavboxRow);
 +
            }
 +
        })
 +
        .fail(function(jqxhr, textStatus, error) {
 +
            var errorMessage = textStatus + ", " + error;
 +
            //Detach the spinner
 +
            $loadingSpinnerRow.detach();
 +
            //Error condition -- no navbox found
 +
            var $failNavboxRow = $("&lt;tr&gt;");
 +
            var $failNavboxCol = $("&lt;td&gt;");
 +
            $failNavboxCol.attr('colSpan', "" + 3 + "");
 +
            $failNavboxCol.append("Failed to load the user's navbox: " + errorMessage);
 +
            $failNavboxRow.append($failNavboxCol);
 +
            $("#user_navbox").append($failNavboxRow);
 
         }
 
         }
 
     );  
 
     );  
Line 53: Line 93:
 
** The second part generates the same User NavBox we already use. This is [[User:Aarnott/Lego Bin 14/Navbox/User]].
 
** The second part generates the same User NavBox we already use. This is [[User:Aarnott/Lego Bin 14/Navbox/User]].
 
* We would be replacing the Navbox/User reference in [[Template:Navboxes‎]] with Navbox/User/Loader and with the added JS code, it should work.
 
* We would be replacing the Navbox/User reference in [[Template:Navboxes‎]] with Navbox/User/Loader and with the added JS code, it should work.
* I haven't written any exception handlers yet, but that would probably be a good idea...
 

Revision as of 15:02, 7 April 2014

Ghostwheel's Homebrew (310 Articles)
Ghostwheelv

Working prototype for user boxes below:

/**
 * Find the user navbox, which is identified by the id #user_navbox,
 * and append an [Expand] link to the header. The [Expand] link will
 * load the navbox contents dynamically when the user clicks it.
 *
 */
jQuery(document).ready(function($) {
    var $expandSpan = $("<span>");
    $expandSpan.addClass("mw-collapsible-toggle");

    var $expandLink = $("<a>",
        {
            text: '[Expand]',
            href: '#',
            click: function() {
                loadUserNavboxContents();
            }
        }
    );
    $expandSpan.append($expandLink);

    $("#user_navbox th").append($expandSpan);
});

function loadUserNavboxContents() {
    //Attach the spinner icon while the content loads
    var $loadingSpinnerRow = $("<tr>");
    var $loadingSpinnerCol = $("<td>");
    $loadingSpinnerCol.attr('colSpan', "" + 3 + "");
    $loadingSpinnerCol.attr('style', 'text-align:left; vertical-align:middle;');

    var $loadingSpinnerImage = $("<img>");
    $loadingSpinnerImage.attr('src', 'http://dnd-wiki.org/w/images/4/44/Spinner.gif');

    $loadingSpinnerCol.append($loadingSpinnerImage);
    $loadingSpinnerCol.append("  Loading...");    
    $loadingSpinnerRow.append($loadingSpinnerCol);
    $("#user_navbox").append($loadingSpinnerRow);

    //The user navbox has an html data attribute called "data-author", which
    //is used to store the author's name. This author name corresponds directly
    //to a url for the author's fully opened navbox.
    var author = $("#user_navbox").attr('data-author');
    var navboxPath = 'User:Aarnott/Lego_Bin_14/Navbox/' + author;
    var navboxUrl = '/wiki/' + navboxPath;
    $.get(navboxUrl)
        .done(function(data) {
            //The user_navbox with the full content replaces the loader navbox
            var $html = $(data);
            var navbox = $html.find("#user_navbox");
            if(navbox.length > 0) {
                $("#user_navbox").replaceWith(navbox[0]);
            } else {
                //Detach the spinner
                $loadingSpinnerRow.detach();
                //Error condition -- no navbox found
                var $noNavboxRow = $("<tr>");
                var $noNavboxCol = $("<td>");
                $noNavboxCol.attr('colSpan', "" + 3 + "");
                $noNavboxCol.append("No user navbox data was found. If this is your navbox, check"
                    + " <a href='"+navboxUrl+"'>"+navboxPath+"<a> to see if it is"
                    + " configured correctly. You can also message one of the"
                    + "<a href='/w/index.php?title=Special%3AListUsers&username=&group=sysop&limit=50'>Administrators<a>"
                    + " if you have any questions.");
                $noNavboxRow.append($noNavboxCol);
                $("#user_navbox").append($noNavboxRow);
            }
        })
        .fail(function(jqxhr, textStatus, error) {
            var errorMessage = textStatus + ", " + error;
            //Detach the spinner
            $loadingSpinnerRow.detach();
            //Error condition -- no navbox found
            var $failNavboxRow = $("<tr>");
            var $failNavboxCol = $("<td>");
            $failNavboxCol.attr('colSpan', "" + 3 + "");
            $failNavboxCol.append("Failed to load the user's navbox: " + errorMessage);
            $failNavboxRow.append($failNavboxCol);
            $("#user_navbox").append($failNavboxRow);
        }
    ); 
}
  • I modified the NavBox to have an optional id element, which is used so I can find the User's Navbox with JQuery more easily.
  • The User's Navbox is split into two parts.
  • We would be replacing the Navbox/User reference in Template:Navboxes‎ with Navbox/User/Loader and with the added JS code, it should work.