-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: library component picker modal
- Loading branch information
1 parent
971de8a
commit b6a8793
Showing
4 changed files
with
61 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* | ||
* ``` | ||
* <div id="library-content-picker" class="picker"></div> | ||
* <div class="picker-cover"></div> | ||
* ``` | ||
* | ||
*/ | ||
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( | ||
`<iframe src="${contentPickerUrl}" onload="this.contentWindow.focus()" frameborder="0" style="width: 100%; height: 100%;"></iframe>` | ||
); | ||
$(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 `<iframe src="${this.contentPickerUrl}" onload="this.contentWindow.focus()" frameborder="0" style="width: 100%; height: 100%;"/>`; | ||
}, | ||
}); | ||
|
||
return { | ||
createComponent: createComponent, | ||
}; | ||
return AddLibraryContent; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters