1- angular . module ( 'cb.content' ) . controller ( 'GalleryModalController' , [ '$scope' , '$timeout' , '$window' , '$filter' , '$modal' , '$modalInstance' , 'GlobalLoaderService' , 'AlertService' , 'MediaManagerFactory ' , function ( $scope , $timeout , $window , $filter , $modal , $modalInstance , GlobalLoaderService , AlertService , MediaManagerFactory ) {
1+ angular . module ( 'cb.content' ) . controller ( 'GalleryModalController' , [ '$scope' , '$timeout' , '$window' , '$filter' , '$modal' , '$modalInstance' , 'GlobalLoaderService' , 'AlertService' , '_CSRF ' , function ( $scope , $timeout , $window , $filter , $modal , $modalInstance , GlobalLoaderService , AlertService , _CSRF ) {
22
33 console . log ( 'GalleryModalController Init' ) ;
44
55 // files
6- $scope . files = [ ] ;
7-
8- /**
9- * Media Manager
10- *
11- * @type {{} }
12- */
13- $scope . mediaManager = { } ;
14- $scope . mediaManager . currentPath = '' ;
15- $scope . mediaManager . paths = [ ] ;
16- $scope . mediaManager . isDoing = false ;
17- $scope . mediaManager . isEmpty = true ;
18-
19- // list dir handle from folder click
20- $scope . mediaManager . ls = function ( path ) {
21- if ( path == '/' ) {
22- queryFiles ( '/' ) ;
23- } else {
24- queryFiles ( path ) ;
25- }
26- } ;
27-
28- // list dir handle from breadcrumbs click
29- $scope . mediaManager . lsBc = function ( index ) {
30-
31- var copiedPaths = angular . copy ( $scope . mediaManager . paths ) ;
32-
33- copiedPaths . splice ( index + 1 , copiedPaths . length ) ;
34-
35- copiedPaths . shift ( ) ;
36-
37- queryFiles ( '/' + copiedPaths . join ( '/' ) ) ;
38- } ;
39-
40- // modal control
41- // -----------------------------
42-
43- // the selected files
44- $scope . selectedFiles = [ ] ;
45-
46- // handles modal ok button and pass the selected images
47- $scope . ok = function ( ) {
48- $modalInstance . close ( $scope . selectedFiles ) ;
49- } ;
50-
51- // handles modal cancel button, nothing here, just cancels the modal
52- $scope . cancel = function ( ) {
53- $modalInstance . dismiss ( 'cancel' ) ;
54- } ;
55-
56- /**
57- * Uploader
58- *
59- * @type {{} }
60- */
61- $scope . uploader = { } ;
62- $scope . uploader . files = [ ] ;
63-
64- // watch the files values so if it changes we can trigger upload
65- $scope . $watch ( 'uploader.files' , function ( ) {
66- upload ( $scope . uploader . files , { path : $scope . mediaManager . currentPath } ) ;
67- } ) ;
68-
69- // just a helper on view
70- $scope . getLastSegment = function ( path ) {
71-
72- var separators = [ "/" , "\\\\" ] ;
73-
74- return path . split ( new RegExp ( separators . join ( '|' ) , 'g' ) ) . pop ( ) ;
75- } ;
76-
77- // get size name. Eg. ( name_large.jpg -> large )
78- $scope . getSizeName = function ( path ) {
79- var sizeName = 'Original' ;
80- var fileName = $scope . getLastSegment ( path ) ;
81-
82- if ( isImage ( fileName ) ) {
83- var f = fileName . split ( '_' ) ;
84- if ( f . length >= 2 ) {
85- sizeName = f [ f . length - 1 ] . split ( '.' ) [ 0 ] ;
6+ $scope . media = {
7+
8+ init : function ( ) {
9+
10+ var opts = {
11+ url : '/backend/media/elFinder' ,
12+ customData : { '_token' : _CSRF } ,
13+ getFileCallback : function ( file ) {
14+ console . log ( 'getFileCallback' ) ;
15+ console . log ( file ) ;
16+ } ,
17+ handlers : {
18+ select : function ( event , elfinderInstance ) {
19+ var selected = event . data . selected ;
20+ if ( selected . length ) {
21+
22+ // empty first to make sure no duplicates
23+ $scope . media . data . selectedFiles = [ ] ;
24+
25+ // iterate to all selected items and push it to selectedFiles
26+ angular . forEach ( selected , function ( item ) {
27+ $scope . $apply ( function ( ) {
28+ $scope . media . data . selectedFiles . push ( {
29+ file : elfinderInstance . file ( item ) ,
30+ relative_path : elfinderInstance . path ( item ) . replace ( 'public\\' , '\\' ) , // remove public from path
31+ url : elfinderInstance . url ( item ) . replace ( '/storage/public' , '/storage' ) // remove public from path
32+ } ) ;
33+ } ) ;
34+ } ) ;
35+ }
36+ }
37+ }
38+ } ;
39+
40+ $timeout ( function ( ) {
41+ angular . element ( '#media-loader' ) . hide ( ) ;
42+ angular . element ( '#elfinder' ) . elfinder ( opts ) ;
43+ } , 3000 ) ;
44+ } ,
45+
46+ data : {
47+ selectedFiles : [ ]
48+ } ,
49+
50+ buttons : {
51+ ok : function ( ) {
52+ $modalInstance . close ( $scope . media . data . selectedFiles ) ;
53+ } ,
54+ cancel : function ( ) {
55+ $modalInstance . dismiss ( 'cancel' ) ;
8656 }
8757 }
88-
89- return sizeName ;
90-
91- function isImage ( src ) {
92- return ( / ( .* ) \. (?: j p e ? g | g i f | p n g ) $ / i) . test ( src ) ;
93- }
9458 } ;
9559
96- // uploads the files
97- function upload ( files , path ) {
98- if ( files && files . length ) {
99- for ( var i = 0 ; i < files . length ; i ++ ) {
100- var file = files [ i ] ;
101- MediaManagerFactory . upload ( file , path ) . progress ( function ( evt ) {
102- var progressPercentage = parseInt ( 100.0 * evt . loaded / evt . total ) ;
103- console . log ( 'progress: ' + progressPercentage + '% ' + evt . config . file . name ) ;
104- } ) . success ( function ( data , status , headers , config ) {
105- queryFiles ( $scope . mediaManager . currentPath ) ;
106- } ) ;
107- }
108- }
109- }
110-
111- // query files in a given dir
112- function queryFiles ( path ) {
113- MediaManagerFactory . ls ( { path :path } ) . then ( function ( success ) {
114- $scope . files = success . data . data ;
115- $scope . mediaManager . currentPath = $scope . files . current_path ;
116- $scope . mediaManager . paths = $scope . files . paths ;
117- $scope . mediaManager . isEmpty = $scope . files . is_empty ;
118- } , function ( error ) {
119- GlobalLoaderService . show ( error . data . message || 'An error has occurred.' , 'danger' ) . hide ( 4000 ) ;
120- } ) ;
121- }
122- queryFiles ( '/' ) ;
60+ $scope . media . init ( ) ;
12361} ] ) ;
0 commit comments