Git Product home page Git Product logo

emberdroplet's Introduction

Ember Droplet

Build Status   NPM version

Heroku: http://ember-droplet.herokuapp.com/

Installation

Install via npm: npm install ember-droplet.

Using with Ember CLI

ember-cli > 1.13

bower install ember-droplet --save

Add the following import to your ember-cli-build.js file:

app.import('bower_components/ember-droplet/dist/ember-droplet.js');

ember-cli <= 1.13

bower install ember-droplet --save

Add the following to import to your Brocfile.js:

app.import('vendor/ember-droplet/dist/ember-droplet.min.js');

Features

Ember Droplet allows HTML5 drag and drop functionality in Ember straight out-of-the-box. Its philosophy is that it doesn't impose anything, and instead allows each individual developer to decide how it should work.

EmberDroplet Screenshot

Note: For pre-Ember 2.0.0 support use v0.10.0.

  • Upload with HTML5's drag and drop;
  • MIME type restrictions on permitted file types;
  • Restrictions on the amount of files to be uploaded at any one time;
  • Allow immediate uploading when the user selects a file;
  • Instant image previews upon dropping;
  • Allows the deletion of files before they're uploaded;
  • Keeps a track of all files – even invalid files;
  • Abort requests after they have been sent to the server;

Methods

The DropletMixin contains the following actions:

  • addFiles – Adds files to the queue;
  • deleteFile – Deletes a specified file by model;
  • clearFiles – Clears all valid and invalid files;
  • uploadFiles – Uploads all valid files;
  • abortUpload – Abort the current request;
  • mimeTypes – Specify acceptable MIME types;
  • prepareFiles – Packages file objects into Droplet models;

In addition to the actions, the mixin also has the following computed properties for convenience:

  • validFiles – Provides a list of valid files;
  • invalidFiles – Provides a list of invalid files;
  • uploadedFiles – All uploaded files;
  • deletedFiles – All deleted files;

Getting Started

In order to begin using EmberDroplet, you need to construct an Ember.Component using the Droplet mixin:

App.XDropletComponent = Ember.Component.extend(Droplet, {
    url: location.origin + '/upload'
});

From there you can then add the component in block form to your application as follows — the reason we use it in block form is that other Droplet related mixins can be added as children to x-droplet.;

