+ /**
+ * Event listener for sort changes.
+ * Updates the file ordering on the server.
+ * @param event
+ * @param ui
+ */
+ function sortUpdate(event, ui) {
+ let newOrder = $scope.files.map(file => {return file.id}).join(':');
+ if (newOrder === currentOrder) return;
+
+ currentOrder = newOrder;
+ $http.put(window.baseUrl(`/attachments/sort/page/${pageId}`), {files: $scope.files}).then(resp => {
+ events.emit('success', resp.data.message);
+ }, checkError('sort'));
+ }
+
+ /**
+ * Used by dropzone to get the endpoint to upload to.
+ * @returns {string}
+ */
+ $scope.getUploadUrl = function (file) {
+ let suffix = (typeof file !== 'undefined') ? `/${file.id}` : '';
+ return window.baseUrl(`/attachments/upload${suffix}`);
+ };
+
+ /**
+ * Get files for the current page from the server.
+ */
+ function getFiles() {
+ let url = window.baseUrl(`/attachments/get/page/${pageId}`);
+ $http.get(url).then(resp => {
+ $scope.files = resp.data;
+ currentOrder = resp.data.map(file => {return file.id}).join(':');
+ }, checkError('get'));
+ }
+ getFiles();
+
+ /**
+ * Runs on file upload, Adds an file to local file list
+ * and shows a success message to the user.
+ * @param file
+ * @param data
+ */
+ $scope.uploadSuccess = function (file, data) {
+ $scope.$apply(() => {
+ $scope.files.push(data);
+ });
+ events.emit('success', trans('entities.attachments_file_uploaded'));
+ };