Skip to content

Commit a0e639c

Browse files
committed
initial commit
0 parents  commit a0e639c

18 files changed

+2811
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
######################
2+
# Compiled source #
3+
######################
4+
*.com
5+
*.class
6+
*.dll
7+
*.exe
8+
*.o
9+
*.so
10+
11+
######################
12+
# Packages #
13+
######################
14+
# it's better to unpack these files and commit the raw source
15+
# git has its own built in compression methods
16+
*.7z
17+
*.dmg
18+
*.gz
19+
*.iso
20+
*.jar
21+
22+
######################
23+
# Global #
24+
######################
25+
.DS_Store
26+
.DS_Store\?
27+
._*
28+
.Spotlight-V100
29+
.Trashes
30+
Icon\?
31+
*.sublime-workspace
32+
*.sublime-project
33+
atlassian-ide-plugin.xml
34+
.idea/
35+
.project
36+
ehthumbs.db
37+
Thumbs.db
38+
.sass-cache
39+
package-lock.json

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# License
2+
Copyright (C) 2021 DACHCOM.DIGITAL
3+
4+
This software is available under the GNU General Public License version 3 (GPLv3).
5+
6+
### GNU General Public License version 3 (GPLv3)
7+
If you decide to choose the GPLv3 license, you must comply with the following terms:
8+
9+
This program is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
[GNU General Public License](https://www.gnu.org/licenses/gpl-3.0.en.html)

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# jQuery Pimcore FormBuilder Extensions
2+
3+
We're providing some helpful Javascript Extensions to simplify your daily work with the PIMCORE FormBuilder.
4+
This Library provides all jQuery extensions for the [PIMCORE FormBuilder Bundle](https://github.com/dachcom-digital/pimcore-formbuilder).
5+
6+
## Installation
7+
```bash
8+
npm i jquery-pimcore-formbuilder
9+
```
10+
11+
## Extensions Overview
12+
- [Core Extension](./docs/01_core.md)
13+
- [Dynamic Multi File Libraries](./docs/01_core.md#install-dynamic-multi-file-libraries)
14+
- [Conditional Logic Extension](./docs/02_conditionalLogic.md)
15+
- [Repeater Extension](./docs/03_repeater.md)
16+
- [Tracker Extension](./docs/04_tracker.md)
17+
- [reCAPTCHA V3 Extension](./docs/05_recaptchaV3.md)
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
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

Comments
 (0)