Skip to content

Commit

Permalink
[#3816] add query field to resource form
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Sep 15, 2017
1 parent af1ceba commit a65b0b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
29 changes: 28 additions & 1 deletion ckan/public/base/javascript/modules/image-upload.js
Expand Up @@ -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: ''
},

Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -54,6 +58,16 @@ this.ckan.module('image-upload', function($) {
this.field_clear = $('<input type="hidden" name="' + options.field_clear +'">')
.appendTo(this.el);

// Button to use a query for this resource
if (this.field_query_input) {
this.button_query = $('<a href="javascript:;" class="btn">' +
'<i class="fa fa-filter"></i>' +
this._('Query') + '</a>')
.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 = $('<a href="javascript:;" class="btn btn-default">' +
'<i class="fa fa-globe"></i>' +
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions ckan/templates/macros/form.html
Expand Up @@ -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') %}
Expand All @@ -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 %}
Expand All @@ -439,6 +438,10 @@
{% endif %}
{% endif %}

{% if is_upload_enabled %}</div>{% 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 %}</div>{% endif %}

{% endmacro %}
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/resource_form.html
Expand Up @@ -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 %}
Expand Down

0 comments on commit a65b0b0

Please sign in to comment.