function show_modal_shared(calendarId){
var html = DataLayer.render('templates/shared_calendar.ejs', {
calendar: Calendar.calendars
});
//Variaval global para manipulação dos usuários
//melhor perforface
currentUsers = {};
currentUsers[User.me.id] = true;
changePublic = false;
if (!UI.dialogs.sharedCalendar) {
UI.dialogs.sharedCalendar = jQuery('#calendarShared').append('
').find('.shared-calendar.active').html(html).dialog({
resizable: false,
modal:true,
autoOpen: false,
width:620,
position: 'center',
close: function(event, ui) {
//Implementações de cancelar
DataLayer.rollback('calendarToPermission');
currentUsers = {};
}
});
} else {
UI.dialogs.sharedCalendar.html(html);
}
/*Seleciona a agenda selecionada para compartilhamento*/
UI.dialogs.sharedCalendar.find('option[value="'+calendarId+'"]').attr('selected','selected').trigger('change');
UI.dialogs.sharedCalendar.find('input[name="isPublic"]').change(function(event){
$(this).parent().find('.free-busy').toggleClass('hidden');
changePublic = changePublic ? false: true;
});
UI.dialogs.sharedCalendar.find('.button').button()
.filter('.cancel').click(function(evt){
UI.dialogs.sharedCalendar.dialog("close");
}).end()
.filter('.save').click(function(evt){
//TODO - API ainda não suporta
//UI.dialogs.sharedCalendar.find('form').submit();
var calendarSelected = UI.dialogs.sharedCalendar.find('option:selected').val();
var save = function(){
if($('li.not-user').length == 0)
$.each(UI.dialogs.sharedCalendar.find('.user-list li.new'), function( i , element ){
var user = $(element).find('input[name="user[]"]').val();
var acl = $(element).find('input[name="attendeeAcl[]"]').val();
if(acl == "")
return true;
DataLayer.put('calendarToPermission', {
calendar: calendarSelected,
type: 0,
user: user,
acl: acl
});
});
$.each(UI.dialogs.sharedCalendar.find('.user-list li.current'), function( i , element ){
var id = $(element).find('input[type="checkbox"]').val();
var acl = $(element).find('input[name="attendeeAcl[]"]').val();
if(acl == "")
DataLayer.remove('calendarToPermission', id);
else
DataLayer.put('calendarToPermission', {
calendar: calendarSelected,
type: 0,
id: id,
acl: acl
});
});
DataLayer.commit(false, false, function(data){
UI.dialogs.sharedCalendar.dialog("close");
});
};
//Trata a criação de um acesso público a agenda
if(changePublic){
if(UI.dialogs.sharedCalendar.find('input[name="isPublic"]').is(':checked')){
var acl = UI.dialogs.sharedCalendar.find('input[name="busy"]').is(':checked') ? 'b' : 'w';
DataLayer.put('calendarToPermission', {
calendar: calendarSelected,
type: '1',
user: '0',
acl: acl
});
}else{
//TODO - remover quando for implementado remove com criteria
var permission = DataLayer.get('calendarToPermission', {
filter: ['AND', ['=', 'calendar', calendarId], ['=', 'user', '0']]
}, true);
if($.isArray(permission))
permission = permission[0];
DataLayer.remove('calendarToPermission', permission.id);
}
}
if(!!UI.dialogs.sharedCalendar.find('.user-list li input[name="attendeeAcl[]"][value=""]').length){
$.Zebra_Dialog('Alguns usuários estão sem permissões e serão automáticamente removidos, deseja continuar ?', {
'type': 'question',
'overlay_opacity': '0.5',
'buttons': ['Continuar', 'Cancelar'],
'onClose': function(clicked) {
if(clicked == 'Continuar') {
save();
}
}
});
}else
save();
});
UI.dialogs.sharedCalendar.find('.add-user-search .ui-icon-search').click(function(event) {
UI.dialogs.sharedCalendar.find('.add-user-search input').keydown();
});
var incompatibleAcl = function (obj, acls, buttons){
for (var i = 0; i < acls.length; i++){
var objremove = obj.parent().find('.'+buttons[i]+'');
if(objremove.hasClass('attendee-permissions-change-button'))
changeAcl(objremove, acls[i]);
}
}
var removeAcl = function(current, acl){
var acls = '';
for(var i = 0; i < current.length; i++)
if(current.charAt(i) != acl)
acls += current.charAt(i) ;
return acls;
}
var dependsAcl = function(obj, acls, buttons){
for (var i = 0; i < acls.length; i++){
var objremove = obj.parent().find('.'+buttons[i]+'');
if(!objremove.hasClass('attendee-permissions-change-button'))
changeAcl(objremove, acls[i]);
}
}
var changeAcl = function(obj, acl){
if(obj.hasClass('attendee-permissions-change-button')){
obj.parent().siblings('input[name="attendeeAcl[]"]').val(removeAcl( obj.parent().siblings('input[name="attendeeAcl[]"]').val(), acl)) ;
}else{
var acls = obj.parent().siblings('input[name="attendeeAcl[]"]').val();
obj.parent().siblings('input[name="attendeeAcl[]"]').val(acls + acl);
}
obj.toggleClass('attendee-permissions-change-button')
.find('span:first').toggleClass('attendee-permissions-change').end();
}
UI.dialogs.sharedCalendar.find('.add-user-search input').keydown(function(event) {
if(event.keyCode == '13' || typeof(event.keyCode) == 'undefined') {
var result = DataLayer.get('user', ["*", "name", $(this).val()], true);
/**
* TODO: trocar por template
*/
UI.dialogs.sharedCalendar.find('ul.search-result-list').empty().css('overflow', 'hidden');
if (!result) {
UI.dialogs.sharedCalendar.find('ul.search-result-list').append('');
}
for(i=0; i