Commit bb863a3e by Xavier Antoviaque

light-children: Add support for JS handling of children

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