+ ngApp.controller('PageAttachmentController', ['$scope', '$http', '$attrs',
+ function ($scope, $http, $attrs) {
+
+ const pageId = $scope.uploadedTo = $attrs.pageId;
+ let currentOrder = '';
+ $scope.files = [];
+ $scope.editFile = false;
+ $scope.file = getCleanFile();
+ $scope.errors = {
+ link: {},
+ edit: {}
+ };
+
+ function getCleanFile() {
+ return {
+ page_id: pageId
+ };
+ }
+
+ // Angular-UI-Sort options
+ $scope.sortOptions = {
+ handle: '.handle',
+ items: '> tr',
+ containment: "parent",
+ axis: "y",
+ stop: sortUpdate,
+ };
+
+ /**
+ * 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'));
+ }