lutim.js.ep 22.3 KB
Newer Older
1
% # vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
Luc Didry's avatar
Luc Didry committed
2
3
4
5
function selectInput() {
    $(this).select();
}
function cleanName(name, empty) {
Luc Didry's avatar
Fix #68    
Luc Didry committed
6
7
8
    if (typeof(name) === 'undefined') {
        return name;
    }
Luc Didry's avatar
Luc Didry committed
9
10
11
12
    if (empty !== undefined && empty !== null && empty) {
        return name.replace(/&(l|g)t;/g, '').replace(/"/g, '\'');
    } else {
        return name.replace(/</g, '&lt;').replace(/>/g, '&gt;');
13
    }
Luc Didry's avatar
Luc Didry committed
14
15
}
function tw_url(url) {
Luc Didry's avatar
Luc Didry committed
16
    return btn = [
Luc Didry's avatar
Luc Didry committed
17
18
19
20
        '<a title="<%= l('Tweet it!') %>" target="_blank" href="https://twitter.com/share?url=<%== url_for('/')->to_abs() %>', url, '?t" class="btn btn-default">',
           '<span class="icon icon-twitter"></span>',
        '</a>'
    ].join('');
Luc Didry's avatar
Luc Didry committed
21
22
23
24
25
26
}
function modifyImage(e) {
    e.preventDefault();
    var url   = $(this).data('modlink');
    var short = $(this).data('modshort');
    modify(url, short);
Luc Didry's avatar
Luc Didry committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
}
function modify(url, short) {
    var limit       = $('#day-'+short).val();
    var del_at_view = ($('#first-view-'+short).prop('checked')) ? 1 : 0;
    $.ajax({
        url  : url,
        type : 'POST',
        data : {
            'image_url'      : '<%== url_for('/')->to_abs() %>'+short,
            'format'         : 'json',
            'delete-day'     : limit,
            'first-view'     : del_at_view
        },
        success: function(data) {
            updateItem(short, limit, del_at_view);
Luc Didry's avatar
Luc Didry committed
42
            goodToast(data.msg);
Luc Didry's avatar
Luc Didry committed
43
44
        },
        error: function() {
Luc Didry's avatar
Luc Didry committed
45
            badToast('<%= l('Error while trying to modify the image.') %>');
Luc Didry's avatar
Luc Didry committed
46
47
48
        }
    });
}
49

Luc Didry's avatar
Luc Didry committed
50
51
52
53
54
55
56
57
58
59
60
61
function buildMessage(success, msg) {
    if(success) {
        var s_url = link([msg.short, '.', msg.ext].join(''), '');
        var thumb = (msg.thumb !== null) ? [
            '<div class="col-sm-1">',
                '<a href="', s_url, '" target="_blank">',
                    '<img class="thumbnail img-responsive" alt="', cleanName(msg.filename, true), ' thumbnail" src="', msg.thumb, '">',
                '</a>',
            '</div>'
        ].join('') : ''
        return [
            '<div class="alert alert-success" id="alert-', msg.real_short, '">',
Luc Didry's avatar
Luc Didry committed
62
                '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>',
Luc Didry's avatar
Luc Didry committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                '<div class="row">', thumb,
                    '<div class="col-sm-11">',
                        '<h4>',
                            '<a href="', s_url, '" target="_blank">',
                                msg.filename,
                            '</a>',
                        '</h4>',
                        '<form class="form">',
                            '<div class="form-group">',
                                '<label class="sr-only" for="view', msg.real_short, '"><%= l('View link') %></label>',
                                '<div class="input-group input-group-sm col-sm-6">',
                                    '<div class="input-group-btn adjust-addon">',
                                        '<a href="', s_url, '" target="_blank" class="btn btn-default">',
                                            '<span class="icon icon-eye" title=" <%= l('View link') %>"></span>',
Luc Didry's avatar
Luc Didry committed
77
78
                                        '</a>',
                                    '</div>',
Luc Didry's avatar
Luc Didry committed
79
                                    '<input type="text" class="form-control view-link-input" id="view', msg.real_short, '" value="', s_url, '" readonly>',
Luc Didry's avatar
Luc Didry committed
80
                                    '<a href="#" class="input-group-addon copy-to-clipboard-link" title="<%= l('Copy to clipboard') %>">',
Luc Didry's avatar
Luc Didry committed
81
82
                                        '<span class="icon icon-clipboard"></span>',
                                    '</a>',
Luc Didry's avatar
Luc Didry committed
83
                                '</div>',
Luc Didry's avatar
Luc Didry committed
84
85
86
87
88
89
                            '</div>',
                            '<div class="form-group">',
                                '<label class="sr-only" for="markdown', msg.real_short, '"><%= l('Markdown syntax') %></label>',
                                '<div class="input-group input-group-sm col-sm-6">',
                                    '<div class="input-group-addon adjust-addon">',
                                        '<span class="markdown-mark-solid" title="<%= l('Markdown syntax') %>"></span>',
Luc Didry's avatar
Luc Didry committed
90
                                    '</div>',
Luc Didry's avatar
Luc Didry committed
91
                                    '<input type="text" class="form-control" id="markdown', msg.real_short, '" value="![](', link(msg.short, ''), ')" readonly>',
Luc Didry's avatar
Luc Didry committed
92
                                    '<a href="#" class="input-group-addon copy-to-clipboard-link" title="<%= l('Copy to clipboard') %>">',
Luc Didry's avatar
Luc Didry committed
93
94
                                        '<span class="icon icon-clipboard"></span>',
                                    '</a>',
Luc Didry's avatar
Luc Didry committed
95
                                '</div>',
Luc Didry's avatar
Luc Didry committed
96
97
98
99
100
101
102
                            '</div>',
                            '<div class="form-group">',
                                '<label class="sr-only" for="download', msg.real_short, '"><%= l('Download link') %></label>',
                                '<div class="input-group input-group-sm col-sm-6">',
                                    '<div class="input-group-btn adjust-addon">',
                                        '<a href="', link(msg.short, 'dl'), '" class="btn btn-default">',
                                            '<span class="icon icon-download" title="<%= l('Download link') %>"></span>',
Luc Didry's avatar
Luc Didry committed
103
104
                                        '</a>',
                                    '</div>',
Luc Didry's avatar
Luc Didry committed
105
                                    '<input type="text" class="form-control" id="download', msg.real_short, '" value="', link(msg.short, 'dl'), '" readonly>',
Luc Didry's avatar
Luc Didry committed
106
                                    '<a href="#" class="input-group-addon copy-to-clipboard-link" title="<%= l('Copy to clipboard') %>">',
Luc Didry's avatar
Luc Didry committed
107
108
                                        '<span class="icon icon-clipboard"></span>',
                                    '</a>',
Luc Didry's avatar
Luc Didry committed
109
                                '</div>',
Luc Didry's avatar
Luc Didry committed
110
111
112
113
114
115
116
                            '</div>',
                            '<div class="form-group">',
                                '<label class="sr-only" for="share', msg.real_short, '"><%= l('Link for share on social networks') %></label>',
                                '<div class="input-group input-group-sm col-sm-6">',
                                    '<div class="input-group-btn adjust-addon">',
                                        '<a href="', link(msg.short, 't'), '" target="_blank" class="btn btn-default">',
                                            '<span class="icon icon-share" title="<%= l('Link for share on social networks') %>"></span>',
Luc Didry's avatar
Luc Didry committed
117
                                        '</a>',
Luc Didry's avatar
Luc Didry committed
118
                                        tw_url(msg.short),
Luc Didry's avatar
Luc Didry committed
119
                                    '</div>',
Luc Didry's avatar
Luc Didry committed
120
                                    '<input type="text" class="form-control" id="share', msg.real_short, '" value="', link(msg.short, 't'), '" readonly>',
Luc Didry's avatar
Luc Didry committed
121
                                    '<a href="#" class="input-group-addon copy-to-clipboard-link" title="<%= l('Copy to clipboard') %>">',
Luc Didry's avatar
Luc Didry committed
122
123
                                        '<span class="icon icon-clipboard"></span>',
                                    '</a>',
Luc Didry's avatar
Luc Didry committed
124
                                '</div>',
Luc Didry's avatar
Luc Didry committed
125
126
127
128
                            '</div>',
                            '<div class="form-group">',
                                '<div class="input-group col-sm-6 col-xs-12">',
                                    '<span class="form-control-static">', link(msg.real_short, '', msg.token), '</span>',
Luc Didry's avatar
Luc Didry committed
129
                                '</div>',
Luc Didry's avatar
Luc Didry committed
130
131
                            '</div>',
                        '</form>',
Luc Didry's avatar
Luc Didry committed
132
                    '</div>',
Luc Didry's avatar
Luc Didry committed
133
134
135
136
137
                '</div>',
                '<div class="row">',
                    '<form class="form col-sm-11 col-sm-offset-1" role="form" method="POST" action="', link(msg.real_short, '', msg.token, true), '">',
                        '<div class="form-group form-inline">',
                            '<select id="day-', msg.real_short, '" name="delete-day" class="form-control">',
138
139
% my @delays = split(',', $self->config('proposed_delays'));
% for my $delay (@delays) {
b_b's avatar
b_b committed
140
%   my $text = (defined($d->{'delay_'.$delay})) ? $d->{'delay_'.$delay} : l('%1 days', $delay);
141
142
143
%   if (config('max_delay')) {
%       if ($delay) {
%           if ($delay < config('max_delay')) {
Luc Didry's avatar
Luc Didry committed
144
                                '<option value="<%= $delay %>" <%== is_selected($delay) %>><%= $text %></option>',
145
%           } elsif ($delay == config('max_delay')) {
Luc Didry's avatar
Luc Didry committed
146
                                '<option value="<%= $delay %>" <%== is_selected($delay) %>><%= $text %></option>',
147
148
149
%               last;
%           } else {
%               my $text = ($delay == 1) ? l('24 hours') : l('%1 days', $delay);
Luc Didry's avatar
Luc Didry committed
150
                                '<option value="<%= config('max_delay') %>" <%== is_selected(config('max_delay')) %>><%= l('%1 days', config('max_delay')) %></option>',
151
152
153
154
%               last;
%           }
%       }
%   } else {
Luc Didry's avatar
Luc Didry committed
155
                                '<option value="<%= $delay %>" <%== is_selected($delay) %>><%= $text %></option>',
156
157
%   }
% }
Luc Didry's avatar
Luc Didry committed
158
159
160
161
162
163
                            '</select>&nbsp;',
                            '<div class="checkbox">',
                                '<label>',
                                    '<input id="first-view-', msg.real_short, '" type="checkbox" name="first-view"> <%= l('Delete at first view?') %>',
                                '</label>',
                            '</div>&nbsp;',
Luc Didry's avatar
Luc Didry committed
164
                            '<a href="#" class="btn btn-sm btn-default btn-primary modify-image" data-modlink="', link(msg.real_short, '', msg.token, true),'" data-modshort="', msg.real_short,'">',
Luc Didry's avatar
Luc Didry committed
165
166
167
168
169
170
171
172
173
174
175
176
                                '<%= l('Let\'s go!') %>',
                            '</a>',
                        '</div>',
                    '</form>',
                '</div>',
            '</div>'
        ].join('');
    } else {
        return [
            '<div class="alert alert-danger">',
                '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>',
                '<strong><%= l('Something bad happened') %></strong><br>',
Luc Didry's avatar
Fix #65    
Luc Didry committed
177
                cleanName(msg.filename, false),
Luc Didry's avatar
Luc Didry committed
178
                '<br>',
Luc Didry's avatar
Fix #65    
Luc Didry committed
179
                cleanName(msg.msg, false),
Luc Didry's avatar
Luc Didry committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
            '</div>'
        ].join('');
    }
}
function bindddz(firstview, deleteday) {
    $('#drag-and-drop-zone').dmUploader({
        url: '<%== url_for('/') %>',
        dataType: 'json',
        allowedTypes: 'image/*',
        maxFileSize: <%= config('max_file_size') %>,
        onNewFile: function(id, file){
            $('.messages').append([
                '<div id="', id, '-div">',
                    cleanName(file.name), '<br>',
                    '<div class="progress">',
Luc Didry's avatar
Luc Didry committed
195
196
                        '<div id="', id, '"class="progress-bar progress-striped active width-zero" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">',
                            '<span id="', id, '-text" class="pull-left pdg-left-10"> 0%</span>',
Luc Didry's avatar
Luc Didry committed
197
                        '</div>',
Luc Didry's avatar
Luc Didry committed
198
                    '</div>',
199
                '</div>'
Luc Didry's avatar
Luc Didry committed
200
201
202
203
204
            ].join(''));
        },
        onUploadProgress: function(id, percent){
            var percentStr = ' '+percent+'%';
            $('#'+id).prop('aria-valuenow', percent);
Luc Didry's avatar
Luc Didry committed
205
            $('#'+id).css('width', percent+'%');
Luc Didry's avatar
Luc Didry committed
206
207
208
209
210
211
            $('#'+id+'-text').html(percentStr);
        },
        onUploadSuccess: function(id, data){
            data.msg.filename = cleanName(data.msg.filename);
            $('#'+id+'-div').remove();
            if ($('#copy-all').length === 0 && data.success) {
Luc Didry's avatar
Luc Didry committed
212
213
214
215
216
217
218
                $('.messages').prepend([
                    '<div class="col-xs-12 col-sm-11 col-sm-offset-1">',
                        '<a id="copy-all" href="#" class="btn btn-info copy-all-to-clipboard-link">',
                            '<%= l('Copy all view links to clipboard') %>',
                        '</a>',
                    '</div>'
                ].join(''));
Luc Didry's avatar
Luc Didry committed
219
220
            }
            $('.messages').append(buildMessage(data.success, data.msg));
221
222
            $('#del-'+data.msg.real_short).on('click', function(e) {
                e.preventDefault();
Luc Didry's avatar
Luc Didry committed
223
224
                rmFromShortHash(data.msg.short+'.'+data.msg.ext)
                rmFromZipHash(data.msg.short);
225
                rmFromRandomHash(data.msg.short);
Luc Didry's avatar
Luc Didry committed
226
227
228
229
230
            });
            $('#del-'+data.msg.real_short).on('click', delImage);
            if (data.success) {
                addToShortHash(data.msg.short+'.'+data.msg.ext);
                addToZipHash(data.msg.short);
231
                addToRandomHash(data.msg.short);
Luc Didry's avatar
Luc Didry committed
232
233
234
235
                $('.close').unbind('click', evaluateCopyAll);
                $('.close').on('click', evaluateCopyAll);
                $('input[type=\'text\']').unbind("click", selectInput);
                $('input[type=\'text\']').on("click", selectInput);
Luc Didry's avatar
Luc Didry committed
236
237
238
239
240
241
                $('.copy-all-to-clipboard-link').unbind('click', copyAllToClipboard);
                $('.copy-all-to-clipboard-link').on('click', copyAllToClipboard);
                $('.modify-image').unbind('click', modifyImage);
                $('.modify-image').on('click', modifyImage);
                $('.copy-to-clipboard-link').unbind('click', clickOnCopyLink);
                $('.copy-to-clipboard-link').on('click', clickOnCopyLink);
Luc Didry's avatar
Luc Didry committed
242
243
244
245
246
247
248
249
                addItem(data.msg);
            }
        },
        onUploadError: function(id, message){
            $('.messages').append(buildMessage(false, ''));
        },
        onFileSizeError: function(file){
            $('.messages').append(buildMessage(false, { filename: file.name, msg: '<%= l('The file exceed the size limit (%1)', config('max_file_size')) %>'}));
250
        }
Luc Didry's avatar
Luc Didry committed
251
252
253
    });
}