{{#x-droplet}}{{/x-droplet}}

Options

Note: Specifying a url parameter is mandatory, since no default is assumed.

To override the default options you can assign the following properties on the options object:

  • requestMethod – Changed the request verb from default POST;
  • maximumSize – Set the maximum size for each individual file;
  • maximumValidFiles – Amount of valid files permitted to be in the queue;
  • uploadImmediately – Upload files as they're added to the queue;
  • includeXFileSize – Whether to include the X-File-Size header for progress;
  • useArray – Changes the FormData name of the file to either file[] or file;
  • mimeTypes – List of valid MIME types – can also be changed with mimeTypes method;
  • requestHeaders – Additional request headers to be sent;
  • requestPostData – Additional POST data to be sent;
  App.XDropletComponent = Ember.Component.extend(Droplet, {
    options: {
      requestMethod: Droplet.METHOD.PATCH
      // ...
    }
  });

Once you have instantiated the Droplet Ember.Component in your application, you can instantiate other provided Ember.Component objects for additional functionality:

Hooks

To attach hooks, define them in the hooks object:

  App.XDropletComponent = Ember.Component.extend(Droplet, {
    hooks: {
      didUpload: function() {
        console.log("did an upload");
      }
    }
  });

Droppable Area

App.XDropletAreaComponent = Ember.Component.extend(Droplet.Area);

Use as singular or in block form — Droplet.Area will create a div with the droppable class name for you to style accordingly:

{{x-droplet-area}}

If you need have several droppable areas on the same page handling hooks independently, a context must be supplied:

{{x-droplet-area ctx=this}}

Image Preview

App.XDropletPreviewComponent = Ember.Component.extend(Droplet.Preview);

Use as follows where file is derived from iterating over a computed property:

{{x-droplet-preview image=file}}

Input Field

App.XDropletInputComponent = Ember.Component.extend(Droplet.MultipleInput);

Use in its singular form – can use either Droplet.MultipleInput or Droplet.SingleInput:

{{x-droplet-input}}

If you need have several input fields on the same page handling hooks independently, a context must be supplied:

{{x-droplet-input ctx=this}}

Example

The example uses the Node.js server to upload files, which is available in example/serer. Simply run: npm start — or `foreman start — to create it.

Testing

All of the related tests are written in Jasmine, and can be run with npm run test. You'll also need to run npm i to install the project's dependencies.

Jasmine

emberdroplet's People

Contributors

danielkywang avatar g-cassie avatar gonzalo-bulnes avatar gregoryaveryweir avatar ismyrnow avatar jcowley avatar john-kurkowski avatar mattboldt avatar onlymejosh avatar rupurt avatar wildhoney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

emberdroplet's Issues

v0.6.1 tag?

we use https://rails-assets.org/ to get assets to manage all our js assets. In order for rails assets to compile the newest version it compiles based off tag.

Thanks!

Unbind events on willDestroy

Howdy again,

I'm creating a PR which unbinds the success, load, error & readystatechange handlers when the controller is destroyed but I'm unsure how I should go about mocking XMLHttpRequest. I've used sinon successfully in the past which provides a fake implementation of XMLHttpRequest but I thought I'd check before adding the dependency. The nice thing is that they do provide a standalone server package which handles mocking out ajax requests but doesn't include their spies, mocks, matchers etc...

Droparea

Is there any way to set the drop target to the whole body? I want to then fire a dragEnter event that pops up with a "drag here" dialog.

Currently I'm just trying to expand .droppable to cover the window but it's kind of a messy solution.

Thanks!

Bower

Firstly, what a great component! Thanks for sharing.

I couldn't see it being registered in Bower and therefore wasn't able to install it. Is this something that will come soon?

Cancel File Upload?

To cancel the upload, we would have to run request.abort() however the request object is trapped inside the uploadAllFiles method.

var request = new $window.XMLHttpRequest();

Anyway we could break the request out so we can cancel the upload?

Single input

Howdy again,

Is it possible to support single input files in addition to the existing multiple input of files?

Action returning promise

I don't know if I am missing something, but from what I have read an action cannot return a result, however your action appears to be attempting to return a promise, which much to my frustration I could not access unless I modified the file to set the promise to a property I could access.

No license

You make no mention of ownership or licensing in your README, nor is there a standard "LICENSE" file.

Cheers. :)

Conditional X-File-Size header

Howdy,

So far we've had great success with droplet. Nice work! We're using it to upload videos to Wistia and it looks like they don't support the X-File-Size http header. They throw the following error:

XMLHttpRequest cannot load https://upload.wistia.com/?api_password=[api_password]&project_id=[project_id]. Request header field X-File-Size is not allowed by Access-Control-Allow-Headers. 

How do you feel about making this conditional based on some sort of flag on the DropletView?

cc/ @ryanong

Mixins share Properties

If you are using DropletController in multiple Controllers in your app you will get some strange behaviour. The reason is this:
"Note that mixins extend a constructor's prototype so arrays and object literals defined as properties will be shared amongst objects that implement the mixin. If you want to define a property in a mixin that is not shared, you can define it either as a computed property or have it be created on initialization of the object."
See http://emberjs.com/api/classes/Ember.Mixin.html

The particular problem I am having would be solved if uploadStatus were defined like this:

    uploadStatus: Ember.computed(function(){
        return { uploading: false, percentComplete: 0, error: false };
    }),

Reset uploadedFiles after upload

What's going to be the best way to reset uploadedFiles after the uploads have completed?

I'm trying to provide some user feedback and while the uploadStatus.uploading is fine while the files are being uploaded, it disappears several seconds before the files actually reappear in my app. So, I'm trying to use uploadedFiles to display when the files are being uploaded, but it never resets after the files have been uploaded so I was trying to do it manually.

This code works:

didUploadFiles: function(response) {
  this.set('uploadedFiles', null);
  var images = this.store.findAll('image');
}

But then while trying to upload more files, uploadedFiles never shows up again, I broked it until I refresh the browser of course. :-)

Or perhaps there's a better way to do this? I'll keep looking myself and let you know if I find something.

EmberDroplet witn ember-cli?

Have you tested it using ember-cli? Cause I tried to use it and I get DropletController' is not defined no matter what.

onreadystate handler should only parse JSON responses

Hi,

As mentioned previously we're using droplet with Wistia and they return error responses as text/html. Droplet is throwing an error when trying to parse the response as JSON.

How do you feel about checking the content type for any of the following and return an empty object literal if it doesn't match?

application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json

Override uploadAllFiles?

How can I hook into the file upload process. Can I override an action on the controller to manage what is done with the response? Perhaps a callback on the controller didUploadAllFiles?

Avoid autorun

Howdy,

It looks like a run loop isn't explicitly started in the request event handlers. This can cause a little bit of a delay when between the result coming in from the request and it getting rendered in the app. The two areas I've identified are

Relax bower jQuery dependency

Is there a hard requirement that EmberDroplet only supports jQuery with minor version >= 2.0.3?

We have an app that needs to use jQuery 1.11.1 so would like to relax the range to

"jquery": "1.11.1 - 2.0.x"

Browser support?

Would be nice with information about the browser support in the readme =)

Packaging in amd, named-amd, globals etc

Currently EmberDroplet is only packaged using globals, but amd modules is becoming the prevalent way of packaging components in Ember. Are you interested in changing the packaging to support globals, amd, and named-amd?

I've followed what @instructure does here as an example on how to achieve the same:

when packaging my ember-declarative-authorization component:

It was actually not that hard with the added benefit that the code is cleaner.

uploadAllFiles's new Ember promise has no failure callback

Pretty self explanatory. You added this:

return new Ember.RSVP.Promise(function(resolve, reject) {
         jqXhr.done(resolve).fail(reject);
})
.then($ember.run.bind(this, function(response) {
         // Invoke the `didUploadFiles` callback if it exists.
         $ember.tryInvoke(this, 'didUploadFiles', [response]);
         return response;
}))

But forgot the:

.then(this, function(response) {
    # success stuff
}), function(response){
     # failure stuff
     $ember.tryInvoke(self, 'didUploadFiles', [response]);
     return response;
 });

Expose MultipleInput

Howdy,

I'm trying to figure out how to modify the attributeBindings of MultipleInput so I can set the name property but can't figure out how to get a handle on the MultipleInput class. How do you recommend approaching this. Ideally I would be able to access window.MultipleInput or even better namespace the classes e.g.

Droplet = $ember.Namespace.create();
Droplet.MultipleInput = $ember.View.extend(
   // ...
);

Should `useArray` be the default behaviour?

Would require a small change on the Node.js side, since enforcing the array in the name attribute causes Node.js to nest an array inside of an array. It's a bit clumsy, but at least it's not completely broken for Ruby/PHP scripts, and can also be changed by setting useArray to false.

cannot access JSON response data in uploadAllFiles callback

I'm not sure if I am missing something but I don't think there is currently a way to do this. When I upload files to my API, my API responds with json representing the newly created file objects. I would like to take this json data and inject it into the store (I'm using ember data).

Right now I am using the following code as a work around - but it results in two requests to the server when I really only need one:

          uploadAllFiles: function() {
            var self = this;
            $.when(this._super()).then(function(){
                // reload all of the file objects so the new data is loaded
                self.get('store').find('file');
            });
          }

I'd like to do something like this:

          uploadAllFiles: function() {
            var self = this;
            $.when(this._super()).then(function(data){
                // push the newly created file records into the store
                store.pushPayload('file', data);
            });
          }

Upload status per file

I've needed to hack around EmberDroplet to work how we need it, which is as follows:

  1. Drag file
  2. Upload file instantly
  3. Update Model

I've look at the code but can't figure out how we can attach an upload status to each individual file, is this possible?

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.