Skip to content

Commit

Permalink
Merge branch 'release/3.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 14, 2019
2 parents c71ffeb + cbc5ab1 commit c93dc82
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 353 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# v3.0.8
## 08/14/2019

1. [](#improved)
* Change form save action location to `user-data://` stream [#353](https://github.com/getgrav/grav-plugin-form/issues/353)
* Updated `eu`, `fr` and `pl` language
* Make `Form::initialize()` chainable
* Added `folder` option to `save:` action with fallback
1. [](#bugfix)
* Fixed Submit & Redirect not working as expected [#355](https://github.com/getgrav/grav-plugin-form/issues/355)
* Fixed oversensitive refresh prevention [#354](https://github.com/getgrav/grav-plugin-form/issues/354)
* Fixed issue with Form JS when pipeline is enabled [grav#2592](https://github.com/getgrav/grav/issues/2592)
* Fixed `accept` for SVG in file field [#364](https://github.com/getgrav/grav-plugin-form/pull/364)
* Fixed issue with plugin not returning expected form [#309](https://github.com/getgrav/grav-plugin-form/pull/309)
* Fixed form message not showing up after reset process
* Fixed form fields inside a single tab not using value from the form object if it is available
* Fixed file form field failing resolution checks in certain circumstances

# v3.0.7
## 07/01/2019

Expand Down
82 changes: 52 additions & 30 deletions app/fields/file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import $ from 'jquery';
import Dropzone from 'dropzone';
import EXIF from 'exif-js';
import { config, translations } from 'grav-form';
import {config, translations} from 'grav-form';

// translations
const Dictionary = {
Expand All @@ -21,7 +21,7 @@ const Dictionary = {
Dropzone.autoDiscover = false;

const DropzoneMediaConfig = {
createImageThumbnails: { thumbnailWidth: 150 },
createImageThumbnails: {thumbnailWidth: 150},
addRemoveLinks: false,
dictDefaultMessage: Dictionary.dictDefaultMessage,
dictRemoveFileConfirmation: Dictionary.dictRemoveFileConfirmation,
Expand All @@ -31,9 +31,11 @@ const DropzoneMediaConfig = {
window.EXIF = EXIF;

export default class FilesField {
constructor({ container = '.dropzone.files-upload', options = {} } = {}) {
constructor({container = '.dropzone.files-upload', options = {}} = {}) {
this.container = $(container);
if (!this.container.length) { return; }
if (!this.container.length) {
return;
}

this.urls = {};
DropzoneMediaConfig.previewTemplate = $('#dropzone-template').html();
Expand All @@ -55,7 +57,9 @@ export default class FilesField {
initDropzone() {
let files = this.options.klass.container.find('[data-file]');
let dropzone = this;
if (!files.length) { return; }
if (!files.length) {
return;
}

files.each((index, file) => {
file = $(file);
Expand Down Expand Up @@ -144,7 +148,9 @@ export default class FilesField {
}

onDropzoneRemovedFile(file, ...extra) {
if (!file.accepted || file.rejected) { return; }
if (!file.accepted || file.rejected) {
return;
}
const form = this.container.closest('form');
const unique_id = form.find('[name="__unique_form_id__"]');
let url = file.removeUrl || this.urls.delete || `${location.href}.json`;
Expand Down Expand Up @@ -173,7 +179,9 @@ export default class FilesField {
contentType: false,
processData: false,
success: () => {
if (!path) { return; }
if (!path) {
return;
}

path = global.atob(path[1]);
let input = this.container.find('[name][type="hidden"]');
Expand All @@ -190,7 +198,7 @@ export default class FilesField {

return this.handleError({
file,
data: { status: 'error' },
data: {status: 'error'},
msg: `<pre>${message}</pre>`
});
}
Expand Down Expand Up @@ -241,7 +249,9 @@ let instances = [];
let cache = $();
const onAddedNodes = (event, target/* , record, instance */) => {
let files = $(target).find('.dropzone.files-upload');
if (!files.length) { return; }
if (!files.length) {
return;
}

files.each((index, file) => {
file = $(file);
Expand Down Expand Up @@ -275,37 +285,49 @@ const addNode = (container) => {
let error = '';
if (!resolution) return done();

setTimeout(() => {
if ((this.options.maxFiles != null) && (this.getAcceptedFiles().length >= this.options.maxFiles)) {
done(this.options.dictMaxFilesExceeded.replace('{{maxFiles}}', this.options.maxFiles));
return this.emit('maxfilesexceeded', file);
}
if ((this.options.maxFiles != null) && (this.getAcceptedFiles().length >= this.options.maxFiles)) {
done(this.options.dictMaxFilesExceeded.replace('{{maxFiles}}', this.options.maxFiles));
return this.emit('maxfilesexceeded', file);
}

if (resolution.min) {
Object.keys(resolution.min).forEach((attr) => {
if (file[attr] < resolution.min[attr]) {
error += translations.PLUGIN_FORM.RESOLUTION_MIN.replace(/{{attr}}/g, attr).replace(/{{min}}/g, resolution.min[attr]);
const reader = new FileReader();
if (resolution.min || (!(settings.resizeWidth || settings.resizeHeight) && resolution.max)) {
reader.onload = function(event) {
const image = new Image();
image.src = event.target.result;
image.onload = function() {
if (resolution.min) {
Object.keys(resolution.min).forEach((attr) => {
if (this[attr] < resolution.min[attr]) {
error += translations.PLUGIN_FORM.RESOLUTION_MIN.replace(/{{attr}}/g, attr).replace(/{{min}}/g, resolution.min[attr]);
}
});
}
});
}

if (!(settings.resizeWidth || settings.resizeHeight)) {
if (resolution.max) {
Object.keys(resolution.max).forEach((attr) => {
if (file[attr] > resolution.max[attr]) {
error += translations.PLUGIN_FORM.RESOLUTION_MAX.replace(/{{attr}}/g, attr).replace(/{{max}}/g, resolution.max[attr]);
if (!(settings.resizeWidth || settings.resizeHeight)) {
if (resolution.max) {
Object.keys(resolution.max).forEach((attr) => {
if (this[attr] > resolution.max[attr]) {
error += translations.PLUGIN_FORM.RESOLUTION_MAX.replace(/{{attr}}/g, attr).replace(/{{max}}/g, resolution.max[attr]);
}
});
}
});
}
}
}

done(error);
};
};

reader.readAsDataURL(file);
} else {
return done(error);
}, 50);
}
}
};

cache = cache.add(container);
container = container[0];
instances.push(new FilesField({ container, options }));
instances.push(new FilesField({container, options}));
};

export let Instances = (() => {
Expand Down
256 changes: 11 additions & 245 deletions assets/form.min.js

Large diffs are not rendered by default.

61 changes: 17 additions & 44 deletions assets/form.vendor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Form
version: 3.0.7
version: 3.0.8
testing: false
description: Enables the forms handling
icon: check-square
Expand Down
18 changes: 16 additions & 2 deletions classes/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public function __construct(PageInterface $page, $name = null, $form = null)
$this->initialize();
}

/**
* @return $this
*/
public function initialize()
{
// Reset and initialize the form
Expand All @@ -188,7 +191,11 @@ public function initialize()

// Remember form state.
$flash = $this->getFlash();
$data = ($flash->exists() ? $flash->getData() : null) ?? $this->header_data;
if ($flash->exists()) {
$data = $flash->getData() ?? $this->header_data;
} else {
$data = $this->header_data;
}

// Remember data and files.
$this->setAllData($data);
Expand All @@ -198,6 +205,8 @@ public function initialize()
// Fire event
$grav = Grav::instance();
$grav->fireEvent('onFormInitialized', new Event(['form' => $this]));

return $this;
}

protected function setAllFiles(FormFlash $flash)
Expand Down Expand Up @@ -252,6 +261,11 @@ public function reset(): void
$this->setAllData($this->header_data);
$this->values = new Data();

// Reset unique id (allow multiple form submits)
$uniqueid = $this->items['uniqueid'] ?? null;
$this->set('remember_redirect', null === $uniqueid && !empty($this->items['remember_state']));
$this->setUniqueId($uniqueid ?? strtolower(Utils::generateRandomString($this->items['uniqueid_len'] ?? 20)));

// Fire event
$grav = Grav::instance();
$grav->fireEvent('onFormInitialized', new Event(['form' => $this]));
Expand Down Expand Up @@ -572,7 +586,7 @@ public function uploadFiles()
}

$isMime = strstr($type, '/');
$find = str_replace(['.', '*'], ['\.', '.*'], $type);
$find = str_replace(['.', '*', '+'], ['\.', '.*', '\+'], $type);

if ($isMime) {
$match = preg_match('#' . $find . '$#', $mime);
Expand Down
30 changes: 26 additions & 4 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class FormPlugin extends Plugin
/** @var array */
protected $flat_forms = [];

/** @var array */
protected $active_forms = [];

/** @var array */
protected $json_response = [];

Expand Down Expand Up @@ -490,7 +493,9 @@ public function onFormProcessed(Event $event)
break;
case 'reset':
if (Utils::isPositive($params)) {
$message = $form->message;
$form->reset();
$form->message = $message;
}
break;
case 'display':
Expand Down Expand Up @@ -534,6 +539,7 @@ public function onFormProcessed(Event $event)
$postfix = $params['filepostfix'] ?? '';
$ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
$filename = $params['filename'] ?? '';
$folder = !empty($params['folder']) ? $params['folder'] : $form->getName();
$operation = $params['operation'] ?? 'create';

if (!$filename) {
Expand All @@ -554,8 +560,8 @@ public function onFormProcessed(Event $event)
$filename = $twig->processString($filename, $vars);

$locator = $this->grav['locator'];
$path = $locator->findResource('user://data', true);
$dir = $path . DS . $form->getName();
$path = $locator->findResource('user-data://', true);
$dir = $path . DS . $folder;
$fullFileName = $dir. DS . $filename;

if (!empty($params['raw']) || !empty($params['template'])) {
Expand Down Expand Up @@ -747,7 +753,7 @@ public function getForm($data = null)
if (!empty($this->forms[$page_route])) {
$forms = $this->forms[$page_route];
$first_form = reset($forms) ?: null;
$form_name = $first_form['name'] ?? null;
return $first_form;
} else {
//No form on this route. Try looking up in the current page first
/** @var Forms $forms */
Expand Down Expand Up @@ -866,7 +872,23 @@ protected function getCurrentPageRoute()
*/
protected function getFormByName($form_name)
{
return $this->flat_forms[$form_name] ?? null;
$form = $this->active_forms[$form_name] ?? null;
if (!$form) {
$form = $this->flat_forms[$form_name] ?? null;

if (!$form) {
return null;
}

// Reset form to change the cached unique id and to fire onFormInitialized event.
$form->setUniqueId('');
$form->reset();

// Register form to the active forms to get the same instance back next time.
$this->active_forms[$form_name] = $form;
}

return $form;
}

/**
Expand Down
Loading

0 comments on commit c93dc82

Please sign in to comment.