OC.Share={
SHARE_TYPE_USER:0,
SHARE_TYPE_GROUP:1,
SHARE_TYPE_LINK:3,
SHARE_TYPE_EMAIL:4,
itemShares:[],
statuses:{},
droppedDown:false,
/**
* Loads ALL share statuses from server, stores them in OC.Share.statuses then
* calls OC.Share.updateIcons() to update the files "Share" icon to "Shared"
* according to their share status and share type.
*/
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') {
OC.Share.statuses = {};
$.each(result.data, function(item, data) {
OC.Share.statuses[item] = data;
});
OC.Share.updateIcons(itemType);
}
});
},
/**
* Updates the files' "Share" icons according to the known
* sharing states stored in OC.Share.statuses.
* (not reloaded from server)
*/
updateIcons:function(itemType){
var item;
for (item in OC.Share.statuses){
var data = OC.Share.statuses[item];
var hasLink = data['link'];
// Links override shared in terms of icon display
if (hasLink) {
var image = OC.imagePath('core', 'actions/public');
} else {
var image = OC.imagePath('core', 'actions/shared');
}
if (itemType != 'file' && itemType != 'folder') {
$('a.share[data-item="'+item+'"]').css('background', 'url('+image+') no-repeat center');
} else {
var file = $('tr[data-id="'+item+'"]');
if (file.length > 0) {
var action = $(file).find('.fileactions .action[data-action="Share"]');
var img = action.find('img').attr('src', image);
action.addClass('permanent');
action.html(' '+t('core', 'Shared')).prepend(img);
} else {
var dir = $('#dir').val();
if (dir.length > 1) {
var last = '';
var path = dir;
// Search for possible parent folders that are shared
while (path != last) {
if (path == data['path']) {
var actions = $('.fileactions .action[data-action="Share"]');
$.each(actions, function(index, action) {
var img = $(action).find('img');
if (img.attr('src') != OC.imagePath('core', 'actions/public')) {
img.attr('src', image);
$(action).addClass('permanent');
$(action).html(' '+t('core', 'Shared')).prepend(img);
}
});
}
last = path;
path = OC.Share.dirname(path);
}
}
}
}
}
},
updateIcon:function(itemType, itemSource) {
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');
} else {
var file = $('tr').filterAttr('data-id', String(itemSource));
if (file.length > 0) {
var action = $(file).find('.fileactions .action').filterAttr('data-action', 'Share');
var img = action.find('img').attr('src', image);
if (shares) {
action.addClass('permanent');
action.html(' '+ escapeHTML(t('core', 'Shared'))).prepend(img);
} else {
action.removeClass('permanent');
action.html(' '+ escapeHTML(t('core', 'Share'))).prepend(img);
}
}
}
if (shares) {
OC.Share.statuses[itemSource] = OC.Share.statuses[itemSource] || {};
OC.Share.statuses[itemSource]['link'] = link;
} else {
delete OC.Share.statuses[itemSource];
}
},
loadItem:function(itemType, itemSource) {
var data = '';
var checkReshare = true;
if (typeof OC.Share.statuses[itemSource] === 'undefined') {
// NOTE: Check does not always work and misses some shares, fix later
var checkShares = true;
} else {
var 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 {
if (result.data && result.data.message) {
var msg = result.data.message;
} else {
var msg = t('core', 'Error');
}
OC.dialogs.alert(msg, 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 while unsharing'), t('core', 'Error'));
}
});
},
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 while changing permissions'), t('core', 'Error'));
}
});
},
showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions) {
var data = OC.Share.loadItem(itemType, itemSource);
var html = '
';
if (data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
html += ''+t('core', 'Shared with you and the group {group} by {owner}', {group: escapeHTML(data.reshare.share_with), owner: escapeHTML(data.reshare.displayname_owner)})+'';
} else {
html += ''+t('core', 'Shared with you by {owner}', {owner: escapeHTML(data.reshare.displayname_owner)})+'';
}
html += ' ';
}
if (possiblePermissions & OC.PERMISSION_SHARE) {
// Determine the Allow Public Upload status.
// Used later on to determine if the
// respective checkbox should be checked or
// not.
var publicUploadEnabled = $('#filestable').data('allow-public-upload');
if (typeof publicUploadEnabled == 'undefined') {
publicUploadEnabled = 'no';
}
var allowPublicUploadStatus = false;
$.each(data.shares, function(key, value) {
if (value.share_type === OC.Share.SHARE_TYPE_LINK) {
allowPublicUploadStatus = (value.permissions & OC.PERMISSION_CREATE) ? true : false;
return true;
}
});
html += '';
html += '
';
html += '
';
var linksAllowed = $('#allowShareWithLink').val() === 'yes';
if (link && linksAllowed) {
html += '
';
html += '';
html += ' ';
html += '';
html += '';
html += '
';
html += '';
html += '
';
if (itemType === 'folder' && (possiblePermissions & OC.PERMISSION_CREATE) && publicUploadEnabled === 'yes') {
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(share.token, share.share_with, itemSource);
} else {
if (share.collection) {
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, share.collection);
} else {
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, false);
}
}
if (share.expiration != null) {
OC.Share.showExpirationDate(share.expiration);
}
});
}
$('#shareWith').autocomplete({minLength: 1, 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) {
$( "#shareWith" ).autocomplete( "option", "autoFocus", true );
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 {
$( "#shareWith" ).autocomplete( "option", "autoFocus", false );
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 Edit (CRUD) and Share
var permissions = OC.PERMISSION_ALL;
OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, function() {
OC.Share.addShareWith(shareType, shareWith, selected.item.label, permissions, possiblePermissions);
$('#shareWith').val('');
OC.Share.updateIcon(itemType, itemSource);
});
return false;
}
})
// customize internal _renderItem function to display groups and users differently
.data("ui-autocomplete")._renderItem = function( ul, item ) {
var insert = $( "" );
var text = (item.value.shareType == 1)? item.label + ' ('+t('core', 'group')+')' : item.label;
insert.text( text );
if(item.value.shareType == 1) {
insert = insert.wrapInner('');
}
return $( "