1/**
2 * Attaches the mechanics on our plugin's button
3 *
4 * @param {jQuery} $btn the button itself
5 * @param {object} props unused
6 * @param {string} edid the editor's ID
7 * @return {string}
8 */
9function addBtnActionPlugin_scrapbook($btn, props, edid) {
10    var pickerid = 'picker' + (pickercounter++);
11    var $picker = jQuery(createPicker(pickerid, [], edid))
12            .attr('aria-hidden', 'true')
13            .addClass('plugin-scrapbook')
14        ;
15
16    /**
17     * Insert the scrap associated witht he clicked button
18     *
19     * @param {jQuery} $el the clicked button in the picker
20     */
21    var insertScrap = function ($el) {
22        pickerInsert($el.data('scrap'), edid);
23    };
24
25    // handle click in the picker
26    $picker.click(function (e) {
27        if (e.target.nodeName.toLowerCase() != 'button') return;
28        var $el = jQuery(e.target);
29        if (!$el.data('id')) return;
30
31        // scraps are loaded on demand, but only once
32        if ($el.data('scrap')) {
33            insertScrap($el);
34        } else {
35            jQuery.post(
36                DOKU_BASE + 'doku.php',
37                {
38                    id: $el.data('id'),
39                    do: 'export_raw',
40                    scrapbookinsert: JSINFO.id
41                },
42                function (data) {
43                    $el.data('scrap', data);
44                    insertScrap($el);
45                }
46            );
47        }
48
49        e.preventDefault();
50        pickerClose();
51    });
52
53    // when the toolbar button is clicked
54    $btn.click(
55        function (e) {
56            // load the list of scraps if they haven't been loaded, yet
57            if (!$picker.find('button').length) {
58                $picker.html('<div>' + LANG.plugins.scrapbook.loading + '</div>');
59                jQuery.post(
60                    DOKU_BASE + 'lib/exe/ajax.php',
61                    {
62                        call: 'plugin_scrapbook'
63                    },
64                    function (data) {
65                        $picker.html(data);
66                    }
67                )
68            }
69
70            // open/close the picker
71            pickerToggle(pickerid, $btn);
72            e.preventDefault();
73            return '';
74        }
75    );
76
77    return pickerid;
78}
79