~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Contrib/JSCalendarContrib.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=begin TML
 
2
 
 
3
Read [[%ATTACHURL%/doc/html/reference.html][the Mishoo documentation]] or
 
4
[[%ATTACHURL%][visit the demo page]] for detailed information on using the
 
5
calendar widget.
 
6
 
 
7
This package also includes a small Perl module to make using the calendar
 
8
easier from Foswiki plugins. This module includes the functions:
 
9
 
 
10
=cut
 
11
 
 
12
package Foswiki::Contrib::JSCalendarContrib;
 
13
 
 
14
use strict;
 
15
 
 
16
require Foswiki::Func;    # The plugins API
 
17
 
 
18
use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION );
 
19
 
 
20
$VERSION = '$Rev: 3417 (2009-04-12) $';
 
21
$RELEASE = '03 Aug 2008';
 
22
$SHORTDESCRIPTION = "[[http://dynarch.com/mishoo/calendar.epl][Mishoo JSCalendar]], packaged for use by plugins, skins and add-ons";
 
23
 
 
24
# Max width of different mishoo format components
 
25
my %w = (
 
26
    a => 3,     # abbreviated weekday name
 
27
    A => 9,     # full weekday name
 
28
    b => 3,     # abbreviated month name
 
29
    B => 9,     # full month name
 
30
    C => 2,     # century number
 
31
    d => 2,     # the day of the month ( 00 .. 31 )
 
32
    e => 2,     # the day of the month ( 0 .. 31 )
 
33
    H => 2,     # hour ( 00 .. 23 )
 
34
    I => 2,     # hour ( 01 .. 12 )
 
35
    j => 3,     # day of the year ( 000 .. 366 )
 
36
    k => 2,     # hour ( 0 .. 23 )
 
37
    l => 2,     # hour ( 1 .. 12 )
 
38
    m => 2,     # month ( 01 .. 12 )
 
39
    M => 2,     # minute ( 00 .. 59 )
 
40
    n => 1,     # a newline character
 
41
    p => 2,     # 'PM' or 'AM'
 
42
    P => 2,     # 'pm' or 'am'
 
43
    S => 2,     # second ( 00 .. 59 )
 
44
    s => 12,# number of seconds since Epoch
 
45
    t => 1,     # a tab character
 
46
    U => 2,     # the week number
 
47
    u => 1,     # the day of the week ( 1 .. 7, 1 = MON )
 
48
    W => 2,     # the week number
 
49
    w => 1,     # the day of the week ( 0 .. 6, 0 = SUN )
 
50
    V => 2,     # the week number
 
51
    y => 2,     # year without the century ( 00 .. 99 )
 
52
    Y => 4,     # year including the century ( ex. 1979 )
 
53
);
 
54
 
 
55
=begin TML
 
56
 
 
57
---+++ Foswiki::Contrib::JSCalendarContrib::renderDateForEdit($name, $value, $format [, \%cssClass]) -> $html
 
58
 
 
59
This is the simplest way to use calendars from a plugin.
 
60
   * =$name= is the name of the CGI parameter for the calendar
 
61
     (it should be unique),
 
62
   * =$value= is the current value of the parameter (may be undef)
 
63
   * =$format= is the format to use (optional; the default is set
 
64
     in =configure=). The HTML returned will display a date field
 
65
     and a drop-down calendar.
 
66
   * =\%options= is an optional hash containing base options for
 
67
     the textfield.
 
68
Example:
 
69
<verbatim>
 
70
use Foswiki::Contrib::JSCalendarContrib;
 
71
...
 
72
my $fromDate = Foswiki::Contrib::JSCalendarContrib::renderDateForEdit(
 
73
   'from', '1 April 1999');
 
74
my $toDate = Foswiki::Contrib::JSCalendarContrib::renderDateForEdit(
 
75
   'to', undef, '%Y');
 
76
</verbatim>
 
77
 
 
78
=cut
 
79
 
 
80
sub renderDateForEdit {
 
81
    my ($name, $value, $format, $options) = @_;
 
82
 
 
83
    $format ||= $Foswiki::cfg{JSCalendarContrib}{format} || '%e %B %Y';
 
84
 
 
85
    addHEAD('foswiki');
 
86
 
 
87
    # Work out how wide it has to be from the format
 
88
    # SMELL: add a space because pattern skin default fonts on FF make the
 
89
    # box half a character too narrow if the exact size is used
 
90
    my $wide = $format.' ';
 
91
    $wide =~ s/(%(.))/$w{$2} ? ('_' x $w{$2}) : $1/ge;
 
92
    $options ||= {};
 
93
    $options->{name} = $name;
 
94
    $options->{id} = 'id_'.$name;
 
95
    $options->{value} = $value || '';
 
96
    $options->{size} ||= length($wide);
 
97
 
 
98
    return CGI::textfield($options)
 
99
      . CGI::image_button(
 
100
          -name => 'img_'.$name,
 
101
          -onclick =>
 
102
            "javascript: return showCalendar('id_$name','$format')",
 
103
            -src=> Foswiki::Func::getPubUrlPath() . '/' .
 
104
              $Foswiki::cfg{SystemWebName} .
 
105
                  '/JSCalendarContrib/img.gif',
 
106
          -alt => 'Calendar',
 
107
          -align => 'middle');
 
108
}
 
109
 
 
110
=begin TML
 
111
 
 
112
---+++ Foswiki::Contrib::JSCalendarContrib::addHEAD($setup)
 
113
 
 
114
This function will automatically add the headers for the calendar to the page
 
115
being rendered. It's intended for use when you want more control over the
 
