Skip to content

Commit

Permalink
Allow filepicker field to peak at the pending uploaded files and op…
Browse files Browse the repository at this point in the history
…timistically select them (fixes #792)
  • Loading branch information
w00fz committed Oct 2, 2016
1 parent 74b756d commit c4ed5af
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Fixed issue when reading the file size setting if set to `0` (in Pagemedia and File fields)
* Fixed issue with `file` field in collections that caused unexpected duplication of items ([#775](https://github.com/getgrav/grav-plugin-admin/issues/775))
* Dramatically improved `filepicker` performance. Data is only ever loaded when the dropdown is on focus, as it was supposed to be. Image preview of a selected item won't be rendered unless the field gains focus to avoid wasting resources. ([#788](https://github.com/getgrav/grav-plugin-admin/issues/788))
* Allow `filepicker` field to peak at the pending uploaded files and optimistically select them ([#792](https://github.com/getgrav/grav-plugin-admin/issues/792))

# v1.2.2
## 09/08/2016
Expand Down
57 changes: 44 additions & 13 deletions classes/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,55 @@ protected function taskGetFilesInFolder()

$available_files = Folder::all($folder, ['recursive' => false]);

// Handle Accepted file types
// Accept can only be file extensions (.pdf|.jpg)
$accepted_files = [];
if (!isset($settings['accept'])) {
$accepted_files = $available_files;
} else {
foreach ((array) $available_files as $available_file) {
foreach ((array) $settings['accept'] as $type) {
$find = str_replace('*', '.*', $type);
$match = preg_match('#' . $find . '$#', $available_file);
if ($match) {
$accepted_files[] = $available_file;
// Peak in the flashObject for optimistic filepicker updates
$pending_files = [];
$sessionField = base64_encode($this->uri);
$flash = $this->admin->session()->getFlashObject('files-upload');

if ($flash && isset($flash[$sessionField])) {
foreach ($flash[$sessionField] as $field => $data) {
foreach ($data as $file) {
if (dirname($file['path']) === $folder) {
$pending_files[] = $file['name'];
}
}
}
}

$this->admin->json_response = ['status' => 'success', 'files' => $accepted_files, 'folder' => $folder];
$this->admin->session()->setFlashObject('files-upload', $flash);

// Handle Accepted file types
// Accept can only be file extensions (.pdf|.jpg)
if (isset($settings['accept'])) {
$available_files = array_filter($available_files, function($file) use ($settings) {
return $this->filterAcceptedFiles($file, $settings);
});

$pending_files = array_filter($pending_files, function($file) use ($settings) {
return $this->filterAcceptedFiles($file, $settings);
});
}

$this->admin->json_response = [
'status' => 'success',
'files' => $available_files,
'pending' => $pending_files,
'folder' => $folder
];

return true;
}

protected function filterAcceptedFiles($file, $settings)
{
$valid = false;

foreach ((array) $settings['accept'] as $type) {
$find = str_replace('*', '.*', $type);
$valid |= preg_match('#' . $find . '$#', $file);
}

return $valid;
}

protected function taskGetNewsFeed()
Expand Down
17 changes: 14 additions & 3 deletions themes/grav/app/forms/fields/filepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export default class FilePickerField {

let data = [];
for (let i = 0; i < response.files.length; i++) {
data.push({'name': response.files[i]});
data.push({'name': response.files[i], 'status': 'available'});
}
for (let i = 0; i < response.pending.length; i++) {
data.push({'name': response.pending[i], 'status': 'pending'});
}

folder = response.folder;
Expand All @@ -66,7 +69,7 @@ export default class FilePickerField {

let renderOption = function renderOption(item, escape) {
let image = '';
if (imagesPreview && folder && item.name.match(/\.(jpg|jpeg|png|gif)$/i)) {
if (imagesPreview && folder && item.status == 'available' && item.name.match(/\.(jpg|jpeg|png|gif)$/i)) {
image = `
<img class="filepicker-field-image"
src="${config.base_url_relative}/../${folder}/${item.name}"/>`;
Expand All @@ -83,6 +86,12 @@ export default class FilePickerField {
valueField: 'name',
labelField: 'name',
searchField: 'name',
optgroups: [
{$order: 1, value: 'pending', label: 'Pending'},
{$order: 2, value: 'available', label: 'Available'}
],
optgroupField: 'status',
// lockOptgroupOrder: true,
create: false,
preload: false, // 'focus',
render: {
Expand All @@ -105,7 +114,9 @@ export default class FilePickerField {
},

onFocus: function() {
this.load((callback) => getData(field, (data) => callback(data)));
this.load((callback) => getData(field, (data) => {
callback(data);
}));
}
});
}
Expand Down
30 changes: 15 additions & 15 deletions themes/grav/js/admin.min.js

Large diffs are not rendered by default.

0 comments on commit c4ed5af

Please sign in to comment.