Luc Didry's avatar
Luc Didry committed
254
255
function upload_url(e) {
    e.preventDefault();
Luc Didry's avatar
Luc Didry committed
256
257
258
259
260
261
262
263
264
265
266
267
268
    var val = $('#lutim-file-url').val();
    if (val !== undefined && val !== '') {
        $('#lutim-file-url').prop('disabled', 'disabled');
        $('.hidden-spin').css('display', 'block');
        $.ajax({
            url  : '<%== url_for('/') %>',
            type : 'POST',
            data : {
                'lutim-file-url' : val,
                'format'         : 'json',
                'first-view'     : ($('#first-view').prop('checked')) ? 1 : 0,
                'crypt'          : ($('#crypt').prop('checked')) ? 1 : 0,
                'delete-day'     : $('#delete-day').val()
269
            },
Luc Didry's avatar
Luc Didry committed
270
            success: function(data) {
Luc Didry's avatar
Fix #40    
Luc Didry committed
271
                data.msg.filename = cleanName(data.msg.filename);
Luc Didry's avatar
Luc Didry committed
272
273
274
275
                $('.messages').append(buildMessage(data.success, data.msg));
                if (data.success) {
                    if ($('#copy-all').length === 0) {
                        $('.messages').prepend([
Luc Didry's avatar
Luc Didry committed
276
                            '<div class="col-xs-12 col-sm-11 col-sm-offset-1">',
Luc Didry's avatar
Luc Didry committed
277
                                '<a id="copy-all" href="#" class="btn btn-info copy-all-to-clipboard-link">',
Luc Didry's avatar
Luc Didry committed
278
279
                                    '<%= l('Copy all view links to clipboard') %>',
                                '</a>',
Luc Didry's avatar
Luc Didry committed
280
                            '</div>'
Luc Didry's avatar
Luc Didry committed
281
282
283
                        ].join(''));
                    }
                    $('#lutim-file-url').val('');
284
285
                    addToShortHash(data.msg.short+'.'+data.msg.ext);
                    addToZipHash(data.msg.short);
286
                    addToRandomHash(data.msg.short);
Luc Didry's avatar
Luc Didry committed
287
                    $('#del-'+data.msg.real_short).on('click', delImage);
288
289
290
291
                    $('.close').unbind('click', evaluateCopyAll);
                    $('.close').on('click', evaluateCopyAll);
                    addItem(data.msg);
                }
292
            },
Luc Didry's avatar
Luc Didry committed
293
            error: function() {
294
                $('.messages').append(buildMessage(false, ''));
295
            },
Luc Didry's avatar
Luc Didry committed
296
297
298
            complete: function() {
                $('#lutim-file-url').prop('disabled', '');
                $('.hidden-spin').css('display', 'none');
Luc Didry's avatar
Luc Didry committed
299
300
301
302
303
304
                $('.copy-all-to-clipboard-link').unbind('click', copyAllToClipboard);
                $('.copy-all-to-clipboard-link').on('click', copyAllToClipboard);
                $('.modify-image').unbind('click', modifyImage);
                $('.modify-image').on('click', modifyImage);
                $('.copy-to-clipboard-link').unbind('click', clickOnCopyLink);
                $('.copy-to-clipboard-link').on('click', clickOnCopyLink);
305
306
307
            }
        });
    }
Luc Didry's avatar
Luc Didry committed
308
}
309

