summaryrefslogtreecommitdiff
path: root/lib/scripts/tree.js
diff options
context:
space:
mode:
authorMatthias Schulte <post@lupo49.de>2011-07-17 12:18:37 +0200
committerMatthias Schulte <post@lupo49.de>2011-07-17 12:18:37 +0200
commit8e5a3957cd8de15f48dc27e9c07dfe4033fd6997 (patch)
treee819b734e24a3fb1a40da50383dfbaf34ba3b1d6 /lib/scripts/tree.js
parent3f3f8d1d768a4996d5a2fcc0ce8715e455ce7cad (diff)
parent1e542e417725bb148253929fac9146832d978e45 (diff)
downloadrpg-8e5a3957cd8de15f48dc27e9c07dfe4033fd6997.tar.gz
rpg-8e5a3957cd8de15f48dc27e9c07dfe4033fd6997.tar.bz2
Merge remote branch 'upstream/master'
Diffstat (limited to 'lib/scripts/tree.js')
-rw-r--r--lib/scripts/tree.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/scripts/tree.js b/lib/scripts/tree.js
new file mode 100644
index 000000000..98d3f55d4
--- /dev/null
+++ b/lib/scripts/tree.js
@@ -0,0 +1,96 @@
+/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, newcap: true, immed: true, sloppy: true */
+/*global jQuery, window, DOKU_BASE, DEPRECATED, bind*/
+
+jQuery.fn.dw_tree = function(overrides) {
+ var dw_tree = {
+
+ /**
+ * Delay in ms before showing the throbber.
+ * Used to skip the throbber for fast AJAX calls.
+ */
+ throbber_delay: 500,
+
+ $obj: this,
+
+ toggle_selector: 'a.idx_dir',
+
+ init: function () {
+ this.$obj.delegate(this.toggle_selector, 'click', this,
+ this.toggle);
+ },
+
+ /**
+ * Open or close a subtree using AJAX
+ * 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) {
+ var $listitem, $sublist, timeout, $clicky, show_sublist, dw_tree, opening;
+
+ e.preventDefault();
+
+ dw_tree = e.data;
+ $clicky = jQuery(this);
+ $listitem = $clicky.closest('li');
+ $sublist = $listitem.find('ul').first();
+ opening = $listitem.hasClass('closed');
+ $listitem.toggleClass('open closed');
+ dw_tree.toggle_display($clicky, opening);
+
+ // if already open, close by hiding the sublist
+ if (!opening) {
+ $sublist.dw_hide();
+ return;
+ }
+
+ show_sublist = function (data) {
+ $sublist.hide();
+ if (typeof data !== 'undefined') {
+ $sublist.html(data);
+ }
+ if ($listitem.hasClass('open')) {
+ // Only show if user didn’t close the list since starting
+ // to load the content
+ $sublist.dw_show();
+ }
+ };
+
+ // just show if already loaded
+ if ($sublist.length > 0) {
+ show_sublist();
+ return;
+ }
+
+ //prepare the new ul
+ $sublist = jQuery('<ul class="idx"/>');
+ $listitem.append($sublist);
+
+ timeout = window.setTimeout(
+ bind(show_sublist, '<li><img src="' + DOKU_BASE + 'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'), dw_tree.throbber_delay);
+
+ dw_tree.load_data(function (data) {
+ window.clearTimeout(timeout);
+ show_sublist(data);
+ }, $clicky);
+ },
+
+ toggle_display: function ($clicky, opening) {
+ },
+
+ load_data: function (show_data, $clicky) {
+ show_data();
+ }
+ };
+
+ jQuery.extend(dw_tree, overrides);
+
+ if (!overrides.deferInit) {
+ dw_tree.init();
+ }
+
+ return dw_tree;
+};