Skip to content

Commit

Permalink
[#1602,js][m]: replace old Data Explorer with new Recline Data Explorer.
Browse files Browse the repository at this point in the history
* Everything working (as far as I can tell using my demo resource data).
  • Loading branch information
rufuspollock committed Jan 26, 2012
1 parent 92504e7 commit 7c78221
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
67 changes: 38 additions & 29 deletions ckan/public/scripts/application.js
Expand Up @@ -31,7 +31,7 @@

var isResourceView = $('body.package.resource_read').length > 0;
if (isResourceView) {
CKANEXT.DATAPREVIEW.setupDataPreview(preload_resource);
CKANEXT.DATAPREVIEW.loadPreviewDialog(preload_resource);
}

var isDatasetNew = $('body.package.new').length > 0;
Expand Down Expand Up @@ -742,17 +742,6 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
my.dialogId = 'ckanext-datapreview';
my.$dialog = $('#' + my.dialogId);

// Initialize data explorer on Resource view page
//
// resourceData: resource as simple hash (suitable for initializing backbone model or result of backboneModel.toJSON())
my.setupDataPreview = function(resourceData) {
// initialize the tableviewer system
DATAEXPLORER.TABLEVIEW.initialize(my.dialogId);
resourceData.formatNormalized = my.normalizeFormat(resourceData.format);

my.loadPreviewDialog(resourceData);
};

// **Public: Loads a data preview**
//
// Fetches the preview data object from the link provided and loads the
Expand All @@ -763,16 +752,30 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
//
// Returns nothing.
my.loadPreviewDialog = function(resourceData) {
resourceData.url = my.normalizeUrl(resourceData.url);
my.$dialog.html('<h4>Loading ... <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="loading-spinner" /></h4>');

function initializeDataExplorer(dataset) {
var dataExplorer = new recline.View.DataExplorer({
el: my.$dialog
, model: dataset
, config: {
readOnly: true
}
});
// will have to refactor if this can get called multiple times
Backbone.history.start();
}

// 4 situations
// a) have a webstore_url
// b) csv or xls (but not webstore)
// c) can be treated as plain text
// d) none of the above but worth iframing (assumption is
// that if we got here (i.e. preview shown) worth doing
// something ...)
resourceData.formatNormalized = my.normalizeFormat(resourceData.format);

resourceData.url = my.normalizeUrl(resourceData.url);
if (resourceData.formatNormalized === '') {
var tmp = resourceData.url.split('/');
tmp = tmp[tmp.length - 1];
Expand All @@ -785,18 +788,21 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
}

if (resourceData.webstore_url) {
var _url = resourceData.webstore_url + '.jsontuples?_limit=500';
my.getResourceDataDirect(_url, function(data) {
DATAEXPLORER.TABLEVIEW.showData(data);
DATAEXPLORER.TABLEVIEW.$dialog.dialog('open');
var backend = new recline.Model.BackendWebstore({
url: resourceData.webstore_url
});
recline.Model.setBackend(backend);
var dataset = backend.getDataset();
initializeDataExplorer(dataset);
}
else if (resourceData.formatNormalized in {'csv': '', 'xls': ''}) {
var _url = my.jsonpdataproxyUrl + '?url=' + resourceData.url + '&type=' + resourceData.formatNormalized;
my.getResourceDataDirect(_url, function(data) {
DATAEXPLORER.TABLEVIEW.showData(data);
DATAEXPLORER.TABLEVIEW.$dialog.dialog('open');
var backend = new recline.Model.BackendDataProxy({
url: resourceData.url
, type: resourceData.formatNormalized
});
recline.Model.setBackend(backend);
var dataset = backend.getDataset();
initializeDataExplorer(dataset);
}
else if (resourceData.formatNormalized in {
'rdf+xml': '',
Expand All @@ -816,7 +822,6 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
var _url = my.jsonpdataproxyUrl + '?type=csv&url=' + resourceData.url;
my.getResourceDataDirect(_url, function(data) {
my.showPlainTextData(data);
DATAEXPLORER.TABLEVIEW.$dialog.dialog('open');
});
}
else if (resourceData.formatNormalized in {'html':'', 'htm':''}
Expand All @@ -833,7 +838,10 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
// Cannot reliably preview this item - with no mimetype/format information,
// can't guarantee it's not a remote binary file such as an executable.
var _msg = $('<p class="error">We are unable to preview this type of resource: ' + resourceData.formatNormalized + '</p>');
my.$dialog.html(_msg);
my.showError({
title: 'Unable to preview'
, message: _msg
});
}
};

Expand Down Expand Up @@ -884,22 +892,23 @@ CKAN.View.ResourceAddLink = Backbone.View.extend({
//
// Returns nothing.
my.showPlainTextData = function(data) {
// HACK: have to reach into DATAEXPLORER.TABLEVIEW dialog a lot ...
DATAEXPLORER.TABLEVIEW.setupFullscreenDialog();

if(data.error) {
DATAEXPLORER.TABLEVIEW.showError(data.error);
my.showError(data.error);
} else {
var content = $('<pre></pre>');
for (var i=0; i<data.data.length; i++) {
var row = data.data[i].join(',') + '\n';
content.append(my.escapeHTML(row));
}
DATAEXPLORER.TABLEVIEW.$dialog.dialog(DATAEXPLORER.TABLEVIEW.dialogOptions);
DATAEXPLORER.TABLEVIEW.$dialog.append(content);
my.$dialog.html(content);
}
};

my.showError = function (error) {
var _html = '<strong>' + $.trim(error.title) + '</strong><br />' + $.trim(error.message);
my.$dialog.html(_html);
};

my.normalizeFormat = function(format) {
var out = format.toLowerCase();
out = out.split('/');
Expand Down
13 changes: 4 additions & 9 deletions ckan/templates/package/resource_read.html
Expand Up @@ -13,9 +13,8 @@

<py:def function="optional_head">
<!-- data preview -->
<link rel="stylesheet" href="${h.url_for('/scripts/vendor/slickgrid/slick.grid.css')}" />
<link rel="stylesheet" href="${h.url_for('/scripts/vendor/slickgrid/slick.columnpicker.css')}" />
<link rel="stylesheet" href="${h.url_for('/scripts/dataexplorer/table-view.css')}" />
<link rel="stylesheet" href="${h.url_for('/scripts/vendor/recline/css/data-explorer.css')}" />
<link rel="stylesheet" href="${h.url_for('/scripts/vendor/recline/css/graph-flot.css')}" />
<!-- /data preview -->
<script type="text/javascript">
var preload_resource = ${c.resource_json};
Expand Down Expand Up @@ -118,13 +117,9 @@ <h3>Additional Information</h3>

<py:def function="optional_footer">
<!-- data preview -->
<script type="text/javascript" src="${h.url_for('/scripts/vendor/jquery.event.drag/2.0/jquery.event.drag.min.js')}"></script>
<script type="text/javascript" src="${h.url_for('/scripts/vendor/slickgrid/slick.grid.js')}"></script>
<script type="text/javascript" src="${h.url_for('/scripts/vendor/slickgrid/slick.columnpicker.js')}"></script>
<script type="text/javascript" src="${h.url_for('/scripts/vendor/jquery.mustache/jquery.mustache.js')}"></script>
<script type="text/javascript" src="${h.url_for('/scripts/vendor/flot/0.7/jquery.flot.js')}"></script>
<script src="${h.url_for('/scripts/dataexplorer/table-view.js')}"></script>
<script src="${h.url_for('/scripts/dataexplorer/table-view.ui.js')}"></script>
<script src="${h.url_for('/scripts/dataexplorer/table-view-template.js')}"></script>
<script src="${h.url_for('/scripts/vendor/recline/recline.js')}"></script>
</py:def>

<xi:include href="layout.html" />
Expand Down

0 comments on commit 7c78221

Please sign in to comment.