Uniquify js fragments pulled from XModules, and load both XModuleDescriptor and XModule js into the cms

This commit is contained in:
Calen Pennington
2012-07-18 08:08:36 -04:00
parent 3ba03d79aa
commit 3cdceab16c
6 changed files with 46 additions and 23 deletions

View File

@@ -98,7 +98,7 @@ def edit_item(request):
item = modulestore().get_item(item_location)
return render_to_response('unit.html', {
'contents': item.get_html(),
'js_module': item.__class__.__name__,
'js_module': item.js_module_name,
'category': item.category,
'name': item.name,
'previews': get_module_previews(item),

View File

@@ -26,6 +26,7 @@ import os
import errno
import glob2
import lms.envs.common
import hashlib
from path import path
############################ FEATURE CONFIGURATION #############################
@@ -190,18 +191,27 @@ except OSError as exc:
else:
raise
module_js_sources = []
for xmodule in XModuleDescriptor.load_classes() + [RawDescriptor]:
js = xmodule.get_javascript()
fragments = set()
for descriptor in XModuleDescriptor.load_classes() + [RawDescriptor]:
descriptor_js = descriptor.get_javascript()
module = getattr(descriptor, 'module_class', None)
if module is not None:
module_js = module.get_javascript()
else:
module_js = {}
for filetype in ('coffee', 'js'):
for idx, fragment in enumerate(js.get(filetype, [])):
path = os.path.join(js_file_dir, "{name}.{idx}.{type}".format(
name=xmodule.__name__,
idx=idx,
type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
for fragment in descriptor_js.get(filetype, []) + module_js.get(filetype, []):
fragments.add((filetype, fragment))
module_js_sources = []
for filetype, fragment in fragments:
path = os.path.join(js_file_dir, "{hash}.{type}".format(
hash=hashlib.md5(fragment).hexdigest(),
type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
PIPELINE_JS = {
'main': {

View File

@@ -55,7 +55,7 @@
<ul class="modules">
% for module in week.get_children():
<li class="module" data-id="${module.location.url()}" data-type="${module.js_module_name()}">
<li class="module" data-id="${module.location.url()}" data-type="${module.js_module_name}">
<a href="#" class="module-edit">${module.name}</a>
<a href="#" class="draggable">handle</a>
</li>

View File

@@ -36,7 +36,7 @@
<ol>
% for child in module.get_children():
<li class="${module.category}">
<a href="#" class="module-edit" data-id="${child.location.url()}" data-type="${child.js_module_name()}">${child.name}</a>
<a href="#" class="module-edit" data-id="${child.location.url()}" data-type="${child.js_module_name}">${child.name}</a>
<a href="#" class="draggable">handle</a>
</li>
%endfor

View File

@@ -196,6 +196,11 @@ class XModule(object):
return ""
# ================================== HTML INTERFACE DEFINITIONS ======================
@property
def js_module_name(self):
return self.__class__.__name__
@classmethod
def get_javascript(cls):
"""
@@ -401,6 +406,10 @@ class XModuleDescriptor(Plugin):
raise NotImplementedError('Modules must implement export_to_xml to enable xml export')
# ================================== HTML INTERFACE DEFINITIONS ======================
@property
def js_module_name(self):
return self.__class__.__name__
@classmethod
def get_javascript(cls):
"""

View File

@@ -23,6 +23,7 @@ import os
import tempfile
import glob2
import errno
import hashlib
import djcelery
from path import path
@@ -341,7 +342,7 @@ except OSError as exc:
else:
raise
module_js_sources = []
fragments = set()
for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]:
module = getattr(descriptor, 'module_class', None)
if module is None:
@@ -349,14 +350,17 @@ for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]:
js = module.get_javascript()
for filetype in ('coffee', 'js'):
for idx, fragment in enumerate(js.get(filetype, [])):
path = os.path.join(js_file_dir, "{name}.{idx}.{type}".format(
name=module.__name__,
idx=idx,
type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
for fragment in js.get(filetype, []):
fragments.add((filetype, fragment))
module_js_sources = []
for filetype, fragment in fragments:
path = os.path.join(js_file_dir, "{hash}.{type}".format(
hash=hashlib.md5(fragment).hexdigest(),
type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
PIPELINE_JS = {