diff --git a/cms/static/js/views/components/add_library_content.js b/cms/static/js/views/components/add_library_content.js index 4c425c9f50b1..75a7ab1a8805 100644 --- a/cms/static/js/views/components/add_library_content.js +++ b/cms/static/js/views/components/add_library_content.js @@ -1,51 +1,56 @@ /** * Provides utilities to open and close the library content picker. - * - * To use this picker you need to add the following code into your template: - * - * ``` - *
- * - * ``` + * */ -define(['jquery'], -function($) { +define(['jquery', 'underscore', 'gettext', 'js/views/modals/base_modal'], +function($, _, gettext, BaseModal) { 'use strict'; - const closePicker = (picker, pickerCover) => { - $(pickerCover).css('display', 'none'); - $(picker).empty(); - $(picker).css('display', 'none'); - $('body').removeClass('picker-open'); - }; + var AddLibraryContent = BaseModal.extend({ + options: $.extend({}, BaseModal.prototype.options, { + modalName: 'add-component-from-library', + modalSize: 'lg', + view: 'studio_view', + viewSpecificClasses: 'modal-add-component-picker confirm', + // Translators: "title" is the name of the current component being edited. + titleFormat: gettext('Add library content'), + addPrimaryActionButton: false, + }), - const openPicker = (contentPickerUrl, picker, pickerCover) => { - // Add event listen to close picker when the iframe tells us to - window.addEventListener("message", function (event) { - if (event.data === 'closeComponentPicker') { - closePicker(picker, pickerCover); - } - }.bind(this)); + initialize: function() { + BaseModal.prototype.initialize.call(this); + }, - $(pickerCover).css('display', 'block'); - // xss-lint: disable=javascript-jquery-html - $(picker).html( - `` - ); - $(picker).css('display', 'block'); + /** + * Adds the action buttons to the modal. + */ + addActionButtons: function() { + this.addActionButton('cancel', gettext('Cancel')); + }, - // Prevent background from being scrollable when picker is open - $('body').addClass('picker-open'); - }; + /** + * Show a component picker modal from library. + * @param contentPickerUrl Url for component picker + * @param refreshFunction A function to refresh the block after it has been updated + */ + showComponentPicker: function(contentPickerUrl, refreshFunction) { + this.contentPickerUrl = contentPickerUrl; + this.refreshFunction = refreshFunction; - const createComponent = (contentPickerUrl) => { - const picker = document.querySelector("#library-content-picker"); - const pickerCover = document.querySelector(".picker-cover"); + this.render(); + this.show(); + }, - return openPicker(contentPickerUrl, picker, pickerCover); - }; + getContentHtml: function() { + // Add event listen to close picker when the iframe tells us to + window.addEventListener("message", function (event) { + if (event.data === 'closeComponentPicker') { + this.hide(); + } + }.bind(this)); + return ``; + }, + }); - return { - createComponent: createComponent, - }; + return AddLibraryContent; }); diff --git a/cms/static/js/views/components/add_xblock.js b/cms/static/js/views/components/add_xblock.js index 0feccab0ab73..9070b82a7750 100644 --- a/cms/static/js/views/components/add_xblock.js +++ b/cms/static/js/views/components/add_xblock.js @@ -70,7 +70,8 @@ function($, _, gettext, BaseView, ViewUtils, AddXBlockButton, AddXBlockMenu, Add this.closeNewComponent(event); if (saveData.type === 'library_v2') { - AddLibraryContent.createComponent(this.options.libraryContentPickerUrl); + var modal = new AddLibraryContent(); + modal.showComponentPicker(this.options.libraryContentPickerUrl, null); } else { ViewUtils.runOperationShowingMessage( gettext('Adding'), diff --git a/cms/static/sass/views/_container.scss b/cms/static/sass/views/_container.scss index 5a674ba6e03a..7b13122cbb61 100644 --- a/cms/static/sass/views/_container.scss +++ b/cms/static/sass/views/_container.scss @@ -566,3 +566,18 @@ width:100%; height:100% } + +// Modal for adding components from libary using component picker +// cms/static/js/views/components/add_library_content_with_picker.js +.modal-add-component-picker { + top: 10%; + .modal-content { + padding: 0 !important; + + & > iframe { + width: 100%; + min-height: 80vh; + background: url('#{$static-path}/images/spinner.gif') center center no-repeat; + } + } +} diff --git a/cms/templates/container.html b/cms/templates/container.html index 8551039bcca9..007d94bb0975 100644 --- a/cms/templates/container.html +++ b/cms/templates/container.html @@ -300,7 +300,4 @@