1jQNew(document).ready(function() {
2    jQNew('.ui-dialog-titlebar-close').click(function() {
3        jQNew('.ui-dialog').hide();
4    });
5
6    jQNew('.sidebar-page ul .li').each(function() {
7        var $that = jQNew(this);
8        if ($that.parent().find('ul').length > 0) {
9            $that.prepend('<span class="glyphicon glyphicon-chevron-down pull-right"></span>');
10        }
11    });
12
13    jQNew('.sidebar-page ul .li').click(function() {
14        jQNew(this).parent().find('ul').first().slideToggle(300);
15    });
16
17    jQNew('#toc_contents ul > li:first-child').each(function(e) {
18        if (jQNew(this).children(':first').filter('ul').length > 0) {
19            jQNew(this).css('list-style-type', 'none');
20        }
21    });
22
23    jQNew('abbr').tooltip({
24        delay: { open: 500, close: 100 }
25    });
26
27    /* http://www.kryogenix.org/code/browser/sorttable/ */
28    jQNew('.page table').each(function() {
29        sorttable.makeSortable(jQNew(this).get(0));
30    });
31
32    // Scroll to a link smoothly
33    smoothToPadding = jQNew('#dokuwiki__top .navbar').first().innerHeight() + 10;
34    function smoothTo() {
35        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
36            var target = jQNew(this.hash);
37            target = target.length ? target : jQNew('[name=' + this.hash.slice(1) +']');
38            if (target.length) {
39                jQNew('html,body').animate({
40                    scrollTop: target.offset().top - smoothToPadding
41                }, 200);
42                return false;
43            }
44        } else {
45            return true;
46        }
47    }
48    jQNew('a').click(smoothTo);
49
50    /* override footnote popups */
51    // kill old event
52    jQNew('a.fn_top').unbind('mouseover', dw_page.footnoteDisplay);
53    dw_page.insituPopup = function(target, popup_id) {
54        // on first hover, set up and show the popover
55        $el = jQNew(target);
56        if (!$el.hasClass('popover-trigger')) {
57            content = jQNew($el.attr('href')).closest('div.fn').html().replace(/((^|\s*,\s*)<sup>.*?<\/sup>)+\s*/gi, '').replace(/\bid=(['"])([^"']+)\1/gi,'id="insitu__$2');
58            $el.popover({
59                'content': content,
60                'toggle': 'popover',
61                'placement': 'bottom',
62                'trigger': 'hover',
63                'html': true,
64                'delay': {
65                    show: 0,
66                    hide: 1000
67                },
68                'container': '#dokuwiki__content'
69            }).addClass('popover-trigger').popover('show');
70        }
71    };
72    dw_page.footnoteDisplay = function() {
73        dw_page.insituPopup(this, 'insitu__fn');
74    };
75    // rebind new event
76    jQNew('a.fn_top').mouseover(dw_page.footnoteDisplay);
77});
78
79/* toolbar button to add a table */
80function addBtnActionInsertTable($btn, props, edid) {
81    // set up what happens when the button is clicked
82    $btn.click(function() {
83        // create a new element on the page.
84        var $picker = jQNew(document.createElement('div'));
85        $picker.addClass('modal fade');
86        $picker.attr('role', 'dialog')
87               .attr('aria-hidden', 'true')
88               .attr('aria-labelledby', 'Insert Table Modal Box')
89               .attr('id', 'insert-table-popup')
90               .css('position', 'absolute');
91
92        // set up the content of the element
93        var html = '<div class="modal-dialog">' +
94                    '<div class="modal-content">' +
95                     '<div class="modal-header">' +
96                      '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
97                      '<h3 class="modal-title">Insert table markup</h3>' +
98                     '</div>' +
99                     '<div class="modal-body">' +
100                      '<form class="form-horizontal">' +
101                       '<div class="form-group">' +
102                        '<label class="control-label col-lg-6" for="hrows">Header rows</label>' +
103                        '<div class="col-lg-2">' +
104                         '<input class="form-control" type="number" id="hrows" value=1 min=0 step=1>' +
105                        '</div>' +
106                       '</div>' +
107                       '<div class="form-group">' +
108                        '<label class="control-label col-lg-6" for="hcols">Header columns</label>' +
109                        '<div class="col-lg-2">' +
110                         '<input class="form-control" type="number" id="hcols" value=0 min=0 step=1>' +
111                        '</div>' +
112                       '</div>' +
113                       '<div class="form-group">' +
114                        '<label class="control-label col-lg-6" for="rows">Body Rows</label>' +
115                        '<div class="col-lg-2">' +
116                         '<input class="form-control" type="number" id="rows" value=3 min=0 step=1>' +
117                        '</div>' +
118                       '</div>' +
119                       '<div class="form-group">' +
120                        '<label class="control-label col-lg-6" for="cols">Body Columns</label>' +
121                        '<div class="col-lg-2">' +
122                         '<input class="form-control" type="number" id="cols" value=3 min=0 step=1>' +
123                        '</div>' +
124                       '</div>' +
125                      '</form>' +
126                      '<div class="preview">' +
127                       '<h4>Preview</h4>' +
128                       '<table class="table table-bordered">' +
129                       '</table>' +
130                      '</div>' +
131                      '<div class="markup">' +
132                       '<h4>Markup</h4>' +
133                       '<pre>' +
134                       '</pre>' +
135                      '</div>' +
136                     '</div>' +
137                     '<div class="modal-footer">' +
138                      '<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>' +
139                      '<button type="button" class="btn btn-primary">Insert</button>' +
140                     '</div>' +
141                    '</div>' +
142                   '</div>';
143
144        // the ultimate action of the new button
145        function tableInsert($picker) {
146            // get the size of the table
147            var tabletext = "\n";
148            hrows = $picker.find('#hrows').val();
149            hcols = $picker.find('#hcols').val();
150            rows = $picker.find('#rows').val();
151            cols = $picker.find('#cols').val();
152
153            // make sure the table has cells
154            if (hrows + rows < 1) {
155                alert("You need more than 0 rows.");
156                return false;
157            } else if (hcols + cols < 1) {
158                alert("You need more than 0 columns.");
159                return false;
160            }
161
162            var i, j;
163            // create header rows
164            for (i = 0; i < hrows; i++) {
165                for (j = 0; j < parseInt(hcols) + parseInt(cols); j++) {
166                    tabletext += "^  ";
167                }
168                tabletext += "^\n";
169            }
170            // create body rows
171            for (i = 0; i < rows; i++) {
172                // create header columns
173                for (j = 0; j < hcols; j++) {
174                    tabletext += "^  ";
175                }
176                // create body columns
177                for (j = 0; j < cols; j++) {
178                    tabletext += "|  ";
179                }
180                tabletext += "|\n";
181            }
182
183            // insert the table into the page
184            insertAtCarret(edid, tabletext + '\n');
185
186            // hide and remove the element
187            $picker.modal('hide');
188        }
189
190        function updatePreview() {
191            var $table = $preview.find('table').html('');
192            var $markup = $picker.find('pre').text('');
193
194            var tablehtml = '';
195            var tabletext = "";
196
197            // make sure the table has cells
198            if (hrows + rows < 1) {
199                return false;
200            } else if (hcols + cols < 1) {
201                return false;
202            }
203
204            var i, j;
205            // create header rows
206            for (i = 0; i < hrows; i++) {
207                tablehtml += '<tr>';
208                for (j = 0; j < parseInt(hcols) + parseInt(cols); j++) {
209                    tablehtml += "<th></th>";
210                    tabletext += "^ Header ";
211                }
212                tablehtml += "</tr>\n";
213                tabletext += "^\n";
214            }
215            // create body rows
216            for (i = 0; i < rows; i++) {
217                tablehtml += '<tr>';
218                // create header columns
219                for (j = 0; j < hcols; j++) {
220                    tablehtml += "<th></th>";
221                    tabletext += "^ Header ";
222                }
223                // create body columns
224                for (j = 0; j < cols; j++) {
225                    tablehtml += "<td></td>";
226                    tabletext += "| content ";
227                }
228                tablehtml += "</tr>\n";
229                tabletext += "|\n";
230            }
231
232            $table.html(tablehtml);
233            $markup.text(tabletext);
234        }
235
236        // add the content to the element and insert it into the page
237        $picker.append(html);
238        jQNew('body').append($picker);
239
240        var hrows = $picker.find('#hrows').val();
241        var hcols = $picker.find('#hcols').val();
242        var rows = $picker.find('#rows').val();
243        var cols = $picker.find('#cols').val();
244        var $preview = jQNew('.preview');
245
246        // set up the insert table action
247        $picker.find('.btn-primary').bind('click', bind(tableInsert, $picker));
248
249        // set up handlers to show table preview
250        $picker.find('#hrows').on('propertychange keyup input paste', function(e) {
251            hrows = jQNew(this).val();
252            updatePreview();
253        });
254        $picker.find('#hcols').on('propertychange keyup input paste', function(e) {
255            hcols = jQNew(this).val();
256            updatePreview();
257        });
258        $picker.find('#rows').on('propertychange keyup input paste', function(e) {
259            rows = jQNew(this).val();
260            updatePreview();
261        });
262        $picker.find('#cols').on('propertychange keyup input paste', function(e) {
263            cols = jQNew(this).val();
264            updatePreview();
265        });
266
267        updatePreview();
268
269        $picker.on('hidden.bs.modal', function() {
270            $picker.remove();
271        });
272
273        // show the element as a modal window
274        $picker.modal('show');
275
276        return $picker[0];
277    });
278
279    return true;
280}
281// add a new toolbar button
282if (window.toolbar !== undefined) {
283    window.toolbar[window.toolbar.length] = {
284        'type'  : 'InsertTable', // new type that links to the function
285        'title' : 'Insert Table',
286        'icon'  : '../../tpl/starterbootstrap/img/table.png'
287    };
288}
289// hide toc initially
290setTimeout(function() {
291    jQNew('#toc_contents').collapse('hide');
292}, 10);
293
294function createPermalinks() {
295    jQNew(':header').each(function() {
296        console.log(jQNew(this));
297        var headerName = jQNew(this).attr('id');
298        if (headerName) {
299            jQNew(this).prepend('<a href="#' + headerName + '"' + ' id="' + headerName + '_anchor"' + ' aria-hidden="true" class="anchor"><span class="glyphicon glyphicon-link"></span></a>');
300        }
301    });
302};
303
304createPermalinks();
305