Skip to content

Commit

Permalink
Add classes to edit, delete, add related buttons if user owned docume…
Browse files Browse the repository at this point in the history
…nt (#1254)

* Add check if document owned by session user, add classes for Edit and Delete button if true

* Missing functionality of customService option for hiding Add Related if not logged in

* Set classes for add related item button

* Add Related button pointer cursor, missing French translations
  • Loading branch information
alexgao1 authored Dec 3, 2024
1 parent aa0271d commit c313040
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 33 deletions.
6 changes: 6 additions & 0 deletions nunaliit2-js/src/main/js/nunaliit2/css/basic/n2.theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ CreateDocument widget
display: inline-block;
}

.nunaliit_form_link_add_related_item_wrapper
.nunaliit_form_link.nunaliit_form_link_add_related_item
{
cursor: pointer;
}

.intro b {
font-size: 26px;
font-weight: normal;
Expand Down
78 changes: 53 additions & 25 deletions nunaliit2-js/src/main/js/nunaliit2/n2.couchDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,39 +524,63 @@ var Display = $n2.Class({
// Show 'edit' button
if( opt.edit
&& $n2.couchMap.canEditDoc(data) ) {
buttonDisplay.drawButton({
elem: $buttons
,name: 'edit'
,label: _loc('Edit')
,click: function(){
_this._performDocumentEdit(data, opt);
}
});
const editButtonCfg = {
elem: $buttons
,name: 'edit'
,label: _loc('Edit')
,click: function(){
_this._performDocumentEdit(data, opt);
}
}
if ($n2.couchMap.documentOwnedBySessionUser(data)) {
editButtonCfg.classNames = ['n2_document_user_owned_editable']
}
buttonDisplay.drawButton(editButtonCfg);
};

// Show 'delete' button
if( opt['delete']
&& $n2.couchMap.canDeleteDoc(data) ) {
buttonDisplay.drawButton({
elem: $buttons
,name: 'delete'
,label: _loc('Delete')
,click: function(){
_this._performDocumentDelete(data, opt);
}
});
const deleteButtonCfg = {
elem: $buttons
,name: 'delete'
,label: _loc('Delete')
,click: function(){
_this._performDocumentDelete(data, opt);
}
}
if ($n2.couchMap.documentOwnedBySessionUser(data)) {
deleteButtonCfg.classNames = ['n2_document_user_owned_deletable']
}
buttonDisplay.drawButton(deleteButtonCfg);
};

// Show 'add related' button
if( opt.related
&& this.displayRelatedInfoProcess ) {
this.displayRelatedInfoProcess.addButton({
display: this
,div: $buttons[0]
,doc: data
,schema: opt.schema
,buttonDisplay: buttonDisplay
});
let showAddRelatedButton = true
if (this.restrictAddRelatedButtonToLoggedIn) {
var isLoggedInMsg = {
type: 'authIsLoggedIn'
, isLoggedIn: false
}
if (dispatcher) {
dispatcher.synchronousCall(DH, isLoggedInMsg);
}
if (!isLoggedInMsg.isLoggedIn) {
showAddRelatedButton = false;
}
}
if (showAddRelatedButton) {
this.displayRelatedInfoProcess.addButton({
display: this
,div: $buttons[0]
,doc: data
,schema: opt.schema
,buttonDisplay: buttonDisplay
});
}

};

// Show 'reply' button
Expand Down Expand Up @@ -1466,8 +1490,12 @@ var LegacyDisplayRelatedFunctionAdapter = $n2.Class({
,onElementCreated: function($addRelatedButton){
$addRelatedButton.addClass('nunaliit_form_link');
$addRelatedButton.addClass('nunaliit_form_link_add_related_item');

$addRelatedButton.menuselector();
const options = {}
if ($n2.couchMap.documentOwnedBySessionUser(this.doc)) {
options.wrapperSpanClass = 'n2_document_user_owned_can_add_related'
}

$addRelatedButton.menuselector(options);
}
,onRelatedDocumentCreated: function(docId){}
});
Expand Down
19 changes: 14 additions & 5 deletions nunaliit2-js/src/main/js/nunaliit2/n2.couchMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ function canEditDoc(data) {

// If a document is not on a controlled layer, then the creator of the
// document can edit it
if( data.nunaliit_created
&& data.nunaliit_created.nunaliit_type
&& data.nunaliit_created.nunaliit_type === 'actionstamp'
&& data.nunaliit_created.name === userName
) {
if (documentOwnedBySessionUser(data)) {
return true;
};

Expand All @@ -177,6 +173,18 @@ function canDeleteDoc(data) {
return canEditDoc(data);
};

function documentOwnedBySessionUser(doc) {
const sessionContext = getCurrentContext()
if (sessionContext) {
const username = sessionContext.name
return (doc
&& doc.nunaliit_created
&& doc.nunaliit_created.nunaliit_type === 'actionstamp'
&& doc.nunaliit_created.name === username)
}
return false
};

function documentContainsMedia(doc){
var containsMedia = false;

Expand Down Expand Up @@ -238,6 +246,7 @@ $n2.couchMap = {
,documentContainsMedia: documentContainsMedia
,documentContainsApprovedMedia: documentContainsApprovedMedia
,documentContainsDeniedMedia: documentContainsDeniedMedia
,documentOwnedBySessionUser
};

})(nunaliit2);
12 changes: 9 additions & 3 deletions nunaliit2-js/src/main/js/nunaliit2/n2.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,22 @@ if( typeof $.widget === 'function' ){
$.widget( 'nunaliit.menuselector', {

options: {
menuClass: null
menuClass: null,
spanClass: null
}

,_create: function() {
var _this = this;

this.wrapper = $('<span>')
.addClass('nunaliit-menuselector')
.addClass('nunaliit_form_link_add_related_item_wrapper')
.insertAfter(this.element);


if (this.options.wrapperSpanClass) {
this.wrapper.addClass(this.options.wrapperSpanClass)
}

var classes = this.element.attr('class');

var text = this.element.find('option').first().text();
Expand All @@ -126,7 +132,7 @@ if( typeof $.widget === 'function' ){
.css('z-index',1000)
.hide()
.appendTo(this.wrapper);

this.element.hide();
}

Expand Down
3 changes: 3 additions & 0 deletions nunaliit2-js/src/main/js/nunaliit2/nunaliit2.fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ $n2.l10n.addLocalizedStrings('fr',{
,"{index}/{count}":"{index}/{count}"
,"{label} definition ({key}) already exists - not loaded or updated":"Définition {label} ({key}) existe déjà - ignorée"
,"No documents found.": "Aucun document n'a été trouvé."
,"File Upload": "Téléchargement de fichiers"
,"Record Audio": "Enregistrer l'audio"
,"Record Video": "Enregistrer une vidéo"
});

if( $ && $.datepicker ) {
Expand Down

0 comments on commit c313040

Please sign in to comment.