Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external JavaScript file #54

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ WARNING: **New major upstream release (backwards incompatible!).**
* Remove ``dropzone.load()`` method.
* Added more options to customize messages.
* Drop Python 2 support.
* Add support to pass external JavaScript file path into ``custom_init`` parameter in ``dropzone.config()``.

1.6.0
-----
Expand Down
12 changes: 11 additions & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ details on these options.
Custom Configuration String
----------------------------

Sometimes you may need more flexible, you can use ``custom_init``and ``custom_options``
Sometimes you may need more flexibility, you can use ``custom_init`` and ``custom_options``
to pass custom JavaScript code:

.. code-block:: jinja
Expand All @@ -81,6 +81,16 @@ The code pass with ``custom_init`` will into ``init: function() {}``, the code p
``Dropzone.options.myDropzone = {}``. See the full list of available configuration settings on
`Dropzone documentation <https://www.dropzonejs.com/#configuration>`__.

More recommended, you can move the ``custom_init`` code into a external JavaScript file
and pass the file path into ``custom_init`` parameter for a better code readability and maintainability.:

.. code-block:: jinja

{{ dropzone.config(custom_init='js/dz_init.js',
custom_options='autoProcessQueue: false, addRemoveLinks: true, parallelUploads: 20,') }}

Notice that you need put the JavaScript file under your application static folder.

Overwriting Global Configuration
----------------------------------

Expand Down
6 changes: 6 additions & 0 deletions examples/custom-options/static/js/dz_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dz = this
document
.getElementById('upload-btn-bar')
.addEventListener('click', function handler(e) {
dz.processQueue()
})
9 changes: 6 additions & 3 deletions examples/custom-options/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
{{ dropzone.style('border: 2px dashed #0087F7; margin: 10%; min-height: 400px;') }}
</head>
<body>
{{ dropzone.create(action='upload') }}
<button id="upload-btn">Upload</button>
{{ dropzone.create(id='foo', action='upload') }}
<button id="upload-btn-foo">Upload</button>
{{ dropzone.create(id='bar', action='upload') }}
<button id="upload-btn-bar">Upload</button>
{{ dropzone.load_js() }}
{{ dropzone.config(custom_init='dz = this;document.getElementById("upload-btn").addEventListener("click", function handler(e) {dz.processQueue();});',
{{ dropzone.config(id='foo', custom_init='dz = this;document.getElementById("upload-btn-foo").addEventListener("click", function handler(e) {dz.processQueue();});',
custom_options='autoProcessQueue: false, addRemoveLinks: true, parallelUploads: 20,') }}
{{ dropzone.config(id='bar', custom_init='js/dz_init.js')}}
</body>
</html>
17 changes: 15 additions & 2 deletions flask_dropzone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:copyright: (c) 2017 by Grey Li.
:license: MIT, see LICENSE for more details.
"""
import os
import warnings
from flask import Blueprint, current_app, url_for, Markup, render_template_string

Expand Down Expand Up @@ -181,6 +182,11 @@ def load_js(js_url=None, version='5.2.0'):
def config(redirect_url=None, custom_init='', custom_options='', nonce=None, id='myDropzone', **kwargs):
"""Initialize dropzone configuration.

.. versionchanged:: 2.0.0
Support to pass external JavaScript file path into ``custom_init`` parameter,
notice the file need to be under the application static folder.
For example: ``custom_init='js/dz_init.js'``.

.. versionchanged:: 1.5.4
Added ``id`` parameter.

Expand All @@ -195,8 +201,15 @@ def config(redirect_url=None, custom_init='', custom_options='', nonce=None, id=
:param **kwargs: Mirror configuration variable, lowercase and without prefix.
For example, ``DROPZONE_UPLOAD_MULTIPLE`` becomes ``upload_multiple`` here.
"""
if custom_init and not custom_init.strip().endswith(';'):
custom_init += ';'
custom_init = custom_init.strip().strip('/')
if custom_init:
if custom_init.endswith('.js'):
path = os.path.split(custom_init)
final_path = os.path.join(current_app.static_folder, *path)
with open(final_path, 'r') as f:
custom_init = f.read()
elif not custom_init.endswith(';'):
custom_init += ';'

if custom_options and not custom_options.strip().endswith(','):
custom_options += ','
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
install_requires=[
'Flask'
],
test_suite='tests',
keywords='flask extension development upload',
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
Empty file added tests/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions tests/static/dz_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.on('addedfile', () => {
alert('Added file.')
})
3 changes: 3 additions & 0 deletions tests/static/js/dz_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this.on('success', () => {
alert('Success.')
})
14 changes: 14 additions & 0 deletions test_flask_dropzone.py → tests/test_flask_dropzone.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ def test_custom_js(self):
rv = self.dropzone.config(custom_options='foo = true')
self.assertIn('foo = true,', rv)

def test_external_js_path(self):
rv = self.dropzone.config(custom_init='dz_init.js')
self.assertIn('Added file.', rv)

rv = self.dropzone.config(custom_init='js/dz_init.js')
self.assertIn('Success.', rv)

def test_external_js_path_with_slash(self):
rv = self.dropzone.config(custom_init='/dz_init.js/')
self.assertIn('Added file.', rv)

rv = self.dropzone.config(custom_init='/js/dz_init.js/')
self.assertIn('Success.', rv)

def test_custom_id(self):
rv = self.dropzone.create(action=url_for('upload'))
self.assertIn('id="myDropzone"', rv)
Expand Down