Commit 0dcf53fa by Calen Pennington

Uniquify js fragments pulled from XModules, and load both XModuleDescriptor and…

Uniquify js fragments pulled from XModules, and load both XModuleDescriptor and XModule js into the cms
parent 9dd916ff
...@@ -98,7 +98,7 @@ def edit_item(request): ...@@ -98,7 +98,7 @@ def edit_item(request):
item = modulestore().get_item(item_location) item = modulestore().get_item(item_location)
return render_to_response('unit.html', { return render_to_response('unit.html', {
'contents': item.get_html(), 'contents': item.get_html(),
'js_module': item.__class__.__name__, 'js_module': item.js_module_name,
'category': item.category, 'category': item.category,
'name': item.name, 'name': item.name,
'previews': get_module_previews(item), 'previews': get_module_previews(item),
......
...@@ -26,6 +26,7 @@ import os ...@@ -26,6 +26,7 @@ import os
import errno import errno
import glob2 import glob2
import lms.envs.common import lms.envs.common
import hashlib
from path import path from path import path
############################ FEATURE CONFIGURATION ############################# ############################ FEATURE CONFIGURATION #############################
...@@ -190,18 +191,27 @@ except OSError as exc: ...@@ -190,18 +191,27 @@ except OSError as exc:
else: else:
raise raise
module_js_sources = [] fragments = set()
for xmodule in XModuleDescriptor.load_classes() + [RawDescriptor]: for descriptor in XModuleDescriptor.load_classes() + [RawDescriptor]:
js = xmodule.get_javascript() 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 filetype in ('coffee', 'js'):
for idx, fragment in enumerate(js.get(filetype, [])): for fragment in descriptor_js.get(filetype, []) + module_js.get(filetype, []):
path = os.path.join(js_file_dir, "{name}.{idx}.{type}".format( fragments.add((filetype, fragment))
name=xmodule.__name__,
idx=idx, module_js_sources = []
type=filetype)) for filetype, fragment in fragments:
with open(path, 'w') as js_file: path = os.path.join(js_file_dir, "{hash}.{type}".format(
js_file.write(fragment) hash=hashlib.md5(fragment).hexdigest(),
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", "")) type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
PIPELINE_JS = { PIPELINE_JS = {
'main': { 'main': {
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
<ul class="modules"> <ul class="modules">
% for module in week.get_children(): % 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="module-edit">${module.name}</a>
<a href="#" class="draggable">handle</a> <a href="#" class="draggable">handle</a>
</li> </li>
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
<ol> <ol>
% for child in module.get_children(): % for child in module.get_children():
<li class="${module.category}"> <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> <a href="#" class="draggable">handle</a>
</li> </li>
%endfor %endfor
......
...@@ -196,6 +196,11 @@ class XModule(object): ...@@ -196,6 +196,11 @@ class XModule(object):
return "" return ""
# ================================== HTML INTERFACE DEFINITIONS ====================== # ================================== HTML INTERFACE DEFINITIONS ======================
@property
def js_module_name(self):
return self.__class__.__name__
@classmethod @classmethod
def get_javascript(cls): def get_javascript(cls):
""" """
...@@ -401,6 +406,10 @@ class XModuleDescriptor(Plugin): ...@@ -401,6 +406,10 @@ class XModuleDescriptor(Plugin):
raise NotImplementedError('Modules must implement export_to_xml to enable xml export') raise NotImplementedError('Modules must implement export_to_xml to enable xml export')
# ================================== HTML INTERFACE DEFINITIONS ====================== # ================================== HTML INTERFACE DEFINITIONS ======================
@property
def js_module_name(self):
return self.__class__.__name__
@classmethod @classmethod
def get_javascript(cls): def get_javascript(cls):
""" """
......
...@@ -23,6 +23,7 @@ import os ...@@ -23,6 +23,7 @@ import os
import tempfile import tempfile
import glob2 import glob2
import errno import errno
import hashlib
import djcelery import djcelery
from path import path from path import path
...@@ -341,7 +342,7 @@ except OSError as exc: ...@@ -341,7 +342,7 @@ except OSError as exc:
else: else:
raise raise
module_js_sources = [] fragments = set()
for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]: for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]:
module = getattr(descriptor, 'module_class', None) module = getattr(descriptor, 'module_class', None)
if module is None: if module is None:
...@@ -349,14 +350,17 @@ for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]: ...@@ -349,14 +350,17 @@ for descriptor in XModuleDescriptor.load_classes() + [HiddenDescriptor]:
js = module.get_javascript() js = module.get_javascript()
for filetype in ('coffee', 'js'): for filetype in ('coffee', 'js'):
for idx, fragment in enumerate(js.get(filetype, [])): for fragment in js.get(filetype, []):
path = os.path.join(js_file_dir, "{name}.{idx}.{type}".format( fragments.add((filetype, fragment))
name=module.__name__,
idx=idx, module_js_sources = []
type=filetype)) for filetype, fragment in fragments:
with open(path, 'w') as js_file: path = os.path.join(js_file_dir, "{hash}.{type}".format(
js_file.write(fragment) hash=hashlib.md5(fragment).hexdigest(),
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", "")) type=filetype))
with open(path, 'w') as js_file:
js_file.write(fragment)
module_js_sources.append(path.replace(PROJECT_ROOT / "static/", ""))
PIPELINE_JS = { PIPELINE_JS = {
......
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