var noop = function(){};

function FileUploadDropArea(){
  return {
    view: function(vnode){
      var cb = vnode.attrs.cb;
      if(typeof cb !== 'Function'){
        cb = noop;
        }
      return m('.file-upload-drop-area',{
        ondragover: function(e){
          e.preventDefault();
          },
        ondrop: function(e){
          e.preventDefault();  // don't open the file in the browser
          var files = [];
          
          // some browsers use `.items` and some use `.files`, so check for both:
          if (e.dataTransfer.items) {
            for (var i = 0; i < e.dataTransfer.items.length; i++) {
              if (e.dataTransfer.items[i].kind === 'file') {  // If dropped items aren't files, reject them
                files.push( e.dataTransfer.items[i].getAsFile() );
                }
              }
            }
          else {
            for (var i = 0; i < e.dataTransfer.files.length; i++) {
              files.push( e.dataTransfer.files[i] );
              }
            }
          for(var i=0, file=null; i<files.length; i++){
            file = files[i]
            vnode.attrs.cb(file);
            }
          }
        }, 'Drop files here...');
      }
    };
  }

export default FileUploadDropArea;