From 7456c0a5b290569d40398865228ade11b2972f7e Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Sat, 19 Oct 2024 17:03:13 -0700 Subject: [PATCH] feat: minimal UI for the Problem Bank block --- .../views/modals/select_v2_library_content.js | 68 +++++++++++++++++++ cms/static/js/views/pages/container.js | 45 +++++++++++- xmodule/item_bank_block.py | 68 ++++++++++--------- xmodule/templates/item_bank/author_view.html | 14 ++++ .../templates/item_bank/author_view_add.html | 12 ++++ 5 files changed, 173 insertions(+), 34 deletions(-) create mode 100644 cms/static/js/views/modals/select_v2_library_content.js create mode 100644 xmodule/templates/item_bank/author_view.html create mode 100644 xmodule/templates/item_bank/author_view_add.html diff --git a/cms/static/js/views/modals/select_v2_library_content.js b/cms/static/js/views/modals/select_v2_library_content.js new file mode 100644 index 000000000000..87523679678c --- /dev/null +++ b/cms/static/js/views/modals/select_v2_library_content.js @@ -0,0 +1,68 @@ +/** + * Provides utilities to open and close the library content picker. + * + */ +define(['jquery', 'underscore', 'gettext', 'js/views/modals/base_modal'], +function($, _, gettext, BaseModal) { + 'use strict'; + + var SelectV2LibraryContent = 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, + }), + + initialize: function() { + BaseModal.prototype.initialize.call(this); + // Add event listen to close picker when the iframe tells us to + const handleMessage = (event) => { + if (event.data?.type === 'pickerComponentSelected') { + var requestData = { + library_content_key: event.data.usageKey, + category: event.data.category, + } + this.callback(requestData); + this.hide(); + } + }; + this.messageListener = window.addEventListener("message", handleMessage); + this.cleanupListener = () => { window.removeEventListener("message", handleMessage) }; + }, + + hide: function() { + BaseModal.prototype.hide.call(this); + this.cleanupListener(); + }, + + /** + * Adds the action buttons to the modal. + */ + addActionButtons: function() { + this.addActionButton('cancel', gettext('Cancel')); + }, + + /** + * Show a component picker modal from library. + * @param contentPickerUrl Url for component picker + * @param callback A function to call with the selected block(s) + */ + showComponentPicker: function(contentPickerUrl, callback) { + this.contentPickerUrl = contentPickerUrl; + this.callback = callback; + + this.render(); + this.show(); + }, + + getContentHtml: function() { + return `