@@ -538,6 +538,10 @@ module.exports = function (ngApp, events) {
538538 $scope . files = [ ] ;
539539 $scope . editFile = false ;
540540 $scope . file = getCleanFile ( ) ;
541+ $scope . errors = {
542+ link : { } ,
543+ edit : { }
544+ } ;
541545
542546 function getCleanFile ( ) {
543547 return {
@@ -567,7 +571,7 @@ module.exports = function (ngApp, events) {
567571 currentOrder = newOrder ;
568572 $http . put ( `/files/sort/page/${ pageId } ` , { files : $scope . files } ) . then ( resp => {
569573 events . emit ( 'success' , resp . data . message ) ;
570- } , checkError ) ;
574+ } , checkError ( 'sort' ) ) ;
571575 }
572576
573577 /**
@@ -587,7 +591,7 @@ module.exports = function (ngApp, events) {
587591 $http . get ( url ) . then ( resp => {
588592 $scope . files = resp . data ;
589593 currentOrder = resp . data . map ( file => { return file . id } ) . join ( ':' ) ;
590- } , checkError ) ;
594+ } , checkError ( 'get' ) ) ;
591595 }
592596 getFiles ( ) ;
593597
@@ -599,7 +603,7 @@ module.exports = function (ngApp, events) {
599603 */
600604 $scope . uploadSuccess = function ( file , data ) {
601605 $scope . $apply ( ( ) => {
602- $scope . files . unshift ( data ) ;
606+ $scope . files . push ( data ) ;
603607 } ) ;
604608 events . emit ( 'success' , 'File uploaded' ) ;
605609 } ;
@@ -612,10 +616,10 @@ module.exports = function (ngApp, events) {
612616 $scope . uploadSuccessUpdate = function ( file , data ) {
613617 $scope . $apply ( ( ) => {
614618 let search = filesIndexOf ( data ) ;
615- if ( search !== - 1 ) $scope . files [ search ] = file ;
619+ if ( search !== - 1 ) $scope . files [ search ] = data ;
616620
617621 if ( $scope . editFile ) {
618- $scope . editFile = data ;
622+ $scope . editFile = angular . copy ( data ) ;
619623 data . link = '' ;
620624 }
621625 } ) ;
@@ -627,10 +631,14 @@ module.exports = function (ngApp, events) {
627631 * @param file
628632 */
629633 $scope . deleteFile = function ( file ) {
634+ if ( ! file . deleting ) {
635+ file . deleting = true ;
636+ return ;
637+ }
630638 $http . delete ( `/files/${ file . id } ` ) . then ( resp => {
631639 events . emit ( 'success' , resp . data . message ) ;
632640 $scope . files . splice ( $scope . files . indexOf ( file ) , 1 ) ;
633- } , checkError ) ;
641+ } , checkError ( 'delete' ) ) ;
634642 } ;
635643
636644 /**
@@ -641,19 +649,20 @@ module.exports = function (ngApp, events) {
641649 $scope . attachLinkSubmit = function ( file ) {
642650 file . uploaded_to = pageId ;
643651 $http . post ( '/files/link' , file ) . then ( resp => {
644- $scope . files . unshift ( resp . data ) ;
652+ $scope . files . push ( resp . data ) ;
645653 events . emit ( 'success' , 'Link attached' ) ;
646654 $scope . file = getCleanFile ( ) ;
647- } , checkError ) ;
655+ } , checkError ( 'link' ) ) ;
648656 } ;
649657
650658 /**
651659 * Start the edit mode for a file.
652660 * @param fileId
653661 */
654662 $scope . startEdit = function ( file ) {
663+ console . log ( file ) ;
655664 $scope . editFile = angular . copy ( file ) ;
656- if ( ! file . external ) $scope . editFile . link = '' ;
665+ $scope . editFile . link = ( file . external ) ? file . path : '' ;
657666 } ;
658667
659668 /**
@@ -670,16 +679,23 @@ module.exports = function (ngApp, events) {
670679 $scope . updateFile = function ( file ) {
671680 $http . put ( `/files/${ file . id } ` , file ) . then ( resp => {
672681 let search = filesIndexOf ( resp . data ) ;
673- if ( search !== - 1 ) $scope . files [ search ] = file ;
682+ if ( search !== - 1 ) $scope . files [ search ] = resp . data ;
674683
675684 if ( $scope . editFile && ! file . external ) {
676685 $scope . editFile . link = '' ;
677686 }
678687 $scope . editFile = false ;
679688 events . emit ( 'success' , 'Attachment details updated' ) ;
680- } ) ;
689+ } , checkError ( 'edit' ) ) ;
681690 } ;
682691
692+ /**
693+ * Get the url of a file.
694+ */
695+ $scope . getFileUrl = function ( file ) {
696+ return window . baseUrl ( '/files/' + file . id ) ;
697+ }
698+
683699 /**
684700 * Search the local files via another file object.
685701 * Used to search via object copies.
@@ -697,9 +713,16 @@ module.exports = function (ngApp, events) {
697713 * Check for an error response in a ajax request.
698714 * @param response
699715 */
700- function checkError ( response ) {
701- if ( typeof response . data !== 'undefined' && typeof response . data . error !== 'undefined' ) {
702- events . emit ( 'error' , response . data . error ) ;
716+ function checkError ( errorGroupName ) {
717+ $scope . errors [ errorGroupName ] = { } ;
718+ return function ( response ) {
719+ if ( typeof response . data !== 'undefined' && typeof response . data . error !== 'undefined' ) {
720+ events . emit ( 'error' , response . data . error ) ;
721+ }
722+ if ( typeof response . data !== 'undefined' && typeof response . data . validation !== 'undefined' ) {
723+ $scope . errors [ errorGroupName ] = response . data . validation ;
724+ console . log ( $scope . errors [ errorGroupName ] )
725+ }
703726 }
704727 }
705728
0 commit comments