diff options
author | Kate Arzamastseva <pshns@ukr.net> | 2011-06-04 18:59:05 +0300 |
---|---|---|
committer | Kate Arzamastseva <pshns@ukr.net> | 2011-06-04 18:59:05 +0300 |
commit | 8d56551e75f273694893a29a06d8164d3d60ae17 (patch) | |
tree | 80c181cbb7b4df28fe7691a2dd48fd6761dbe785 /lib/scripts/index.js | |
parent | e4f389ef1728a0f86164a0e4b88626be9860dabb (diff) | |
parent | 0f80b0bcd4ca247f7bc4c3830946741546ad6e88 (diff) | |
download | rpg-8d56551e75f273694893a29a06d8164d3d60ae17.tar.gz rpg-8d56551e75f273694893a29a06d8164d3d60ae17.tar.bz2 |
Merge remote-tracking branch 'upstream/master' into media-revisions
Diffstat (limited to 'lib/scripts/index.js')
-rw-r--r-- | lib/scripts/index.js | 140 |
1 files changed, 60 insertions, 80 deletions
diff --git a/lib/scripts/index.js b/lib/scripts/index.js index 9c7943d0c..da9ce96a4 100644 --- a/lib/scripts/index.js +++ b/lib/scripts/index.js @@ -1,43 +1,22 @@ +/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, strict: true, newcap: true, immed: true */ +/*global jQuery, window, DOKU_BASE*/ +"use strict"; + /** * Javascript for index view * * @author Andreas Gohr <andi@splitbrain.org> + * @author Pierre Spring <pierre.spring@caillou.ch> */ -var index = { +(function ($) { + var throbber_delay, toggle; /** * Delay in ms before showing the throbber. * Used to skip the throbber for fast AJAX calls. */ - throbber_delay: 500, - - /** - * Attach event handlers to all "folders" below the given element - * - * @author Andreas Gohr <andi@splitbrain.org> - */ - treeattach: function(obj){ - if(!obj) return; - - var items = getElementsByClass('idx_dir',obj,'a'); - for(var i=0; i<items.length; i++){ - var elem = items[i]; - - // attach action to make the link clickable by AJAX - addEvent(elem,'click',function(e){ return index.toggle(e,this); }); - - // get the listitem the elem belongs to - var listitem = elem.parentNode; - while (listitem.tagName != 'LI') { - listitem = listitem.parentNode; - } - //when there are uls under this listitem mark this listitem as opened - if (listitem.getElementsByTagName('ul').length) { - listitem.open = true; - } - } - }, + throbber_delay = 500; /** * Open or close a subtree using AJAX @@ -46,70 +25,71 @@ var index = { * * @author Andreas Gohr <andi@splitbrain.org> * @author Ben Coburn <btcoburn@silicodon.net> + * @author Pierre Spring <pierre.spring@caillou.ch> */ - toggle: function(e,clicky){ - var listitem = clicky.parentNode.parentNode; + toggle = function (e) { + + var listitem, sublist, timeout, ul, clicky; - listitem.open = !listitem.open; - // listitem.open represents now the action to be done + clicky = $(this); + listitem = clicky.parentsUntil('li').last().parent(); + sublist = listitem.find('ul').first(); // if already open, close by removing the sublist - var sublists = listitem.getElementsByTagName('ul'); - if(!listitem.open){ - if (sublists.length) { - sublists[0].style.display='none'; - } - listitem.className='closed'; + if (listitem.hasClass('open')) { + sublist.slideUp( + function () { + listitem.addClass('closed').removeClass('open'); + } + ); e.preventDefault(); - return false; + return; } // just show if already loaded - if(sublists.length && listitem.open){ - sublists[0].style.display=''; - listitem.className='open'; + if (sublist.size() > 0 && !listitem.hasClass('open')) { + listitem.addClass('open').removeClass('closed'); + sublist.slideDown(); e.preventDefault(); - return false; + return; } - // prepare an AJAX call to fetch the subtree - var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php'); - ajax.AjaxFailedAlert = ''; - ajax.encodeURIString = false; - if(ajax.failed) return true; - //prepare the new ul - var ul = document.createElement('ul'); - ul.className = 'idx'; - timeout = window.setTimeout(function(){ + ul = $('<ul class="idx"/>'); + + timeout = window.setTimeout(function () { // show the throbber as needed - if (listitem.open) { - ul.innerHTML = '<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'; - listitem.appendChild(ul); - listitem.className='open'; - } - }, this.throbber_delay); - ajax.elementObj = ul; - ajax.afterCompletion = function(){ - window.clearTimeout(timeout); - index.treeattach(ul); - if (listitem.className!='open') { - if (!listitem.open) { - ul.style.display='none'; - } - listitem.appendChild(ul); - if (listitem.open) { - listitem.className='open'; - } + if (!listitem.hasClass('open')) { + ul.html('<li><img src="' + DOKU_BASE + 'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'); + listitem + .append(ul) + .addClass('open') + .removeClass('closed'); } - }; - ajax.runAJAX(clicky.search.substr(1)+'&call=index'); - e.preventDefault(); - return false; - } -}; + }, throbber_delay); + $.post( + DOKU_BASE + 'lib/exe/ajax.php', + clicky.attr('search').substr(1) + '&call=index', + function (data) { + window.clearTimeout(timeout); + ul.html(data); + if (listitem.className !== 'open') { + if (ul.parent().size() === 0) { + // if the UL has not been attached when showing the + // throbber, then let's do it now. + listitem.append(ul); + } + listitem.addClass('open').removeClass('closed'); + } + }, + 'html' + ); + e.preventDefault(); + }; -addInitEvent(function(){ - index.treeattach($('index__tree')); -}); + $(function () { + // Initialze tree when the DOM is ready. + $('#index__tree').delegate('a.idx_dir', 'click', toggle); + }); +}(jQuery));
\ No newline at end of file |