Luc Didry's avatar
Luc Didry committed
310
311
312
function fileUpload(file) {
    var fd = new FormData();
    fd.append('file', file);
313

Luc Didry's avatar
Luc Didry committed
314
315
316
317
    fd.append('format', 'json');
    fd.append('first-view', ($('#first-view').prop('checked')) ? 1 : 0);
    fd.append('crypt', ($('#crypt').prop('checked')) ? 1 : 0);
    fd.append('delete-day', ($('#delete-day').val()));
318

Luc Didry's avatar
Luc Didry committed
319
320
321
    $('.messages').append([
        '<div id="1-div">', cleanName(file.name), '<br>',
            '<div class="progress">',
Luc Didry's avatar
Luc Didry committed
322
323
                '<div id="1" class="progress-bar progress-striped active width-zero" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">',
                    '<span id="1-text" class="pull-left pdg-left-10"> 0%</span>',
Luc Didry's avatar
Luc Didry committed
324
                '</div>',
Luc Didry's avatar
Luc Didry committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
            '</div>',
        '</div>'
    ].join(''));
    // Ajax Submit
    $.ajax({
        url: '<%== url_for('/') %>',
        type: 'POST',
        dataType: 'json',
        data: fd,
        cache: false,
        contentType: false,
        processData: false,
        forceSync: false,
        xhr: function(){
            var xhrobj = $.ajaxSettings.xhr();
            if(xhrobj.upload){
                xhrobj.upload.addEventListener('progress', function(event) {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total || e.totalSize;
                    if(event.lengthComputable){
                      percent = Math.ceil(position / total * 100);
                    }
348

Luc Didry's avatar
Luc Didry committed
349
350
                    var percentStr = ' '+percent+'%';
                    $('#1').prop('aria-valuenow', percent);
Luc Didry's avatar
Luc Didry committed
351
                    $('#1').css('width', percent+'%');
Luc Didry's avatar
Luc Didry committed
352
353
354
                    $('#1-text').html(percentStr);
                }, false);
            }
355

Luc Didry's avatar
Luc Didry committed
356
357
358
359
360
361
362
            return xhrobj;
        },
        success: function (data, message, xhr){
            $('#1-div').remove();
            if ($('#copy-all').length === 0 && data.success) {
                $('.messages').prepend([
                    '<div class="col-xs-12 col-sm-11 col-sm-offset-1">',
Luc Didry's avatar
Luc Didry committed
363
                        '<a id="copy-all" href="#" class="btn btn-info copy-all-to-clipboard-link">',
Luc Didry's avatar
Luc Didry committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
                            '<%= l('Copy all view links to clipboard') %>',
                        '</a>',
                    '</div>'
                ].join(''));
            }
            data.msg.filename = cleanName(data.msg.filename);
            $('.messages').append(buildMessage(data.success, data.msg));
            if (data.success) {
                $('.close').unbind('click', evaluateCopyAll);
                $('.close').on('click', evaluateCopyAll);
                addItem(data.msg);
            }
        },
        error: function (xhr, status, errMsg){
            $('.messages').append(buildMessage(false, ''));
        },
Luc Didry's avatar
Luc Didry committed
380
381
382
383
384
385
386
387
        complete: function () {
            $('.copy-all-to-clipboard-link').unbind('click', copyAllToClipboard);
            $('.copy-all-to-clipboard-link').on('click', copyAllToClipboard);
            $('.modify-image').unbind('click', modifyImage);
            $('.modify-image').on('click', modifyImage);
            $('.copy-to-clipboard-link').unbind('click', clickOnCopyLink);
            $('.copy-to-clipboard-link').on('click', clickOnCopyLink);
        }
Luc Didry's avatar
Luc Didry committed
388
389
    });
}
390

