From 3107bb9c45865cd8e1656c2c3f6b5ca429e02041 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 21 Sep 2012 10:57:06 -0400 Subject: [PATCH] Make separate json and xml editing interfaces --- common/lib/xmodule/xmodule/editing_module.py | 28 +++++++++++++++---- common/lib/xmodule/xmodule/error_module.py | 4 +-- .../xmodule/xmodule/js/src/raw/edit.coffee | 5 ---- .../xmodule/js/src/raw/edit/json.coffee | 7 +++++ .../xmodule/js/src/raw/edit/xml.coffee | 7 +++++ common/lib/xmodule/xmodule/raw_module.py | 6 ++-- 6 files changed, 41 insertions(+), 16 deletions(-) delete mode 100644 common/lib/xmodule/xmodule/js/src/raw/edit.coffee create mode 100644 common/lib/xmodule/xmodule/js/src/raw/edit/json.coffee create mode 100644 common/lib/xmodule/xmodule/js/src/raw/edit/xml.coffee diff --git a/common/lib/xmodule/xmodule/editing_module.py b/common/lib/xmodule/xmodule/editing_module.py index 833d994b99..c52483ace4 100644 --- a/common/lib/xmodule/xmodule/editing_module.py +++ b/common/lib/xmodule/xmodule/editing_module.py @@ -5,6 +5,7 @@ import logging log = logging.getLogger(__name__) + class EditingDescriptor(MakoModuleDescriptor): """ Module that provides a raw editing view of its data and children. It does not @@ -14,16 +15,31 @@ class EditingDescriptor(MakoModuleDescriptor): """ mako_template = "widgets/raw-edit.html" - js = {'coffee': [resource_string(__name__, 'js/src/raw/edit.coffee')]} - js_module_name = "RawDescriptor" - - # cdodge: a little refactoring here, since we're basically doing the same thing # here as with our parent class, let's call into it to get the basic fields # set and then add our additional fields. Trying to keep it DRY. def get_context(self): _context = MakoModuleDescriptor.get_context(self) # Add our specific template information (the raw data body) - _context.update({ 'data' : self.definition.get('data','') }) + _context.update({'data': self.definition.get('data', '')}) return _context - + + +class XMLEditingDescriptor(EditingDescriptor): + """ + Module that provides a raw editing view of its data as XML. It does not perform + any validation of its definition + """ + + js = {'coffee': [resource_string(__name__, 'js/src/raw/edit/xml.coffee')]} + js_module_name = "XMLEditingDescriptor" + + +class JSONEditingDescriptor(EditingDescriptor): + """ + Module that provides a raw editing view of its data as XML. It does not perform + any validation of its definition + """ + + js = {'coffee': [resource_string(__name__, 'js/src/raw/edit/json.coffee')]} + js_module_name = "JSONEditingDescriptor" diff --git a/common/lib/xmodule/xmodule/error_module.py b/common/lib/xmodule/xmodule/error_module.py index c95e734258..ca0f2c5718 100644 --- a/common/lib/xmodule/xmodule/error_module.py +++ b/common/lib/xmodule/xmodule/error_module.py @@ -5,7 +5,7 @@ import sys from lxml import etree from xmodule.x_module import XModule -from xmodule.editing_module import EditingDescriptor +from xmodule.editing_module import JSONEditingDescriptor from xmodule.errortracker import exc_info_to_str @@ -45,7 +45,7 @@ class NonStaffErrorModule(XModule): }) -class ErrorDescriptor(EditingDescriptor): +class ErrorDescriptor(JSONEditingDescriptor): """ Module that provides a raw editing view of broken xml. """ diff --git a/common/lib/xmodule/xmodule/js/src/raw/edit.coffee b/common/lib/xmodule/xmodule/js/src/raw/edit.coffee deleted file mode 100644 index da40effcb3..0000000000 --- a/common/lib/xmodule/xmodule/js/src/raw/edit.coffee +++ /dev/null @@ -1,5 +0,0 @@ -class @RawDescriptor - constructor: (@element) -> - @edit_box = $(".edit-box", @element) - - save: -> @edit_box.val() diff --git a/common/lib/xmodule/xmodule/js/src/raw/edit/json.coffee b/common/lib/xmodule/xmodule/js/src/raw/edit/json.coffee new file mode 100644 index 0000000000..4dc88310ba --- /dev/null +++ b/common/lib/xmodule/xmodule/js/src/raw/edit/json.coffee @@ -0,0 +1,7 @@ +class @JSONEditingDescriptor + constructor: (@element) -> + @edit_box = CodeMirror.fromTextArea($(".edit-box", @element)[0], { + mode: { name: "javascript", json: true } + }) + + save: -> JSON.parse @edit_box.getValue() diff --git a/common/lib/xmodule/xmodule/js/src/raw/edit/xml.coffee b/common/lib/xmodule/xmodule/js/src/raw/edit/xml.coffee new file mode 100644 index 0000000000..286da45dc1 --- /dev/null +++ b/common/lib/xmodule/xmodule/js/src/raw/edit/xml.coffee @@ -0,0 +1,7 @@ +class @XMLEditingDescriptor + constructor: (@element) -> + @edit_box = CodeMirror.fromTextArea($(".edit-box", @element)[0], { + mode: "xml" + }) + + save: -> @edit_box.getValue() diff --git a/common/lib/xmodule/xmodule/raw_module.py b/common/lib/xmodule/xmodule/raw_module.py index 9fdb5d0b38..5ff16098ac 100644 --- a/common/lib/xmodule/xmodule/raw_module.py +++ b/common/lib/xmodule/xmodule/raw_module.py @@ -1,19 +1,19 @@ from lxml import etree -from xmodule.editing_module import EditingDescriptor +from xmodule.editing_module import XMLEditingDescriptor from xmodule.xml_module import XmlDescriptor import logging import sys log = logging.getLogger(__name__) -class RawDescriptor(XmlDescriptor, EditingDescriptor): +class RawDescriptor(XmlDescriptor, XMLEditingDescriptor): """ Module that provides a raw editing view of its data and children. It requires that the definition xml is valid. """ @classmethod def definition_from_xml(cls, xml_object, system): - return {'data': etree.tostring(xml_object)} + return {'data': etree.tostring(xml_object, pretty_print=True)} def definition_to_xml(self, resource_fs): try: