Skip to content

Commit

Permalink
Merge branch '2648-demo-file-upload' into 2375-demo-theme-development
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jul 16, 2012
2 parents e1995cb + c21359d commit c09d8e3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
9 changes: 8 additions & 1 deletion ckan/public/base/javascript/client.js
Expand Up @@ -87,9 +87,16 @@
var modifiedISO = jQuery.date.toISOString(modified);
var filename = meta['filename-original'] || meta.key;
var format = meta._format || filename.split('.').pop();
var url = meta._location;

// If this is a local upload then the returned url will not have a domain
// and we should add one.
if (url.indexOf('://') === -1) {
url = ckan.url(url);
}

return {
url: meta._location,
url: url,
key: meta.key, /* Not strictly Resource data but may be useful */
name: filename,
size: meta._content_length,
Expand Down
54 changes: 48 additions & 6 deletions ckan/public/base/javascript/main.js
@@ -1,13 +1,55 @@
// Global ckan namespace
this.ckan = this.ckan || {};

this.ckan.initialize = function () {
this.module.initialize();
};
(function (ckan, jQuery) {
ckan.initialize = function () {
var body = jQuery('body');
var trailingSlash = /\/$/;
var location = window.location;
var root = location.protocol + location.host;

this.jQuery(function () {
ckan.initialize();
});
this.SITE_ROOT = (body.data('siteRoot') || root).replace(trailingSlash, '');
this.LOCALE_ROOT = (body.data('localeRoot') || root).replace(trailingSlash, '');

this.module.initialize();
};

/* Returns a full url for the current site with the provided path appended.
*
* path - A path to append to the url (default: '/')
* includeLocale - If true the current locale will be added to the page.
*
* Examples
*
* var imageUrl = sandbox.url('/my-image.png');
* // => http://example.ckan.org/my-image.png
*
* var imageUrl = sandbox.url('/my-image.png', true);
* // => http://example.ckan.org/en/my-image.png
*
* var localeUrl = sandbox.url(true);
* // => http://example.ckan.org/en
*
* Returns a url string.
*/
ckan.url = function (path, includeLocale) {
if (typeof path === 'boolean') {
includeLocale = path;
path = '';
}

path = path.replace(/^\//, '');

var root = includeLocale ? ckan.SITE_ROOT : ckan.LOCALE_ROOT;
return path ? root + '/' + path : root;
};

ckan.sandbox.extend({url: ckan.url});

jQuery(function () {
ckan.initialize();
});
})(this.ckan, this.jQuery);

// Temporary banner to let users on IE7 know that it may not display as
// expected.
Expand Down
15 changes: 13 additions & 2 deletions ckan/public/base/javascript/modules/resource-form.js
Expand Up @@ -8,9 +8,20 @@ this.ckan.module('resource-form', function (jQuery, _) {
this.sandbox.unsubscribe('resource:uploaded', this._onResourceUploaded);
},
_onResourceUploaded: function (resource) {
for (var key in resource) {
var key;
var field;

for (key in resource) {
if (resource.hasOwnProperty(key)) {
this.$('[name="' + key + '"]').val(resource[key]);
field = this.$('[name="' + key + '"]');

if (field.is(':checkbox, :radio')) {
this.$('[value="' + resource[key] + '"]').prop('checked', true);
} else if (field.is('select')) {
field.prop('selected', resource[key]);
} else {
field.val(resource[key]);
}
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions ckan/public/base/javascript/modules/resource-upload-field.js
Expand Up @@ -27,8 +27,8 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {
'<span class="resource-upload-field">',
'<i class="ckan-icon ckan-icon-link-plugin"></i>',
'<input type="file" />',
'<input id="field-resource-source-file" type="radio" name="resource_source" value="file.upload" />',
'<label class="radio inline" for="field-resource-source-file"></label>',
'<input id="field-resource-type-file" type="radio" name="resource_type" value="file.upload" />',
'<label class="radio inline" for="field-resource-type-file"></label>',
'</span>'
].join('\n')
},
Expand Down Expand Up @@ -143,10 +143,9 @@ this.ckan.module('resource-upload-field', function (jQuery, _, i18n) {

_onMetadataSuccess: function (data, response) {
var resource = this.sandbox.client.convertStorageMetadataToResource(response);

this.sandbox.notify(_('Resource uploaded').fetch(), '', 'success');
this.sandbox.publish('resource:uploaded', resource);

this.$('input[type=radio]').prop('checked', true);
},

_onMetadataError: function () {
Expand Down
6 changes: 3 additions & 3 deletions ckan/templates/base.html
Expand Up @@ -3,15 +3,15 @@

{# Allows custom attributes to be added to the <html> tag #}
{%- block htmltag -%}
{% set lang = 'en-gb' %}
{% set lang = h.lang() %}
<!--[if IE 7]> <html lang="{{ lang }}" class="ie ie7"> <![endif]-->
<!--[if IE 8]> <html lang="{{ lang }}" class="ie ie8"> <![endif]-->
<!--[if IE 9]> <html lang="{{ lang }}" class="ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="{{ lang }}"> <!--<![endif]-->
{%- endblock -%}

{# Allows custom attributes to be added to the <head> tag #}
{% block headtag %}<head>{% endblock -%}
<head{% block headtag %}{% endblock %}>
{# Append a .js class to the html element #}
<script>document.getElementsByTagName('html')[0].className += ' js';</script>

Expand Down Expand Up @@ -78,7 +78,7 @@
</head>

{# Allows custom attributes to be added to the <body> tag #}
{%- block bodytag %}<body>{% endblock -%}
<body{% block bodytag %} data-site-root="{{ h.url('/', locale='default', qualified=true) }}" data-locale-root="{{ h.url('/', qualified=true) }}"{% endblock %}>

{#
The page block allows you to add content to the page. Most of the time it is
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/new_resource.html
Expand Up @@ -2,7 +2,7 @@

{% block title %}{{ _('Add data to the dataset') }} - {{ super }}{% endblock %}

{% block form %}{{ h.snippet('package/snippets/resource_form.html', data=data, errors=errors, include_metadata=false, pkg_name=pkg_name, stage=stage) }}{% endblock %}
{% block form %}{% snippet 'package/snippets/resource_form.html', data=data, errors=errors, include_metadata=false, pkg_name=pkg_name, stage=stage %}{% endblock %}

{% block secondary_content %}
<section class="module module-narrow">
Expand Down
11 changes: 5 additions & 6 deletions ckan/templates/package/snippets/resource_form.html
Expand Up @@ -19,11 +19,11 @@
of the radio using css. eg. input[type=radio]+label {}
#}
<i class="ckan-icon ckan-icon-link-file"></i>
<input id="field-resource-source-file" type="radio" name="resource_source" checked="checked" value="file" />
<label class="radio inline" for="field-resource-source-file">{{ _('Link to a file') }}</label>
<input id="field-resource-type-file" type="radio" name="resource_type" checked="checked" value="file" />
<label class="radio inline" for="field-resource-type-file">{{ _('Link to a file') }}</label>
<i class="ckan-icon ckan-icon-link-plugin"></i>
<input id="field-resource-source-api" type="radio" name="resource_source" value="api" />
<label class="radio inline" for="field-resource-source-api">{{ _('Link to an API') }}</label>
<input id="field-resource-type-api" type="radio" name="resource_type" value="api" />
<label class="radio inline" for="field-resource-type-api">{{ _('Link to an API') }}</label>
</div>
</div>

Expand All @@ -39,7 +39,7 @@
{% block metadata_fields %}
{% if include_metadata %}
{# TODO: Where do these come from, they don't exist in /package/new_package_form.html #}
{{ form.select('resource_type', id='field-type', label=_('Resource Type'), options={empty: _('Select a type…')}, selected="empty", error=errors.type) }}
{# {{ form.select('resource_type', id='field-type', label=_('Resource Type'), options={empty: _('Select a type…')}, selected="empty", error=errors.type) }} #}

{{ form.input('last_modified', id='field-last-modified', label=_('Last Modified'), placeholder=_('eg. 2012-06-05'), value=data.last_modified, error=errors.last_modified, classes=[]) }}

Expand All @@ -49,7 +49,6 @@

{{ form.input('mimetype_inner', id='field-mimetype-inner', label=_('MIME Type'), placeholder=_('eg. application/json'), value=data.mimetype_inner, error=errors.mimetype_inner, classes=[]) }}
{% else %}
<input type="hidden" name="resource_type" value="{{ data.resource_type }}" />
<input type="hidden" name="last_modified" value="{{ data.last_modified }}" />
<input type="hidden" name="size" value="{{ data.size }}" />
<input type="hidden" name="mimetype" value="{{ data.mimetype }}" />
Expand Down

0 comments on commit c09d8e3

Please sign in to comment.