Skip to content

Commit

Permalink
Item14479: added attachment formfield
Browse files Browse the repository at this point in the history
also:
- fixed dropdown ui for user formfields
- fixed thumbnail previews of topics and users

note: this now requires the latest TopicInteractionPlugin and ImagePlugin
  • Loading branch information
MichaelDaum committed Sep 13, 2017
1 parent 5d332da commit aadba6b
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -12,6 +12,8 @@ pub/System/MoreFormfieldsPlugin/networkaddress.js
pub/System/MoreFormfieldsPlugin/topicfield.js
pub/System/MoreFormfieldsPlugin/iconfield.js
pub/System/MoreFormfieldsPlugin/select2.js
pub/System/MoreFormfieldsPlugin/attachmentfield.js
pub/System/MoreFormfieldsPlugin/smartbox.js
pub/System/MoreFormfieldsPlugin/moreformfields.css
/MoreFormfieldsPlugin.md5
/MoreFormfieldsPlugin.sha1
Expand Down
19 changes: 14 additions & 5 deletions data/System/MoreFormfieldsPlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1485349736" format="1.1" version="1"}%
%META:TOPICINFO{author="micha" comment="reprev" date="1496927925" format="1.1" reprev="9" version="9"}%
---+!! %TOPIC%
%FORMFIELD{"Description"}%

Expand All @@ -11,10 +11,18 @@ This plugin implements a set of additional special purpose formfields to be used
To make use of these formfields use their type identifier in the =Type= column of a !DataForm definition, such as in

| *Name* | *Type* | *Size* | *Values* | *Description* | *Attributes* |
| <nop>IpV4 | ipaddress | 15 | | network address | |
| <nop>IpV6 | ipv6address | 40 | | network address | |
| Netmask | netmask | 15 | | netmask | |
| <nop>MacAddress | macaddress | 17 | | hardware-addresse | |
| attachment | attachment | 20 | | | |
| date2 | date2 | 30 | | | |
| icon | icon | 20 | | | |
| ipaddress | ipaddress | 15 | | network address | |
| ipv6address | ipv6address | 40 | | network address | |
| macaddress | macaddress | 17 | | hardware-addresse | |
| netmask | netmask | 15 | | netmask | |
| phonenumber | phonenumber | 20 | | | |
| select2 | select2 | 20 | | | |
| time | time | 20 | | | |
| topic | topic | 20 | | | |
| user | user | 20 | | | |

When defining a formfield additional parameters may be used in the =Values= column to further customize the behavior of the element.

Expand Down Expand Up @@ -205,6 +213,7 @@ A user reference using JQSelect2Contrib for autocompletion.

---++ Change History
%TABLE{columnwidths="7em" tablewidth="100%"}%
| 13 Sep 2017: | added =attachment= formfield |
| 27 Jan 2017: | render empty =date2= formfields as an empty string, not 01 Jan 1970 |
| 25 Jan 2017: | fixed =topic= formfield to properly store web dot topic values |
| 16 Jan 2017: | replace <nop>MoreFormfieldsAjaxHelper with a template solution to be able to override it when required; \
Expand Down
231 changes: 231 additions & 0 deletions lib/Foswiki/Form/Attachment.pm
@@ -0,0 +1,231 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# MoreFormfieldsPlugin is Copyright (C) 2010-2017 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
package Foswiki::Form::Attachment;

use strict;
use warnings;

use Foswiki::Func();
use Foswiki::Form::FieldDefinition ();
use Foswiki::Plugins::JQueryPlugin ();
our @ISA = ('Foswiki::Form::FieldDefinition');

sub new {
my $class = shift;
my $this = $class->SUPER::new(@_);
my $size = $this->{size} || '';
$size =~ s/\D//g;
$size = 10 if (!$size || $size < 1);
$this->{size} = $size;

Foswiki::Func::readTemplate("moreformfields");

$this->{_formfieldClass} = 'foswikiAttachmentField';
$this->{_web} = $this->param("web") || $this->{session}{webName};
$this->{_topic} = $this->param("topic") || $this->{session}{topicName};
$this->{_url} = Foswiki::Func::expandTemplate("select2::attachments::url");
return $this;
}

sub isMultiValued { return (shift->{type} =~ m/\+multi/); }

sub getDefaultValue {
my $this = shift;

my $value = $this->{default};
$value = '' unless defined $value;

return $value;
}

sub finish {
my $this = shift;
$this->SUPER::finish();
undef $this->{_params};
undef $this->{_options};
}

sub populateMetaFromQueryData {
my ($this, $query, $meta, $old) = @_;

if ($this->isMultiValued()) {
my @values = $query->multi_param($this->{name});

if (scalar(@values) == 1 && defined $values[0]) {
@values = split(/,|%2C/, $values[0]);
}
my %vset = ();
foreach my $val (@values) {
$val ||= '';
$val =~ s/^\s*//o;
$val =~ s/\s*$//o;

# skip empty values
$vset{$val} = (defined $val && $val =~ /\S/);
}

# populate options first
$this->{_options} = [sort keys %vset];
}

return $this->SUPER::populateMetaFromQueryData($query, $meta, $old);
}

