diff options
author | Adrian Lang <mail@adrianlang.de> | 2011-06-21 13:30:18 +0200 |
---|---|---|
committer | Adrian Lang <mail@adrianlang.de> | 2011-06-21 14:07:50 +0200 |
commit | cd06d16faa924afeb14f864b058e01ce8867057c (patch) | |
tree | 3a10fdbded20ee4e524a00cc935ed578a9523f3d /lib/scripts/index.js | |
parent | 4b93850916b217e4bf2ede3ba5e5457e1916e9e7 (diff) | |
download | dokuwiki-cd06d16faa924afeb14f864b058e01ce8867057c.tar.gz dokuwiki-cd06d16faa924afeb14f864b058e01ce8867057c.zip |
Fix index Javascript, introduce compatibility.js
* Removed "use strict" statement, since it does not work with our script file
joining (the statement has to be the first in a file or function)
* Make index a global object again to allow overriding and enhancing
* Use prefix dw_ for index object
* Reintroduce index.treeattach
* Support deprecated index.toggle calling convention
* Add $ prefix for jQuery variables
* Use slide animation for freshly loaded sublists as well
* Fix various errors
Diffstat (limited to 'lib/scripts/index.js')
-rw-r--r-- | lib/scripts/index.js | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/lib/scripts/index.js b/lib/scripts/index.js index da9ce96a4..c575ab618 100644 --- a/lib/scripts/index.js +++ b/lib/scripts/index.js @@ -1,6 +1,5 @@ -/*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"; +/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, newcap: true, immed: true */ +/*global jQuery, window, DOKU_BASE, DEPRECATED, bind*/ /** * Javascript for index view @@ -9,87 +8,90 @@ * @author Pierre Spring <pierre.spring@caillou.ch> */ -(function ($) { - var throbber_delay, toggle; +var dw_index = { - /** + /** * Delay in ms before showing the throbber. * Used to skip the throbber for fast AJAX calls. */ - throbber_delay = 500; + throbber_delay: 500, + + /** + * Initialize tree when the DOM is ready. + */ + init: function () { + dw_index.treeattach('#index__tree'); + }, + + treeattach: function (obj) { + jQuery(obj).delegate('a.idx_dir', 'click', dw_index.toggle); + }, /** * Open or close a subtree using AJAX - * The contents of subtrees are "cached" untill the page is reloaded. + * The contents of subtrees are "cached" until the page is reloaded. * A "loading" indicator is shown only when the AJAX call is slow. * * @author Andreas Gohr <andi@splitbrain.org> * @author Ben Coburn <btcoburn@silicodon.net> * @author Pierre Spring <pierre.spring@caillou.ch> */ - toggle = function (e) { + toggle: function (e, _this) { + e.preventDefault(); - var listitem, sublist, timeout, ul, clicky; + var $listitem, $sublist, timeout, $clicky, show_sublist; + + if (_this) { + DEPRECATED('Use dw_index.toggle(e) (or dw_index.toggle.call(clicky, e) if you need to override clicky), not dw_index.toggle(e, clicky)'); + } - clicky = $(this); - listitem = clicky.parentsUntil('li').last().parent(); - sublist = listitem.find('ul').first(); + $clicky = jQuery(_this || this); + $listitem = $clicky.closest('li'); + $sublist = $listitem.find('ul').first(); // if already open, close by removing the sublist - if (listitem.hasClass('open')) { - sublist.slideUp( + if ($listitem.hasClass('open')) { + $sublist.slideUp('fast', function () { - listitem.addClass('closed').removeClass('open'); + $listitem.addClass('closed').removeClass('open'); } ); - e.preventDefault(); return; } + show_sublist = function (data) { + if (!$listitem.hasClass('open') || $sublist.parent().length === 0) { + $listitem.append($sublist).addClass('open').removeClass('closed'); + } + $sublist.hide(); + if (data) { + $sublist.html(data); + } + $sublist.slideDown('fast'); + }; + // just show if already loaded - if (sublist.size() > 0 && !listitem.hasClass('open')) { - listitem.addClass('open').removeClass('closed'); - sublist.slideDown(); - e.preventDefault(); + if ($sublist.length > 0) { + show_sublist(); return; } //prepare the new ul - ul = $('<ul class="idx"/>'); - - timeout = window.setTimeout(function () { - // show the throbber as needed - 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'); - } - }, throbber_delay); + $sublist = jQuery('<ul class="idx"/>'); - $.post( + timeout = window.setTimeout( + bind(show_sublist, '<li><img src="' + DOKU_BASE + 'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'), dw_index.throbber_delay); + + jQuery.post( DOKU_BASE + 'lib/exe/ajax.php', - clicky.attr('search').substr(1) + '&call=index', + $clicky[0].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'); - } + show_sublist(data); }, 'html' ); - e.preventDefault(); - }; + } +}; - $(function () { - // Initialze tree when the DOM is ready. - $('#index__tree').delegate('a.idx_dir', 'click', toggle); - }); -}(jQuery));
\ No newline at end of file +jQuery(dw_index.init); |