diff --git a/ckan/public/base/javascript/modules/image-upload.js b/ckan/public/base/javascript/modules/image-upload.js index 49255b711d5..dedbedcf723 100644 --- a/ckan/public/base/javascript/modules/image-upload.js +++ b/ckan/public/base/javascript/modules/image-upload.js @@ -11,6 +11,7 @@ this.ckan.module('image-upload', function($) { field_url: 'image_url', field_clear: 'clear_upload', field_name: 'name', + field_query: 'query', upload_label: '' }, @@ -33,6 +34,7 @@ this.ckan.module('image-upload', function($) { var field_url = 'input[name="' + options.field_url + '"]'; var field_clear = 'input[name="' + options.field_clear + '"]'; var field_name = 'input[name="' + options.field_name + '"]'; + var field_query = 'textarea[name="' + options.field_query + '"]'; this.input = $(field_upload, this.el); this.field_url = $(field_url, this.el).parents('.form-group'); @@ -43,6 +45,8 @@ this.ckan.module('image-upload', function($) { this.label_location = $('label[for="field-image-url"]'); // determines if the resource is a data resource this.is_data_resource = (this.options.field_url === 'url') && (this.options.field_upload === 'upload'); + this.field_query = $(field_query).parents('.control-group'); + this.field_query_input = $(field_query)[0]; // Is there a clear checkbox on the form already? var checkbox = $(field_clear, this.el); @@ -54,6 +58,16 @@ this.ckan.module('image-upload', function($) { this.field_clear = $('') .appendTo(this.el); + // Button to use a query for this resource + if (this.field_query_input) { + this.button_query = $('' + + '' + + this._('Query') + '') + .prop('title', this._('Query data from other resources')) + .on('click', this._onQuery) + .insertAfter(this.input); + } + // Button to set the field to be a URL this.button_url = $('' + '' + @@ -93,7 +107,8 @@ this.ckan.module('image-upload', function($) { .add(this.button_url) .add(this.input) .add(this.field_url) - .add(this.field_image); + .add(this.field_image) + .add(this.field_query); // Disables autoName if user modifies name field this.field_name @@ -117,6 +132,8 @@ this.ckan.module('image-upload', function($) { this.field_url_input.val(filename); this._updateUrlLabel(this._('File')); + } else if (this.field_query_input && this.field_query_input.value) { + this._showOnlyQuery(); } else { this._showOnlyButtons(); } @@ -178,6 +195,12 @@ this.ckan.module('image-upload', function($) { this._updateUrlLabel(this._('URL')); }, + _onQuery: function() { + this._showOnlyQuery(); + this.field_query_input.focus() + .on('blur', this._onQueryBlur); + }, + /* Event listener for resetting the field back to the blank state * * Returns nothing. @@ -231,6 +254,10 @@ this.ckan.module('image-upload', function($) { this.field_url.show(); }, + _showOnlyQuery: function() { + this.fields.hide(); + this.field_query.show(); + }, /* Event listener for when a user mouseovers the hidden file input * * Returns nothing. diff --git a/ckan/templates/macros/form.html b/ckan/templates/macros/form.html index 4459f6d615b..fb0ffc5d195 100644 --- a/ckan/templates/macros/form.html +++ b/ckan/templates/macros/form.html @@ -418,7 +418,7 @@ #} {% macro image_upload(data, errors, field_url='image_url', field_upload='image_upload', field_clear='clear_upload', is_url=false, is_upload=false, is_upload_enabled=false, placeholder=false, - url_label='', upload_label='', field_name='image_url') %} + url_label='', upload_label='', field_name='image_url', query=false) %} {% set placeholder = placeholder if placeholder else _('http://example.com/my-image.jpg') %} {% set url_label = url_label or _('Image URL') %} {% set upload_label = upload_label or _('Image') %} @@ -428,8 +428,7 @@ data-module-field_url="{{ field_url }}" data-module-field_upload="{{ field_upload }}" data-module-field_clear="{{ field_clear }}" data-module-upload_label="{{ upload_label }}" data-module-field_name="{{ field_name }}"> {% endif %} - - {{ input(field_url, label=url_label, id='field-image-url', type='url', placeholder=placeholder, value=data.get(field_url), error=errors.get(field_url), classes=['control-full']) }} + {{ input(field_url, label=url_label, id='field-image-url', type='url', placeholder=placeholder, value=data.get(field_url), error=errors.get(field_url), classes=['control-full']) }} {% if is_upload_enabled %} @@ -439,6 +438,10 @@ {% endif %} {% endif %} - {% if is_upload_enabled %}{% endif %} + {% if query %} + {{ textarea('query', label=_('Query'), id='field-query', value=data.get('query'), placeholder="SELECT * FROM ...", rows=5, cols=20) }} + {% endif %} + + {% if is_upload_enabled %}{% endif %} {% endmacro %} diff --git a/ckan/templates/package/snippets/resource_form.html b/ckan/templates/package/snippets/resource_form.html index 640234be3d9..9468d5260a8 100644 --- a/ckan/templates/package/snippets/resource_form.html +++ b/ckan/templates/package/snippets/resource_form.html @@ -23,7 +23,7 @@ {% set is_upload = (data.url_type == 'upload') %} {{ form.image_upload(data, errors, field_url='url', field_upload='upload', field_clear='clear_upload', is_upload_enabled=h.uploads_enabled(), is_url=data.url and not is_upload, is_upload=is_upload, - upload_label=_('Data'), url_label=_('URL'), placeholder=_('http://example.com/external-data.csv'), field_name='name') }} + upload_label=_('Data'), url_label=_('URL'), placeholder=_('http://example.com/external-data.csv'), field_name='name', query=true) }} {% endblock %} {% block basic_fields_name %}