sub getOptions {
my ($this, $topicObject) = @_;

return $this->{_options} if $this->{_options};

if ($topicObject) {
$this->{_options} = [''];

foreach my $attachment ($topicObject->find('FILEATTACHMENT')) {
push @{$this->{_options}}, $attachment->{name};
}

return $this->{_options};
}

return [];
}

sub param {
my ($this, $key) = @_;

unless (defined $this->{_params}) {
my %params = Foswiki::Func::extractParameters($this->{value});
$this->{_params} = \%params;
}

return (defined $key) ? $this->{_params}{$key} : $this->{_params};
}

sub getDisplayValue {
my ($this, $value) = @_;

return '' unless defined $value && $value ne '';

my @result = ();
my $format = Foswiki::Func::expandTemplate("attachments::preview");

if ($this->isMultiValued) {
foreach my $val (split(/\s*,\s*/, $value)) {
my $line = $format;
$line =~ s/\$file/$val/g;
push @result, $line;
}
} else {
$format =~ s/\$file/$value/g;
push @result, $format;
}

return Foswiki::Func::expandCommonVariables(join("", @result), $this->{_topic}, $this->{_web});
}

sub renderForDisplay {
my ($this, $format, $value, $attrs) = @_;

my $displayValue = $this->getDisplayValue($value);
$format =~ s/\$value\(display\)/$displayValue/g;
$format =~ s/\$value/$value/g;

return $this->SUPER::renderForDisplay($format, $value, $attrs);
}

sub renderForEdit {
my ($this, $topicObject, $value) = @_;

$this->getOptions($topicObject);

my @htmlData = ();
push @htmlData, 'type="hidden"';
push @htmlData, 'class="' . $this->cssClasses($this->{_formfieldClass}) . '"';
push @htmlData, 'name="' . $this->{name} . '"';
push @htmlData, 'value="' . $value . '"';

my @uploadButtonHtmlData = ();
push @uploadButtonHtmlData, "data-topic='%WEB%.%TOPIC%'";
push @uploadButtonHtmlData, "data-auto-upload='false'";

my $size = $this->{size};
if (defined $size) {
$size .= "em";
} else {
$size = "element";
}
push @htmlData, 'data-width="' . $size . '"';

unless (defined $this->param("url")) {
if (defined $this->{_url}) {
my $url = Foswiki::Func::expandCommonVariables($this->{_url}, $this->{_topic}, $this->{_web});
push @htmlData, 'data-url="' . $url . '"';
}
push @htmlData, 'data-topic="' . $this->{_web} . '.' . $this->{_topic} .'"';
}

while (my ($key, $val) = each %{$this->param()}) {
next if $key =~ /^_DEFAULT$/;
$key = lc(Foswiki::spaceOutWikiWord($key, "-"));
if ($key eq 'filter') {
$val = join("|", split(/\s*,\s*/, $val));
push @uploadButtonHtmlData, 'data-accept-file-types-="' . $val . '"';
push @htmlData, 'data-' . $key . '="' . $val . '"';
} else {
push @htmlData, 'data-' . $key . '="' . $val . '"';
}
}

if ($this->isMultiValued) {
push @htmlData, 'data-multiple="true"';
}

$this->addJavascript();
$this->addStyles();

my $htmlData = join(" ", @htmlData);
my $result = "<input $htmlData />";

my $uploadButtonHtmlData = join(" ", @uploadButtonHtmlData);
my $name = "_" . $this->{name} . ($this->isMultiValued ? '[]' : '');
$result .= <<HERE;
<span class='jqButton jqButtonSimple jqUploadButton' $uploadButtonHtmlData>
<i class='jqButtonIcon fa-fw fa fa-upload'></i>
<input type='file' name='$name' / >
</span>
HERE

return ('', $result);
}

sub addStyles {
#my $this = shift;
Foswiki::Func::addToZone("head",
"MOREFORMFIELDSPLUGIN::CSS",
"<link rel='stylesheet' href='%PUBURLPATH%/%SYSTEMWEB%/MoreFormfieldsPlugin/moreformfields.css' media='all' />",
"JQUERYPLUGIN::SELECT2");
}

sub addJavascript {
#my $this = shift;

Foswiki::Plugins::JQueryPlugin::createPlugin("fontawesome");
Foswiki::Plugins::JQueryPlugin::createPlugin("uploader");
Foswiki::Plugins::JQueryPlugin::createPlugin("select2");
Foswiki::Func::addToZone("script", "FOSWIKI::TOPICFIELD", <<"HERE", "JQUERYPLUGIN::SELECT2, JQUERYPLUGIN::UPLOADER");
<script type='text/javascript' src='%PUBURLPATH%/%SYSTEMWEB%/MoreFormfieldsPlugin/attachmentfield.js'></script>
HERE
}

