1
- import Uploader from '@upfluence/ember-upf-utils/uploader' ;
2
1
import Configuration from '@upfluence/ember-upf-utils/configuration' ;
3
2
4
3
export default class ModuleBuilder {
5
- constructor ( session , toast ) {
6
- Object . assign ( this , { session , toast } ) ;
4
+ constructor ( uploader , toast ) {
5
+ Object . assign ( this , { uploader , toast } ) ;
7
6
}
8
7
9
8
build ( editor , element ) {
10
- return new Module ( editor , element , this . session , this . toast ) ;
9
+ return new Module ( editor , element , this . uploader , this . toast ) ;
11
10
}
12
11
}
13
12
14
13
class Module {
15
- constructor ( editor , element , session , toast ) {
16
- Object . assign ( this , { editor, element, session , toast } ) ;
14
+ constructor ( editor , element , uploader , toast ) {
15
+ Object . assign ( this , { editor, element, uploader , toast } ) ;
17
16
18
17
this . editor . registerModule ( this ) ;
19
18
@@ -22,35 +21,17 @@ class Module {
22
21
} ;
23
22
}
24
23
25
- _uploaderBuilder ( ) {
26
- let uploader = Uploader . create ( {
27
- ajaxSettings : {
28
- dataType : 'json' ,
29
- headers : {
30
- ...this . uploaderHeaders ,
31
- Authorization : `Bearer ${ this . session . data . authenticated . access_token } `
32
- }
33
- } ,
34
- url : Configuration . uploaderUrl ,
35
- allowedExtensions : 'jpg,jpeg,png,gif' ,
36
- maxSize : null
37
- } ) ;
38
-
39
- uploader
40
- . on ( 'didValidationError' , ( error ) => {
41
- this . toast . error ( error || 'Your file is invalid. Please check the requirements.' ) ;
42
- this . _removeLoadingState ( ) ;
43
- } )
44
- . on ( 'didUpload' , ( element ) => {
45
- this . editor . insertImage ( element . artifact . url ) ;
46
- this . _removeLoadingState ( ) ;
47
- } ) ;
48
-
49
- return uploader ;
50
- }
51
-
52
24
_uploadFile ( file ) {
53
- this . _uploaderBuilder ( ) . upload ( file , { privacy : 'public' } ) ;
25
+ this . uploader . upload (
26
+ {
27
+ file : file ,
28
+ privacy : 'public' ,
29
+ scope : 'anonymous' ,
30
+ onSuccess : this . _onSuccess . bind ( this ) ,
31
+ onFailure : this . _removeLoadingState
32
+ } ,
33
+ [ { type : 'filetype' , value : [ 'image' , 'gif' ] } ]
34
+ ) ;
54
35
}
55
36
56
37
_createLoadingStates ( ) {
@@ -90,9 +71,14 @@ class Module {
90
71
document . querySelector ( '.uedit__loading-image-upload' ) . classList . add ( 'uedit__loading-image-upload--hidden' ) ;
91
72
}
92
73
74
+ _onSuccess ( artifact ) {
75
+ this . editor . insertImage ( artifact . url ) ;
76
+ this . _removeLoadingState ( ) ;
77
+ }
78
+
93
79
onImageUpload ( files ) {
94
80
this . _addLoadingStates ( ) ;
95
- Array . prototype . forEach . call ( files , ( file ) => this . _uploadFile ( file ) ) ;
81
+ Array . from ( files || [ ] ) . forEach ( ( file ) => this . _uploadFile ( file ) ) ;
96
82
}
97
83
98
84
getOptions ( ) {
0 commit comments