+ /**
+ * Start the edit mode for a file.
+ * @param fileId
+ */
+ $scope.startEdit = function(file) {
+ console.log(file);
+ $scope.editFile = angular.copy(file);
+ $scope.editFile.link = (file.external) ? file.path : '';
+ };
+
+ /**
+ * Cancel edit mode
+ */
+ $scope.cancelEdit = function() {
+ $scope.editFile = false;
+ };
+
+ /**
+ * Update the name and link of a file.
+ * @param file
+ */
+ $scope.updateFile = function(file) {
+ $http.put(`/files/${file.id}`, file).then(resp => {
+ let search = filesIndexOf(resp.data);
+ if (search !== -1) $scope.files[search] = resp.data;
+
+ if ($scope.editFile && !file.external) {
+ $scope.editFile.link = '';
+ }
+ $scope.editFile = false;
+ events.emit('success', 'Attachment details updated');
+ }, checkError('edit'));
+ };
+
+ /**
+ * Get the url of a file.
+ */
+ $scope.getFileUrl = function(file) {
+ return window.baseUrl('/files/' + file.id);
+ }
+
+ /**
+ * Search the local files via another file object.
+ * Used to search via object copies.
+ * @param file
+ * @returns int
+ */
+ function filesIndexOf(file) {
+ for (let i = 0; i < $scope.files.length; i++) {
+ if ($scope.files[i].id == file.id) return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Check for an error response in a ajax request.
+ * @param response
+ */
+ function checkError(errorGroupName) {
+ $scope.errors[errorGroupName] = {};
+ return function(response) {
+ if (typeof response.data !== 'undefined' && typeof response.data.error !== 'undefined') {
+ events.emit('error', response.data.error);
+ }
+ if (typeof response.data !== 'undefined' && typeof response.data.validation !== 'undefined') {
+ $scope.errors[errorGroupName] = response.data.validation;
+ console.log($scope.errors[errorGroupName])
+ }
+ }
+ }
+