2014-11-18 20:53:45 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
2014-12-15 14:43:16 +03:00
|
|
|
|
|
|
|
/* global Handlebars */
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
(function(OCA) {
|
|
|
|
|
2014-12-15 19:20:41 +03:00
|
|
|
var TEMPLATE_FAVORITE_ACTION =
|
2014-12-15 14:43:16 +03:00
|
|
|
'<a href="#" ' +
|
|
|
|
'class="action action-favorite {{#isFavorite}}permanent{{/isFavorite}}">' +
|
|
|
|
'<img class="svg" alt="{{altText}}" src="{{imgFile}}" />' +
|
|
|
|
'</a>';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path to the star image
|
|
|
|
*
|
|
|
|
* @param {boolean} state true if starred, false otherwise
|
|
|
|
* @return {string} path to star image
|
|
|
|
*/
|
|
|
|
function getStarImage(state) {
|
|
|
|
return OC.imagePath('core', state ? 'actions/starred' : 'actions/star');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the star icon with the given state
|
2014-12-15 19:20:41 +03:00
|
|
|
*
|
2014-12-15 14:43:16 +03:00
|
|
|
* @param {boolean} state true if starred, false otherwise
|
|
|
|
* @return {Object} jQuery object
|
|
|
|
*/
|
|
|
|
function renderStar(state) {
|
|
|
|
if (!this._template) {
|
|
|
|
this._template = Handlebars.compile(TEMPLATE_FAVORITE_ACTION);
|
|
|
|
}
|
|
|
|
return this._template({
|
|
|
|
isFavorite: state,
|
2015-01-17 18:52:36 +03:00
|
|
|
altText: state ? t('files', 'Favorited') : t('files', 'Favorite'),
|
2014-12-15 14:43:16 +03:00
|
|
|
imgFile: getStarImage(state)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-15 17:50:57 +03:00
|
|
|
/**
|
|
|
|
* Toggle star icon on action element
|
|
|
|
*
|
|
|
|
* @param {Object} action element
|
|
|
|
* @param {boolean} state true if starred, false otherwise
|
|
|
|
*/
|
|
|
|
function toggleStar($actionEl, state) {
|
|
|
|
$actionEl.find('img').attr('src', getStarImage(state));
|
2015-01-14 22:09:09 +03:00
|
|
|
$actionEl.hide().show(0); //force Safari to redraw element on src change
|
2014-12-15 17:50:57 +03:00
|
|
|
$actionEl.toggleClass('permanent', state);
|
|
|
|
}
|
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
OCA.Files = OCA.Files || {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @namespace OCA.Files.TagsPlugin
|
|
|
|
*
|
|
|
|
* Extends the file actions and file list to include a favorite action icon
|
|
|
|
* and addition "data-tags" and "data-favorite" attributes.
|
|
|
|
*/
|
|
|
|
OCA.Files.TagsPlugin = {
|
|
|
|
name: 'Tags',
|
|
|
|
|
|
|
|
allowedLists: [
|
|
|
|
'files',
|
|
|
|
'favorites'
|
|
|
|
],
|
|
|
|
|
|
|
|
_extendFileActions: function(fileActions) {
|
|
|
|
var self = this;
|
|
|
|
// register "star" action
|
|
|
|
fileActions.registerAction({
|
2015-08-12 18:30:20 +03:00
|
|
|
name: 'Favorite',
|
2015-09-22 15:52:52 +03:00
|
|
|
displayName: t('files', 'Favorite'),
|
2014-11-18 20:53:45 +03:00
|
|
|
mime: 'all',
|
|
|
|
permissions: OC.PERMISSION_READ,
|
2015-07-16 16:28:45 +03:00
|
|
|
type: OCA.Files.FileActions.TYPE_INLINE,
|
2014-11-18 20:53:45 +03:00
|
|
|
render: function(actionSpec, isDefault, context) {
|
|
|
|
var $file = context.$file;
|
|
|
|
var isFavorite = $file.data('favorite') === true;
|
2014-12-15 14:43:16 +03:00
|
|
|
var $icon = $(renderStar(isFavorite));
|
2014-12-12 19:06:57 +03:00
|
|
|
$file.find('td:first>.favorite').replaceWith($icon);
|
2014-11-18 20:53:45 +03:00
|
|
|
return $icon;
|
|
|
|
},
|
|
|
|
actionHandler: function(fileName, context) {
|
|
|
|
var $actionEl = context.$file.find('.action-favorite');
|
|
|
|
var $file = context.$file;
|
2015-09-01 20:29:55 +03:00
|
|
|
var fileInfo = context.fileList.files[$file.index()];
|
2014-11-18 20:53:45 +03:00
|
|
|
var dir = context.dir || context.fileList.getCurrentDirectory();
|
|
|
|
var tags = $file.attr('data-tags');
|
|
|
|
if (_.isUndefined(tags)) {
|
|
|
|
tags = '';
|
|
|
|
}
|
|
|
|
tags = tags.split('|');
|
|
|
|
tags = _.without(tags, '');
|
|
|
|
var isFavorite = tags.indexOf(OC.TAG_FAVORITE) >= 0;
|
|
|
|
if (isFavorite) {
|
|
|
|
// remove tag from list
|
|
|
|
tags = _.without(tags, OC.TAG_FAVORITE);
|
|
|
|
} else {
|
|
|
|
tags.push(OC.TAG_FAVORITE);
|
|
|
|
}
|
2015-09-01 20:29:55 +03:00
|
|
|
|
|
|
|
// pre-toggle the star
|
2015-01-26 13:27:51 +03:00
|
|
|
toggleStar($actionEl, !isFavorite);
|
2014-12-15 17:50:57 +03:00
|
|
|
|
2015-09-01 20:29:55 +03:00
|
|
|
context.fileInfoModel.trigger('busy', context.fileInfoModel, true);
|
2015-08-14 16:54:27 +03:00
|
|
|
|
2014-11-18 20:53:45 +03:00
|
|
|
self.applyFileTags(
|
|
|
|
dir + '/' + fileName,
|
2015-01-26 14:01:07 +03:00
|
|
|
tags,
|
|
|
|
$actionEl,
|
|
|
|
isFavorite
|
2014-12-15 14:43:16 +03:00
|
|
|
).then(function(result) {
|
2015-09-01 20:29:55 +03:00
|
|
|
context.fileInfoModel.trigger('busy', context.fileInfoModel, false);
|
2015-01-12 19:54:35 +03:00
|
|
|
// response from server should contain updated tags
|
|
|
|
var newTags = result.tags;
|
|
|
|
if (_.isUndefined(newTags)) {
|
|
|
|
newTags = tags;
|
|
|
|
}
|
2015-09-01 20:29:55 +03:00
|
|
|
context.fileInfoModel.set({
|
|
|
|
'tags': newTags,
|
|
|
|
'favorite': !isFavorite
|
|
|
|
});
|
2014-11-18 20:53:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_extendFileList: function(fileList) {
|
|
|
|
// extend row prototype
|
2014-12-15 17:18:29 +03:00
|
|
|
fileList.$el.addClass('has-favorites');
|
2014-11-18 20:53:45 +03:00
|
|
|
var oldCreateRow = fileList._createRow;
|
|
|
|
fileList._createRow = function(fileData) {
|
|
|
|
var $tr = oldCreateRow.apply(this, arguments);
|
|
|
|
if (fileData.tags) {
|
|
|
|
$tr.attr('data-tags', fileData.tags.join('|'));
|
|
|
|
if (fileData.tags.indexOf(OC.TAG_FAVORITE) >= 0) {
|
|
|
|
$tr.attr('data-favorite', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tr.find('td:first').prepend('<div class="favorite"></div>');
|
|
|
|
return $tr;
|
|
|
|
};
|
2015-08-12 18:30:20 +03:00
|
|
|
var oldElementToFile = fileList.elementToFile;
|
|
|
|
fileList.elementToFile = function($el) {
|
|
|
|
var fileInfo = oldElementToFile.apply(this, arguments);
|
2015-09-02 20:02:06 +03:00
|
|
|
var tags = $el.attr('data-tags');
|
|
|
|
if (_.isUndefined(tags)) {
|
|
|
|
tags = '';
|
|
|
|
}
|
|
|
|
tags = tags.split('|');
|
|
|
|
tags = _.without(tags, '');
|
|
|
|
fileInfo.tags = tags;
|
2015-08-12 18:30:20 +03:00
|
|
|
return fileInfo;
|
|
|
|
};
|
2014-11-18 20:53:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
attach: function(fileList) {
|
|
|
|
if (this.allowedLists.indexOf(fileList.id) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._extendFileActions(fileList.fileActions);
|
|
|
|
this._extendFileList(fileList);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the given files' tags with the specified ones.
|
|
|
|
*
|
|
|
|
* @param {String} fileName path to the file or folder to tag
|
|
|
|
* @param {Array.<String>} tagNames array of tag names
|
2015-01-26 14:01:07 +03:00
|
|
|
* @param {Object} $actionEl element
|
|
|
|
* @param {boolean} isFavorite Was the item favorited before
|
2014-11-18 20:53:45 +03:00
|
|
|
*/
|
2015-01-26 14:01:07 +03:00
|
|
|
applyFileTags: function(fileName, tagNames, $actionEl, isFavorite) {
|
2014-11-18 20:53:45 +03:00
|
|
|
var encodedPath = OC.encodePath(fileName);
|
|
|
|
while (encodedPath[0] === '/') {
|
|
|
|
encodedPath = encodedPath.substr(1);
|
|
|
|
}
|
|
|
|
return $.ajax({
|
|
|
|
url: OC.generateUrl('/apps/files/api/v1/files/') + encodedPath,
|
|
|
|
contentType: 'application/json',
|
|
|
|
data: JSON.stringify({
|
|
|
|
tags: tagNames || []
|
|
|
|
}),
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST'
|
2015-01-26 13:27:51 +03:00
|
|
|
}).fail(function(response) {
|
|
|
|
var message = '';
|
|
|
|
// show message if it is available
|
|
|
|
if(response.responseJSON && response.responseJSON.message) {
|
|
|
|
message = ': ' + response.responseJSON.message;
|
|
|
|
}
|
|
|
|
OC.Notification.showTemporary(t('files', 'An error occurred while trying to update the tags') + message);
|
2015-01-26 14:01:07 +03:00
|
|
|
toggleStar($actionEl, isFavorite);
|
2014-11-18 20:53:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(OCA);
|
|
|
|
|
|
|
|
OC.Plugins.register('OCA.Files.FileList', OCA.Files.TagsPlugin);
|
|
|
|
|