Code Monkey home page Code Monkey logo

drop.js's Introduction

Please read the whole readme in order to have a clear idea of what Drop.js does.

Installation

Download the .zip file from github (and extract it), or clone the folder using git clone. Import the drop.js script in your willing HTML file.

<script type="text/javascript" src="path/to/drop.js"></script>

Drop constructor usage

You can initialize a drop object from constructor, without calling any additional functions.

function dropHandler(dropData, e) {
    // ...
}
var dropzone = new Drop({
    node: "#dropzone", // a selector, could be a Node instance
    ondrop: dropHandler,
});

The dropData argument

The dropData argument in ondrop method is the important stuff. It contains all the files, or strings it could retrieve from the event object. It contains 2 properties, files and strings.

Files property's value is an array containing all the files that occured in the drag operation. Strings property's value is an object containing value for every mimeType that was dropped on the node. (example and further explanations below).

Example dropObject

dropObject = {
    files: [{
        // usable File object
        file: File,
        // if in chrome, full relative path
        path: "Folder/Subfolder/Image.png",
    }],
    strings: {
        plain: {
            type: "text/plain",
            // when URL is dropped, the plain value is also present
            value: "http://example.com",
        },
        uri_list: {
            type: "text/uri-list",
            value: "http://example.com",
        },
        html: {
            // if the link was dragged from another HTML page
            // the HTML might be present too
            type: "text/html",
            value: "<a href='http://example.com'>Link</a>",
        }
    },
}

Some Notices

So, if links, texts or an image from other page is dragged and dropped to your node, there are several values present in the drag operation. Usually, there always is a text/plain value which represents an URL, a string, or just the url of the image. In other scenarios the text/uri-list might be present too, and even text/html value that contains the <img src="..."> that can be parsed if you will.

But those values are presented in the DataTransferItemList which is only supported in chrome yet via accessing the DataTransfer.items property. In safari and firefox, those values are not present. But DataTransfer.getItem method allows us to extract data from DataTransfer by mime type.

So in that case where .items is not present, the code will extract three values, text/plain, text/uri-list or the text/html values.

The keys in the .items property are generated from the mime type by using the second part of mime type (after the slash) and replacing '-' character with '_', so that it can directly be accessed.

drop.js's People

Stargazers

 avatar Revaz Benidze avatar Nikolozi Bodaveli avatar  avatar Tornike Shavidze avatar Guri Getsadze avatar

Watchers

James Cloos avatar Luka Kvavilashvili avatar

drop.js's Issues

jQuery Plugin

I suggest to create a jQuery plugin with same functionality.

Repo works great ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.