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