Skip to content

Commit

Permalink
Item12962: convert to plugin
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/MoreFormfieldsPlugin@17810 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Jul 11, 2014
1 parent 5ac871c commit d9bd103
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
61 changes: 56 additions & 5 deletions lib/Foswiki/Form/Icon.pm
Expand Up @@ -69,10 +69,16 @@ HERE
foreach my $group (sort keys %icons) {
next if $this->{groupPattern} && $group !~ /$this->{groupPattern}/i;

$html .= " <optgroup label='$group'>\n" if scalar$this->{hasMultipleGroups};
my $groupLabel = $group;
$groupLabel =~ s/^FamFamFam//;
$groupLabel =~ s/([a-z])([A-Z0-9])/$1 $2/g;

$html .= " <optgroup label='$groupLabel'>\n" if scalar$this->{hasMultipleGroups};

foreach my $entry (sort {$a->{id} cmp $b->{id}} @{$icons{$group}}) {
$html .= " <option value='$entry->{id}'".($value && $entry->{id} eq $value?"selected":"").">$entry->{id}</option>\n";
my $text = $entry->{id};
$text =~ s/^fa\-//;
$html .= " <option value='$entry->{id}'".($value && $entry->{id} eq $value?"selected":"")." ".($entry->{url}?"data-url='$entry->{url}'":"").">$text</option>\n";
}

$html .= " </optgroup>\n" if $this->{hasMultipleGroups};
Expand All @@ -87,24 +93,63 @@ sub readIcons {

return if %icons;

# read fontawesome icons
my $iconFile = $Foswiki::cfg{PubDir}.'/'.$Foswiki::cfg{SystemWebName}.'/MoreFormfieldsContrib/icons.yml';

my $yml = YAML::LoadFile($iconFile);


my $numIcons = 0;
foreach my $entry (@{$yml->{icons}}) {
$entry->{id} = 'fa-'.$entry->{id};
foreach my $cat (@{$entry->{categories}}) {
push @{$icons{$cat}}, $entry;
if ($entry->{aliases}) {
foreach my $alias (@{$entry->{aliases}}) {
my %clone = %$entry;
$clone{id} = $alias;
$clone{id} = 'fa-'.$alias;
$clone{_isAlias} = 1;
push @{$icons{$cat}}, \%clone;
}
}
$numIcons += scalar(@{$icons{$cat}});
}
}

# read icons from icon path
my $iconSearchPath = $Foswiki::cfg{JQueryPlugin}{IconSearchPath}
|| 'FamFamFamSilkIcons, FamFamFamSilkCompanion1Icons, FamFamFamSilkCompanion2Icons, FamFamFamSilkGeoSilkIcons, FamFamFamFlagIcons, FamFamFamMiniIcons, FamFamFamMintIcons';

my @iconSearchPath = split( /\s*,\s*/, $iconSearchPath );

foreach my $item (@iconSearchPath) {
my ( $web, $topic ) = Foswiki::Func::normalizeWebTopicName(
$Foswiki::cfg{SystemWebName}, $item );

my $iconDir =
$Foswiki::cfg{PubDir} . '/'
. $web . '/'
. $topic . '/';

opendir(my $dh, $iconDir) || next;
foreach my $icon (grep { /\.(png|gif|jpe?g)$/i } readdir($dh)) {
next if $icon =~ /^(SilkCompanion1Thumb|index_abc|igp_.*)\.png$/; # filter some more
my $id = $icon;
$id =~ s/\.(png|gif|jpe?g)$//i;
push @{$icons{$topic}}, {
id => $id,
name => $id,
url => Foswiki::Func::getPubUrlPath() . '/' . $web . '/' . $topic . '/' . $icon,
categories => [$topic],
};
}
closedir $dh;

#print STDERR "no icons at $web.$topic\n" unless $icons{$topic};

$numIcons += scalar(@{$icons{$topic}}) if $icons{$topic};
}

#print STDERR "num icons found: $numIcons\n";
}

sub renderForDisplay {
Expand All @@ -122,7 +167,13 @@ sub renderForDisplay {
sub getDisplayValue {
my ( $this, $value ) = @_;

return "<i class='fa fa-$value'></i> ".$value;
my $icon = Foswiki::Plugins::JQueryPlugin::handleJQueryIcon($this->{session}, {
_DEFAULT => $value
});

my $text = $value;
$text =~ s/^fa\-//;
return $icon.' '.$text;
}

1;
18 changes: 3 additions & 15 deletions lib/Foswiki/Form/Time.pm
Expand Up @@ -18,6 +18,7 @@ package Foswiki::Form::Time;
use strict;
use warnings;

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

Expand All @@ -43,24 +44,11 @@ sub new {
sub renderForEdit {
my ($this, $topicObject, $value) = @_;

Foswiki::Func::addToZone("script", "FOSWIKI::TIMEFIELD", <<'HERE', "JQUERYPLUGIN");
<script type='text/javascript' src='%PUBURLPATH%/%SYSTEMWEB%/MoreFormfieldsContrib/clockpicker.js'></script>
<script type='text/javascript'>
jQuery(function($) {
$(".foswikiTimeField").livequery(function() {
$(this).clockpicker();
});
});
</script>
HERE

Foswiki::Func::addToZone("head", "FOSWIKI::TIMEFIELD", <<"HERE", "JQUERYPLUGIN");
<link rel='stylesheet' type='text/css' href='%PUBURLPATH%/%SYSTEMWEB%/MoreFormfieldsContrib/clockpicker.css'></script>
HERE
Foswiki::Plugins::JQueryPlugin::createPlugin("clockpicker");

return (
'',
"<input type='text' name='$this->{name}' size='$this->{size}' value='$value' data-autoclose='true' class='" . $this->cssClasses('foswikiInputField', 'foswikiTimeField') . "' />",
"<input type='text' name='$this->{name}' size='$this->{size}' value='$value' data-autoclose='true' class='" . $this->cssClasses('foswikiInputField', 'foswikiTimeField', 'jqClockPicker') . "' />",
);
}

Expand Down
1 change: 1 addition & 0 deletions pub/System/MoreFormfieldsContrib/Makefile
Expand Up @@ -6,6 +6,7 @@ TARGET= \
topicfield.js \
phonenumber.js \
clockpicker.js \
clockpicker.init.js \
clockpicker.css \
iconfield.js

Expand Down
15 changes: 11 additions & 4 deletions pub/System/MoreFormfieldsContrib/iconfield.uncompressed.js
@@ -1,10 +1,17 @@
jQuery(function($) {
var format = function(value) {
if (!value.text.match(/^[A-Z]/)) {
return '<i class="fa fa-' + value.text + '"></i> ' + value.text;
} else {

function format(value) {
if (typeof(value.id) === 'undefined') {
return value.text;
}
if (value.id.match(/^fa\-/)) {
return '<i class="fa ' + value.id + '"></i> ' + value.text;
}
var url = $(value.element).data("url");
if (url) {
return '<img src="'+url+'" class="foswikiIcon" /> ' + value.text;
}
return value.text;
};

$(".foswikiFontAwesomeIconPicker").select2({
Expand Down

0 comments on commit d9bd103

Please sign in to comment.