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, data) {
OC.Share.statuses[item] = data;
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').filterAttr('data-id', item);
if (file.length > 0) {
var action = $(file).find('.fileactions .action').filterAttr('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').filterAttr('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]['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
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 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: data.reshare.share_with, owner: data.reshare.displayname_owner})+'';
} else {
html += ''+t('core', 'Shared with you by {owner}', {owner: data.reshare.displayname_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 += '
';
$(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.collection);
} else {
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, 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) {
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, 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 = $( "" ).text( item.label );
if(item.label.length > 8 && item.label.substr(item.label.length-8) === ' (group)') {
// current label is group - wrap "strong" element
insert = insert.wrapInner('');
}
return $( "