Quiq/JQuery/Function.pm

filter: Statementstmt 85.1% 23 / 27 Branchbran 100% 2 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 6 / 8 Podpod 100% 3 / 3 totaltotal 81.4% 35 / 43 CC 3 SCAR 5.9
CC
3
Cov
84%
ready
3.0
new
2.0
formatDate
2.0
CRAP 1.8
« Quiq/Excel/Writer.pm ↑ Summary Quiq/JQuery/ContextMenu.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::JQuery::Function - Nützliche Funktionen für jQuery
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Diese Klasse erweitert das jQuery-Objekt um nützliche Funktionen.
16  
17 =cut
18  
19 # -----------------------------------------------------------------------------
20  
21 package Quiq::JQuery::Function;
22 10 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 10 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 10 our $VERSION = '1.240';
29  
30 10 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # -----------------------------------------------------------------------------
33  
34 =head1 METHODS
35  
36 =head2 Konstruktor
37  
38 =head3 new() - Instantiiere Objekt
39  
40 =head4 Synopsis
41  
42 $jq = $class->new;
43  
44 =head4 Returns
45  
46 Path-Objekt
47  
48 =head4 Description
49  
50 Instantiiere ein Objekt der Klasse und liefere eine Referenz auf
51 dieses Objekt zurück. Da die Klasse nur Klassenmethoden enthält,
52 hat das Objekt lediglich die Aufgabe, eine abkürzende
53 Aufrufschreibweise zu ermöglichen.
54  
55 =cut
56  
57 # -----------------------------------------------------------------------------
58  
59 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
60 0 my $class = shift;
61 0 return bless \(my $dummy),$class;
62 }
63  
64 # -----------------------------------------------------------------------------
65  
66 =head2 Klassenmethoden
67  
68 =head3 formatDate() - JavaScript-Code der Funktion
69  
70 =head4 Synopsis
71  
72 $javaScript = $this->formatDate;
73  
74 =head4 Description
75  
76 Die JavaScript-Funktion formatiert ein JavaScript Datums-Objekt
77 gemäß dem angegebenen Muster:
78  
79 str = $.formatDate(d,fmt);
80  
81 =over 4
82  
83 =item YYYY
84  
85 Jahreszahl, vierstellig.
86  
87 =item YY
88  
89 Jahreszahl, zweistellig,
90  
91 =item MMMM
92  
93 Monatsname, voll ausgeschrieben.
94  
95 =item MMM
96  
97 Monatsname, die ersten drei Buchstaben.
98  
99 =item MM
100  
101 Monatsnummer, zweistellig.
102  
103 =item M
104  
105 Monatsnummer.
106  
107 =item DDDD
108  
109 Wochentag, voll ausgeschrieben.
110  
111 =item DDD
112  
113 Wochentag, die ersten drei Buchstaben.
114  
115 =item DD
116  
117 Tag des Monats, zweistellig.
118  
119 =item D
120  
121 Tag des Monats.
122  
123 =item hh
124  
125 Stunde, zweistellig.
126  
127 =item h
128  
129 Stunde.
130  
131 =item mm
132  
133 Minute, zweistellig.
134  
135 =item m
136  
137 Minute
138  
139 =item ss
140  
141 Sekunde, zweistellig.
142  
143 =item s
144  
145 Sekunde.
146  
147 =item xxx
148  
149 Millisekunden, dreistellig.
150  
151 =back
152  
153 Die Implementierung basiert auf der Funktion formatDate() im
154 Buch L<jQuery in Action, Third Edition|https://www.manning.com/books/jquery-in-action-third-edition>, S. 352 ff.
155  
156 =cut
157  
158 # -----------------------------------------------------------------------------
159  
160 0 ▶sub formatDate {
Subroutine formatDate: not called CC=1 SCAR=6.9
Pod documented
161 0 my $this = shift;
162  
163 0 return Quiq::Unindent->hereDoc(<<' __JS__');
164 (function($) {
165 var patternParts =
166 /^(YY(YY)?|M(M(M(M)?)?)?|D(D)?|EEE(E)?|h(h)?|m(m)?|s(s)?|xxx)/;
167  
168 var patternValue = {
169 YY: function(date) {
170 return toFixedWidth(date.getFullYear(), 2);
171 },
172 YYYY: function(date) {
173 return date.getFullYear().toString();
174 },
175 MMMM: function(date) {
176 return $.FormatDate.monthNames[date.getMonth()];
177 },
178 MMM: function(date) {
179 return $.FormatDate.monthNames[date.getMonth()]
180 .substr(0, 3);
181 },
182 MM: function(date) {
183 return toFixedWidth(date.getMonth() + 1, 2);
184 },
185 M: function(date) {
186 return date.getMonth() + 1;
187 },
188 DD: function(date) {
189 return toFixedWidth(date.getDate(), 2);
190 },
191 D: function(date) {
192 return date.getDate();
193 },
194 EEEE: function(date) {
195 return $.FormatDate.dayNames[date.getDay()];
196 },
197 EEE: function(date) {
198 return $.FormatDate.dayNames[date.getDay()].substr(0, 3);
199 },
200 hh: function(date) {
201 return toFixedWidth(date.getHours(), 2);
202 },
203 h: function(date) {
204 return date.getHours();
205 },
206 mm: function(date) {
207 return toFixedWidth(date.getMinutes(), 2);
208 },
209 m: function(date) {
210 return date.getMinutes();
211 },
212 ss: function(date) {
213 return toFixedWidth(date.getSeconds(), 2);
214 },
215 s: function(date) {
216 return date.getSeconds();
217 },
218 xxx: function(date) {
219 return toFixedWidth(date.getMilliseconds(), 3);
220 },
221 };
222  
223 function toFixedWidth(value, length, fill) {
224 var result = (value || '').toString();
225 fill = fill || '0';
226 var padding = length - result.length;
227 if (padding < 0) {
228 result = result.substr(-padding);
229 } else {
230 for (var n = 0; n < padding; n++) {
231 result = fill + result;
232 }
233 }
234 return result;
235 }
236  
237 $.formatDate = function(date, pattern) {
238 var result = [];
239 while (pattern.length > 0) {
240 patternParts.lastIndex = 0;
241 var matched = patternParts.exec(pattern);
242 if (matched) {
243 result.push(patternValue[matched[0]].call(this, date));
244 pattern = pattern.slice(matched[0].length);
245 } else {
246 result.push(pattern.charAt(0));
247 pattern = pattern.slice(1);
248 }
249 }
250 return result.join('');
251 };
252  
253 $.formatDate.monthNames = [
254 'January','February','March','April','May','June','July',
255 'August','September','October','November','December'
256 ];
257  
258 $.formatDate.dayNames = [
259 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday',
260 'Saturday'
261 ];
262 })(jQuery);
263 __JS__
264 }
265  
266 # -----------------------------------------------------------------------------
267  
268 =head3 ready() - Erzeuge JQuery Ready-Handler
269  
270 =head4 Synopsis
271  
272 $handler = $this->ready($js);
273  
274 =head4 Arguments
275  
276 =over 4
277  
278 =item $js
279  
280 JavaScript-Code (String).
281  
282 =back
283  
284 =head4 Returns
285  
286 JQuery Ready-Handler (String)
287  
288 =head4 Description
289  
290 Fasse JavaScript-Code $js in einen JQuery Ready-Handler ein und liefere
291 den resultierenden JavaScript-Code zurück. Der Ready-Handler hat
292 den Aufbau
293  
294 $(function() {
295 <JAVASCRIPT>
296 });
297  
298 =head4 See Also
299  
300 L<https://api.jquery.com/ready/>
301  
302 =cut
303  
304 # -----------------------------------------------------------------------------
305  
306 5 ▶sub ready {
Subroutine ready: called CC=3 SCAR=11.0
Pod documented
307 5 my $this = shift;
308 5 ▶ my $js = shift // '';
Condition50%1/2
shift() // ""
l!l
50
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
309  
310 5 ▶ if ($js) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js) 4 1 5
311 4 $js = Quiq::Unindent->trim($js);
312 4 $js =~ s/^/ /mg;
313 4 $js = "\$(function() {\n$js\n});";
314 }
315  
316 5 return $js;
317 }
318  
319 # -----------------------------------------------------------------------------
320  
321 =head1 VERSION
322  
323 1.240
324  
325 =head1 AUTHOR
326  
327 Frank Seitz, L<http://fseitz.de/>
328  
329 =head1 COPYRIGHT
330  
331 Copyright (C) 2026 Frank Seitz
332  
333 =head1 LICENSE
334  
335 This code is free software; you can redistribute it and/or modify
336 it under the same terms as Perl itself.
337  
338 =cut
339  
340 # -----------------------------------------------------------------------------
341  
342 10 1;
343  
344 # eof
« Quiq/Excel/Writer.pm ↑ Summary Quiq/JQuery/ContextMenu.pm »