Display validation messages for any xblock on the container page.
TNL-683
This commit is contained in:
@@ -1290,10 +1290,12 @@ class GroupConfiguration(object):
|
||||
'container_handler',
|
||||
course.location.course_key.make_usage_key(unit.location.block_type, unit.location.name)
|
||||
)
|
||||
|
||||
validation_summary = split_test.general_validation_message()
|
||||
usage_info[split_test.user_partition_id].append({
|
||||
'label': '{} / {}'.format(unit.display_name, split_test.display_name),
|
||||
'url': unit_url,
|
||||
'validation': split_test.general_validation_message,
|
||||
'validation': validation_summary.to_json() if validation_summary else None,
|
||||
})
|
||||
return usage_info
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ from contentstore.views.course import GroupConfiguration
|
||||
from contentstore.tests.utils import CourseTestCase
|
||||
from xmodule.partitions.partitions import Group, UserPartition
|
||||
from xmodule.modulestore.tests.factories import ItemFactory
|
||||
from xmodule.split_test_module import ValidationMessage, ValidationMessageType
|
||||
from xmodule.validation import StudioValidation, StudioValidationMessage
|
||||
from xmodule.modulestore.django import modulestore
|
||||
from xmodule.modulestore import ModuleStoreEnum
|
||||
|
||||
@@ -541,87 +541,75 @@ class GroupConfigurationsValidationTestCase(CourseTestCase, HelperMethods):
|
||||
def setUp(self):
|
||||
super(GroupConfigurationsValidationTestCase, self).setUp()
|
||||
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validation_messages')
|
||||
def test_error_message_present(self, mocked_validation_messages):
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validate_split_test')
|
||||
def verify_validation_add_usage_info(self, expected_result, mocked_message, mocked_validation_messages):
|
||||
"""
|
||||
Tests if validation message is present.
|
||||
Helper method for testing validation information present after add_usage_info.
|
||||
"""
|
||||
self._add_user_partitions()
|
||||
split_test = self._create_content_experiment(cid=0, name_suffix='0')[1]
|
||||
|
||||
mocked_validation_messages.return_value = [
|
||||
ValidationMessage(
|
||||
split_test,
|
||||
u"Validation message",
|
||||
ValidationMessageType.error
|
||||
)
|
||||
]
|
||||
group_configuration = GroupConfiguration.add_usage_info(self.course, self.store)[0]
|
||||
self.assertEqual(
|
||||
group_configuration['usage'][0]['validation'],
|
||||
{
|
||||
'message': u'This content experiment has issues that affect content visibility.',
|
||||
'type': 'error'
|
||||
}
|
||||
)
|
||||
validation = StudioValidation(split_test.location)
|
||||
validation.add(mocked_message)
|
||||
mocked_validation_messages.return_value = validation
|
||||
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validation_messages')
|
||||
def test_warning_message_present(self, mocked_validation_messages):
|
||||
group_configuration = GroupConfiguration.add_usage_info(self.course, self.store)[0]
|
||||
self.assertEqual(expected_result.to_json(), group_configuration['usage'][0]['validation'])
|
||||
|
||||
def test_error_message_present(self):
|
||||
"""
|
||||
Tests if validation message is present.
|
||||
Tests if validation message is present (error case).
|
||||
"""
|
||||
mocked_message = StudioValidationMessage(StudioValidationMessage.ERROR, u"Validation message")
|
||||
expected_result = StudioValidationMessage(
|
||||
StudioValidationMessage.ERROR, u"This content experiment has issues that affect content visibility."
|
||||
)
|
||||
self.verify_validation_add_usage_info(expected_result, mocked_message) # pylint: disable=no-value-for-parameter
|
||||
|
||||
def test_warning_message_present(self):
|
||||
"""
|
||||
Tests if validation message is present (warning case).
|
||||
"""
|
||||
mocked_message = StudioValidationMessage(StudioValidationMessage.WARNING, u"Validation message")
|
||||
expected_result = StudioValidationMessage(
|
||||
StudioValidationMessage.WARNING, u"This content experiment has issues that affect content visibility."
|
||||
)
|
||||
self.verify_validation_add_usage_info(expected_result, mocked_message) # pylint: disable=no-value-for-parameter
|
||||
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validate_split_test')
|
||||
def verify_validation_update_usage_info(self, expected_result, mocked_message, mocked_validation_messages):
|
||||
"""
|
||||
Helper method for testing validation information present after update_usage_info.
|
||||
"""
|
||||
self._add_user_partitions()
|
||||
split_test = self._create_content_experiment(cid=0, name_suffix='0')[1]
|
||||
|
||||
mocked_validation_messages.return_value = [
|
||||
ValidationMessage(
|
||||
split_test,
|
||||
u"Validation message",
|
||||
ValidationMessageType.warning
|
||||
)
|
||||
]
|
||||
group_configuration = GroupConfiguration.add_usage_info(self.course, self.store)[0]
|
||||
validation = StudioValidation(split_test.location)
|
||||
if mocked_message is not None:
|
||||
validation.add(mocked_message)
|
||||
mocked_validation_messages.return_value = validation
|
||||
|
||||
group_configuration = GroupConfiguration.update_usage_info(
|
||||
self.store, self.course, self.course.user_partitions[0]
|
||||
)
|
||||
self.assertEqual(
|
||||
group_configuration['usage'][0]['validation'],
|
||||
{
|
||||
'message': u'This content experiment has issues that affect content visibility.',
|
||||
'type': 'warning'
|
||||
}
|
||||
expected_result.to_json() if expected_result is not None else None,
|
||||
group_configuration['usage'][0]['validation']
|
||||
)
|
||||
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validation_messages')
|
||||
def test_update_usage_info(self, mocked_validation_messages):
|
||||
def test_update_usage_info(self):
|
||||
"""
|
||||
Tests if validation message is present when updating usage info.
|
||||
"""
|
||||
self._add_user_partitions()
|
||||
split_test = self._create_content_experiment(cid=0, name_suffix='0')[1]
|
||||
|
||||
mocked_validation_messages.return_value = [
|
||||
ValidationMessage(
|
||||
split_test,
|
||||
u"Validation message",
|
||||
ValidationMessageType.warning
|
||||
)
|
||||
]
|
||||
|
||||
group_configuration = GroupConfiguration.update_usage_info(self.store, self.course, self.course.user_partitions[0])
|
||||
|
||||
self.assertEqual(
|
||||
group_configuration['usage'][0]['validation'],
|
||||
{
|
||||
'message': u'This content experiment has issues that affect content visibility.',
|
||||
'type': 'warning'
|
||||
}
|
||||
mocked_message = StudioValidationMessage(StudioValidationMessage.WARNING, u"Validation message")
|
||||
expected_result = StudioValidationMessage(
|
||||
StudioValidationMessage.WARNING, u"This content experiment has issues that affect content visibility."
|
||||
)
|
||||
# pylint: disable=no-value-for-parameter
|
||||
self.verify_validation_update_usage_info(expected_result, mocked_message)
|
||||
|
||||
@patch('xmodule.split_test_module.SplitTestDescriptor.validation_messages')
|
||||
def test_update_usage_info_no_message(self, mocked_validation_messages):
|
||||
def test_update_usage_info_no_message(self):
|
||||
"""
|
||||
Tests if validation message is not present when updating usage info.
|
||||
"""
|
||||
self._add_user_partitions()
|
||||
self._create_content_experiment(cid=0, name_suffix='0')
|
||||
mocked_validation_messages.return_value = []
|
||||
group_configuration = GroupConfiguration.update_usage_info(self.store, self.course, self.course.user_partitions[0])
|
||||
self.assertEqual(group_configuration['usage'][0]['validation'], None)
|
||||
self.verify_validation_update_usage_info(None, None) # pylint: disable=no-value-for-parameter
|
||||
|
||||
@@ -130,8 +130,10 @@ class GetItemTest(ItemTest):
|
||||
root_usage_key = self._create_vertical()
|
||||
html, __ = self._get_container_preview(root_usage_key)
|
||||
|
||||
# Verify that the Studio wrapper is not added
|
||||
self.assertNotIn('wrapper-xblock', html)
|
||||
# XBlock messages are added by the Studio wrapper.
|
||||
self.assertIn('wrapper-xblock-message', html)
|
||||
# Make sure that "wrapper-xblock" does not appear by itself (without -message at end).
|
||||
self.assertNotRegexpMatches(html, r'wrapper-xblock[^-]+')
|
||||
|
||||
# Verify that the header and article tags are still added
|
||||
self.assertIn('<header class="xblock-header xblock-header-vertical">', html)
|
||||
|
||||
@@ -213,6 +213,7 @@ define([
|
||||
"js/spec/models/component_template_spec",
|
||||
"js/spec/models/explicit_url_spec",
|
||||
"js/spec/models/xblock_info_spec",
|
||||
"js/spec/models/xblock_validation_spec",
|
||||
|
||||
"js/spec/utils/drag_and_drop_spec",
|
||||
"js/spec/utils/handle_iframe_binding_spec",
|
||||
@@ -228,6 +229,7 @@ define([
|
||||
"js/spec/views/xblock_spec",
|
||||
"js/spec/views/xblock_editor_spec",
|
||||
"js/spec/views/xblock_string_field_editor_spec",
|
||||
"js/spec/views/xblock_validation_spec",
|
||||
|
||||
"js/spec/views/utils/view_utils_spec",
|
||||
|
||||
|
||||
46
cms/static/js/models/xblock_validation.js
Normal file
46
cms/static/js/models/xblock_validation.js
Normal file
@@ -0,0 +1,46 @@
|
||||
define(["backbone", "gettext", "underscore"], function (Backbone, gettext, _) {
|
||||
/**
|
||||
* Model for xblock validation messages as displayed in Studio.
|
||||
*/
|
||||
var XBlockValidationModel = Backbone.Model.extend({
|
||||
defaults: {
|
||||
summary: {},
|
||||
messages: [],
|
||||
empty: true,
|
||||
xblock_id: null
|
||||
},
|
||||
|
||||
WARNING : "warning",
|
||||
ERROR: "error",
|
||||
NOT_CONFIGURED: "not-configured",
|
||||
|
||||
parse: function(response) {
|
||||
if (!response.empty) {
|
||||
var summary = "summary" in response ? response.summary : {};
|
||||
var messages = "messages" in response ? response.messages : [];
|
||||
if (!(_.has(summary, "text")) || !summary.text) {
|
||||
summary.text = gettext("This component has validation issues.");
|
||||
}
|
||||
if (!(_.has(summary, "type")) || !summary.type) {
|
||||
summary.type = this.WARNING;
|
||||
// Possible types are ERROR, WARNING, and NOT_CONFIGURED. NOT_CONFIGURED is treated as a warning.
|
||||
_.find(messages, function (message) {
|
||||
if (message.type === this.ERROR) {
|
||||
summary.type = this.ERROR;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}, this);
|
||||
}
|
||||
response.summary = summary;
|
||||
if (response.showSummaryOnly) {
|
||||
messages = [];
|
||||
}
|
||||
response.messages = messages;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
});
|
||||
return XBlockValidationModel;
|
||||
});
|
||||
152
cms/static/js/spec/models/xblock_validation_spec.js
Normal file
152
cms/static/js/spec/models/xblock_validation_spec.js
Normal file
@@ -0,0 +1,152 @@
|
||||
define(['js/models/xblock_validation'],
|
||||
function(XBlockValidationModel) {
|
||||
var verifyModel;
|
||||
|
||||
verifyModel = function(model, expected_empty, expected_summary, expected_messages, expected_xblock_id) {
|
||||
expect(model.get("empty")).toBe(expected_empty);
|
||||
expect(model.get("summary")).toEqual(expected_summary);
|
||||
expect(model.get("messages")).toEqual(expected_messages);
|
||||
expect(model.get("xblock_id")).toBe(expected_xblock_id);
|
||||
};
|
||||
|
||||
describe('XBlockValidationModel', function() {
|
||||
it('handles empty variable', function() {
|
||||
verifyModel(new XBlockValidationModel({parse: true}), true, {}, [], null);
|
||||
verifyModel(new XBlockValidationModel({"empty": true}, {parse: true}), true, {}, [], null);
|
||||
|
||||
// It is assumed that the "empty" state on the JSON object passed in is correct
|
||||
// (no attempt is made to correct other variables based on empty==true).
|
||||
verifyModel(
|
||||
new XBlockValidationModel(
|
||||
{"empty": true, "messages": [{"text": "Bad JSON case"}], "xblock_id": "id"},
|
||||
{parse: true}
|
||||
),
|
||||
true,
|
||||
{},
|
||||
[{"text": "Bad JSON case"}], "id"
|
||||
);
|
||||
});
|
||||
|
||||
it('creates a summary if not defined', function() {
|
||||
// Single warning message.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"xblock_id": "id"
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "This component has validation issues.", "type": "warning"},
|
||||
[],
|
||||
"id"
|
||||
);
|
||||
// Two messages that compute to a "warning" state in the summary.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"messages": [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
|
||||
"xblock_id": "id"
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "This component has validation issues.", "type": "warning"},
|
||||
[{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
|
||||
"id"
|
||||
);
|
||||
// Two messages, with one of them "error", resulting in an "error" state in the summary.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"xblock_id": "id"
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "This component has validation issues.", "type": "error"},
|
||||
[{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"id"
|
||||
);
|
||||
});
|
||||
|
||||
it('respects summary properties that are defined', function() {
|
||||
// Summary already present (both text and type), no messages.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"xblock_id": "id",
|
||||
"summary": {"text": "my summary", "type": "custom type"}
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "my summary", "type": "custom type"},
|
||||
[],
|
||||
"id"
|
||||
);
|
||||
// Summary text present, but not type (will get default value of warning).
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"xblock_id": "id",
|
||||
"summary": {"text": "my summary"}
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "my summary", "type": "warning"},
|
||||
[],
|
||||
"id"
|
||||
);
|
||||
// Summary type present, but not text.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"summary": {"type": "custom type"},
|
||||
"messages": [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
|
||||
"xblock_id": "id"
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "This component has validation issues.", "type": "custom type"},
|
||||
[{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
|
||||
"id"
|
||||
);
|
||||
// Summary text present, type will be computed as error.
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"summary": {"text": "my summary"},
|
||||
"messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"xblock_id": "id"
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "my summary", "type": "error"},
|
||||
[{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"id"
|
||||
);
|
||||
});
|
||||
|
||||
it('clears messages if showSummaryOnly is true', function() {
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"xblock_id": "id",
|
||||
"summary": {"text": "my summary"},
|
||||
"messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"showSummaryOnly": true
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "my summary", "type": "error"},
|
||||
[],
|
||||
"id"
|
||||
);
|
||||
|
||||
verifyModel(
|
||||
new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"xblock_id": "id",
|
||||
"summary": {"text": "my summary"},
|
||||
"messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"showSummaryOnly": false
|
||||
}, {parse: true}),
|
||||
false,
|
||||
{"text": "my summary", "type": "error"},
|
||||
[{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
|
||||
"id"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -215,7 +215,7 @@ define([
|
||||
'label': 'label1',
|
||||
'url': 'url1',
|
||||
'validation': {
|
||||
'message': "Warning message",
|
||||
'text': "Warning message",
|
||||
'type': 'warning'
|
||||
}
|
||||
}
|
||||
@@ -233,7 +233,7 @@ define([
|
||||
'label': 'label1',
|
||||
'url': 'url1',
|
||||
'validation': {
|
||||
'message': "Error message",
|
||||
'text': "Error message",
|
||||
'type': 'error'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,14 @@ define([ "jquery", "js/common_helpers/ajax_helpers", "URI", "js/views/xblock", "
|
||||
]);
|
||||
expect(promise.isRejected()).toBe(true);
|
||||
});
|
||||
|
||||
it('Triggers an event to the runtime when a notification-action-button is clicked', function () {
|
||||
var notifySpy = spyOn(xblockView, "notifyRuntime").andCallThrough();
|
||||
|
||||
postXBlockRequest(AjaxHelpers.requests(this), []);
|
||||
xblockView.$el.find(".notification-action-button").click();
|
||||
expect(notifySpy).toHaveBeenCalledWith("add-missing-groups", model.get("id"));
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
132
cms/static/js/spec/views/xblock_validation_spec.js
Normal file
132
cms/static/js/spec/views/xblock_validation_spec.js
Normal file
@@ -0,0 +1,132 @@
|
||||
define(['jquery', 'js/models/xblock_validation', 'js/views/xblock_validation', 'js/common_helpers/template_helpers'],
|
||||
function($, XBlockValidationModel, XBlockValidationView, TemplateHelpers) {
|
||||
|
||||
beforeEach(function () {
|
||||
TemplateHelpers.installTemplate('xblock-validation-messages');
|
||||
});
|
||||
|
||||
describe('XBlockValidationView helper methods', function() {
|
||||
var model, view;
|
||||
|
||||
beforeEach(function () {
|
||||
model = new XBlockValidationModel({parse: true});
|
||||
view = new XBlockValidationView({model: model});
|
||||
view.render();
|
||||
});
|
||||
|
||||
it('has a getIcon method', function() {
|
||||
var getIcon = view.getIcon.bind(view);
|
||||
|
||||
expect(getIcon(model.WARNING)).toBe('icon-warning-sign');
|
||||
expect(getIcon(model.NOT_CONFIGURED)).toBe('icon-warning-sign');
|
||||
expect(getIcon(model.ERROR)).toBe('icon-exclamation-sign');
|
||||
expect(getIcon("unknown")).toBeNull();
|
||||
});
|
||||
|
||||
it('has a getDisplayName method', function() {
|
||||
var getDisplayName = view.getDisplayName.bind(view);
|
||||
|
||||
expect(getDisplayName(model.WARNING)).toBe("Warning");
|
||||
expect(getDisplayName(model.NOT_CONFIGURED)).toBe("Warning");
|
||||
expect(getDisplayName(model.ERROR)).toBe("Error");
|
||||
expect(getDisplayName("unknown")).toBeNull();
|
||||
});
|
||||
|
||||
it('can add additional classes', function() {
|
||||
var noContainerContent = "no-container-content", notConfiguredModel, nonRootView, rootView;
|
||||
|
||||
expect(view.getAdditionalClasses()).toBe("");
|
||||
expect(view.$('.validation')).not.toHaveClass(noContainerContent);
|
||||
|
||||
notConfiguredModel = new XBlockValidationModel({
|
||||
"empty": false, "summary": {"text": "Not configured", "type": model.NOT_CONFIGURED},
|
||||
"xblock_id": "id"
|
||||
},
|
||||
{parse: true}
|
||||
);
|
||||
nonRootView = new XBlockValidationView({model: notConfiguredModel});
|
||||
nonRootView.render();
|
||||
expect(nonRootView.getAdditionalClasses()).toBe("");
|
||||
expect(view.$('.validation')).not.toHaveClass(noContainerContent);
|
||||
|
||||
rootView = new XBlockValidationView({model: notConfiguredModel, root: true});
|
||||
rootView.render();
|
||||
expect(rootView.getAdditionalClasses()).toBe(noContainerContent);
|
||||
expect(rootView.$('.validation')).toHaveClass(noContainerContent);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('XBlockValidationView rendering', function() {
|
||||
var model, view;
|
||||
|
||||
beforeEach(function () {
|
||||
model = new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"summary": {
|
||||
"text": "Summary message", "type": "error",
|
||||
"action_label": "Summary Action", "action_class": "edit-button"
|
||||
},
|
||||
"messages": [
|
||||
{
|
||||
"text": "First message", "type": "warning",
|
||||
"action_label": "First Message Action", "action_runtime_event": "fix-up"
|
||||
},
|
||||
{"text": "Second message", "type": "error"}
|
||||
],
|
||||
"xblock_id": "id"
|
||||
});
|
||||
view = new XBlockValidationView({model: model});
|
||||
view.render();
|
||||
});
|
||||
|
||||
it('renders summary and detailed messages types', function() {
|
||||
var details;
|
||||
|
||||
expect(view.$('.xblock-message')).toHaveClass("has-errors");
|
||||
details = view.$('.xblock-message-item');
|
||||
expect(details.length).toBe(2);
|
||||
expect(details[0]).toHaveClass("warning");
|
||||
expect(details[1]).toHaveClass("error");
|
||||
});
|
||||
|
||||
it('renders summary and detailed messages text', function() {
|
||||
var details;
|
||||
|
||||
expect(view.$('.xblock-message').text()).toContain("Summary message");
|
||||
|
||||
details = view.$('.xblock-message-item');
|
||||
expect(details.length).toBe(2);
|
||||
expect($(details[0]).text()).toContain("Warning");
|
||||
expect($(details[0]).text()).toContain("First message");
|
||||
expect($(details[1]).text()).toContain("Error");
|
||||
expect($(details[1]).text()).toContain("Second message");
|
||||
});
|
||||
|
||||
it('renders action info', function() {
|
||||
expect(view.$('a.edit-button .action-button-text').text()).toContain("Summary Action");
|
||||
|
||||
expect(view.$('a.notification-action-button .action-button-text').text()).
|
||||
toContain("First Message Action");
|
||||
expect(view.$('a.notification-action-button').data("notification-action")).toBe("fix-up");
|
||||
});
|
||||
|
||||
it('renders a summary only', function() {
|
||||
var summaryOnlyModel = new XBlockValidationModel({
|
||||
"empty": false,
|
||||
"summary": {"text": "Summary message", "type": "warning"},
|
||||
"xblock_id": "id"
|
||||
}), summaryOnlyView, details;
|
||||
|
||||
summaryOnlyView = new XBlockValidationView({model: summaryOnlyModel});
|
||||
summaryOnlyView.render();
|
||||
|
||||
expect(summaryOnlyView.$('.xblock-message')).toHaveClass("has-warnings");
|
||||
expect(view.$('.xblock-message').text()).toContain("Summary message");
|
||||
|
||||
details = summaryOnlyView.$('.xblock-message-item');
|
||||
expect(details.length).toBe(0);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -13,6 +13,12 @@ define(["jquery", "underscore", "gettext", "js/views/pages/base_page", "js/views
|
||||
var XBlockContainerPage = BasePage.extend({
|
||||
// takes XBlockInfo as a model
|
||||
|
||||
events: {
|
||||
"click .edit-button": "editXBlock",
|
||||
"click .duplicate-button": "duplicateXBlock",
|
||||
"click .delete-button": "deleteXBlock"
|
||||
},
|
||||
|
||||
options: {
|
||||
collapsedClass: 'is-collapsed'
|
||||
},
|
||||
@@ -81,12 +87,6 @@ define(["jquery", "underscore", "gettext", "js/views/pages/base_page", "js/views
|
||||
// Hide both blocks until we know which one to show
|
||||
xblockView.$el.addClass(hiddenCss);
|
||||
|
||||
if (!options || !options.refresh) {
|
||||
// Add actions to any top level buttons, e.g. "Edit" of the container itself.
|
||||
// Do not add the actions on "refresh" though, as the handlers are already registered.
|
||||
self.addButtonActions(this.$el);
|
||||
}
|
||||
|
||||
// Render the xblock
|
||||
xblockView.render({
|
||||
done: function() {
|
||||
@@ -119,7 +119,6 @@ define(["jquery", "underscore", "gettext", "js/views/pages/base_page", "js/views
|
||||
},
|
||||
|
||||
onXBlockRefresh: function(xblockView) {
|
||||
this.addButtonActions(xblockView.$el);
|
||||
this.xblockView.refresh();
|
||||
// Update publish and last modified information from the server.
|
||||
this.model.fetch();
|
||||
@@ -137,25 +136,12 @@ define(["jquery", "underscore", "gettext", "js/views/pages/base_page", "js/views
|
||||
});
|
||||
},
|
||||
|
||||
addButtonActions: function(element) {
|
||||
var self = this;
|
||||
element.find('.edit-button').click(function(event) {
|
||||
event.preventDefault();
|
||||
self.editComponent(self.findXBlockElement(event.target));
|
||||
});
|
||||
element.find('.duplicate-button').click(function(event) {
|
||||
event.preventDefault();
|
||||
self.duplicateComponent(self.findXBlockElement(event.target));
|
||||
});
|
||||
element.find('.delete-button').click(function(event) {
|
||||
event.preventDefault();
|
||||
self.deleteComponent(self.findXBlockElement(event.target));
|
||||
});
|
||||
},
|
||||
|
||||
editComponent: function(xblockElement) {
|
||||
var self = this,
|
||||
editXBlock: function(event) {
|
||||
var xblockElement = this.findXBlockElement(event.target),
|
||||
self = this,
|
||||
modal = new EditXBlockModal({ });
|
||||
event.preventDefault();
|
||||
|
||||
modal.edit(xblockElement, this.model, {
|
||||
refresh: function() {
|
||||
self.refreshXBlock(xblockElement);
|
||||
@@ -163,6 +149,16 @@ define(["jquery", "underscore", "gettext", "js/views/pages/base_page", "js/views
|
||||
});
|
||||
},
|
||||
|
||||
duplicateXBlock: function(event) {
|
||||
event.preventDefault();
|
||||
this.duplicateComponent(this.findXBlockElement(event.target));
|
||||
},
|
||||
|
||||
deleteXBlock: function(event) {
|
||||
event.preventDefault();
|
||||
this.deleteComponent(this.findXBlockElement(event.target));
|
||||
},
|
||||
|
||||
createPlaceholderElement: function() {
|
||||
return $("<div/>", { class: "studio-xblock-wrapper" });
|
||||
},
|
||||
|
||||
@@ -4,6 +4,10 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
|
||||
var XBlockView = BaseView.extend({
|
||||
// takes XBlockInfo as a model
|
||||
|
||||
events: {
|
||||
"click .notification-action-button": "fireNotificationActionEvent"
|
||||
},
|
||||
|
||||
initialize: function() {
|
||||
BaseView.prototype.initialize.call(this);
|
||||
this.view = this.options.view;
|
||||
@@ -195,6 +199,14 @@ define(["jquery", "underscore", "js/views/baseview", "xblock/runtime.v1"],
|
||||
}
|
||||
// Return an already resolved promise for synchronous updates
|
||||
return $.Deferred().resolve().promise();
|
||||
},
|
||||
|
||||
fireNotificationActionEvent: function(event) {
|
||||
var eventName = $(event.currentTarget).data("notification-action");
|
||||
if (eventName) {
|
||||
event.preventDefault();
|
||||
this.notifyRuntime(eventName, this.model.get("id"));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
76
cms/static/js/views/xblock_validation.js
Normal file
76
cms/static/js/views/xblock_validation.js
Normal file
@@ -0,0 +1,76 @@
|
||||
define(["jquery", "underscore", "js/views/baseview", "gettext"],
|
||||
function ($, _, BaseView, gettext) {
|
||||
/**
|
||||
* View for xblock validation messages as displayed in Studio.
|
||||
*/
|
||||
var XBlockValidationView = BaseView.extend({
|
||||
|
||||
// Takes XBlockValidationModel as a model
|
||||
initialize: function(options) {
|
||||
BaseView.prototype.initialize.call(this);
|
||||
this.template = this.loadTemplate('xblock-validation-messages');
|
||||
this.root = options.root;
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.$el.html(this.template({
|
||||
validation: this.model,
|
||||
additionalClasses: this.getAdditionalClasses(),
|
||||
getIcon: this.getIcon.bind(this),
|
||||
getDisplayName: this.getDisplayName.bind(this)
|
||||
}));
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the icon css class based on the message type.
|
||||
* @param messageType
|
||||
* @returns string representation of css class that will render the correct icon, or null if unknown type
|
||||
*/
|
||||
getIcon: function (messageType) {
|
||||
if (messageType === this.model.ERROR) {
|
||||
return 'icon-exclamation-sign';
|
||||
}
|
||||
else if (messageType === this.model.WARNING || messageType === this.model.NOT_CONFIGURED) {
|
||||
return 'icon-warning-sign';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a display name for a message (useful for screen readers), based on the message type.
|
||||
* @param messageType
|
||||
* @returns string display name (translated)
|
||||
*/
|
||||
getDisplayName: function (messageType) {
|
||||
if (messageType === this.model.WARNING || messageType === this.model.NOT_CONFIGURED) {
|
||||
// Translators: This message will be added to the front of messages of type warning,
|
||||
// e.g. "Warning: this component has not been configured yet".
|
||||
return gettext("Warning");
|
||||
}
|
||||
else if (messageType === this.model.ERROR) {
|
||||
// Translators: This message will be added to the front of messages of type error,
|
||||
// e.g. "Error: required field is missing".
|
||||
return gettext("Error");
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns additional css classes that can be added to HTML containing the validation messages.
|
||||
* Useful for rendering NOT_CONFIGURED in a special way.
|
||||
*
|
||||
* @returns string of additional css classes (or empty string)
|
||||
*/
|
||||
getAdditionalClasses: function () {
|
||||
if (this.root && this.model.get("summary").type === this.model.NOT_CONFIGURED &&
|
||||
this.model.get("messages").length === 0) {
|
||||
|
||||
return "no-container-content";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
return XBlockValidationView;
|
||||
});
|
||||
@@ -71,7 +71,7 @@
|
||||
<i class="icon-exclamation-sign"></i>
|
||||
<% } %>
|
||||
<span class="group-configuration-validation-message">
|
||||
<%= unit.validation.message %>
|
||||
<%= unit.validation.text %>
|
||||
</span>
|
||||
</p>
|
||||
<% } %>
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
<span data-tooltip="Drag to reorder" class="drag-handle action"></span>
|
||||
</li>
|
||||
</ul>
|
||||
<a href="#" class="button action-button notification-action-button" data-notification-action="add-missing-groups">
|
||||
<span class="action-button-text">Add Missing Groups</span>
|
||||
</a>
|
||||
</div>
|
||||
</header>
|
||||
<article class="xblock-render">
|
||||
|
||||
49
cms/templates/js/xblock-validation-messages.underscore
Normal file
49
cms/templates/js/xblock-validation-messages.underscore
Normal file
@@ -0,0 +1,49 @@
|
||||
<%
|
||||
var summaryMessage = validation.get("summary");
|
||||
var aggregateMessageType = summaryMessage.type;
|
||||
var aggregateValidationClass = aggregateMessageType === "error"? "has-errors" : "has-warnings";
|
||||
%>
|
||||
<div class="xblock-message validation <%= aggregateValidationClass %> <%= additionalClasses %>">
|
||||
<p class="<%= aggregateMessageType %>"><i class="<%= getIcon(aggregateMessageType) %>"></i>
|
||||
<%- summaryMessage.text %>
|
||||
<% if (summaryMessage.action_class) { %>
|
||||
<a href="#" class="button action-button <%- summaryMessage.action_class %>">
|
||||
<span class="action-button-text"><%- summaryMessage.action_label %></span>
|
||||
</a>
|
||||
<% } else if (summaryMessage.action_runtime_event) {%>
|
||||
<a href="#" class="button action-button notification-action-button" data-notification-action="<%- summaryMessage.action_runtime_event %>">
|
||||
<span class="action-button-text"><%- summaryMessage.action_label %></span>
|
||||
</a>
|
||||
<% } %>
|
||||
</p>
|
||||
<% var detailedMessages = validation.get("messages"); %>
|
||||
<% if (detailedMessages.length > 0) { %>
|
||||
<ul class="xblock-message-list">
|
||||
<% for (var i = 0; i < detailedMessages.length; i++) { %>
|
||||
<%
|
||||
var message = detailedMessages[i];
|
||||
var messageType = message.type
|
||||
var messageTypeDisplayName = getDisplayName(messageType)
|
||||
%>
|
||||
<li class="xblock-message-item <%= messageType %>">
|
||||
<span class="message-text">
|
||||
<% if (messageTypeDisplayName) { %>
|
||||
<span class="sr"><%- messageTypeDisplayName %>:</span>
|
||||
<% } %>
|
||||
<%- message.text %>
|
||||
|
||||
<% if (message.action_class) { %>
|
||||
<a href="#" class="button action-button <%- message.action_class %>">
|
||||
<span class="action-button-text"><%- message.action_label %></span>
|
||||
</a>
|
||||
<% } else if (message.action_runtime_event) {%>
|
||||
<a href="#" class="button action-button notification-action-button" data-notification-action="<%- message.action_runtime_event %>">
|
||||
<span class="action-button-text"><%- message.action_label %></span>
|
||||
</a>
|
||||
<% } %>
|
||||
</span>
|
||||
</li>
|
||||
<% } %>
|
||||
</ul>
|
||||
<% } %>
|
||||
</div>
|
||||
@@ -1,16 +1,51 @@
|
||||
|
||||
<%!
|
||||
from django.utils.translation import ugettext as _
|
||||
from contentstore.views.helpers import xblock_studio_url
|
||||
import json
|
||||
%>
|
||||
|
||||
<%
|
||||
xblock_url = xblock_studio_url(xblock)
|
||||
show_inline = xblock.has_children and not xblock_url
|
||||
section_class = "level-nesting" if show_inline else "level-element"
|
||||
collapsible_class = "is-collapsible" if xblock.has_children else ""
|
||||
label = xblock.display_name or xblock.scope_ids.block_type
|
||||
messages = json.dumps(xblock.validate().to_json())
|
||||
%>
|
||||
|
||||
<%namespace name='static' file='static_content.html'/>
|
||||
|
||||
<%block name="header_extras">
|
||||
<script type="text/template" id="xblock-validation-messages-tpl">
|
||||
<%static:include path="js/xblock-validation-messages.underscore" />
|
||||
</script>
|
||||
</%block>
|
||||
|
||||
<script type='text/javascript'>
|
||||
require(["js/views/xblock_validation", "js/models/xblock_validation"],
|
||||
function (XBlockValidationView, XBlockValidationModel) {
|
||||
var validationMessages = ${messages};
|
||||
% if xblock_url and not is_root:
|
||||
validationMessages.showSummaryOnly = true;
|
||||
% endif
|
||||
|
||||
var model = new XBlockValidationModel(validationMessages, {parse: true});
|
||||
if (!model.get("empty")) {
|
||||
var validationEle = $('div.xblock-validation-messages[data-locator="${xblock.location | h}"]');
|
||||
var viewOptions = {
|
||||
el: validationEle,
|
||||
model: model
|
||||
};
|
||||
% if is_root:
|
||||
viewOptions.root = true;
|
||||
% endif
|
||||
var view = new XBlockValidationView(viewOptions);
|
||||
view.render();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
% if not is_root:
|
||||
% if is_reorderable:
|
||||
<li class="studio-xblock-wrapper is-draggable" data-locator="${xblock.location | h}" data-course-key="${xblock.location.course_key | h}">
|
||||
@@ -64,21 +99,29 @@ label = xblock.display_name or xblock.scope_ids.block_type
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
% if xblock_url and not is_root:
|
||||
<div class="xblock-header-secondary">
|
||||
<div class="meta-info">${_('This block contains multiple components.')}</div>
|
||||
<ul class="actions-list">
|
||||
<li class="action-item action-view">
|
||||
<a href="${xblock_url}" class="action-button">
|
||||
## Translators: this is a verb describing the action of viewing more details
|
||||
<span class="action-button-text">${_('View')}</span>
|
||||
<i class="icon-arrow-right"></i>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
% if not is_root:
|
||||
<div class="wrapper-xblock-message xblock-validation-messages" data-locator="${xblock.location | h}"/>
|
||||
% if xblock_url:
|
||||
<div class="xblock-header-secondary">
|
||||
<div class="meta-info">${_('This block contains multiple components.')}</div>
|
||||
<ul class="actions-list">
|
||||
<li class="action-item action-view">
|
||||
<a href="${xblock_url}" class="action-button">
|
||||
## Translators: this is a verb describing the action of viewing more details
|
||||
<span class="action-button-text">${_('View')}</span>
|
||||
<i class="icon-arrow-right"></i>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
% endif
|
||||
% endif
|
||||
</header>
|
||||
|
||||
% if is_root:
|
||||
<div class="wrapper-xblock-message xblock-validation-messages" data-locator="${xblock.location | h}"/>
|
||||
% endif
|
||||
|
||||
% if is_root or not xblock_url:
|
||||
<article class="xblock-render">
|
||||
${content}
|
||||
@@ -86,7 +129,6 @@ label = xblock.display_name or xblock.scope_ids.block_type
|
||||
% else:
|
||||
<div class="xblock-message-area">
|
||||
${content}
|
||||
</div>
|
||||
% endif
|
||||
|
||||
% if not is_root:
|
||||
|
||||
Reference in New Issue
Block a user