xref: /plugin/imgpaste/script.js (revision 8829523c63efcea1f21dafbd541a669e71164341)
1/* DOKUWIKI:include jquery.paste_image_reader.js */
2
3jQuery(function () {
4    var _didInit = false;
5    function init() {
6        if (!jQuery('#wiki__text').length || _didInit) return;
7        _didInit = true;
8        jQuery('html').pasteImageReader({
9            callback: function (x) {
10                if (!jQuery('#wiki__text').length) return;
11
12                console.log(x);
13
14                // create dialog
15                var offset = jQuery('.plugin_imagepaste').length * 20;
16                var $box = jQuery('<div><div class="content">' + LANG.plugins.imgpaste.inprogress + '</div></div>');
17                $box.dialog({
18                    title: 'Upload',
19                    dialogClass: 'plugin_imagepaste',
20                    closeOnEscape: false,
21                    resizable: false,
22                    position: {
23                        my: 'center+' + offset + ' center+' + offset
24                    },
25                    appendTo: '.dokuwiki'
26                });
27
28                // upload via AJAX
29                jQuery.ajax({
30                    url: DOKU_BASE + 'lib/exe/ajax.php',
31                    type: 'POST',
32                    data: {
33                        call: 'plugin_imgpaste',
34                        data: x.dataURL,
35                        id: JSINFO.id
36                    },
37
38                    // insert syntax and close dialog
39                    success: function (data) {
40                        $box.find('.content').addClass('success').text(data.message);
41                        insertAtCarret('wiki__text', '{{:' + data.id + '}}');
42                        $box.delay(500).fadeOut(500, function () {
43                            $box.dialog('destroy').remove()
44                        });
45                    },
46
47                    // display error and close dialog
48                    error: function (xhr, status, error) {
49                        $box.find('.content').addClass('error').text(error);
50                        $box.delay(1000).fadeOut(500, function () {
51                            $box.dialog('destroy').remove()
52                        });
53                    }
54                });
55            }
56        });
57    }
58
59    init();
60
61    // fastwiki plugin support
62    jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) {
63        if (viewMode == 'edit' || isSectionEdit) {
64            init();
65        }
66    });
67});
68