-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2522 from NCEAS/feature-1758-data-table-previews-5
Create the ViewObjectButtonView and use in Metadata View (Step 5 of issue #1758)
- Loading branch information
Showing
3 changed files
with
214 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"use strict"; | ||
|
||
define(["jquery", "backbone", "views/DataObjectView"], ( | ||
$, | ||
Backbone, | ||
DataObjectView, | ||
) => { | ||
// The base class for the view | ||
const BASE_CLASS = "view-data-button"; | ||
const CLASS_NAMES = { | ||
button: [BASE_CLASS, "btn"], | ||
icon: ["icon", "icon-eye-open"], | ||
modal: ["modal", "hide", "fade", "full-screen"], | ||
header: ["modal-header"], | ||
closeButton: ["close"], | ||
body: ["modal-body"], | ||
footer: ["modal-footer"], | ||
}; | ||
const BUTTON_TEXT = "View"; | ||
|
||
/** | ||
* @class ViewDataButtonView | ||
* @classdesc Creates a button that opens a modal to view a DataONE object | ||
* (using the DataObjectView). | ||
* @classcategory Views | ||
* @augments Backbone.View | ||
* @class | ||
* @since 0.0.0 | ||
* @screenshot views/ViewDataButtonView.png //TODO | ||
*/ | ||
const ViewDataButtonView = Backbone.View.extend( | ||
/** @lends ViewDataButtonView.prototype */ | ||
{ | ||
/** @inheritdoc */ | ||
type: "ViewDataButtonView", | ||
|
||
/** @inheritdoc */ | ||
className: CLASS_NAMES.button.join(" "), | ||
|
||
/** @inheritdoc */ | ||
tagName: "a", | ||
|
||
/** @inheritdoc */ | ||
events: { | ||
click: "openModal", | ||
}, | ||
|
||
/** | ||
* A template for the modal that will be displayed when the button is | ||
* clicked. The modal will contain a DataObjectView. | ||
* @param {object} options - Options for the modal | ||
* @param {string} options.title - The title of the modal | ||
* @returns {string} The HTML for the modal | ||
*/ | ||
modalTemplate({ title = "Data" } = {}) { | ||
const id = `modal-${this.model.cid}`; | ||
return `<div id="${id}" class="${CLASS_NAMES.modal.join(" ")}"> | ||
<div class="${CLASS_NAMES.header.join(" ")}"> | ||
<button type="button" class="${CLASS_NAMES.closeButton.join(" ")}" data-dismiss="modal">×</button> | ||
<h3>${title}</h3> | ||
</div> | ||
<div class="${CLASS_NAMES.body.join(" ")}"> | ||
<p>loading...</p> | ||
</div> | ||
<div class="${CLASS_NAMES.footer.join(" ")}"></div> | ||
</div>`; | ||
}, | ||
|
||
/** | ||
* Initializes the ViewDataButtonView | ||
* @param {object} options - Options for the view | ||
*/ | ||
initialize(options) { | ||
this.model = options.model; | ||
this.modalContainer = options.modalContainer || document.body; | ||
}, | ||
|
||
/** @inheritdoc */ | ||
render() { | ||
this.el.innerHTML = BUTTON_TEXT; | ||
const icon = document.createElement("i"); | ||
icon.classList.add(...CLASS_NAMES.icon); | ||
this.el.appendChild(icon); | ||
return this; | ||
}, | ||
|
||
/** | ||
* Renders the modal & DataObjectView | ||
* @returns {JQuery} The modal | ||
*/ | ||
renderModal() { | ||
const modalHTML = this.modalTemplate({ | ||
title: this.model.get("title") || "Data", | ||
}); | ||
const modal = $(modalHTML).modal(); | ||
const modalBody = modal.find(`.${CLASS_NAMES.body.join(".")}`); | ||
const objectView = new DataObjectView({ | ||
model: this.model, | ||
}); | ||
modalBody.empty(); | ||
modalBody.append(objectView.render().el); | ||
$(this.modalContainer).append(modal); | ||
return modal; | ||
}, | ||
|
||
/** | ||
* Opens the modal. Called when the button is clicked. | ||
* @param {Event} e - The click event | ||
*/ | ||
openModal(e) { | ||
e.preventDefault(); | ||
if (!this.modal) { | ||
this.modal = this.renderModal(); | ||
} | ||
this.modal.modal("show"); | ||
}, | ||
}, | ||
); | ||
|
||
return ViewDataButtonView; | ||
}); |