1/**
2 * AJAX functions for the pagename quicksearch
3 *
4 * @license  GPL2 (http://www.gnu.org/licenses/gpl.html)
5 * @author   Håkan Sandell <sandell.hakan@gmail.com>
6 */
7
8var twistienav_plugin = {
9
10    $callerObj: null,
11
12    init: function () {
13		var $match = 0;
14        if ((JSINFO['conf']['breadcrumbs'] > 0) && (jQuery('.trace').length !== 0)) {
15            twistienav_plugin.breadcrumbs('.trace', 'bc_ns');
16			$match++;
17        }
18        if ((JSINFO['conf']['youarehere'] == 1) && (jQuery('.youarehere').length !== 0)) {
19            twistienav_plugin.breadcrumbs('.youarehere', 'yah_ns');
20			$match++;
21        }
22        if ($match == 0) {
23			if ((JSINFO['conf']['breadcrumbs'] > 0) && (jQuery('.breadcrumbs:has("span.bcsep")').length !== 0)) {
24				twistienav_plugin.breadcrumbs('.breadcrumbs:has("span.bcsep")', 'bc_ns');
25			}
26			if ((JSINFO['conf']['youarehere'] == 1) && (jQuery('.breadcrumbs:not(:has("span.bcsep"))').length !== 0)) {
27				twistienav_plugin.breadcrumbs('.breadcrumbs:not(:has("span.bcsep"))', 'yah_ns');
28			}
29		}
30        if ((JSINFO['plugin_twistienav']['pit_skeleton'] != null) && (jQuery('.pageId').length !== 0)) {
31            twistienav_plugin.pageIdTrace('.pageId', 'yah_ns');
32        }
33
34        return;
35    },
36
37    /**
38     * Add twisties and link events
39     */
40    breadcrumbs: function(div, ns_list){
41        var do_search;
42        var $traceObj = jQuery(div);
43        var $list = JSINFO['plugin_twistienav'][ns_list];
44
45        jQuery(document).click(function(e) {
46            twistienav_plugin.clear_results();
47        });
48
49        do_search = function (caller, namespace) {
50            twistienav_plugin.$callerObj = jQuery(caller);
51            jQuery.post(
52                DOKU_BASE + 'lib/exe/ajax.php',
53                {
54                    call: 'plugin_twistienav',
55                    idx: encodeURI(namespace)
56                },
57                twistienav_plugin.onCompletion,
58                'html'
59            );
60        };
61
62        // NOT REQUIRED ANYMORE
63        // remove current id highlight because it is inherited by popup
64        //trace = $traceObj.html();
65        //trace = trace.replace(/<span class="curid">/gi,'')
66        //             .replace(/<\/span>$/gi,'');
67        //$traceObj.html(trace);
68
69        // add new twisties
70        var linkNo = 0;
71        $links = $traceObj.find('a');
72        $links.each(function () {
73            var ns = $list[linkNo];
74            if (ns == false) {
75                ns = '';
76            }
77            if ($list[linkNo] || $list[linkNo] == '') {
78                var $classes = 'twistienav_twistie' + ' ' + JSINFO['plugin_twistienav']['style'];
79                if ((JSINFO['plugin_twistienav']['twistiemap'] == 1) && (ns == '')) {
80                    $classes = 'twistienav_map' + ' ' + JSINFO['plugin_twistienav']['style'];
81                }
82                jQuery(document.createElement('span'))
83                            .addClass($classes)
84                            .show()
85                            .insertAfter(jQuery(this).parent())
86                            .click(function() {
87                                twistie_active = jQuery(this).hasClass('twistienav_down');
88                                twistienav_plugin.clear_results();
89                                if (!twistie_active) {
90                                    do_search(this, ns);
91                                }
92                            });
93            }
94            linkNo++;
95        });
96    },
97
98    /**
99     * Turn 'pageId' element into a minimalistic hierarchical trace
100     */
101    pageIdTrace: function(div, ns_list){
102        var do_search;
103        var $traceObj = jQuery(div);
104        var $list = JSINFO['plugin_twistienav'][ns_list];
105
106        jQuery(document).click(function(e) {
107            twistienav_plugin.clear_results();
108        });
109
110        do_search = function (caller, namespace) {
111            twistienav_plugin.$callerObj = jQuery(caller);
112            jQuery.post(
113                DOKU_BASE + 'lib/exe/ajax.php',
114                {
115                    call: 'plugin_twistienav_pageid',
116                    idx: encodeURI(namespace)
117                },
118                twistienav_plugin.onCompletion,
119                'html'
120            );
121        };
122
123        // Replace pageId text by prepared skeleton
124        $traceObj.html(JSINFO['plugin_twistienav']['pit_skeleton']);
125
126        // transform links into text "twisties"
127        var linkNo = 1;
128        $links = $traceObj.find('a');
129        $links.each(function () {
130            var ns = $list[linkNo];
131            if (ns == false) {
132                ns = '';
133            }
134            if ($list[linkNo] || $list[linkNo] == '') {
135                jQuery(this)
136                            .addClass('twistienav_twistie')
137                            .show()
138                            .insertAfter(this)
139                            .click(function() {
140                                twistie_active = jQuery(this).hasClass('twistienav_down');
141                                twistienav_plugin.clear_results();
142                                if (!twistie_active) {
143                                    do_search(this, ns);
144                                }
145                            });
146            } else {
147                jQuery(this)
148                            .addClass('twistienav_twistie')
149                            .show()
150                            .insertAfter(this)
151                            .click(function() {
152                                twistie_active = jQuery(this).hasClass('twistienav_down');
153                                twistienav_plugin.clear_results();
154                                if (!twistie_active) {
155                                    do_search(this, '');
156                                }
157                            });
158            }
159            linkNo++;
160        });
161    },
162
163    /**
164     * Remove output div
165     */
166    clear_results: function(){
167        jQuery('.twistienav_twistie').removeClass('twistienav_down');
168        jQuery('.twistienav_map').removeClass('twistienav_down');
169        jQuery('#twistienav__popup').remove();
170    },
171
172    /**
173     * Callback. Reformat and display the results.
174     *
175     * Namespaces are shortened here to keep the results from overflowing
176     * or wrapping
177     *
178     * @param data The result HTML
179     */
180    onCompletion: function(data) {
181        var pos = twistienav_plugin.$callerObj.position();
182
183        if (data === '') { return; }
184
185        twistienav_plugin.$callerObj.addClass('twistienav_down');
186
187        jQuery(document.createElement('div'))
188                        .html(data)
189                        .attr('id','twistienav__popup')
190                        .css({
191                            'position':    'absolute'
192                        })
193                        .appendTo("body")
194                        .position({
195                            "my": "left top",
196                            "at": "right bottom",
197                            "of": twistienav_plugin.$callerObj,
198                            "collision": "flipfit"
199                        })
200                        .click(function() {
201                            twistienav_plugin.clear_results();
202                        });
203    }
204};
205
206jQuery(function () {
207    twistienav_plugin.init();
208});
209// vim: set fileencoding=utf-8 expandtab ts=4 sw=4 :
210