From 764620aa013600c3f202d3fd60300862c221d086 Mon Sep 17 00:00:00 2001 From: MichaelDaum Date: Thu, 23 Sep 2010 14:35:32 +0000 Subject: [PATCH] Item9742: initial checkin git-svn-id: http://svn.foswiki.org/trunk/MoreFormfieldsContrib@9319 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- data/System/MoreFormfieldsContrib.txt | 39 ++++++ lib/Foswiki/Contrib/MoreFormfieldsContrib.pm | 21 +++ .../MoreFormfieldsContrib/DEPENDENCIES | 1 + .../Contrib/MoreFormfieldsContrib/MANIFEST | 16 +++ .../Contrib/MoreFormfieldsContrib/build.pl | 24 ++++ lib/Foswiki/Form/Ipaddress.pm | 31 +++++ lib/Foswiki/Form/Macaddress.pm | 31 +++++ lib/Foswiki/Form/Netmask.pm | 32 +++++ lib/Foswiki/Form/NetworkAddressField.pm | 101 +++++++++++++++ pub/System/MoreFormfieldsContrib/Makefile | 6 + .../moreformfields.uncompressed.css | 5 + .../networkaddress.uncompressed.js | 120 ++++++++++++++++++ 12 files changed, 427 insertions(+) create mode 100644 data/System/MoreFormfieldsContrib.txt create mode 100644 lib/Foswiki/Contrib/MoreFormfieldsContrib.pm create mode 100644 lib/Foswiki/Contrib/MoreFormfieldsContrib/DEPENDENCIES create mode 100644 lib/Foswiki/Contrib/MoreFormfieldsContrib/MANIFEST create mode 100755 lib/Foswiki/Contrib/MoreFormfieldsContrib/build.pl create mode 100644 lib/Foswiki/Form/Ipaddress.pm create mode 100644 lib/Foswiki/Form/Macaddress.pm create mode 100644 lib/Foswiki/Form/Netmask.pm create mode 100644 lib/Foswiki/Form/NetworkAddressField.pm create mode 100644 pub/System/MoreFormfieldsContrib/Makefile create mode 100644 pub/System/MoreFormfieldsContrib/moreformfields.uncompressed.css create mode 100644 pub/System/MoreFormfieldsContrib/networkaddress.uncompressed.js diff --git a/data/System/MoreFormfieldsContrib.txt b/data/System/MoreFormfieldsContrib.txt new file mode 100644 index 0000000..5b2d652 --- /dev/null +++ b/data/System/MoreFormfieldsContrib.txt @@ -0,0 +1,39 @@ +---+!! %TOPIC% +%SHORTDESCRIPTION% + +%TOC% + +This plugin implements a set of additional special purpose formfields to be used in [[%SYSTEMWEB%.DataForms][DataForm]] definitions. + +These are: + + * =ipaddress=: a general purpose IP4 address, consisting of 4 octets, e.g. =10.0.0.1= + * =netmask=: a special IP address used for routing, consisting of 4 octets, e.g. =255.255.255.0= + * =macaddress=: a hardware address given in 6 hex values, e.g. =00:1a:10:6f:ba:12= + +---++ !ExampleForm definition + +| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* | +| IpAddress | ipaddress | 15 | | network address | M | +| Netmask | netmask | 15 | | netmask | | +| MacAddress | macaddress | 17 | | hardware-addresse | | + +---++ Installation Instructions + +%$INSTALL_INSTRUCTIONS% + +---++ Info + + +| Author(s): | Michael Daum| +| Copyright: | © 2010 Michael Daum http://michaeldaumconsulting.com | +| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] | +| Release: | %$RELEASE% | +| Version: | %$VERSION% | +| Change History: |   | +| 23 Sep 2010: | initial release | +| Dependencies: | %$DEPENDENCIES% | +| Home page: | Foswiki:Extensions/%TOPIC% | +| Support: | Foswiki:Support/%TOPIC% | diff --git a/lib/Foswiki/Contrib/MoreFormfieldsContrib.pm b/lib/Foswiki/Contrib/MoreFormfieldsContrib.pm new file mode 100644 index 0000000..96cd83f --- /dev/null +++ b/lib/Foswiki/Contrib/MoreFormfieldsContrib.pm @@ -0,0 +1,21 @@ +# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ +# +# MoreFormfieldsContrib is Copyright (C) 2010 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::Contrib::MoreFormfieldsContrib; +our $VERSION = '$Rev$'; +our $RELEASE = '0.01'; +our $SHORTDESCRIPTION = 'Additionall formfield types for %SYSTEMWEB%.DataForms'; + +1; diff --git a/lib/Foswiki/Contrib/MoreFormfieldsContrib/DEPENDENCIES b/lib/Foswiki/Contrib/MoreFormfieldsContrib/DEPENDENCIES new file mode 100644 index 0000000..1841949 --- /dev/null +++ b/lib/Foswiki/Contrib/MoreFormfieldsContrib/DEPENDENCIES @@ -0,0 +1 @@ +Foswiki::Plugins::JQueryPlugin,>3.31,perl,Required diff --git a/lib/Foswiki/Contrib/MoreFormfieldsContrib/MANIFEST b/lib/Foswiki/Contrib/MoreFormfieldsContrib/MANIFEST new file mode 100644 index 0000000..b79d21e --- /dev/null +++ b/lib/Foswiki/Contrib/MoreFormfieldsContrib/MANIFEST @@ -0,0 +1,16 @@ +data/System/MoreFormfieldsContrib.txt 0644 +lib/Foswiki/Contrib/MoreFormfieldsContrib.pm 0644 +lib/Foswiki/Form/Ipaddress.pm 0644 +lib/Foswiki/Form/Macaddress.pm 0644 +lib/Foswiki/Form/Netmask.pm 0644 +lib/Foswiki/Form/NetworkAddressField.pm 0644 +pub/System/MoreFormfieldsContrib/Makefile 0644 +pub/System/MoreFormfieldsContrib/moreformfields.css 0644 +pub/System/MoreFormfieldsContrib/moreformfields.css.gz 0644 +pub/System/MoreFormfieldsContrib/moreformfields.uncompressed.css 0644 +pub/System/MoreFormfieldsContrib/networkaddress.js 0644 +pub/System/MoreFormfieldsContrib/networkaddress.js.gz 0644 +pub/System/MoreFormfieldsContrib/networkaddress.uncompressed.js 0644 +pub/System/MoreFormfieldsContrib/validate.js 0644 +pub/System/MoreFormfieldsContrib/validate.js.gz 0644 +pub/System/MoreFormfieldsContrib/validate.uncompressed.js 0644 diff --git a/lib/Foswiki/Contrib/MoreFormfieldsContrib/build.pl b/lib/Foswiki/Contrib/MoreFormfieldsContrib/build.pl new file mode 100755 index 0000000..a1c6f9c --- /dev/null +++ b/lib/Foswiki/Contrib/MoreFormfieldsContrib/build.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w +BEGIN { unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); } +use Foswiki::Contrib::Build; + +# Create the build object +$build = new Foswiki::Contrib::Build('MoreFormfieldsContrib'); + +# (Optional) Set the details of the repository for uploads. +# This can be any web on any accessible Foswiki installation. +# These defaults will be used when expanding tokens in .txt +# files, but be warned, they can be overridden at upload time! + +# name of web to upload to +$build->{UPLOADTARGETWEB} = 'Extensions'; +# Full URL of pub directory +$build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub'; +# Full URL of bin directory +$build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin'; +# Script extension +$build->{UPLOADTARGETSUFFIX} = ''; + +# Build the target on the command line, or the default target +$build->build($build->{target}); + diff --git a/lib/Foswiki/Form/Ipaddress.pm b/lib/Foswiki/Form/Ipaddress.pm new file mode 100644 index 0000000..dbd0d38 --- /dev/null +++ b/lib/Foswiki/Form/Ipaddress.pm @@ -0,0 +1,31 @@ +# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ +# +# MoreFormfieldsContrib is Copyright (C) 2010 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::Ipaddress; + +use strict; +use warnings; + +use Foswiki::Form::NetworkAddressField (); +our @ISA = ('Foswiki::Form::NetworkAddressField'); + +sub new { + my $class = shift; + my $this = $class->SUPER::new(@_); + $this->{_class} = 'foswikiIpAddress'; + return $this; +} + +1; diff --git a/lib/Foswiki/Form/Macaddress.pm b/lib/Foswiki/Form/Macaddress.pm new file mode 100644 index 0000000..9b2a535 --- /dev/null +++ b/lib/Foswiki/Form/Macaddress.pm @@ -0,0 +1,31 @@ +# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ +# +# MoreFormfieldsContrib is Copyright (C) 2010 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::Macaddress; + +use strict; +use warnings; + +use Foswiki::Form::NetworkAddressField (); +our @ISA = ('Foswiki::Form::NetworkAddressField'); + +sub new { + my $class = shift; + my $this = $class->SUPER::new(@_); + $this->{_class} = 'foswikiMacAddress'; + return $this; +} + +1; diff --git a/lib/Foswiki/Form/Netmask.pm b/lib/Foswiki/Form/Netmask.pm new file mode 100644 index 0000000..1eb6d1f --- /dev/null +++ b/lib/Foswiki/Form/Netmask.pm @@ -0,0 +1,32 @@ +# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ +# +# MoreFormfieldsContrib is Copyright (C) 2010 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::Netmask; + +use strict; +use warnings; + +use Foswiki::Form::NetworkAddressField (); +our @ISA = ('Foswiki::Form::NetworkAddressField'); + +sub new { + my $class = shift; + my $this = $class->SUPER::new(@_); + $this->{_class} = 'foswikiNetmask'; + return $this; +} + + +1; diff --git a/lib/Foswiki/Form/NetworkAddressField.pm b/lib/Foswiki/Form/NetworkAddressField.pm new file mode 100644 index 0000000..c860896 --- /dev/null +++ b/lib/Foswiki/Form/NetworkAddressField.pm @@ -0,0 +1,101 @@ +# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ +# +# MoreFormfieldsContrib is Copyright (C) 2010 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::NetworkAddressField; + +use strict; +use warnings; + +use Foswiki::Form::Text (); +use Foswiki::Plugins::JQueryPlugin (); +our @ISA = ('Foswiki::Form::Text'); + +sub addJavascript { + #my $this = shift; + Foswiki::Func::addToZone("script", + "MOREFORMFIELDSCONTRIB::IPADDRESS::JS", + "", + "JQUERYPLUGIN::FOSWIKI, JQUERYPLUGIN::LIVEQUERY, JQUERYPLUGIN::VALIDATE"); + + if ($Foswiki::cfg{Plugins}{MoreFormfieldsContrib}{Debug}) { + Foswiki::Plugins::JQueryPlugin::createPlugin("debug"); + } + +} + +sub addStyles { + #my $this = shift; + Foswiki::Func::addToZone("head", + "MOREFORMFIELDSCONTRIB::IPADDRESS::CSS", + ""); + +} + +sub renderForEdit { + my $this = shift; + + # get args in a backwards compatible manor: + my $metaOrWeb = shift; + + my $meta; + my $web; + my $topic; + + if (ref($metaOrWeb)) { + # new: $this, $meta, $value + $meta = $metaOrWeb; + $web = $meta->web; + $topic = $meta->topic; + } else { + # old: $this, $web, $topic, $value + $web = $metaOrWeb; + $topic = shift; + ($meta, undef) = Foswiki::Func::readTopic($web, $topic); + } + + my $value = shift; + + $this->addJavascript(); + $this->addStyles(); + + my $required = ''; + if ($this->{attributes} =~ /\bM\b/i) { + $required = 'required'; + } + + return ( + '', + CGI::textfield( + -class => $this->cssClasses('foswikiInputField', $this->{_class}, $required), + -name => $this->{name}, + -size => $this->{size}, + -value => $value + ) + ); +} + +sub renderForDisplay { + my ($this, $format, $value, $attrs) = @_; + + my $result = "
$value
"; + $format =~ s/\$value/$result/g; + + $this->addStyles(); + + return $this->SUPER::renderForDisplay($format, $value, $attrs); +} + + +1; diff --git a/pub/System/MoreFormfieldsContrib/Makefile b/pub/System/MoreFormfieldsContrib/Makefile new file mode 100644 index 0000000..7532808 --- /dev/null +++ b/pub/System/MoreFormfieldsContrib/Makefile @@ -0,0 +1,6 @@ +FOSWIKI_ROOT?=~/foswiki/trunk/core +TARGET= \ + moreformfields.css \ + networkaddress.js + +-include $(FOSWIKI_ROOT)/pub/System/JQueryPlugin/Makefile.include diff --git a/pub/System/MoreFormfieldsContrib/moreformfields.uncompressed.css b/pub/System/MoreFormfieldsContrib/moreformfields.uncompressed.css new file mode 100644 index 0000000..13d13b6 --- /dev/null +++ b/pub/System/MoreFormfieldsContrib/moreformfields.uncompressed.css @@ -0,0 +1,5 @@ +.foswikiIpAddress, +.foswikiNetmask, +.foswikiMacAddress { + font-family:monospace; +} diff --git a/pub/System/MoreFormfieldsContrib/networkaddress.uncompressed.js b/pub/System/MoreFormfieldsContrib/networkaddress.uncompressed.js new file mode 100644 index 0000000..924f467 --- /dev/null +++ b/pub/System/MoreFormfieldsContrib/networkaddress.uncompressed.js @@ -0,0 +1,120 @@ +jQuery(function($) { + var addressRegex = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; + var macRegex = /^([a-f\d]+)[:\.-]([a-f\d]+)[:\.-]([a-f\d]+)[:\.-]([a-f\d]+)[:\.-]([a-f\d]+)[:\.-]([a-f\d]+)$/i; + var errorMsgIp = 'Please provide a valid IP address'; + var errorMsgNetmask = 'Please provide a valid netmask'; + var errorMsgMac = 'Please provide a valid mac address'; + var $errorElem; + + function error(text, elem) { + if ($errorElem) { + $errorElem.remove(); + } + $errorElem = $('').insertAfter(elem); + } + + function testAddress(elem) { + var isRequired = elem.is(".required"); + var isIpAddr = elem.is(".foswikiIpAddress"); + var isNetmask = elem.is(".foswikiNetmask"); + var isMacAddr = elem.is(".foswikiMacAddress"); + var val = elem.val(); + var result = [] + var errorMsg = isNetmask?errorMsgNetmask:(isIpAddr?errorMsgIp:errorMsgMac); + var radix = isMacAddr?16:10; + var nrSegments = isMacAddr?6:4; + var match = isMacAddr?macRegex.exec(val):addressRegex.exec(val); + var separator = isMacAddr?':':'.'; + + if (!isRequired && val == '') { + $.log("empty value ... not required ... abording test"); + return true; + } + + if (match === null) { + $.log("formfield doesn't match"); + error(errorMsg, elem); + return false; + } + + var max; + var min = 0; + if (isIpAddr) { + max = 254; + } else { + max = 255; + } + for (var i = 1; i <= nrSegments; i++) { + var segment = parseInt(match[i], radix); + if (isNaN(segment)) { + $.log("can't parse segment"+match[i]); + error(errorMsg, elem); + return false; + } + if (isIpAddr) { + if (i == 1 || i == nrSegments) { + min = 1; // first and last segment must be > 0 + } else { + min = 0; + } + } + if (segment > max) { + error(errorMsg, elem); + $.log("segment too high"); + return false; + } + if (segment < min) { + $.log("segment too low"); + error(errorMsg, elem); + return false; + } + result.push(addZeros(segment.toString(radix), isMacAddr?2:3).toUpperCase()); + } + elem.val(result.join(separator)); + + if ($errorElem) { + $errorElem.remove(); + } + return true; + } + + function addZeros(str, maxLen) { + var len = str.length; + if (len < 1) str = '000'; + if (len == 1) str = '00'+str; + if (len == 2) str = '0'+str; + return str.substr(3-maxLen, maxLen); + } + + // dom ready + $(function() { + $(".foswikiIpAddress, .foswikiNetmask, .foswikiMacAddress").livequery(function() { + var $input = $(this); + var $form = $input.parents("form:first"); + + // form validation + $input.blur(function() { + testAddress($input); + }); + + // validate formfields before submit + $form.submit(function() { + if(!testAddress($input)) { + // get current action + var action = $form.find("input[name*=action_][value!='']"); + if(action.length) { + action = action.attr('name').substr(7,10); + } else { + action = 'save'; + } + if (action == "save" || action == "checkpoint") { + $.log("checking input field "+$input.attr('class')); + alert("Validation Error: please check '"+$input.attr('name')+"'"); + $.log("blocking submit"); + return false; + } + } + }); + }); + }); +});