1;
4 changes: 2 additions & 2 deletions lib/Foswiki/Form/Autofill.pm
Expand Up @@ -102,8 +102,8 @@ sub afterSaveHandler {
my $field = $topicObject->get('FIELD', $name);
next unless defined $field;
my $value = $field->{value};
$value = '' unless defined $value;
push @result, $field->{value};
next unless defined $value;
push @result, $field->{value} unless $value eq '';
}

$result = join($sep, @result);
Expand Down
16 changes: 5 additions & 11 deletions lib/Foswiki/Form/Topic.pm
Expand Up @@ -12,23 +12,17 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Form::Topic;

use strict;
use warnings;
use Foswiki::Func ();

use Foswiki::Func();
use Foswiki::Form::FieldDefinition ();
use Foswiki::Plugins::JQueryPlugin ();
use Assert;
our @ISA = ('Foswiki::Form::FieldDefinition');

BEGIN {
if ($Foswiki::cfg{UseLocale}) {
require locale;
import locale();
}
}

sub new {
my $class = shift;
my $this = $class->SUPER::new(@_);
Expand All @@ -49,7 +43,7 @@ sub getDefaultValue {
my $this = shift;

my $value = $this->{default};
$value = '' unless defined $value; # allow 0 values
$value = '' unless defined $value;

return $value;
}
Expand Down Expand Up @@ -229,7 +223,7 @@ sub addJavascript {
#my $this = shift;

Foswiki::Plugins::JQueryPlugin::createPlugin("select2");
Foswiki::Func::addToZone("script", "FOSWIKI::TOPICFIELD", <<"HERE", "JQUERYPLUGIN::SELECT2");
Foswiki::Func::addToZone("script", "FOSWIKI::FILEFIELD", <<"HERE", "JQUERYPLUGIN::SELECT2");
<script type='text/javascript' src='%PUBURLPATH%/%SYSTEMWEB%/MoreFormfieldsPlugin/topicfield.js'></script>
HERE
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Foswiki/Plugins/MoreFormfieldsPlugin/DEPENDENCIES
@@ -1,5 +1,7 @@
Foswiki::Plugins::DBCachePlugin,>9.00,perl,Required
Foswiki::Plugins::JQueryPlugin,>5.11,perl,Required
Foswiki::Plugins::RenderPlugin,>=4.00,perl,Required
Foswiki::Plugins::TopicInteractionPlugin,>=6.00,perl,Required
Foswiki::Plugins::ImagePlugin,>=8.00,perl,Required
Foswiki::Contrib::JQSelect2Contrib,>1.00,perl,Required
JSON,>=2.59,cpan,Required
5 changes: 5 additions & 0 deletions lib/Foswiki/Plugins/MoreFormfieldsPlugin/MANIFEST
@@ -1,3 +1,4 @@
!noci
data/System/MoreFormfieldsPlugin.txt 0644
lib/Foswiki/Form/Autofill.pm 0644
lib/Foswiki/Form/Date2.pm 0644
Expand All @@ -15,6 +16,7 @@ lib/Foswiki/Form/Smartbox.pm 0644
lib/Foswiki/Form/Time.pm 0644
lib/Foswiki/Form/Topic.pm 0644
lib/Foswiki/Form/User.pm 0644
lib/Foswiki/Form/Attachment.pm 0644
lib/Foswiki/Plugins/MoreFormfieldsPlugin/Clockpicker.pm 0644
lib/Foswiki/Plugins/MoreFormfieldsPlugin/Config.spec 0644
lib/Foswiki/Plugins/MoreFormfieldsPlugin/DEPENDENCIES 0644
Expand Down Expand Up @@ -56,5 +58,8 @@ pub/System/MoreFormfieldsPlugin/topicfield.uncompressed.js 0644
pub/System/MoreFormfieldsPlugin/userfield.js 0644
pub/System/MoreFormfieldsPlugin/userfield.js.gz 0644
pub/System/MoreFormfieldsPlugin/userfield.uncompressed.js 0644
pub/System/MoreFormfieldsPlugin/attachmentfield.js 0644
pub/System/MoreFormfieldsPlugin/attachmentfield.js.gz 0644
pub/System/MoreFormfieldsPlugin/attachmentfield.uncompressed.js 0644
pub/System/MoreFormfieldsPlugin/yaml2json 0644
templates/moreformfields.tmpl 0644
1 change: 1 addition & 0 deletions pub/System/MoreFormfieldsPlugin/Makefile
Expand Up @@ -5,6 +5,7 @@ TARGET= \
networkaddress.js \
userfield.js \
topicfield.js \
attachmentfield.js \
phonenumber.js \
clockpicker.js \
clockpicker.init.js \
Expand Down

0 comments on commit aadba6b

Please sign in to comment.