|
35 | 35 | <script type="text/javascript" src="{{ lms_root_url }}/static/common/js/vendor/require.js"></script>
|
36 | 36 | <script type="text/javascript" src="{{ lms_root_url }}/static/js/RequireJS-namespace-undefine.js"></script>
|
37 | 37 | <script>
|
38 |
| - // The minimal RequireJS configuration required for common LMS building XBlock types to work: |
| 38 | + // The minimal RequireJS configuration required for common LMS and CMS building XBlock types to work: |
| 39 | + require = require || RequireJS.require; |
| 40 | + define = define || RequireJS.define; |
39 | 41 | (function (require, define) {
|
40 |
| - require.config({ |
41 |
| - baseUrl: "{{ lms_root_url }}/static/", |
42 |
| - paths: { |
43 |
| - accessibility: 'js/src/accessibility_tools', |
44 |
| - draggabilly: 'js/vendor/draggabilly', |
45 |
| - hls: 'common/js/vendor/hls', |
46 |
| - moment: 'common/js/vendor/moment-with-locales', |
47 |
| - HtmlUtils: 'edx-ui-toolkit/js/utils/html-utils', |
48 |
| - }, |
49 |
| - }); |
| 42 | + if ('{{ view_name | safe }}' === 'studio_view') { |
| 43 | + // Call `require-config.js` of the CMS |
| 44 | + var script = document.createElement('script'); |
| 45 | + script.type = 'text/javascript'; |
| 46 | + script.src = "{{ cms_root_url }}/static/studio/cms/js/require-config.js"; |
| 47 | + document.head.appendChild(script); |
| 48 | + |
| 49 | + require.config({ |
| 50 | + baseUrl: "{{ cms_root_url }}/static/studio", |
| 51 | + paths: { |
| 52 | + accessibility: 'js/src/accessibility_tools', |
| 53 | + draggabilly: 'js/vendor/draggabilly', |
| 54 | + hls: 'common/js/vendor/hls', |
| 55 | + moment: 'common/js/vendor/moment-with-locales', |
| 56 | + }, |
| 57 | + }); |
| 58 | + } else { |
| 59 | + require.config({ |
| 60 | + baseUrl: "{{ lms_root_url }}/static/", |
| 61 | + paths: { |
| 62 | + accessibility: 'js/src/accessibility_tools', |
| 63 | + draggabilly: 'js/vendor/draggabilly', |
| 64 | + hls: 'common/js/vendor/hls', |
| 65 | + moment: 'common/js/vendor/moment-with-locales', |
| 66 | + HtmlUtils: 'edx-ui-toolkit/js/utils/html-utils', |
| 67 | + }, |
| 68 | + }); |
| 69 | + } |
50 | 70 | define('gettext', [], function() { return window.gettext; });
|
51 | 71 | define('jquery', [], function() { return window.jQuery; });
|
52 | 72 | define('jquery-migrate', [], function() { return window.jQuery; });
|
53 | 73 | define('underscore', [], function() { return window._; });
|
54 |
| - }).call(this, require || RequireJS.require, define || RequireJS.define); |
| 74 | + }).call(this, require, define); |
55 | 75 | </script>
|
56 | 76 | <!-- edX HTML Utils requires GlobalLoader -->
|
57 | 77 | <script type="text/javascript" src="{{ lms_root_url }}/static/edx-ui-toolkit/js/utils/global-loader.js"></script>
|
|
269 | 289 | // const passElement = isStudioView && (window as any).$ ? (window as any).$(element) : element;
|
270 | 290 | const blockJS = new InitFunction(runtime, element, data) || {};
|
271 | 291 | blockJS.element = element;
|
| 292 | + |
| 293 | + if (['MetadataOnlyEditingDescriptor', 'SequenceDescriptor'].includes(data['xmodule-type'])) { |
| 294 | + // The xmodule type `MetadataOnlyEditingDescriptor` and `SequenceDescriptor` renders a `<div>` with |
| 295 | + // the block metadata in the `data-metadata` attribute. But is necessary |
| 296 | + // to call `XBlockEditorView.xblockReady()` to run the scripts to build the |
| 297 | + // editor using the metadata. |
| 298 | + require(['{{ cms_root_url }}/static/studio/js/views/xblock_editor.js'], function(XBlockEditorView) { |
| 299 | + var editorView = new XBlockEditorView({ |
| 300 | + el: element, |
| 301 | + xblock: blockJS, |
| 302 | + }); |
| 303 | + // To render block using metadata |
| 304 | + editorView.xblockReady(blockJS); |
| 305 | + |
| 306 | + // Adding cancel and save buttons |
| 307 | + var xblockActions = ` |
| 308 | + <div class="xblock-actions"> |
| 309 | + <ul> |
| 310 | + <li class="action-item"> |
| 311 | + <input id="poll-submit-options" type="submit" class="button action-primary save-button" value="Save" onclick="return false;"> |
| 312 | + </li> |
| 313 | + <li class="action-item"> |
| 314 | + <a href="#" class="button cancel-button">Cancel</a> |
| 315 | + </li> |
| 316 | + </ul> |
| 317 | + </div> |
| 318 | + `; |
| 319 | + element.innerHTML += xblockActions; |
| 320 | + |
| 321 | + const views = editorView.getMetadataEditor().views; |
| 322 | + Object.values(views).forEach(view => { |
| 323 | + const uniqueId = view.uniqueId; |
| 324 | + const input = element.querySelector(`#${uniqueId}`); |
| 325 | + if (input) { |
| 326 | + input.addEventListener("input", function(event) { |
| 327 | + view.model.setValue(event.target.value); |
| 328 | + }); |
| 329 | + } |
| 330 | + }); |
| 331 | + |
| 332 | + // Adding cancel functionality |
| 333 | + $('.cancel-button', element).bind('click', function() { |
| 334 | + runtime.notify('cancel', {}); |
| 335 | + event.preventDefault(); |
| 336 | + }); |
| 337 | + |
| 338 | + // Adding save functionality |
| 339 | + $('.save-button', element).bind('click', function() { |
| 340 | + //event.preventDefault(); |
| 341 | + var error_message_div = $('.xblock-editor-error-message', element); |
| 342 | + const modifiedData = editorView.getChangedMetadata(); |
| 343 | + |
| 344 | + error_message_div.html(); |
| 345 | + error_message_div.css('display', 'none'); |
| 346 | + |
| 347 | + var handlerUrl = runtime.handlerUrl(element, 'studio_submit'); |
| 348 | + |
| 349 | + runtime.notify('save', {state: 'start', message: gettext("Saving")}); |
| 350 | + |
| 351 | + $.post(handlerUrl, JSON.stringify(modifiedData)).done(function(response) { |
| 352 | + if (response.result === 'success') { |
| 353 | + runtime.notify('save', {state: 'end'}) |
| 354 | + window.location.reload(false); |
| 355 | + } else { |
| 356 | + runtime.notify('error', { |
| 357 | + 'title': 'Error saving changes', |
| 358 | + 'message': response.message, |
| 359 | + }); |
| 360 | + error_message_div.html('Error: '+response.message); |
| 361 | + error_message_div.css('display', 'block'); |
| 362 | + } |
| 363 | + }); |
| 364 | + }); |
| 365 | + }); |
| 366 | + } |
| 367 | + |
272 | 368 | callback(blockJS);
|
273 | 369 | } else {
|
274 | 370 | const blockJS = { element };
|
|
0 commit comments