1/**
2 *  Extends toolbar
3 *  Image captions:
4 *     - copy url from image to magnify button
5 *     - try copy image title to caption
6 *     - try copy alignment of image to caption
7 *     - resize box to width of image
8 */
9
10if (window.toolbar !== undefined) {
11    toolbar[toolbar.length] = {
12        "type": "format",
13        "title": "Adds an ImageCaption tag",
14        "icon": "../../plugins/imagereference/button.png",
15        "key": "",
16        "open": "<imgcaption image1|>",
17        "close": "</imgcaption>"
18    };
19    toolbar[toolbar.length] = {
20        "type": "format",
21        "title": "Adds an ImageReference tag",
22        "icon": "../../plugins/imagereference/refbutton.png",
23        "key": "",
24        "open": "<imgref ",
25        "sample": "image1",
26        "close": ">"
27    };
28}
29
30jQuery(function () {
31
32    // captions of images
33    jQuery('span.imgcaption').each(function () {
34        let $imgcaption = jQuery(this);
35        let $amedia = $imgcaption.find('a.media');
36        let $img = $imgcaption.find('img');
37
38        //copy img url to magnify button
39        if ($amedia[0]) {
40            let link = $amedia.attr('href');
41            $imgcaption.find('span.undercaption a').last()
42                .attr('href', link)//set link
43                .children().show(); //display button
44        }
45
46        //copy possibly img title when no caption is set
47        let captionparts = $imgcaption.find('span.undercaption').text().split(':', 2);
48        if (!jQuery.trim(captionparts[1])) {
49            let title = $img.attr('title');
50            if (title) {
51                $imgcaption.find('span.undercaption a').first().before(': ' + title);
52            }
53        }
54
55        //apply alignment of image to imgcaption
56        if (!($imgcaption.hasClass('left') || $imgcaption.hasClass('right') || $imgcaption.hasClass('center'))) {
57            if ($img.hasClass('medialeft')) {
58                $imgcaption.addClass('left');
59            }
60            else if ($img.hasClass('mediaright')) {
61                $imgcaption.addClass('right');
62            }
63            else if ($img.hasClass('mediacenter')) {
64                $imgcaption.addClass('center');
65            }
66        }
67
68        //add wrapper to center imgcaption
69        if ($imgcaption.hasClass('center')) {
70            $imgcaption.wrap('<span class="imgcaption_centerwrapper"></span>');
71        }
72
73        // width is still zero if called from jQuery.ready() because image is not yet loaded.
74        // Sets correct size of caption after loading image
75        $img.on("load", function(){
76            let width = jQuery(this).width();
77            $imgcaption.width((width + 8) + "px");
78        });
79
80    });
81
82    // // captions of tables
83    // jQuery('div.tabcaption').each(function() {
84    //     let $imgcaption = jQuery(this);
85    //
86    //     //add wrapper to center imgcaption
87    //     if ($imgcaption.hasClass('center')) {
88    //         $imgcaption.wrap('<span class="imgcaption_centerwrapper"></span>');
89    //     }
90    // });
91});
92