From 970bc6a50d8d8e1d479c7b7b9117cf486500efea Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 24 Jan 2018 16:31:20 -0500 Subject: [PATCH] Move the context course and sock into require_page compatible forms --- cms/static/js/factories/base.js | 6 +- cms/static/js/pages/course.js | 6 +- cms/static/js/pages/login.js | 2 +- cms/static/js/sock.js | 7 +- cms/templates/base.html | 62 ++++++++--------- .../templates/static_content.html | 24 +++---- common/djangoapps/terrain/ui_helpers.py | 6 -- webpack.common.config.js | 66 +++++++++++++++---- 8 files changed, 112 insertions(+), 67 deletions(-) diff --git a/cms/static/js/factories/base.js b/cms/static/js/factories/base.js index abaeb89d17..7f61b473c9 100644 --- a/cms/static/js/factories/base.js +++ b/cms/static/js/factories/base.js @@ -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'; + } +); diff --git a/cms/static/js/pages/course.js b/cms/static/js/pages/course.js index 2fc6d14ee3..8bb500f1ed 100644 --- a/cms/static/js/pages/course.js +++ b/cms/static/js/pages/course.js @@ -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); diff --git a/cms/static/js/pages/login.js b/cms/static/js/pages/login.js index 8caff6a833..449a1190f6 100644 --- a/cms/static/js/pages/login.js +++ b/cms/static/js/pages/login.js @@ -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); diff --git a/cms/static/js/sock.js b/cms/static/js/sock.js index 9a536a3c78..44653981ff 100644 --- a/cms/static/js/sock.js +++ b/cms/static/js/sock.js @@ -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); }); - }); + } +); diff --git a/cms/templates/base.html b/cms/templates/base.html index 05e3a718bf..f4e7907954 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -121,39 +121,39 @@ from openedx.core.djangolib.markup import HTML <%block name="modal_placeholder"> <%block name="jsextra"> - <%block name="page_bundle"> - + 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 + % if user.is_authenticated(): + <%static:invoke_page_bundle page_name='js/sock'/> + % endif + <%block name='page_bundle'> + <%include file="widgets/segment-io-footer.html" /> diff --git a/common/djangoapps/pipeline_mako/templates/static_content.html b/common/djangoapps/pipeline_mako/templates/static_content.html index 068bf4d754..6f7db5be07 100644 --- a/common/djangoapps/pipeline_mako/templates/static_content.html +++ b/common/djangoapps/pipeline_mako/templates/static_content.html @@ -164,7 +164,7 @@ source, template_path = Loader(engine).load_template_source(path) -<%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) %> - + % if class_name: + + % endif <%self:webpack entry="${page_name}"/> diff --git a/common/djangoapps/terrain/ui_helpers.py b/common/djangoapps/terrain/ui_helpers.py index 86aa39ca4b..a5ed62268f 100644 --- a/common/djangoapps/terrain/ui_helpers.py +++ b/common/djangoapps/terrain/ui_helpers.py @@ -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', diff --git a/webpack.common.config.js b/webpack.common.config.js index e615f050d1..9dee8eb29d 100644 --- a/webpack.common.config.js +++ b/webpack.common.config.js @@ -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' ] },