Skip to content

Commit

Permalink
Merge pull request #371 from okfn/371-promote-download
Browse files Browse the repository at this point in the history
Promote download when preview is not available
  • Loading branch information
johnmartin committed Feb 12, 2013
2 parents b185d1d + d70d919 commit 27268da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
33 changes: 15 additions & 18 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,8 @@ def resource_preview(resource, pkg_id):
Returns a rendered snippet for a embedded resource preview.
Depending on the type, different previews are loaded.
This could be an img tag where the image is loaded directly or an iframe that
embeds a web page, recline or a pdf preview.
This could be an img tag where the image is loaded directly or an iframe
that embeds a web page, recline or a pdf preview.
'''

format_lower = resource['format'].lower()
Expand All @@ -1376,11 +1376,9 @@ def resource_preview(resource, pkg_id):

if not resource['url']:
log.info('No url for resource {0} defined.'.format(resource['id']))
return snippet(
"dataviewer/snippets/no_preview.html",
resource_type=format_lower,
reason='No valid resource url has been defined.'
)
return snippet("dataviewer/snippets/no_preview.html",
resource_type=format_lower,
reason='No valid resource url has been defined.')
direct_embed = config.get('ckan.preview.direct', '').split()
if not direct_embed:
direct_embed = datapreview.DEFAULT_DIRECT_EMBED
Expand All @@ -1390,24 +1388,23 @@ def resource_preview(resource, pkg_id):

if datapreview.can_be_previewed(data_dict):
url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], id=pkg_id, qualified=True)
resource_id=resource['id'], id=pkg_id, qualified=True)
elif format_lower in direct_embed:
directly = True
url = resource['url']
elif format_lower in loadable_in_iframe:
url = resource['url']
else:
log.info('No preview handler for resource type {0}'.format(format_lower))
return snippet(
"dataviewer/snippets/no_preview.html",
resource_type=format_lower
)

return snippet(
"dataviewer/snippets/data_preview.html",
embed=directly,
resource_url=url
log.info(
'No preview handler for resource type {0}'.format(format_lower)
)
return snippet("dataviewer/snippets/no_preview.html",
resource_type=format_lower)

return snippet("dataviewer/snippets/data_preview.html",
embed=directly,
resource_url=url,
raw_resource_url=resource.get('url'))


def SI_number_span(number):
Expand Down
8 changes: 8 additions & 0 deletions ckan/public/base/javascript/modules/data-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ this.ckan.module('data-viewer', function (jQuery) {
jQuery.proxyAll(this, /_on/);
this.el.on('load', this._onLoad);
this._FirefoxFix();
this.sandbox.subscribe('data-viewer-error', this._onDataViewerError);
},

_onDataViewerError: function(message) {
var parent = this.el.parent();
$('.data-viewer-error .collapse', parent).html(message);
$('.data-viewer-error', parent).removeClass('js-hide');
this.el.hide();
},

_onLoad: function() {
Expand Down
24 changes: 20 additions & 4 deletions ckan/templates/dataviewer/snippets/data_preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@
{# images can be embedded directly #}
<img src="{{ resource_url }}"></img>
{% else %}
<iframe src="{{ resource_url }}" frameborder="0" width="100%" data-module="data-viewer">
<p>{{ _('Your browser does not support iframes.') }}</p>
</iframe>
<div class="data-viewer-error js-hide">
<p class="text-error">
<i class="icon-info-sign"></i>
{{ _('This resource can not be previewed at the moment.') }}
<a href="#" data-toggle="collapse" data-target="#data-view-error">
{{ _('Click here for more information.') }}
</a>
</p>
<p id="data-view-error" class="collapse"></p>
<p>
<a href="{{ raw_resource_url }}" class="btn btn-large" target="_blank">
<i class="icon-large icon-download"></i>
{{ _('Download resource') }}
</a>
</p>
</div>
<iframe src="{{ resource_url }}" frameborder="0" width="100%" data-module="data-viewer">
<p>{{ _('Your browser does not support iframes.') }}</p>
</iframe>
{% endif %}
</div>
</div>
7 changes: 1 addition & 6 deletions ckanext/reclinepreview/theme/public/preview_recline.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ this.ckan.module('reclinepreview', function (jQuery, _) {

function showError(msg){
msg = msg || _('error loading preview');
return self.el
.append('<div></div>')
.addClass('alert alert-error fade in')
.html(msg);
window.parent.ckan.pubsub.publish('data-viewer-error', msg);
}

recline.Backend.DataProxy.timeout = 10000;
Expand Down Expand Up @@ -87,14 +84,12 @@ this.ckan.module('reclinepreview', function (jQuery, _) {
errorMsg = this.options.i18n.errorLoadingPreview + ': ' +this.options.i18n.errorDataProxy;
dataset.fetch()
.done(function(dataset){

dataset.bind('query:fail', function (error) {
jQuery('.data-view-container', self.el).hide();
jQuery('.header', self.el).hide();
});

self.initializeDataExplorer(dataset);
jQuery('.recline-query-editor .text-query').hide();
})
.fail(function(error){
if (error.message) errorMsg += ' (' + error.message + ')';
Expand Down

0 comments on commit 27268da

Please sign in to comment.