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

Schema editor integration #25

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
98 changes: 97 additions & 1 deletion ckanext/validation/fanstatic/js/module-resource-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ this.ckan.module('resource-schema', function($) {
.on('click', this._onFromJSON);
$('.controls', this.buttons_div).append(this.button_json);

// Button to infer schema
this.source_file = null
this.schema_file = null
this.schema_editor = null
this.div_modal = $('#field-schema-modal')
this.div_editor = $('#field-schema-editor')
this.button_infer = $('<a href="javascript:;" class="btn btn-default">' +
'<i class="fa fa-edit"></i>' +
this._('Infer') + '</a>')
.prop('title', this._('Infer a schema based on the data file'))
.on('click', (function(ev) {
this._onEditSchema(this.source_file, null)
}).bind(this))
.hide();
$('.controls', this.buttons_div).append(this.button_infer);
$('#field-image-url').change((function(ev) {
this.source_file = ev.target.value
this.button_infer.show()
}).bind(this))
$('#field-image-upload').change((function(ev) {
this.source_file = ev.target.files[0]
this.button_infer.show()
}).bind(this))
$('.btn-remove-url').click((function(ev) {
this.source_file = null
this.button_infer.hide()
}).bind(this))

// Remove text
var removeText = this._('Clear');

// Change the clear file upload button too
Expand All @@ -98,18 +127,41 @@ this.ckan.module('resource-schema', function($) {
.on('click', this._onRemoveURL)
.insertBefore(this.field_url_input);

// TODO: disable?
Copy link
Contributor Author

@roll roll Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amercader
Can you please take a look how it works now?

TBH I feel like I'm missing something regarding your comment here - #34 (comment). And because of the bad sound quality haven't got fully your explanation during our call.

Of course, I'm pretty neutral on enabling/disabling the linked schema editing - just curious why the user can't edit the linked schema based on the same approach we have for the create resource page.

// Button to open the schema editor when there is a URL set
$('<a href="javascript:;" class="btn btn-default btn-remove-url" style="right: 75px;">'
+ 'Editor' + '</a>')
.prop('title', 'Open the schema editor')
.on('click', (function(ev) {
var schema = this.field_url_input.prop('readonly')
? this.schema_file
: this.field_url_input.val()
this._onEditSchema(null, schema)
}).bind(this))
.insertBefore(this.field_url_input);

// Button for resetting the form when there is a JSON text set
$('<a href="javascript:;" class="btn btn-default btn-remove-url">'
+ removeText + '</a>')
.prop('title', removeText)
.on('click', this._onRemoveJSON)
.insertBefore(this.field_json_input);

// Button to open the schema editor when there is a JSON text set
$('<a href="javascript:;" class="btn btn-default btn-remove-url" style="right: 75px;">'
+ 'Editor' + '</a>')
.prop('title', 'Open the schema editor')
.on('click', (function(ev) {
this._onEditSchema(null, this.field_json_input.val())
}).bind(this))
.insertBefore(this.field_json_input);

// Fields storage. Used in this.changeState
this.fields = $('<i />')
.add(this.button_upload)
.add(this.button_url)
.add(this.button_json)
.add(this.button_infer)
.add(this.field_upload)
.add(this.field_url)
.add(this.field_json);
Expand All @@ -121,6 +173,7 @@ this.ckan.module('resource-schema', function($) {
} else {
this._showOnlyButtons();
}

},

/* Update the `this.label` text
Expand Down Expand Up @@ -190,6 +243,9 @@ this.ckan.module('resource-schema', function($) {
this.field_json_input.val('');
this.field_json_input.prop('readonly', false);

// Show infer button if source file is provided
if (this.source_file) this.button_infer.show()

this.field_schema_input.val('');
},

Expand Down Expand Up @@ -227,9 +283,12 @@ this.ckan.module('resource-schema', function($) {
}
},

_onInputChange: function() {
_onInputChange: function(ev) {
var file_name = this.field_upload_input.val().split(/^C:\\fakepath\\/).pop();

// Save schema file
this.schema_file = ev.target.files[0]

this.field_url_input.val(file_name);
this.field_url_input.prop('readonly', true);

Expand All @@ -253,6 +312,43 @@ this.ckan.module('resource-schema', function($) {
return url; // filename
},

_onEditSchema: function(source, schema) {

// Props
var props = {
source: source,
schema: schema,
onSave: this._onSaveSchema,
disablePreview: true,
}

// Render/show
var element = this.div_editor[0]
var component = tableschemaUI.EditorSchema
if (this.schema_editor) this.schema_editor.dispose()
this.schema_editor = tableschemaUI.render(component, props, element)
this.div_modal.modal('show')

},

_onSaveSchema: function(schema, error) {

// Success
if (!error) {
var schemaText = JSON.stringify(schema, null, 2)
this.field_url_input.prop('readonly', false)
this.field_url_input.val('')
this.field_json_input.val(schemaText)
this.field_schema_input.val(schemaText)
this._showOnlyFieldJSON()
}

// Dispose
this.div_modal.modal('hide')
this.schema_editor.dispose()
this.schema_editor = null

}

};
});
2 changes: 2 additions & 0 deletions ckanext/validation/fanstatic/resource.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ main =
css/validation.css

resource-schema-form =
vendor/tableschema-ui/tableschema-ui.js

js/module-resource-schema.js

report-form =
Expand Down
Loading