OC.Share={
SHARE_TYPE_USER:0,
SHARE_TYPE_GROUP:1,
SHARE_TYPE_LINK:3,
SHARE_TYPE_EMAIL:4,
itemShares:[],
statuses:[],
droppedDown:false,
loadIcons:function(itemType) {
// Load all share icons
$.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getItemsSharedStatuses', itemType: itemType }, function(result) {
if (result && result.status === 'success') {
$.each(result.data, function(item, hasPrivateLink) {
// Private links override shared in terms of icon display
if (itemType != 'file' && itemType != 'folder') {
if (hasPrivateLink) {
var image = OC.imagePath('core', 'actions/public');
} else {
var image = OC.imagePath('core', 'actions/shared');
}
$('a.share[data-item="'+item+'"]').css('background', 'url('+image+') no-repeat center');
}
OC.Share.statuses[item] = hasPrivateLink;
});
}
});
},
updateIcon:function(itemType, itemSource) {
if (itemType == 'file' || itemType == 'folder') {
var filename = $('tr').filterAttr('data-id', String(itemSource)).data('file');
if ($('#dir').val() == '/') {
itemSource = $('#dir').val() + filename;
} else {
itemSource = $('#dir').val() + '/' + filename;
}
}
var shares = false;
var link = false;
var image = OC.imagePath('core', 'actions/share');
$.each(OC.Share.itemShares, function(index) {
if (OC.Share.itemShares[index]) {
if (index == OC.Share.SHARE_TYPE_LINK) {
if (OC.Share.itemShares[index] == true) {
shares = true;
image = OC.imagePath('core', 'actions/public');
link = true;
return;
}
} else if (OC.Share.itemShares[index].length > 0) {
shares = true;
image = OC.imagePath('core', 'actions/shared');
}
}
});
if (itemType != 'file' && itemType != 'folder') {
$('a.share[data-item="'+itemSource+'"]').css('background', 'url('+image+') no-repeat center');
}
if (shares) {
OC.Share.statuses[itemSource] = link;
} else {
delete OC.Share.statuses[itemSource];
}
},
loadItem:function(itemType, itemSource) {
var data = '';
var checkReshare = true;
// Switch file sources to path to check if status is set
if (itemType == 'file' || itemType == 'folder') {
var filename = $('tr').filterAttr('data-id', String(itemSource)).data('file');
if ($('#dir').val() == '/') {
var item = $('#dir').val() + filename;
} else {
var item = $('#dir').val() + '/' + filename;
}
if (item.substring(0, 8) != '/Shared/') {
checkReshare = false;
}
} else {
var item = itemSource;
}
if (typeof OC.Share.statuses[item] === 'undefined') {
// NOTE: Check does not always work and misses some shares, fix later
checkShares = true;
} else {
checkShares = true;
}
$.ajax({type: 'GET', url: OC.filePath('core', 'ajax', 'share.php'), data: { fetch: 'getItem', itemType: itemType, itemSource: itemSource, checkReshare: checkReshare, checkShares: checkShares }, async: false, success: function(result) {
if (result && result.status === 'success') {
data = result.data;
} else {
data = false;
}
}});
return data;
},
share:function(itemType, itemSource, shareType, shareWith, permissions, callback) {
$.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'share', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) {
if (result && result.status === 'success') {
if (callback) {
callback(result.data);
}
} else {
OC.dialogs.alert(result.data.message, t('core', 'Error while sharing'));
}
});
},
unshare:function(itemType, itemSource, shareType, shareWith, callback) {
$.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'unshare', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith }, function(result) {
if (result && result.status === 'success') {
if (callback) {
callback();
}
} else {
OC.dialogs.alert(t('core', 'Error'), t('core', 'Error while unsharing'));
}
});
},
setPermissions:function(itemType, itemSource, shareType, shareWith, permissions) {
$.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'setPermissions', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) {
if (!result || result.status !== 'success') {
OC.dialogs.alert(t('core', 'Error'), t('core', 'Error while changing permissions'));
}
});
},
showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions) {
var data = OC.Share.loadItem(itemType, itemSource);
var html = '
';
if (data.reshare) {
if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
html += ''+t('core', 'Shared with you and the group')+' '+data.reshare.share_with+' '+t('core', 'by')+' '+data.reshare.uid_owner+'';
} else {
html += ''+t('core', 'Shared with you by')+' '+data.reshare.uid_owner+'';
}
html += ' ';
}
if (possiblePermissions & OC.PERMISSION_SHARE) {
html += '';
html += '
';
html += '
';
if (link) {
html += '
';
html += '';
html += '';
html += ' ';
html += '';
html += '
';
html += '';
html += '
';
html += '
';
}
html += '
';
html += '';
html += '';
html += '
';
$(html).appendTo(appendTo);
// Reset item shares
OC.Share.itemShares = [];
if (data.shares) {
$.each(data.shares, function(index, share) {
if (share.share_type == OC.Share.SHARE_TYPE_LINK) {
OC.Share.showLink(itemSource, share.share_with);
} else {
if (share.collection) {
OC.Share.addShareWith(share.share_type, share.share_with, share.permissions, possiblePermissions, share.collection);
} else {
OC.Share.addShareWith(share.share_type, share.share_with, share.permissions, possiblePermissions, false);
}
}
if (share.expiration != null) {
OC.Share.showExpirationDate(share.expiration);
}
});
}
$('#shareWith').autocomplete({minLength: 2, source: function(search, response) {
// if (cache[search.term]) {
// response(cache[search.term]);
// } else {
$.get(OC.filePath('core', 'ajax', 'share.php'), { fetch: 'getShareWith', search: search.term, itemShares: OC.Share.itemShares }, function(result) {
if (result.status == 'success' && result.data.length > 0) {
response(result.data);
} else {
// Suggest sharing via email if valid email address
// var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
// if (pattern.test(search.term)) {
// response([{label: t('core', 'Share via email:')+' '+search.term, value: {shareType: OC.Share.SHARE_TYPE_EMAIL, shareWith: search.term}}]);
// } else {
response([t('core', 'No people found')]);
// }
}
});
// }
},
focus: function(event, focused) {
event.preventDefault();
},
select: function(event, selected) {
event.stopPropagation();
var itemType = $('#dropdown').data('item-type');
var itemSource = $('#dropdown').data('item-source');
var shareType = selected.item.value.shareType;
var shareWith = selected.item.value.shareWith;
$(this).val(shareWith);
// Default permissions are Read and Share
var permissions = OC.PERMISSION_READ | OC.PERMISSION_SHARE;
OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, function() {
OC.Share.addShareWith(shareType, shareWith, permissions, possiblePermissions);
$('#shareWith').val('');
OC.Share.updateIcon(itemType, itemSource);
});
return false;
}
});
} else {
html += '';
html += '
';
$(html).appendTo(appendTo);
}
$('#dropdown').show('blind', function() {
OC.Share.droppedDown = true;
});
$('#shareWith').focus();
},
hideDropDown:function(callback) {
$('#dropdown').hide('blind', function() {
OC.Share.droppedDown = false;
$('#dropdown').remove();
if (typeof FileActions !== 'undefined') {
$('tr').removeClass('mouseOver');
}
if (callback) {
callback.call();
}
});
},
addShareWith:function(shareType, shareWith, permissions, possiblePermissions, collection) {
if (!OC.Share.itemShares[shareType]) {
OC.Share.itemShares[shareType] = [];
}
OC.Share.itemShares[shareType].push(shareWith);
if (collection) {
if (collection.item_type == 'file' || collection.item_type == 'folder') {
var item = collection.path;
} else {
var item = collection.item_source;
}
var collectionList = $('#shareWithList li').filterAttr('data-collection', item);
if (collectionList.length > 0) {
$(collectionList).append(', '+shareWith);
} else {
var html = '