Luc Didry's avatar
Luc Didry committed
391
392
393
394
395
396
397
function initPaste() {
    /*
        actually FF and Chrome doesn't handle paste events the same way...
        for ff we need to create a editable div and register an event to it.
        When user paste, the image is "really" pasted in the div. Then, we need to iterate throught
        the div childs to get images. Previsouly FF didn't have the paste event so it was esay to figure on wich browser we were.
        But firefox now have a paste event so I test it...
398

Luc Didry's avatar
Luc Didry committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
        on Chrome the file object is directlyt in the clipboard.
    */
    var b = 'FF';
    try {
        //FF
        var cbe = new ClipboardEvent('hop');
    } catch(hop) {
        //under webkkit Clipboard doesn't have arguments...
        b = 'WK'
    }
    if (b === 'FF') {
        var pasteDiv = document.createElement('div');
        pasteDiv.addEventListener('paste', onPasteFF);
        pasteDiv.setAttribute('class', 'pasteZone');
        pasteDiv.setAttribute('contenteditable', true);
414

Luc Didry's avatar
Luc Didry committed
415
416
        document.getElementsByTagName('body')[0].appendChild(pasteDiv);
        pasteDiv.focus();
417

Luc Didry's avatar
Luc Didry committed
418
419
        document.addEventListener('click', function(event) {
            var t = $(event.target);
420

Luc Didry's avatar
Luc Didry committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
            switch (t[0].nodeName.toUpperCase()) {
                case 'A':
                case 'BUTTON':
                case 'INPUT':
                case 'SELECT':
                case 'SPAN':
                case 'LABEL':
                    break;
                default:
                    if (t[0].parentNode.nodeName.toUpperCase() !== 'SELECT') {
                        pasteDiv.focus();
                    }
            }
        });
    } else {
        document.addEventListener('paste', onPaste);
437
    }
Luc Didry's avatar
Luc Didry committed
438
}
439

