2012-06-30 07:26:21 +02:00
<%@page pageEncoding="UTF-8" %>
<%@include file="/jsp/init.jsp" %>
2012-06-03 16:16:38 +02:00
<style type="text/css">
#sharingContainer {
2012-06-08 00:45:22 +02:00
height: 180px;
width: 100%;
overflow-y: scroll;
border-top: 1px solid #d3d3d3;
border-bottom: 1px solid #d3d3d3;
}
#sharingContainer table {
font-size: 100%;
}
#collabEmails {
2012-06-30 21:26:31 +02:00
width: 250px;
2012-06-16 16:37:40 +02:00
display: inline-block;
vertical-align: middle;
margin-bottom: 0;
2012-06-03 16:16:38 +02:00
}
2012-06-08 00:45:22 +02:00
#roleBtn {
2012-06-16 16:37:40 +02:00
margin: 0 10px;
display: inline-block;
vertical-align: middle;
}
#addBtn {
display: inline-block;
vertical-align: middle;
}
#collabMessage {
display: block;
width: 100%;
2012-06-08 00:45:22 +02:00
}
2012-06-03 16:16:38 +02:00
</style>
2012-06-30 20:25:24 +02:00
<p><strong><spring:message code="WHO_CAN_ACCESS"/>:</strong></p>
2012-06-03 16:16:38 +02:00
<div id="sharingContainer">
2012-06-08 00:45:22 +02:00
<table class="table" id="collabsTable">
2012-06-13 03:33:51 +02:00
<colgroup>
2012-06-30 21:26:31 +02:00
<col width="65%"/>
<col width="30%"/>
<col width="5%"/>
2012-06-13 03:33:51 +02:00
</colgroup>
2012-06-03 16:16:38 +02:00
</table>
</div>
2012-06-08 00:45:22 +02:00
<div class="well">
<div id="errorMsg" class="alert alert-error"></div>
2012-06-16 16:37:40 +02:00
<div>
2012-06-30 20:25:24 +02:00
<p><strong><spring:message code="ADD_PEOPLE"/>:</strong></p>
2012-06-16 16:37:40 +02:00
<input type="text" id="collabEmails" name="collabEmails"
2012-06-30 20:25:24 +02:00
placeholder="<spring:message code="COLLABORATORS_SEPARATED_BY_COMA"/>" class="span1"/>
2012-06-16 16:37:40 +02:00
<div class="btn-group" id="roleBtn">
2012-06-30 20:25:24 +02:00
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><spring:message code="CAN_EDIT"/>
2012-06-16 16:37:40 +02:00
<span class="caret"> </span>
</a>
<ul class="dropdown-menu" data-role="editor" id="shareRole">
2012-06-30 20:25:24 +02:00
<li><a href="#" data-role="editor"><spring:message code="CAN_EDIT"/></a></li>
<li><a href="#" data-role="viewer"><spring:message code="CAN_VIEW"/></a></li>
2012-06-16 16:37:40 +02:00
</ul>
</div>
2012-06-30 20:25:24 +02:00
<button id="addBtn" class="btn btn-primary"><spring:message code="ADD"/></button>
2012-06-16 16:37:40 +02:00
</div>
<div style="margin-top: 10px;">
2012-06-30 21:26:31 +02:00
<p><strong><spring:message code="EMAIL_NOTIFICATION_MESSAGE"/></strong> - <a href="#"
id="addMessageLink"><spring:message
code="ADD_MESSAGE"/></a></p>
2012-06-30 20:25:24 +02:00
<textarea cols="4" id="collabMessage" placeholder="<spring:message code="OPTIONAL_CUSTOM_MESSAGE"/>">
2012-06-16 16:37:40 +02:00
</textarea>
2012-06-08 00:45:22 +02:00
</div>
</div>
2012-06-03 16:16:38 +02:00
2012-06-16 16:37:40 +02:00
2012-06-03 16:16:38 +02:00
<script type="text/javascript">
2012-06-13 03:33:51 +02:00
$("#errorMsg").hide();
2012-06-16 16:37:40 +02:00
$("#collabMessage").hide();
2012-06-30 21:26:31 +02:00
$("#addMessageLink").click(function (event) {
2012-06-16 16:37:40 +02:00
$("#collabMessage").toggle().val("");
event.preventDefault();
});
2012-06-13 03:33:51 +02:00
var messages = {
2012-06-30 21:26:31 +02:00
owner:'<spring:message code="IS_OWNER"/>',
editor:'<spring:message code="CAN_EDIT"/>',
viewer:'<spring:message code="CAN_VIEW"/>'};
2012-06-13 03:33:51 +02:00
function onClickShare(aElem) {
var role = $(aElem).attr('data-role');
var roleDec = messages[role];
var btnElem = $(aElem).parent().parent().parent().find(".dropdown-toggle");
btnElem.text(roleDec).append(' <span class="caret"> </span>');
return role;
}
2012-06-16 17:12:53 +02:00
function addCollaboration(email, role, changeType) {
2012-06-13 03:33:51 +02:00
var rowStr;
var tableElem = $("#collabsTable");
if (role != "owner") {
2012-06-08 00:45:22 +02:00
// Add row to the table ...
var rowTemplate = '\
<tr data-collab="{email}" data-role="{role}">\
2012-06-16 17:12:53 +02:00
<td>{email}{typeIcon}</td>\
2012-06-08 00:45:22 +02:00
<td>\
<div class="btn-group">\
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#""></a>\
<ul class="dropdown-menu">\
2012-06-13 03:33:51 +02:00
<li><a href="#" data-role="editor">' + messages['editor'] + '</a></li>\
<li><a href="#" data-role="viewer">' + messages['viewer'] + '</a></li>\
2012-06-08 00:45:22 +02:00
</ul>\
</div>\
</td>\
<td><a href="#">x</a></td>\
</tr>';
2012-06-13 03:33:51 +02:00
rowStr = rowTemplate.replace(/{email}/g, email);
2012-06-08 00:45:22 +02:00
rowStr = rowStr.replace(/{role}/g, role);
2012-06-16 17:12:53 +02:00
rowStr = rowStr.replace(/{typeIcon}/g, changeType ? ' <span class="label label-success">' + changeType + '</span>' : "");
2012-06-08 00:45:22 +02:00
var elem = tableElem.append(rowStr);
// Register change role event ...
var rowElem = $("#collabsTable tr:last");
2012-09-20 04:12:47 +02:00
$(rowElem.find(".dropdown-menu a").click(function (event) {
2012-06-13 03:33:51 +02:00
reportError(null);
2012-06-08 00:45:22 +02:00
var role = onClickShare(this);
rowElem.attr('data-role', role);
event.preventDefault();
}));
rowElem.find('.dropdown-menu a[data-role="' + role + '"]').click();
// Register remove event ...
2012-06-30 21:26:31 +02:00
rowElem.find("td:last a").click(function (event) {
2012-06-13 03:33:51 +02:00
reportError(null);
2012-06-08 00:45:22 +02:00
var email = rowElem.attr('data-collab');
removeCollab(email);
event.preventDefault();
});
2012-06-13 03:33:51 +02:00
} else {
rowStr = '<tr data-collab=' + email + ' data-role="' + role + '">\
2012-06-30 21:26:31 +02:00
<td>' + email + ' (<spring:message code="YOU"/>)</td>\
2012-06-13 03:33:51 +02:00
<td><button class="btn btn-secondary">' + messages[role] + '</button></td>\
<td></td>\
</tr>';
tableElem.append(rowStr);
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
}
}
2012-06-30 21:26:31 +02:00
var removeCollab = function (email) {
2012-06-13 03:33:51 +02:00
// Remove html entry ...
$('#collabsTable tr[data-collab="' + email + '"]').detach();
};
2012-06-30 21:26:31 +02:00
$(function () {
2012-11-10 21:19:28 +01:00
jQuery.ajax("c/restful/maps/${mindmap.id}/collabs", {
2012-06-13 03:33:51 +02:00
async:false,
2012-06-30 21:26:31 +02:00
dataType:'json',
type:'GET',
2012-06-13 03:33:51 +02:00
contentType:"text/plain",
2012-06-30 21:26:31 +02:00
success:function (data, textStatus, jqXHR) {
2012-06-13 03:33:51 +02:00
// Owner roles is the first in the table ...
2012-06-30 21:26:31 +02:00
var collabs = data.collaborations.sort(function (a, b) {
2012-06-14 04:04:29 +02:00
return a.role <= b.role;
2012-06-13 03:33:51 +02:00
});
2012-06-08 00:45:22 +02:00
2012-09-20 04:12:47 +02:00
// Add all the columns in the table ...
2012-06-13 03:33:51 +02:00
for (var i = 0; i < collabs.length; i++) {
var collab = collabs[i];
addCollaboration(collab.email, collab.role);
2012-06-10 03:49:54 +02:00
}
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
},
2012-06-30 21:26:31 +02:00
error:function (jqXHR, textStatus, errorThrown) {
2012-06-13 03:33:51 +02:00
alert(textStatus);
}
});
});
2012-06-08 00:45:22 +02:00
2012-06-30 21:26:31 +02:00
$("#addBtn").click(function (event) {
var i, email;
2012-06-13 03:33:51 +02:00
var emailsStr = $("#collabEmails").val();
var role = $("#shareRole").attr('data-role');
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
reportError(null);
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
// Split emails ...
var valid = true;
if (emailsStr.length > 0) {
2012-06-30 21:26:31 +02:00
var emails = jQuery.grep(emailsStr.split(/[;|,|\s]/), function (val) {
2012-06-13 03:33:51 +02:00
return val.length > 0
});
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
var model = buildCollabModel();
for (i = 0; i < emails.length; i++) {
email = emails[i];
if (!isValidEmailAddress(email)) {
reportError("Invalid email address:" + email);
valid = false;
2012-06-08 00:45:22 +02:00
}
2012-06-13 03:33:51 +02:00
// Is there a collab with the same email ?
var useExists = jQuery.grep(model.collaborations,
2012-06-30 21:26:31 +02:00
function (val) {
2012-06-13 03:33:51 +02:00
return val.email.trim() == email.trim();
}).length > 0;
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
if (useExists) {
reportError(email + " is already in the shared list");
valid = false;
2012-06-08 00:45:22 +02:00
}
}
2012-06-13 03:33:51 +02:00
} else {
reportError("Emails address can not be empty");
valid = false;
}
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
if (valid) {
// Emails are valid, add them as rows ...
$("#collabEmails").val("");
for (i = 0; i < emails.length; i++) {
email = emails[i];
2012-06-30 21:26:31 +02:00
addCollaboration(email, role, '<spring:message code="NEW"/>');
2012-06-13 03:33:51 +02:00
}
2012-06-08 00:45:22 +02:00
}
2012-06-13 03:33:51 +02:00
});
// Register change event ...
2012-06-30 21:26:31 +02:00
$("#shareRole a").click(function () {
2012-06-13 03:33:51 +02:00
var role = onClickShare(this);
$(this).parent().attr('data-role', role);
event.preventDefault();
});
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
}
function reportError(msg) {
if (msg) {
2012-06-08 00:45:22 +02:00
$('#errorMsg').show();
$('#errorMsg').append("<p>" + msg + "</p>");
2012-06-13 03:33:51 +02:00
} else {
$("#errorMsg").text("").hide();
2012-06-08 00:45:22 +02:00
}
2012-06-13 03:33:51 +02:00
}
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
function buildCollabModel() {
2012-06-30 21:26:31 +02:00
var collabs = $('#collabsTable tr').map(function () {
2012-06-10 03:49:54 +02:00
return {
2012-06-30 21:26:31 +02:00
email:$(this).attr('data-collab'),
role:$(this).attr('data-role')
2012-06-10 03:49:54 +02:00
};
2012-06-13 03:33:51 +02:00
});
collabs = jQuery.makeArray(collabs);
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
return {
count:collabs.length,
collaborations:collabs
};
}
2012-06-08 00:45:22 +02:00
2012-06-13 03:33:51 +02:00
// Hook for interaction with the main parent window ...
2012-06-30 21:26:31 +02:00
var submitDialogForm = function () {
2012-06-13 03:33:51 +02:00
var collabs = buildCollabModel();
2012-06-30 21:26:31 +02:00
collabs.collaborations = jQuery.grep(collabs.collaborations, function () {
2012-06-13 03:33:51 +02:00
return this.role != 'owner';
});
2012-06-16 16:37:40 +02:00
collabs['message'] = $("#collabMessage").val();
2012-06-13 03:33:51 +02:00
2012-11-10 21:19:28 +01:00
jQuery.ajax("c/restful/maps/${mindmap.id}/collabs", {
2012-06-13 03:33:51 +02:00
async:false,
2012-06-30 21:26:31 +02:00
dataType:'json',
type:'PUT',
data:JSON.stringify(collabs),
2012-06-13 03:33:51 +02:00
contentType:"application/json",
2012-06-30 21:26:31 +02:00
success:function (data, textStatus, jqXHR) {
2012-06-13 03:33:51 +02:00
$('#share-dialog-modal').modal('hide');
},
2012-06-30 21:26:31 +02:00
error:function (jqXHR, textStatus, errorThrown) {
2012-06-13 03:33:51 +02:00
reportError(textStatus);
}
});
}
2012-06-03 16:16:38 +02:00
</script>