diff --git a/CHANGES.rst b/CHANGES.rst index d1c365d..9980ca0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ----- diff --git a/docs/configuration.rst b/docs/configuration.rst index baae4ee..79ccb10 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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 @@ -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 `__. +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 ---------------------------------- diff --git a/examples/custom-options/static/js/dz_init.js b/examples/custom-options/static/js/dz_init.js new file mode 100644 index 0000000..1968260 --- /dev/null +++ b/examples/custom-options/static/js/dz_init.js @@ -0,0 +1,6 @@ +dz = this +document + .getElementById('upload-btn-bar') + .addEventListener('click', function handler(e) { + dz.processQueue() + }) diff --git a/examples/custom-options/templates/index.html b/examples/custom-options/templates/index.html index 9a7f026..7152f4f 100644 --- a/examples/custom-options/templates/index.html +++ b/examples/custom-options/templates/index.html @@ -7,10 +7,13 @@ {{ dropzone.style('border: 2px dashed #0087F7; margin: 10%; min-height: 400px;') }} - {{ dropzone.create(action='upload') }} - + {{ dropzone.create(id='foo', action='upload') }} + + {{ dropzone.create(id='bar', action='upload') }} + {{ 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')}} diff --git a/flask_dropzone/__init__.py b/flask_dropzone/__init__.py index 8688d9b..b2b85ba 100644 --- a/flask_dropzone/__init__.py +++ b/flask_dropzone/__init__.py @@ -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 @@ -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. @@ -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 += ',' diff --git a/setup.py b/setup.py index ea3aece..7293cc9 100644 --- a/setup.py +++ b/setup.py @@ -31,6 +31,7 @@ install_requires=[ 'Flask' ], + test_suite='tests', keywords='flask extension development upload', classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/static/dz_init.js b/tests/static/dz_init.js new file mode 100644 index 0000000..86dd5cf --- /dev/null +++ b/tests/static/dz_init.js @@ -0,0 +1,3 @@ +this.on('addedfile', () => { + alert('Added file.') +}) diff --git a/tests/static/js/dz_init.js b/tests/static/js/dz_init.js new file mode 100644 index 0000000..1e9c993 --- /dev/null +++ b/tests/static/js/dz_init.js @@ -0,0 +1,3 @@ +this.on('success', () => { + alert('Success.') +}) diff --git a/test_flask_dropzone.py b/tests/test_flask_dropzone.py similarity index 94% rename from test_flask_dropzone.py rename to tests/test_flask_dropzone.py index 969dad0..7c79444 100644 --- a/test_flask_dropzone.py +++ b/tests/test_flask_dropzone.py @@ -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)