Commit bb863a3e by Xavier Antoviaque

light-children: Add support for JS handling of children

parent cb569289
......@@ -50,7 +50,8 @@ class HTMLBlock(LightChild):
block.light_children = []
node.tag = 'div'
block.content = etree.tostring(node)
block.content = unicode(etree.tostring(node))
node.tag = 'html'
return block
......
......@@ -84,7 +84,7 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
def add_node_as_child(cls, block, xml_child, child_id):
# Instantiate child
child_class = cls.get_class_by_element(xml_child.tag)
child = child_class()
child = child_class(block.runtime)
child.name = u'{}_{}'.format(block.name, child_id)
# Add any children the child may itself have
......@@ -123,7 +123,10 @@ class LightChildrenMixin(XBlockWithChildrenFragmentsMixin):
"""
Replacement for ```self.runtime.render_child()```
"""
return getattr(child, view_name)(context)
frag = getattr(child, view_name)(context)
frag.content = u'<div class="xblock-light-child" name="{}" data-type="{}">{}</div>'.format(
child.name, child.__class__.__name__, frag.content)
return frag
def get_children_fragment(self, context, view_name='student_view', instance_of=None,
not_instance_of=None):
......@@ -155,6 +158,12 @@ class LightChild(Plugin, LightChildrenMixin):
"""
entry_point = 'xblock.light_children'
def __init__(self, runtime):
self.runtime = runtime
def save(self):
pass
class LightChildField(object):
"""
......@@ -168,9 +177,15 @@ class LightChildField(object):
class String(LightChildField):
def __init__(self, *args, **kwargs):
self.value = kwargs.get('default', '') or ''
def __str__(self):
return self.value
def split(self, *args, **kwargs):
return self.value.split(*args, **kwargs)
class Boolean(LightChildField):
pass
......
......@@ -65,7 +65,6 @@ class MentoringBlock(XBlockWithLightChildren):
has_children = True
def student_view(self, context):
log.warn('xml_content => {}'.format(self.xml_content))
fragment, named_children = self.get_children_fragment(context, view_name='mentoring_view',
not_instance_of=MentoringMessageBlock)
......@@ -129,6 +128,7 @@ class MentoringBlock(XBlockWithLightChildren):
elif completed and self.next_step == self.url_name:
self.next_step = self.followed_by
log.warn(submit_results);
self.completed = bool(completed)
return {
'submitResults': submit_results,
......
......@@ -144,9 +144,8 @@ class QuizzBlock(LightChild):
Returns the tips contained in this block
"""
tips = []
for child_id in self.children: # pylint: disable=E1101
child = self.runtime.get_block(child_id)
if child.xml_element_name() == 'tip':
for child in self.get_children_objects():
if isinstance(child, QuizzTipBlock):
tips.append(child)
return tips
......
......@@ -7,7 +7,7 @@ function MentoringBlock(runtime, element) {
}
function callIfExists(obj, fn) {
if (typeof obj[fn] == 'function') {
if (typeof obj !== 'undefined' && typeof obj[fn] == 'function') {
return obj[fn].apply(obj, Array.prototype.slice.call(arguments, 2));
} else {
return undefined;
......@@ -19,8 +19,9 @@ function MentoringBlock(runtime, element) {
$.each(results.submitResults || [], function(index, submitResult) {
var input = submitResult[0],
result = submitResult[1];
callIfExists(runtime.childMap(element, input), 'handleSubmit', result);
result = submitResult[1],
child = getChildByName(element, input);
callIfExists(child, 'handleSubmit', result);
});
$('.progress', element).data('completed', results.completed ? 'True' : 'False')
......@@ -30,9 +31,36 @@ function MentoringBlock(runtime, element) {
$('.messages', element).append(results.message);
}
function getChildren(element) {
var children_dom = $('.xblock-light-child', element),
children = [];
$.each(children_dom, function(index, child_dom) {
var child_type = $(child_dom).attr('data-type'),
child = window[child_type];
if (typeof child !== 'undefined') {
child = child(runtime, child_dom);
child.name = $(child_dom).attr('name');
children.push(child);
}
});
return children;
}
function getChildByName(element, name) {
var children = getChildren(element);
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.name === name) {
return child;
}
}
}
$(element).find('.submit').bind('click', function() {
var data = {};
var children = runtime.children(element);
var children = getChildren(element);
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.name !== undefined) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment