|
1 =begin twiki |
|
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 TWiki plugins. This module includes the functions: |
|
9 |
|
10 =cut |
|
11 |
|
12 package TWiki::Contrib::JSCalendarContrib; |
|
13 |
|
14 use strict; |
|
15 |
|
16 require TWiki::Func; # The plugins API |
|
17 |
|
18 use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION ); |
|
19 |
|
20 $VERSION = '$Rev: 16236 (22 Jan 2008) $'; |
|
21 $RELEASE = 'TWiki-4'; |
|
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 twiki |
|
56 |
|
57 ---+++ TWiki::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 TWiki::Contrib::JSCalendarContrib; |
|
71 ... |
|
72 my $fromDate = TWiki::Contrib::JSCalendarContrib::renderDateForEdit( |
|
73 'from', '1 April 1999'); |
|
74 my $toDate = TWiki::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 ||= $TWiki::cfg{JSCalendarContrib}{format} || '%e %B %Y'; |
|
84 |
|
85 addHEAD('twiki'); |
|
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=> TWiki::Func::getPubUrlPath() . '/' . |
|
104 TWiki::Func::getTwikiWebname() . |
|
105 '/JSCalendarContrib/img.gif', |
|
106 -alt => 'Calendar', |
|
107 -align => 'middle'); |
|
108 } |
|
109 |
|
110 =begin twiki |
|
111 |
|
112 ---+++ TWiki::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 ='twiki'=, 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 TWiki::Contrib::JSCalendarContrib; |
|
127 ... |
|
128 |
|
129 sub commonTagsHandler { |
|
130 .... |
|
131 # Enable 'showCalendar' |
|
132 TWiki::Contrib::JSCalendarContrib::addHEAD( 'twiki' ); |
|
133 |
|
134 my $cal = CGI::image_button( |
|
135 -name => 'img_datefield', |
|
136 -onclick => |
|
137 "return showCalendar('id_datefield','%Y %b %e')", |
|
138 -src=> TWiki::Func::getPubUrlPath() . '/' . |
|
139 TWiki::Func::getTwikiWebname() . |
|
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 . See the Mishoo documentation for details of the '$e %B %Y' parameter. |
|
149 |
|
150 =addHEAD= can be called from =commonTagsHandler= for adding the header to all pages, or from =beforeEditHandler= just for edit pages etc. |
|
151 |
|
152 =cut |
|
153 |
|
154 sub addHEAD { |
|
155 my $setup = shift; |
|
156 $setup ||= 'calendar-setup'; |
|
157 my $style = $TWiki::cfg{JSCalendarContrib}{style} || 'blue'; |
|
158 my $lang = $TWiki::cfg{JSCalendarContrib}{lang} || 'en'; |
|
159 my $base = '%PUBURLPATH%/%TWIKIWEB%/JSCalendarContrib'; |
|
160 eval { |
|
161 require TWiki::Contrib::BehaviourContrib; |
|
162 if (defined(&TWiki::Contrib::BehaviourContrib::addHEAD)) { |
|
163 TWiki::Contrib::BehaviourContrib::addHEAD(); |
|
164 } else { |
|
165 TWiki::Func::addToHEAD( |
|
166 'BEHAVIOURCONTRIB', |
|
167 '<script type="text/javascript" src="%PUBURLPATH%/%TWIKIWEB%/BehaviourContrib/behaviour.compressed.js"></script>'); |
|
168 } |
|
169 }; |
|
170 my $head = <<HERE; |
|
171 <style type='text/css' media='all'> |
|
172 \@import url('$base/calendar-$style.css'); |
|
173 .calendar {z-index:2000;} |
|
174 </style> |
|
175 <script type='text/javascript' src='$base/calendar.js'></script> |
|
176 <script type='text/javascript' src='$base/lang/calendar-$lang.js'></script> |
|
177 HERE |
|
178 TWiki::Func::addToHEAD( 'JSCALENDARCONTRIB', $head ); |
|
179 |
|
180 # Add the setup separately; there might be different setups required |
|
181 # in a single HTML page. |
|
182 $head = <<HERE; |
|
183 <script type='text/javascript' src='$base/$setup.js'></script> |
|
184 HERE |
|
185 TWiki::Func::addToHEAD( 'JSCALENDARCONTRIB_'.$setup, $head ); |
|
186 } |
|
187 |
|
188 1; |