1+ /*
2+ * Project: PIMCORE FormBuilder
3+ * Extension: Dynamic Multi File | DropZone
4+ * Since: 3.4.0
5+ * Author: DACHCOM.DIGITAL
6+ * License: GPLv3
7+ */
8+ ( function ( global , factory ) {
9+ typeof exports === 'object' && typeof module !== 'undefined' ? factory ( exports , require ( 'jquery' ) , window ) :
10+ typeof define === 'function' && define . amd ? define ( [ 'exports' , 'jquery' , 'window' ] , factory ) :
11+ ( global = typeof globalThis !== 'undefined' ? globalThis : global || self , factory ( { } , global . jQuery , window ) ) ;
12+ } ( this , ( function ( exports , $ , window ) { 'use strict' ;
13+
14+ function FormBuilderDynamicMultiFileDropZone ( ) {
15+ return this ;
16+ }
17+
18+ $ . extend ( FormBuilderDynamicMultiFileDropZone . prototype , {
19+
20+ init : function ( $form , $dmfFields , dataUrls , options ) {
21+
22+ if ( this . initialized === true ) {
23+ return ;
24+ }
25+
26+ this . $form = $form ;
27+ this . $dmfFields = $dmfFields ;
28+ this . dataUrls = dataUrls ;
29+ this . options = options ;
30+ this . initialized = true ;
31+
32+ this . prepareLibrary ( ) ;
33+ } ,
34+
35+ prepareLibrary : function ( ) {
36+
37+ if ( window . Dropzone !== undefined ) {
38+ this . prepareForm ( ) ;
39+ return ;
40+ }
41+
42+ if ( typeof this . options . libPath === 'undefined' || this . options . libPath === null ) {
43+ return ;
44+ }
45+
46+ $ . getScript ( this . options . libPath , function ( data , textStatus , jqxhr ) {
47+ if ( jqxhr . status === 200 ) {
48+ Dropzone . autoDiscover = false ;
49+ this . prepareForm ( ) ;
50+ }
51+ } . bind ( this ) ) ;
52+
53+ } ,
54+
55+ prepareForm : function ( ) {
56+ this . addListener ( ) ;
57+ this . $dmfFields . each ( this . setupDropZoneElement . bind ( this ) ) ;
58+ } ,
59+
60+ setupDropZoneElement : function ( index , el ) {
61+
62+ var _ = this ,
63+ $el = $ ( el ) ,
64+ $submitButton = this . $form . find ( '*[type="submit"]' ) ,
65+ $template = $el . find ( '.dropzone-template' ) ,
66+ $element = $el . find ( '.dropzone-container' ) ,
67+ fieldId = $el . data ( 'field-id' ) ,
68+ storageFieldId = fieldId + '_data' ,
69+ $storageField = this . $form . find ( 'input[type="hidden"][id="' + storageFieldId + '"]' ) ,
70+ config = $el . data ( 'engine-options' ) ,
71+ dropZoneConfiguration ;
72+
73+ $element . addClass ( 'dropzone' ) ;
74+
75+ dropZoneConfiguration = {
76+ paramName : 'dmfData' ,
77+ url : _ . getDataUrl ( 'file_add' ) ,
78+ chunking : config . multiple === false ,
79+ addRemoveLinks : true ,
80+ hiddenInputContainer : $el [ 0 ] ,
81+ maxFiles : config . item_limit ,
82+ acceptedFiles : config . allowed_extensions ,
83+ maxFilesize : config . max_file_size ,
84+ uploadMultiple : config . multiple ,
85+ init : function ( ) {
86+
87+ $template . remove ( ) ;
88+
89+ this . on ( 'removedfile' , function ( file ) {
90+ $ . ajax ( {
91+ type : 'DELETE' ,
92+ url : _ . getDataUrl ( 'file_delete' ) + '/' + file . upload . uuid ,
93+ data : {
94+ uploadStatus : file . status
95+ } ,
96+ success : function ( response ) {
97+
98+ if ( response . success === false ) {
99+ return ;
100+ }
101+
102+ _ . removeFromStorageField ( $storageField , {
103+ id : response . uuid
104+ } ) ;
105+
106+ }
107+ } ) ;
108+ } ) ;
109+
110+ this . on ( 'sending' , function ( file , xhr , formData ) {
111+ $submitButton . attr ( 'disabled' , 'disabled' ) ;
112+ formData . append ( 'uuid' , file . upload . uuid ) ;
113+ } ) ;
114+
115+ this . on ( 'complete' , function ( file ) {
116+ $submitButton . attr ( 'disabled' , false ) ;
117+ } ) ;
118+
119+ this . on ( 'success' , function ( file , response ) {
120+ _ . addToStorageField ( $storageField , {
121+ id : response . uuid ,
122+ fileName : response . fileName
123+ } ) ;
124+
125+ } . bind ( this ) ) ;
126+
127+ this . on ( 'canceled' , function ( file ) {
128+ $submitButton . attr ( 'disabled' , false ) ;
129+ } ) ;
130+
131+ _ . $form . trigger ( 'formbuilder.dynamic_multi_file.drop_zone.init' , [ this ] ) ;
132+ }
133+ } ;
134+
135+ if ( $template . children ( 'div.dz-file-preview' ) . length > 0 ) {
136+ dropZoneConfiguration . previewTemplate = $template . html ( ) ;
137+ }
138+
139+ if ( config . translations ) {
140+ dropZoneConfiguration = $ . extend ( { } , dropZoneConfiguration , config . translations ) ;
141+ }
142+
143+ this . $form . trigger ( 'formbuilder.dynamic_multi_file.init' , [ $el , this . options , dropZoneConfiguration ] ) ;
144+
145+ $element . dropzone ( dropZoneConfiguration ) ;
146+ } ,
147+
148+ addToStorageField : function ( $storage , newData ) {
149+
150+ var data = typeof $storage . val ( ) === 'string' && $storage . val ( ) !== ''
151+ ? $ . parseJSON ( $storage . val ( ) )
152+ : [ ] ;
153+
154+ data . push ( newData ) ;
155+
156+ $storage . val ( JSON . stringify ( data ) ) ;
157+ } ,
158+
159+ removeFromStorageField : function ( $storage , newData ) {
160+
161+ var position ,
162+ data = typeof $storage . val ( ) === 'string' && $storage . val ( ) !== ''
163+ ? $ . parseJSON ( $storage . val ( ) )
164+ : [ ] ;
165+
166+ position = $ . map ( data , function ( block ) {
167+ return block . id
168+ } ) . indexOf ( newData . id ) ;
169+
170+ if ( position === - 1 ) {
171+ return ;
172+ }
173+
174+ data . splice ( position , 1 )
175+
176+ $storage . val ( JSON . stringify ( data ) ) ;
177+ } ,
178+
179+ addListener : function ( ) {
180+
181+ this . $form . on ( {
182+ 'submit' : function ( ev ) {
183+
184+ var $instances = this . $form . find ( '.dz-remove' ) ;
185+
186+ if ( $instances . length === 0 ) {
187+ return ;
188+ }
189+
190+ $instances . hide ( ) ;
191+
192+ } . bind ( this ) ,
193+ 'formbuilder.request-done' : function ( ev ) {
194+
195+ var $instances = this . $form . find ( '.dz-remove' ) ;
196+
197+ if ( $instances . length === 0 ) {
198+ return ;
199+ }
200+
201+ $instances . show ( ) ;
202+
203+ } . bind ( this ) ,
204+ 'formbuilder.success' : function ( ev ) {
205+
206+ var $instances = this . $form . find ( '[data-dynamic-multi-file-instance]' ) ;
207+
208+ if ( $instances . length === 0 ) {
209+ return ;
210+ }
211+
212+ $instances . each ( function ( index , el ) {
213+ var $el = $ ( el ) ,
214+ dzInstance = null ,
215+ fieldId = $el . data ( 'field-id' ) ,
216+ storageFieldId = fieldId + '_data' ,
217+ $storageField = this . $form . find ( 'input[type="hidden"][id="' + storageFieldId + '"]' ) ;
218+
219+ try {
220+ dzInstance = Dropzone . forElement ( $el . find ( '.dropzone-container' ) [ 0 ] )
221+ } catch ( e ) {
222+ console . log ( e ) ;
223+ }
224+
225+ if ( dzInstance !== null ) {
226+ dzInstance . removeAllFiles ( ) ;
227+ }
228+
229+ $storageField . val ( '' ) ;
230+
231+ } . bind ( this ) ) ;
232+
233+ } . bind ( this ) ,
234+ 'formbuilder.layout.post.add' : function ( ev , layout ) {
235+
236+ var $el = $ ( layout ) ,
237+ $instances ;
238+
239+ $instances = $el . find ( '[data-dynamic-multi-file-instance]' ) ;
240+ if ( $instances . length === 0 ) {
241+ return ;
242+ }
243+
244+ $instances . each ( this . setupDropZoneElement . bind ( this ) ) ;
245+
246+ } . bind ( this ) ,
247+ 'formbuilder.layout.pre.remove' : function ( ev , layout ) {
248+
249+ var $el = $ ( layout ) ,
250+ $uploadFields ;
251+
252+ $uploadFields = $el . find ( '[data-dynamic-multi-file-instance]' ) ;
253+ if ( $uploadFields . length === 0 ) {
254+ return ;
255+ }
256+
257+ $uploadFields . each ( function ( index , el ) {
258+
259+ var $el = $ ( el ) ,
260+ dzInstance = null ,
261+ config = $el . data ( 'engine-options' ) ;
262+
263+ try {
264+ dzInstance = Dropzone . forElement ( $el . find ( '.dropzone-container' ) [ 0 ] )
265+ } catch ( e ) {
266+ console . log ( e ) ;
267+ }
268+
269+ if ( dzInstance !== null && dzInstance . files . length > 0 ) {
270+ throw new Error ( config . instance_error ) ;
271+ }
272+ } ) ;
273+ }
274+ } ) ;
275+ } ,
276+
277+ getDataUrl : function ( section ) {
278+ return this . dataUrls [ section ] ;
279+ }
280+ } ) ;
281+
282+ if ( typeof window . formBuilderDynamicMultiFileHandler !== 'undefined' ) {
283+ throw 'Another dynamic multi file handler already has been defined. Only one handler allowed.' ;
284+ }
285+
286+ // window instance requires to be called "formBuilderDynamicMultiFileHandler"
287+ window . formBuilderDynamicMultiFileHandler = new FormBuilderDynamicMultiFileDropZone ( ) ;
288+
289+ } ) ) ) ;
0 commit comments