sweh / ajja

JavaScript library for simple creation of forms and display of collections
MIT License
2 stars 0 forks source link

ListWidget: Provide file upload widget using Dropzone #28

Open sweh opened 8 years ago

sweh commented 8 years ago

Originally reported by: Florian Pilz (Bitbucket: florianpilz, GitHub: florianpilz)


In one project we have used Dropzone to upload files. Each file generates a list entry in ListWidget, thus additional information can be displayed / edited. This seems to be so useful, we should consider integrating it into gocept.jsform.

Our template inside the project:

#!HTML

<div class="field form-group">
  <label class="col-sm-3 control-label">Dokumente</label>
  <div class="col-sm-9">
    <div id="documents-list"
         data-collection-url="${documents_collection_url}"
         data-template="document-item"
         data-form-template="document-edit"
         data-form-options='{
           "title": {"label": "Titel"},
           "category_id": {
             "label": "Kategorie",
             "source": ${category_source}}
         }'
         data-modal-title="Dokument bearbeiten">
    </div>
    <div id="upload" class="dropzone" tal:condition="not view.readonly">
      <div class="dz-message">Bitte Dateien hier ablegen.</div>
    </div>
  </div>
</div>

And JS to set it up:

#!javascript

var widget, dropzone;
widget = new gocept.jsform.ListWidget($('#documents-list'), {
    default_form_actions: []
});
widget.load();

Dropzone.autoDiscover = false;
dropzone = new Dropzone('#upload', {
  'url': '${upload_url}',
  'headers': {
    'X-CSRF-Token': $('#csrf_token').val()
  }
});
dropzone.on('success', function (file, response) {
  widget.render_item(response);
  dropzone.removeFile(file);
});

florianpilz commented 8 years ago

We should not include it by default to keep dependencies low. But maybe we should provide template / integration code, so the developer only has to install dropzone to make it work.