Skip to content

Commit

Permalink
[#1506][editresources][l]: Render form errors inside the editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
teajaymars committed Feb 29, 2012
1 parent 39dd7c7 commit 945aa83
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
4 changes: 4 additions & 0 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,7 @@ def tag_link(tag):
def group_link(group):
url = url_for(controller='group', action='read', id=group['name'])
return link_to(group['name'], url)

def dump_json(obj):
import json
return json.dumps(obj)
19 changes: 19 additions & 0 deletions ckan/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,25 @@ hr.resource-list-divide {
background: #eee;
}

/* HasErrors */
.resource-list li.hasErrors {
border-color: #c00;
}
.resource-list li.hasErrors .drag-bars,
.resource-list li.hasErrors a {
color: #c00;
}
.resource-errors {
display: none;
}
.resource-errors dl {
margin-bottom: 0;
}
body.editresources .error-explanation {
/* Let JS render the resource errors inline */
display: none;
}


/* While dragging.... */
.resource-list-edit li.ui-sortable-helper {
Expand Down
75 changes: 53 additions & 22 deletions ckan/public/scripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,6 @@
var CKAN = CKAN || {};

CKAN.Utils = function($, my) {

my.flashMessage = function(msg, category) {
if (!category) {
category = 'info';
}
var messageDiv = $('<div />').html(msg).addClass(category).hide();
$('.flash-messages').append(messageDiv);
messageDiv.show(1200);
};

// Animate the appearance of an element by expanding its height
my.animateHeight = function(element, animTime) {
if (!animTime) animTime = 350;
Expand Down Expand Up @@ -551,12 +541,6 @@ CKAN.Utils = function($, my) {
var flashWarning = function() {
if (boundToUnload) return;
boundToUnload = true;
// Don't show a flash message
//var parentDiv = $('<div />').addClass('flash-messages');
//var messageDiv = $('<div />').html(CKAN.Strings.youHaveUnsavedChanges).addClass('notice').hide();
//parentDiv.append(messageDiv);
//$('#unsaved-warning').append(parentDiv);
//messageDiv.show(200);
// Bind to the window departure event
window.onbeforeunload = function () {
return CKAN.Strings.youHaveUnsavedChanges;
Expand Down Expand Up @@ -605,6 +589,14 @@ CKAN.Utils = function($, my) {
});
};

my.countObject = function(obj) {
var count=0;
$.each(obj, function() {
count++;
});
return count;
};

return my;

}(jQuery, CKAN.Utils || {});
Expand Down Expand Up @@ -640,6 +632,14 @@ CKAN.View.ResourceEditor = Backbone.View.extend({
// Close details button
this.el.find('.resource-panel-close').click(this.closePanel);

// Did we embed some form errors?
if ((typeof global_form_errors == 'object') && global_form_errors.resources) {
for (i in global_form_errors.resources) {
var resource_errors = global_form_errors.resources[i];
this.collection.at(i).view.setErrors(resource_errors);
}
}
// Initial state
this.openFirstPanel();
},
/*
Expand All @@ -648,7 +648,18 @@ CKAN.View.ResourceEditor = Backbone.View.extend({
*/
openFirstPanel: function() {
if (this.collection.length>0) {
this.collection.at(0).view.openMyPanel();
// Open the first resource with errors
var done = false;
this.collection.each(function(resource) {
if (!done && resource.view.hasErrors) {
resource.view.openMyPanel();
done = true;
}
});
if (!done) {
// Fall-through: No resources have errors. Open the first one.
this.collection.at(0).view.openMyPanel();
}
}
else {
this.openAddPanel();
Expand All @@ -666,10 +677,6 @@ CKAN.View.ResourceEditor = Backbone.View.extend({
this.el.find('.resource-details.resource-add').show();
addLi.addClass('active');
panel.show();
console.log(addLi.position());
console.log(addLi.parent());
console.log(addLi.parent().position());
console.log(panel.height());
panel.css('top', Math.max(0, addLi.position().top + addLi.height() - panel.height()));
},
/*
Expand Down Expand Up @@ -745,7 +752,7 @@ CKAN.View.ResourceEditor = Backbone.View.extend({
*/
CKAN.View.Resource = Backbone.View.extend({
initialize: function() {
_.bindAll(this,'updateName','updateIcon','name','askToDelete','openMyPanel');
_.bindAll(this,'updateName','updateIcon','name','askToDelete','openMyPanel','setErrors');
// Generate DOM elements
var resource_object = {
resource: this.model.toTemplateJSON(),
Expand Down Expand Up @@ -780,6 +787,30 @@ CKAN.View.Resource = Backbone.View.extend({
// Set initial state
this.updateName();
this.updateIcon();
this.hasErrors = false;
},
/*
* Process a JSON object of errors attached to this resource
*/
setErrors: function(obj) {
if (CKAN.Utils.countObject(obj) > 0) {
this.hasErrors = true;
this.errors = obj;
this.li.addClass('hasErrors');
var errorList = $('<dl/>').addClass('errorList');
$.each(obj,function(k,v) {
var errorText = '';
var newLine = false;
$.each(v,function(index,value) {
if (newLine) errorText += '<br/>';
errorText += value;
newLine = true;
});
errorList.append($('<dt/>').html(k));
errorList.append($('<dd/>').html(errorText));
});
this.table.find('.resource-errors').append(errorList).show();
}
},
/*
* Work out what I should be called. Rough-match
Expand Down
3 changes: 3 additions & 0 deletions ckan/public/scripts/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ CKAN.Templates.resourceEntry = ' \

CKAN.Templates.resourceDetails = ' \
<div style="display: none;" class="resource-details"> \
<div class="flash-messages"> \
<div class="error resource-errors"></div> \
</div> \
<table> \
<tbody> \
<tr> \
Expand Down
1 change: 1 addition & 0 deletions ckan/templates/package/new_package_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h2>Errors in form</h2>
</ul>
</py:if>
</li>
<script>var global_form_errors = ${h.dump_json(errors)};</script>
</ul>
</div>

Expand Down

0 comments on commit 945aa83

Please sign in to comment.