Method: Object#exception_import

Defined in:
lib/atome/extensions/atome.rb

#exception_import(atome_id, &proc) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/atome/extensions/atome.rb', line 405

def exception_import(atome_id, &proc)
  if Universe.user_atomes.include?(atome_id.to_sym)
    special_div = JS.global[:document].getElementById(atome_id)
    special_div.addEventListener('dragover') do |native_event|
      event = Native(native_event)
      special_div[:style][:backgroundColor] = 'red'
      event.preventDefault
      event.stopPropagation
    end

    special_div.addEventListener('dragleave') do |native_event|
      event = Native(native_event)
      special_div[:style][:backgroundColor] = 'yellow'
      event.stopPropagation
    end

    special_div.addEventListener('drop') do |native_event|
      event = Native(native_event)
      event.preventDefault
      event.stopPropagation

      files = event[:dataTransfer][:files]

      if files[:length].to_i > 0
        (0...files[:length].to_i).each do |i|
          file = files[i]
          reader = JS.eval('let a= new FileReader(); return a')
          reader.readAsText(file)
          reader.addEventListener('load') do
            proc.call({ content: reader[:result].to_s, name: file[:name].to_s, type: file[:type].to_s, size: file[:size].to_s })
          end
          reader.addEventListener('error') do
            puts 'Error: ' + file[:name].to_s
          end
        end
      end
    end
    JS.global[:document][:body].addEventListener('drop') do |native_event|
      event = Native(native_event)
      event.preventDefault
      event.stopPropagation
    end
  end
end