Move the context course and sock into require_page compatible forms

This commit is contained in:
Calen Pennington
2018-01-24 16:31:20 -05:00
committed by Ari Rizzitano
parent 6998b311fe
commit 970bc6a50d
8 changed files with 112 additions and 67 deletions

View File

@@ -1,2 +1,6 @@
define(['js/base', 'cms/js/main', 'js/src/logger', 'datepair', 'accessibility',
'ieshim', 'tooltip_manager', 'lang_edx', 'js/models/course']);
'ieshim', 'tooltip_manager', 'lang_edx', 'js/models/course'],
function() {
'use strict';
}
);

View File

@@ -1,6 +1,10 @@
define(
(function(define) {
'use strict';
define(
['js/models/course'],
function(ContextCourse) {
window.course = new ContextCourse(window.pageFactoryArguments.ContextCourse[0]);
}
);
}).call(this, define || RequireJS.define);

View File

@@ -1,5 +1,5 @@
define(
['js/factories/login', 'common/js/utils/page_factory'],
['js/factories/login', 'common/js/utils/page_factory', 'js/factories/base'],
function(LoginFactory, invokePageFactory) {
'use strict';
invokePageFactory('LoginFactory', LoginFactory);

View File

@@ -1,5 +1,7 @@
require(['domReady', 'jquery', 'jquery.smoothScroll'],
define(['domReady', 'jquery', 'jquery.smoothScroll'],
function(domReady, $) {
'use strict';
var toggleSock = function(e) {
e.preventDefault();
@@ -33,4 +35,5 @@ require(['domReady', 'jquery', 'jquery.smoothScroll'],
// toggling footer additional support
$('.cta-show-sock').bind('click', toggleSock);
});
});
}
);

View File

@@ -121,39 +121,39 @@ from openedx.core.djangolib.markup import HTML
<%block name="modal_placeholder"></%block>
<%block name="jsextra"></%block>
<%block name="page_bundle">
<script type="text/javascript">
if (typeof window.pageFactoryArguments == "undefined") {
% if context_course:
<script type="text/javascript">
if (typeof window.pageFactoryArguments == "undefined") {
window.pageFactoryArguments = {};
}
% if context_course:
window.pageFactoryArguments['ContextCourse'] = {
id: "${context_course.id | n, js_escaped_string}",
name: "${context_course.display_name_with_default | n, js_escaped_string}",
url_name: "${context_course.location.block_id | n, js_escaped_string}",
org: "${context_course.location.org | n, js_escaped_string}",
num: "${context_course.location.course | n, js_escaped_string}",
display_course_number: "${context_course.display_coursenumber | n, js_escaped_string}",
revision: "${context_course.location.branch | n, js_escaped_string}",
self_paced: ${ context_course.self_paced | n, dump_js_escaped_json }
}
% endif
}
require(['js/factories/base'], function () {
require(['js/models/course'], function(Course) {
% if context_course:
window.course = new Course(
window.pageFactoryArguments['ContextCourse']
);
% endif
% if user.is_authenticated():
require(['js/sock']);
% endif
<%block name='requirejs'></%block>
});
});
</script>
window.pageFactoryArguments['ContextCourse'] = {
id: "${context_course.id | n, js_escaped_string}",
name: "${context_course.display_name_with_default | n, js_escaped_string}",
url_name: "${context_course.location.block_id | n, js_escaped_string}",
org: "${context_course.location.org | n, js_escaped_string}",
num: "${context_course.location.course | n, js_escaped_string}",
display_course_number: "${context_course.display_coursenumber | n, js_escaped_string}",
revision: "${context_course.location.branch | n, js_escaped_string}",
self_paced: ${ context_course.self_paced | n, dump_js_escaped_json }
}
</script>
% endif
% if user.is_authenticated():
<%static:invoke_page_bundle page_name='js/sock'/>
% endif
<%block name='page_bundle'>
<script type="text/javascript">
require(['js/factories/base'], function () {
require(['js/models/course'], function(Course) {
% if context_course:
window.course = new Course(window.pageFactoryArguments['ContextCourse']);
% endif
<%block name='requirejs'></%block>
});
});
</script>
</%block>
<%include file="widgets/segment-io-footer.html" />
<div class="modal-cover"></div>

View File

@@ -164,7 +164,7 @@ source, template_path = Loader(engine).load_template_source(path)
</script>
</%def>
<%def name="invoke_page_bundle(page_name, class_name)">
<%def name="invoke_page_bundle(page_name, class_name=None)">
<%doc>
Loads Javascript onto your page synchronously.
Uses RequireJS in development and a plain script tag in production.
@@ -174,16 +174,18 @@ source, template_path = Loader(engine).load_template_source(path)
<%
body = capture(caller.body)
%>
<script type="text/javascript">
if (typeof pageFactoryArguments == "undefined") {
var pageFactoryArguments = {};
}
% if body:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [${ body | n, decode.utf8 }];
% else:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [];
% endif
</script>
% if class_name:
<script type="text/javascript">
if (typeof pageFactoryArguments == "undefined") {
var pageFactoryArguments = {};
}
% if body:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [${ body | n, decode.utf8 }];
% else:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [];
% endif
</script>
% endif
<%self:webpack entry="${page_name}"/>
</%def>

View File

@@ -51,12 +51,6 @@ REQUIREJS_WAIT = {
"js/sock", "gettext", "js/base",
"jquery.ui", "cms/js/main", "underscore"],
# Upload
re.compile(r'^\s*Files & Uploads'): [
'js/base', 'jquery.ui', 'cms/js/main', 'underscore',
'js/views/assets', 'js/views/asset'
],
# Pages
re.compile(r'^Pages \|'): [
'js/models/explicit_url', 'js/views/tabs',

View File

@@ -21,6 +21,8 @@ module.exports = {
CourseOrLibraryListing: './cms/static/js/features_jsx/studio/CourseOrLibraryListing.jsx',
'js/pages/login': './cms/static/js/pages/login.js',
'js/pages/textbooks': './cms/static/js/pages/textbooks.js',
'js/pages/asset_index': './cms/static/js/pages/asset_index.js',
'js/sock': './cms/static/js/sock.js',
// LMS
SingleSupportForm: './lms/static/support/jsx/single_support_form.jsx',
@@ -149,9 +151,16 @@ module.exports = {
use: 'babel-loader'
},
{
test: /\.coffee$/,
exclude: /node_modules/,
use: 'coffee-loader'
test: path.resolve(__dirname, 'common/static/coffee/src/ajax_prefix.js'),
use: [
'babel-loader',
{
loader: 'exports-loader',
options: {
'this.AjaxPrefix': true
}
}
]
},
{
test: /\.underscore$/,
@@ -161,13 +170,34 @@ module.exports = {
// This file is used by both RequireJS and Webpack and depends on window globals
// This is a dirty hack and shouldn't be replicated for other files.
test: path.resolve(__dirname, 'cms/static/cms/js/main.js'),
use: {
loader: 'imports-loader',
options: {
AjaxPrefix:
'exports-loader?this.AjaxPrefix!../../../../common/static/coffee/src/ajax_prefix.coffee'
loader: StringReplace.replace(
['babel-loader'],
{
replacements: [
{
pattern: /\(function\(AjaxPrefix\) {/,
replacement: function() { return ''; }
},
{
pattern: /], function\(domReady, \$, str, Backbone, gettext, NotificationView\) {/,
replacement: function() {
// eslint-disable-next-line
return '], function(domReady, $, str, Backbone, gettext, NotificationView, AjaxPrefix) {';
}
},
{
pattern: /'..\/..\/common\/js\/components\/views\/feedback_notification',/,
replacement: function() {
return "'../../common/js/components/views/feedback_notification', 'AjaxPrefix',";
}
},
{
pattern: /}\).call\(this, AjaxPrefix\);/,
replacement: function() { return ''; }
}
]
}
}
)
},
{
test: /\.(woff2?|ttf|svg|eot)(\?v=\d+\.\d+\.\d+)?$/,
@@ -177,8 +207,9 @@ module.exports = {
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.coffee'],
extensions: ['.js', '.jsx', '.json'],
alias: {
AjaxPrefix: 'ajax_prefix',
'edx-ui-toolkit': 'edx-ui-toolkit/src/', // @TODO: some paths in toolkit are not valid relative paths
'jquery.ui': 'jQuery-File-Upload/js/vendor/jquery.ui.widget.js',
jquery: 'jquery/src/jquery', // Use the non-dist form of jQuery for better debugging + optimization
@@ -188,14 +219,21 @@ module.exports = {
// https://github.com/webpack/webpack/issues/304#issuecomment-272150177
// (I've tried every other suggestion solution on that page, this
// was the only one that worked.)
sinon: __dirname + '/node_modules/sinon/pkg/sinon.js'
sinon: __dirname + '/node_modules/sinon/pkg/sinon.js',
'jquery.smoothScroll': 'jquery.smooth-scroll.min',
'jquery.timepicker': 'timepicker/jquery.timepicker',
datepair: 'timepicker/datepair',
accessibility: 'accessibility_tools',
ieshim: 'ie_shim'
},
modules: [
'node_modules',
'common/static/js/vendor/',
'cms/static',
'common/static/',
'common/static/js/src'
'common/static',
'common/static/js/src',
'common/static/js/vendor/',
'common/static/js/vendor/jQuery-File-Upload/js/',
'common/static/coffee/src'
]
},