From c906c186cea2af0e56e0cacc904fe3fc591aa59f 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/asset_index.js | 2 +- cms/static/js/pages/course.js | 11 ++++ cms/static/js/pages/login.js | 2 +- cms/static/js/sock.js | 7 ++- cms/templates/base.html | 56 ++++++++++------- .../templates/static_content.html | 24 +++---- common/djangoapps/terrain/ui_helpers.py | 6 -- webpack.common.config.js | 63 +++++++++++++++---- 9 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 cms/static/js/pages/course.js 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/asset_index.js b/cms/static/js/pages/asset_index.js index f1daafff4d..4644fac60e 100644 --- a/cms/static/js/pages/asset_index.js +++ b/cms/static/js/pages/asset_index.js @@ -1,5 +1,5 @@ define( - ['js/factories/asset_index', 'common/js/utils/page_factory'], + ['js/factories/asset_index', 'common/js/utils/page_factory', 'js/factories/base'], function(AssetIndexFactory, invokePageFactory) { 'use strict'; invokePageFactory('AssetIndexFactory', AssetIndexFactory); diff --git a/cms/static/js/pages/course.js b/cms/static/js/pages/course.js new file mode 100644 index 0000000000..892f6c6acb --- /dev/null +++ b/cms/static/js/pages/course.js @@ -0,0 +1,11 @@ +(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 631db5a72a..f4e7907954 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -121,29 +121,39 @@ from openedx.core.djangolib.markup import HTML <%block name="modal_placeholder"> <%block name="jsextra"> - <%block name="page_bundle"> - + + % if context_course: + + % 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 3e48b16cf5..bc5e374483 100644 --- a/common/djangoapps/pipeline_mako/templates/static_content.html +++ b/common/djangoapps/pipeline_mako/templates/static_content.html @@ -158,7 +158,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. @@ -168,16 +168,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 9655245d04..f7e491417f 100644 --- a/webpack.common.config.js +++ b/webpack.common.config.js @@ -38,6 +38,7 @@ module.exports = { CourseOrLibraryListing: './cms/static/js/features_jsx/studio/CourseOrLibraryListing.jsx', 'js/pages/login': './cms/static/js/pages/login.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', @@ -158,9 +159,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$/, @@ -170,13 +178,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+)?$/, @@ -186,8 +215,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 @@ -196,14 +226,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/js/vendor/jQuery-File-Upload/js/' + 'common/static/js/vendor/', + 'common/static/js/vendor/jQuery-File-Upload/js/', + 'common/static/coffee/src' ] },