Quiq/JQuery/Form/ViewEdit.pm

filter: Statementstmt 74.2% 49 / 66 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 100% 4 / 4 totaltotal 71.4% 70 / 98 CC 8 SCAR 16.6
CC
8
Cov
68%
instantiate
20.0
new
3.1
html
3.1
CRAP 5.3
« Quiq/JavaScript.pm ↑ Summary Quiq/Stacktrace.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::JQuery::Form::ViewEdit - Formular zum Ansehen und Bearbeiten von persistenten Daten
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse implementiert ein jQuery Widget Plugin zum Ansehen und
16 Bearbeiten von Daten, die typischerweise aus einer Datenbank
17 stammen.
18  
19 Das Layout muss die Platzhalter C<__SAVE__>, C<__DELETE__> und
20 C<__EDIT__> enthalten. Für diese werden intern drei Widgets
21 generiert: für C<__SAVE__> und C<__DELETE__> ein Button zum
22 Speichern bzw. Löschen, für C<__EDIT__> eine Checkbox zum
23 Umschalten zwischen Ansehen und Bearbeiten.
24  
25 Bei Betätigung der Button werden die Formulardaten an den
26 action-URL gepostet. Im Erfolgsfall wird anschließend die
27 onSuccess-Methode aufgerufen.
28  
29 =head2 CSS-Klassen
30  
31 =over 2
32  
33 =item *
34  
35 editCheckbox
36  
37 =item *
38  
39 saveButton
40  
41 =item *
42  
43 deleteButton
44  
45 =item *
46  
47 enabled
48  
49 =item *
50  
51 disabled
52  
53 =back
54  
55 =head2 Beschriftungen
56  
57 =over 2
58  
59 =item *
60  
61 Speichern
62  
63 =item *
64  
65 Löschen
66  
67 =item *
68  
69 Bearbeiten
70  
71 =back
72  
73 Die Beschriftungen (der Buttons) können über das Attribut text
74 gendändert werden.
75  
76 =head1 ATTRIBUTES
77  
78 =over 4
79  
80 =item action => $url (Default: undef)
81  
82 URL, an den die Daten bei bei Betätigung des Save- oder
83 des Delete-Buttons geschickt werden.
84  
85 =item hidden => \@keyVal (Default: [])
86  
87 Schlüssel/Wert-Paare, die als Hidden-Widgets gesetzt werden.
88  
89 =item id (Default: undef)
90  
91 Die DOM-Id des Formulars.
92  
93 =item instantiate => $bool (Default: 0)
94  
95 Füge die Plugin-Instantiierung beim Aufruf von html()
96 zum HTML-Code hinzu.
97  
98 =item layout => $html (Default: '')
99  
100 Der HTML-Code des Layouts. In das Layout wird der HTML-Code der
101 Widgets eingesetzt.
102  
103 =item onSucces => $javaScript (Default: undef)
104  
105 JavaScript-Methode, die nach einem erfolgreichen Ajax-Aufruf
106 ausgeführt wird. Parameter: onSuccess(data,textStatus,jqXHR,op),
107 wobei op 'save' oder 'delete' ist.
108  
109 =item state => 'update' | 'insert' (Default: 'update')
110  
111 Anfänglicher Zusatand des Formulars:
112  
113 =over 4
114  
115 =item 'update'
116  
117 Der Save- und der Delete-Button werden im Edit-Modus freigeschaltet.
118  
119 =item 'insert'
120  
121 Nur der Save-Button wird im Edit-Modus freigeschaltet.
122  
123 =back
124  
125 =item text => \%keyVal (Default: s. Text)
126  
127 Die Beschriftungen der intern generierten Widgets alle oder einzeln
128 geändert werden:
129  
130 =over 2
131  
132 =item *
133  
134 saveButton => 'Speichern',
135  
136 =item *
137  
138 deleteButton => 'Löschen',
139  
140 =item *
141  
142 editCheckBox => 'Bearbeiten',
143  
144 =back
145  
146 =item widgets => \@widgets (Default: [])
147  
148 Liste der Widgets, die in das Layout eingesetzt werden.
149  
150 =back
151  
152 =head1 EXAMPLE
153  
154 $html = Quiq::JQuery::Form::ViewEdit->html($h,
155 instantiate => 1,
156 id => 'personForm',
157 state => 'insert',
158 action => $c->url_for('/person/speichern'),
159 onSuccess => q|
160 function () {
161 var d = new Date;
162 var date = $.formatDate(d,'YYYY-MM-DD hh:mm:ss');
163 $('input[name=formTime]').val(date);
164 }
165 |,
166 text => {
167 saveButton => 'Speichern',
168 deleteButton => 'Löschen',
169 editCheckbox => 'Bearbeiten',
170 },
171 layout => $h->cat(
172 Quiq::Html::Table::Simple->html($h,
173 class => 'form',
174 rows => [
175 ['form-section',[colspan=>2,'Person']],
176 ['form-widget',['Id:'],['__PER_ID__']],
177 ['form-widget',['Vorname:'],['__PER_VORNAME__']],
178 ['form-widget',['Nachname:'],['__PER_NACHNAME__']],
179 ],
180 ),
181 Quiq::Html::Table::Simple->html($h,
182 class => 'form',
183 rows => [
184 [['__SAVE__ __DELETE__ __EDIT__']],
185 ],
186 ),
187 ),
188 widgets => [
189 Quiq::Html::Widget::Hidden->new(
190 name => 'formular',
191 value => 'person',
192 ),
193 Quiq::Html::Widget::Hidden->new(
194 name => 'formTime',
195 value => $formTime,
196 ),
197 Quiq::Html::Widget::ReadOnly->new(
198 name => 'per_id',
199 value => $per->per_id,
200 ),
201 Quiq::Html::Widget::TextField->new(
202 name => 'per_vorname',
203 size => 30,
204 value => $per->per_vorname,
205 ),
206 Quiq::Html::Widget::TextField->new(
207 name => 'per_nachname',
208 size => 30,
209 value => $per->per_nachname,
210 ),
211 ],
212 );
213  
214 =cut
215  
216 # -----------------------------------------------------------------------------
217  
218 package Quiq::JQuery::Form::ViewEdit;
219 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 1 our $VERSION = '1.240';
227  
228 1 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 1 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 1 ▶use Quiq::Html::Widget::Button;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 1 ▶use Quiq::Html::Widget::CheckBox;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 1 ▶use Quiq::Html::Form::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 # -----------------------------------------------------------------------------
235  
236 =head1 METHODS
237  
238 =head2 Plugin-Code (Klassenmethoden)
239  
240 =head3 pluginCode() - JavaScript-Code des Plugin
241  
242 =head4 Synopsis
243  
244 $javascript = $class->pluginCode;
245  
246 =head4 Description
247  
248 Liefere den JavaScript-Code des jQuery Widget Plugin. Dieser Code
249 kann auf einer HTML-Seite inline verwendet oder - besser - vom
250 Webserver geliefert werden.
251  
252 =cut
253  
254 # -----------------------------------------------------------------------------
255  
256 0 ▶sub pluginCode {
Subroutine pluginCode: not called CC=1 SCAR=6.9
Pod documented
257 0 my $this = shift;
258  
259 0 return Quiq::Unindent->hereDoc(q~
260 $.widget('quiq.viewEditForm',{
261 options: {
262 action: null,
263 onSuccess: null,
264 state: null,
265 },
266 saveButton: null,
267 deleteButton: null,
268 editCheckbox: null,
269 widgets: null,
270 userWidgets: null,
271 _create: function () {
272 this.element.addClass('viewEditForm');
273  
274 this._on(this.element,{
275 'click .editCheckbox': function (event) {
276 this.__render();
277 },
278 'click .saveButton': function (event) {
279 var dataFound = 0;
280  
281 this.userWidgets.each(function () {
282 var $this = $(this);
283 if ($this.is(':radio,:checkbox')) {
284 if ($this.prop('checked'))
285 dataFound++;
286 }
287 else {
288 if ($this.val() != '')
289 dataFound++;
290 }
291 });
292 if (!dataFound) {
293 alert('Bitte füllen Sie das Formular aus');
294 return;
295 }
296 this.__save('save');
297 },
298 'click .deleteButton': function (event) {
299 if (confirm('Wirklich löschen?')) {
300 this.__save('delete');
301 }
302 },
303 });
304  
305 this.saveButton = $('.saveButton',this.element);
306 this.deleteButton = $('.deleteButton',this.element);
307 this.editCheckbox = $('.editCheckbox',this.element);
308  
309 var saveButton = this.saveButton.get(0);
310 var deleteButton = this.deleteButton.get(0);
311 var editCheckbox = this.editCheckbox.get(0);
312  
313 this.widgets = $(':input',this.element).filter(function (i) {
314 return this != editCheckbox;
315 });
316  
317 this.userWidgets = $(':input',this.element).filter(function (i) {
318 return this != saveButton
319 && this != deleteButton
320 && this != editCheckbox
321 && $(this).attr('type') != 'hidden';
322 });
323  
324 this.__render();
325 },
326 __render: function () {
327 var edit = this.editCheckbox.is(':checked');
328  
329 if (edit) {
330 // Widgets enablen
331  
332 this.widgets.prop('disabled',false);
333 this.widgets.removeClass('disabled');
334 this.widgets.addClass('enabled');
335  
336 if (this.options.state == 'insert') {
337 // Im Insert-State wird die Delete-Operation
338 // nicht angeboten
339  
340 var $deleteButton = $('.deleteButton');
341 $deleteButton.prop('disabled',true);
342 $deleteButton.removeClass('enabled');
343 $deleteButton.addClass('disabled');
344 }
345 }
346 else {
347 // Widgets disablen
348  
349 this.widgets.prop('disabled',true);
350 this.widgets.removeClass('enabled');
351 this.widgets.addClass('disabled');
352 }
353 },
354 __save: function (op) {
355 var instance = this;
356 var url = this.options.action;
357 var data = 'op='+op+'&'+$(':input',this.element).serialize();
358  
359 $.ajax({
360 url: url,
361 type: 'POST',
362 data: data,
363 async: false,
364 success: function (data,textStatus,jqXHR) {
365 if (data) {
366 // Fehlermeldung
367 alert(data);
368 return;
369 }
370 instance.editCheckbox.prop('checked',false);
371 if (op == 'delete')
372 instance.__clear();
373 if (instance.options.onSuccess)
374 instance.options.onSuccess(data,textStatus,jqXHR,op);
375 instance.__render();
376 },
377 error: function () {
378 alert('FEHLER: Speichern fehlgeschlagen');
379 },
380 });
381 },
382 __clear: function () {
383 // Feldinhalte löschen
384  
385 this.userWidgets.each(function () {
386 var $this = $(this);
387 if ($this.is(':radio,:checkbox')) {
388 $this.prop('checked',false);
389 }
390 else {
391 $this.val('');
392 }
393 });
394 },
395 });
396 ~);
397 }
398  
399 # -----------------------------------------------------------------------------
400  
401 =head2 Konstruktor
402  
403 =head3 new() - Instantiiere Objekt
404  
405 =head4 Synopsis
406  
407 $e = $class->new(@keyVal);
408  
409 =head4 Description
410  
411 Instantiiere ein Formular-Objekt und liefere eine Referenz auf
412 dieses Objekt zurück.
413  
414 =cut
415  
416 # -----------------------------------------------------------------------------
417  
418 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
419 1 my $class = shift;
420 # @_: @keyVal
421  
422 1 my $self = $class->SUPER::new(
423 action => undef,
424 id => undef,
425 hidden => undef,
426 instantiate => 0,
427 layout => '',
428 onSuccess => undef,
429 state => 'update',
430 text => Quiq::Hash->new(
431 saveButton => 'Speichern',
432 deleteButton => 'Löschen',
433 editCheckbox => 'Bearbeiten',
434 ),
435 widgets => [],
436 );
437  
438 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
439 1 my $key = shift;
440 1 ▶ if ($key eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "text") 0 1 1
441 0 $self->get($key)->join(shift);
442 0 next;
443 }
444 1 $self->set($key => shift);
445 }
446  
447 1 return $self;
448 }
449  
450 # -----------------------------------------------------------------------------
451  
452 =head2 HTML-Generierung
453  
454 =head3 html() - HTML-Code des Widget
455  
456 =head4 Synopsis
457  
458 $html = $e->html($h);
459 $html = $class->html($h,@keyVal);
460  
461 =head4 Description
462  
463 Generiere den HTML-Code des Widget-Objekts und liefere diesen
464 zurück. Als Klassenmethode gerufen, wird das Objekt intern erzeugt
465 und mit den Attributen @keyVal instantiiert.
466  
467 =cut
468  
469 # -----------------------------------------------------------------------------
470  
471 1 ▶sub html {
Subroutine html: called CC=3 SCAR=11.2
Pod documented
472 1 my $this = shift;
473 1 my $h = shift;
474  
475 1 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 1 0 1
476  
477 1 my ($id,$hiddenA,$instantiate,$layout,$state,$textH,$widgetA) =
478 $self->get(qw/id hidden instantiate layout state text widgets/);
479  
480 # Liste der Widgets kopieren und Save-, Delete-, Edit-Widgets hinzufügen
481  
482 1 my @widgets = @$widgetA;
483 1 push @widgets,Quiq::Html::Widget::Button->new(
484 class => 'saveButton',
485 name => 'save',
486 content => $textH->get('saveButton'),
487 );
488 1 push @widgets,Quiq::Html::Widget::Button->new(
489 class => 'deleteButton',
490 name => 'delete',
491 content => $textH->get('deleteButton'),
492 );
493 1 push @widgets,Quiq::Html::Widget::CheckBox->new(
494 class => 'editCheckbox',
495 name => 'edit',
496 label => $textH->get('editCheckbox'),
497 );
498  
499 1 my $html = Quiq::Html::Form::Layout->html($h,
500 form => [
501 id => $id,
502 ],
503 layout => $layout,
504 hidden => $hiddenA,
505 widgets => \@widgets,
506 );
507  
508 1 ▶ if ($instantiate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instantiate) 0 1 1
509 0 $html .= $h->tag('script',
510 $self->instantiate,
511 );
512 }
513  
514 1 return $html;
515 }
516  
517 # -----------------------------------------------------------------------------
518  
519 =head2 Widget-Instantiierung
520  
521 =head3 instantiate() - JavaScript-Code, der das Widget instantiiert
522  
523 =head4 Synopsis
524  
525 $javaScript = $e->instantiate;
526  
527 =head4 Description
528  
529 Liefere den JavaScript-Code, der das Widget instantiiert. Alle Parameter
530 werden intern übergeben, dies sind die Attribute:
531  
532 =over 2
533  
534 =item *
535  
536 state
537  
538 =item *
539  
540 action
541  
542 =item *
543  
544 onSuccess
545  
546 =back
547  
548 =cut
549  
550 # -----------------------------------------------------------------------------
551  
552 0 ▶sub instantiate {
Subroutine instantiate: not called CC=4 SCAR=30.0
Pod documented
553 0 my $self = shift;
554  
555 0 my ($action,$id,$onSuccess,$state) = $self->get(qw/action id onSuccess
556 state/);
557  
558 0 my @att;
559 0 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) 0 0 0
560 0 push @att,"state: '$state'";
561 }
562 0 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 0 0 0
563 0 push @att,"action: '$action'";
564 }
565 0 ▶ if ($onSuccess) {
Branch: 1 branch
BranchTrueFalseTotal
if ($onSuccess) 0 0 0
566 0 $onSuccess = Quiq::Unindent->trim($onSuccess);
567 0 $onSuccess =~ s/\n/\n /g;
568 0 push @att,"onSuccess: $onSuccess";
569 }
570  
571 0 return sprintf q|$('#%s').viewEditForm({%s});|,$id,
572 "\n ".join(",\n ",@att)."\n";
573 }
574  
575 # -----------------------------------------------------------------------------
576  
577 =head1 VERSION
578  
579 1.240
580  
581 =head1 AUTHOR
582  
583 Frank Seitz, L<http://fseitz.de/>
584  
585 =head1 COPYRIGHT
586  
587 Copyright (C) 2026 Frank Seitz
588  
589 =head1 LICENSE
590  
591 This code is free software; you can redistribute it and/or modify
592 it under the same terms as Perl itself.
593  
594 =cut
595  
596 # -----------------------------------------------------------------------------
597  
598 1 1;
599  
600 # eof
« Quiq/JavaScript.pm ↑ Summary Quiq/Stacktrace.pm »