116
formatting of your calendars than =renderDateForEdit= affords. =$setup= is
 
117
the name of
 
118
the calendar setup module; it can either be omitted, in which case the method
 
119
described in the Mishoo documentation can be used to create calendars, or it
 
120
can be ='foswiki'=, in which case a Javascript helper function called
 
121
'showCalendar' is added that simplifies using calendars to set a value in a
 
122
text field. For example, say we wanted to display the date with the calendar
 
123
icon _before_ the text field, using the format =%Y %b %e=
 
124
<verbatim>
 
125
# Add styles and javascript for the calendar
 
126
use Foswiki::Contrib::JSCalendarContrib;
 
127
...
 
128
 
 
129
sub commonTagsHandler {
 
130
  ....
 
131
  # Enable 'showCalendar'
 
132
  Foswiki::Contrib::JSCalendarContrib::addHEAD( 'foswiki' );
 
133
 
 
134
  my $cal = CGI::image_button(
 
135
      -name => 'img_datefield',
 
136
      -onclick =>
 
137
       "return showCalendar('id_datefield','%Y %b %e')",
 
138
      -src=> Foswiki::Func::getPubUrlPath() . '/' .
 
139
             $Foswiki::cfg{SystemWebName} .
 
140
             '/JSCalendarContrib/img.gif',
 
141
      -alt => 'Calendar',
 
142
      -align => 'middle' )
 
143
    . CGI::textfield(
 
144
      { name => 'date', id => "id_datefield" });
 
145
  ....
 
146
}
 
147
</verbatim>
 
148
The first parameter to =showCalendar= is the id of the textfield, and the second parameter is the date format. Default format is '%e %B %Y'.
 
149
 
 
150
All available date specifiers:
 
151
<verbatim>
 
152
%a - abbreviated weekday name 
 
153
%A - full weekday name 
 
154
%b - abbreviated month name 
 
155
%B - full month name 
 
156
%C - century number 
 
157
%d - the day of the month ( 00 .. 31 ) 
 
158
%e - the day of the month ( 0 .. 31 ) 
 
159
%H - hour ( 00 .. 23 ) 
 
160
%I - hour ( 01 .. 12 ) 
 
161
%j - day of the year ( 000 .. 366 ) 
 
162
%k - hour ( 0 .. 23 ) 
 
163
%l - hour ( 1 .. 12 ) 
 
164
%m - month ( 01 .. 12 ) 
 
165
%M - minute ( 00 .. 59 ) 
 
166
%n - a newline character 
 
167
%p - "PM" or "AM"
 
168
%P - "pm" or "am"
 
169
%S - second ( 00 .. 59 ) 
 
170
%s - number of seconds since Epoch (since Jan 01 1970 00:00:00 UTC) 
 
171
%t - a tab character 
 
172
%U, %W, %V - the week number
 
173
   The week 01 is the week that has the Thursday in the current year,
 
174
   which is equivalent to the week that contains the fourth day of January. 
 
175
   Weeks start on Monday.
 
176
%u - the day of the week ( 1 .. 7, 1 = MON ) 
 
177
%w - the day of the week ( 0 .. 6, 0 = SUN ) 
 
178
%y - year without the century ( 00 .. 99 ) 
 
179
%Y - year including the century ( ex. 1979 ) 
 
180
%% - a literal % character 
 
181
</verbatim>
 
182
 
 
183
=addHEAD= can be called from =commonTagsHandler= for adding the header to all pages, or from =beforeEditHandler= just for edit pages etc.
 
184
 
 
185
An alternative to =commonTagsHandler= is =postRenderingHandler= which is more efficient since it is called less often.
 
186
 
 
187
=cut
 
188
 
 
189
sub addHEAD {
 
190
    my $setup = shift;
 
191
    $setup ||= 'calendar-setup';
 
192
    my $style = $Foswiki::cfg{JSCalendarContrib}{style} || 'blue';
 
193
    my $lang = $Foswiki::cfg{JSCalendarContrib}{lang} || 'en';
 
194
    my $base = '%PUBURLPATH%/%SYSTEMWEB%/JSCalendarContrib';
 
195
    eval {
 
196
        require Foswiki::Contrib::BehaviourContrib;
 
197
        if (defined(&Foswiki::Contrib::BehaviourContrib::addHEAD)) {
 
198
            Foswiki::Contrib::BehaviourContrib::addHEAD();
 
199
        } else {
 
200
            Foswiki::Func::addToHEAD(
 
201
                'BEHAVIOURCONTRIB',
 
202
                '<script type="text/javascript" src="%PUBURLPATH%/%SYSTEMWEB%/BehaviourContrib/behaviour.compressed.js"></script>');
 
203
        }
 
204
    };
 
205
    my $head = <<HERE;
 
206
<style type='text/css' media='all'>
 
207
  \@import url('$base/calendar-$style.css');
 
208
  .calendar {z-index:2000;}
 
209
</style>
 
210
<script type='text/javascript' src='$base/calendar.js'></script>
 
211
<script type='text/javascript' src='$base/lang/calendar-$lang.js'></script>
 
212
HERE
 
213
    Foswiki::Func::addToHEAD( 'JSCALENDARCONTRIB', $head );
 
214
 
 
215
    # Add the setup separately; there might be different setups required
 
216
    # in a single HTML page.
 
217
    $head = <<HERE;
 
218
<script type='text/javascript' src='$base/$setup.js'></script>
 
219
HERE
 
220
    Foswiki::Func::addToHEAD( 'JSCALENDARCONTRIB_'.$setup, $head );
 
221
}
 
222
 
 
223
1;