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