From cd06d16faa924afeb14f864b058e01ce8867057c Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Tue, 21 Jun 2011 13:30:18 +0200 Subject: 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 --- lib/exe/js.php | 1 + lib/scripts/compatibility.js | 16 +++++++ lib/scripts/index.js | 104 ++++++++++++++++++++++--------------------- 3 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 lib/scripts/compatibility.js diff --git a/lib/exe/js.php b/lib/exe/js.php index 0d4a08ebd..25dc8496a 100644 --- a/lib/exe/js.php +++ b/lib/exe/js.php @@ -58,6 +58,7 @@ function js_out(){ DOKU_INC.'lib/scripts/linkwiz.js', DOKU_INC.'lib/scripts/media.js', DOKU_INC.'lib/scripts/subscriptions.js', + DOKU_INC.'lib/scripts/compatibility.js', # disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', DOKU_TPLINC.'script.js', DOKU_INC.'lib/scripts/behaviour.js', diff --git a/lib/scripts/compatibility.js b/lib/scripts/compatibility.js new file mode 100644 index 000000000..559cc359a --- /dev/null +++ b/lib/scripts/compatibility.js @@ -0,0 +1,16 @@ +/*jslint sloppy: true */ +/*global dw_index, DEPRECATED */ + +var index = { + throbber_delay: dw_index.throbber_delay, + + toggle: function () { + DEPRECATED(); + dw_index.toggle.apply(dw_index, arguments); + }, + + treeattach: function () { + DEPRECATED(); + dw_index.treeattach.apply(dw_index, arguments); + } +}; 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 */ -(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 * @author Ben Coburn * @author Pierre Spring */ - 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 = $('
    '); - - timeout = window.setTimeout(function () { - // show the throbber as needed - if (!listitem.hasClass('open')) { - ul.html('
  • loading...
  • '); - listitem - .append(ul) - .addClass('open') - .removeClass('closed'); - } - }, throbber_delay); + $sublist = jQuery('
      '); - $.post( + timeout = window.setTimeout( + bind(show_sublist, '
    • loading...
    • '), 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); -- cgit v1.2.3