Luc Didry's avatar
Luc Didry committed
440
441
442
443
444
445
446
447
448
449
function waitforpastedata(elem, savedcontent) {
    if (elem.childNodes && elem.childNodes.length > 0) {
        processpaste(elem, savedcontent);
    } else {
        var that = {
            e: elem,
            s: savedcontent
        };
        that.callself = function () {
            waitforpastedata(that.e, that.s);
450
        }
Luc Didry's avatar
Luc Didry committed
451
        setTimeout(that.callself, 20);
452
    }
Luc Didry's avatar
Luc Didry committed
453
}
454

Luc Didry's avatar
Luc Didry committed
455
456
457
function processpaste(elem, savedcontent) {
    var pasteZone = document.getElementsByClassName('pasteZone')[0];
    var f         = new Image();
458

Luc Didry's avatar
Luc Didry committed
459
460
461
462
    f.onload = function(){
        var canvas    = document.createElement('canvas');
        canvas.width  = f.width;
        canvas.height = f.height;
463

Luc Didry's avatar
Luc Didry committed
464
465
        var ctx = canvas.getContext('2d');
        ctx.drawImage(f, 0, 0, canvas.width, canvas.height);
466

Luc Didry's avatar
Luc Didry committed
467
468
469
470
471
        canvas.toBlob(function(blob) {
            var url = window.URL.createObjectURL(blob);
            fileUpload(blob);
        });
    }
472

Luc Didry's avatar
Luc Didry committed
473
    f.src = pasteZone.childNodes[0].src;
474

Luc Didry's avatar
Luc Didry committed
475
476
    pasteZone.innerHTML = '';
}
477

Luc Didry's avatar
Luc Didry committed
478
479
480
481
function onPasteFF(e) {
    var pasteZone = document.getElementsByClassName('pasteZone')[0];
    waitforpastedata(pasteZone, 'savedcontent');
}
482

Luc Didry's avatar
Luc Didry committed
483
484
485
486
487
488
489
490
491
function onPaste(e) {
    var items = e.clipboardData.items;
    for(var i = 0; i < items.length; i++) {
        var item = items[i];
        if (/image/.test(item.type)) {
           var file = item.getAsFile();
           fileUpload(file);
        } else {
           //not image..
492
493
        }
    }
Luc Didry's avatar
Luc Didry committed
494
}