| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::JQuery::ContextMenu::Ajax - Erzeuge Code für ein jQuery Kontext-Menü | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Hash> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | Die Klasse erzeugt Code für ein Kontext-Menü, welches durch das | ||||||||||
| 16 | jQuery-Plugin L<jQuery contextmenu|https://swisnl.github.io/jQuery-contextMenu/docs.html> realisiert wird. Der Inhalt | ||||||||||
| 17 | des Menüs wird durch einen AJAX-Aufruf beschafft. | ||||||||||
| 18 | |||||||||||
| 19 | Der Perl-Code | ||||||||||
| 20 | |||||||||||
| 21 | my $js = Quiq::JQuery::ContextMenu::Ajax->html( | ||||||||||
| 22 | className => 'contextMenu', | ||||||||||
| 23 | selector => '.popup', | ||||||||||
| 24 | trigger => 'left', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | liefert | ||||||||||
| 28 | |||||||||||
| 29 | $.contextMenu({ | ||||||||||
| 30 | className: 'contextMenu', | ||||||||||
| 31 | selector: '.popup', | ||||||||||
| 32 | trigger: 'left', | ||||||||||
| 33 | build: function(ej,ev) { | ||||||||||
| 34 | var options; | ||||||||||
| 35 | |||||||||||
| 36 | $.ajax({ | ||||||||||
| 37 | type: 'GET', | ||||||||||
| 38 | url: ej.attr('href'), | ||||||||||
| 39 | async: false, | ||||||||||
| 40 | beforeSend: function () { | ||||||||||
| 41 | $('body').css('cursor','wait'); | ||||||||||
| 42 | }, | ||||||||||
| 43 | complete: function () { | ||||||||||
| 44 | $('body').css('cursor','default'); | ||||||||||
| 45 | }, | ||||||||||
| 46 | success: function (data,textStatus,jqXHR) { | ||||||||||
| 47 | // Wir bekommen die Items in einem Array geliefert, | ||||||||||
| 48 | // damit die Reihenfolge wohldefiniert ist. Hier | ||||||||||
| 49 | // wandeln wir das Array in ein Objekt, wie | ||||||||||
| 50 | // das ContexMenu-Plugin es erwartet. | ||||||||||
| 51 | var items = {}; | ||||||||||
| 52 | for (var i = 0; i < data.length; i += 2) { | ||||||||||
| 53 | items[data[i]] = data[i+1]; | ||||||||||
| 54 | } | ||||||||||
| 55 | options = { | ||||||||||
| 56 | items: items, | ||||||||||
| 57 | callback: function(key,options) { | ||||||||||
| 58 | var item = items[key]; | ||||||||||
| 59 | if (item.target) | ||||||||||
| 60 | window.open(item.url,item.target); | ||||||||||
| 61 | else | ||||||||||
| 62 | document.location = item.url; | ||||||||||
| 63 | }, | ||||||||||
| 64 | }; | ||||||||||
| 65 | }, | ||||||||||
| 66 | error: function () { | ||||||||||
| 67 | alert('ERROR: AJAX Request failed'); | ||||||||||
| 68 | }, | ||||||||||
| 69 | }); | ||||||||||
| 70 | |||||||||||
| 71 | return options; | ||||||||||
| 72 | }, | ||||||||||
| 73 | }); | ||||||||||
| 74 | |||||||||||
| 75 | Das JavaScript-Array C<data>, das vom Server geliefert wird, hat | ||||||||||
| 76 | den Aufbau | ||||||||||
| 77 | |||||||||||
| 78 | [ | ||||||||||
| 79 | <key>: { | ||||||||||
| 80 | name: '<name>', | ||||||||||
| 81 | url: '<url>', | ||||||||||
| 82 | target: '<target>', | ||||||||||
| 83 | } | ||||||||||
| 84 | ... | ||||||||||
| 85 | ] | ||||||||||
| 86 | |||||||||||
| 87 | Der serverseitige Perl-Code, der eine Menü-Definition liefert (Beispiel): | ||||||||||
| 88 | |||||||||||
| 89 | return [ | ||||||||||
| 90 | sql => { | ||||||||||
| 91 | name => 'SQL', | ||||||||||
| 92 | url => 'sqlFromLog?'.Quiq::Url->queryEncode( | ||||||||||
| 93 | system => $system, | ||||||||||
| 94 | path => $logfile, | ||||||||||
| 95 | ), | ||||||||||
| 96 | }, | ||||||||||
| 97 | ... | ||||||||||
| 98 | ]; | ||||||||||
| 99 | |||||||||||
| 100 | =head1 SEE ALSO | ||||||||||
| 101 | |||||||||||
| 102 | =over 2 | ||||||||||
| 103 | |||||||||||
| 104 | =item * | ||||||||||
| 105 | |||||||||||
| 106 | L<Allgmeine Doku|https://swisnl.github.io/jQuery-contextMenu/docs.html> | ||||||||||
| 107 | |||||||||||
| 108 | =back | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | # ----------------------------------------------------------------------------- | ||||||||||
| 113 | |||||||||||
| 114 | package Quiq::JQuery::ContextMenu::Ajax; | ||||||||||
| 115 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 116 | |||||||||||
| 117 | 1 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 119 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 120 | |||||||||||
| 121 | 1 | our $VERSION = '1.240'; | |||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | use Quiq::Assert; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 1 | ▶ | use Quiq::Json::Code; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | |||||||||||
| 126 | # ----------------------------------------------------------------------------- | ||||||||||
| 127 | |||||||||||
| 128 | =head1 METHODS | ||||||||||
| 129 | |||||||||||
| 130 | =head2 Klassenmethoden | ||||||||||
| 131 | |||||||||||
| 132 | =head3 new() - Konstruktor | ||||||||||
| 133 | |||||||||||
| 134 | =head4 Synopsis | ||||||||||
| 135 | |||||||||||
| 136 | $obj = $class->new(@keyVal); | ||||||||||
| 137 | |||||||||||
| 138 | =head4 Attributes | ||||||||||
| 139 | |||||||||||
| 140 | =over 4 | ||||||||||
| 141 | |||||||||||
| 142 | =item className => $name | ||||||||||
| 143 | |||||||||||
| 144 | Name der CSS-Klasse des Menüs. Kann explizit angegeben werden, wenn das | ||||||||||
| 145 | Menü customized werden soll. Beispiel ($name ist 'contextMenu'): | ||||||||||
| 146 | |||||||||||
| 147 | .contextMenu { | ||||||||||
| 148 | width: 85px !important; | ||||||||||
| 149 | min-width: 50px !important; | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | =item selector => $selector | ||||||||||
| 153 | |||||||||||
| 154 | Der jQuery-Selektor, der die Elemente identifiziert, auf die das | ||||||||||
| 155 | Kontext-Menü gelegt wird. Siehe Plugin-Doku: L<selector|https://swisnl.github.io/jQuery-contextMenu/docs.html#trigger>. | ||||||||||
| 156 | |||||||||||
| 157 | =item trigger => $event | ||||||||||
| 158 | |||||||||||
| 159 | Das Ereignis, durch das das Kontext-Menü angesprochen wird. | ||||||||||
| 160 | Siehe Plugin-Doku: L<tigger>. | ||||||||||
| 161 | |||||||||||
| 162 | =back | ||||||||||
| 163 | |||||||||||
| 164 | =head4 Description | ||||||||||
| 165 | |||||||||||
| 166 | Instantiiere ein Objekt der Klasse und liefere eine Referenz auf dieses | ||||||||||
| 167 | Objekt zurück. | ||||||||||
| 168 | |||||||||||
| 169 | =cut | ||||||||||
| 170 | |||||||||||
| 171 | # ----------------------------------------------------------------------------- | ||||||||||
| 172 | |||||||||||
| 173 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 174 | 1 | my $class = shift; | |||||||||
| 175 | # @_: @keyVal | ||||||||||
| 176 | |||||||||||
| 177 | 1 | my $self = $class->SUPER::new( | |||||||||
| 178 | className => undef, | ||||||||||
| 179 | selector => undef, | ||||||||||
| 180 | trigger => undef, | ||||||||||
| 181 | ); | ||||||||||
| 182 | 1 | $self->set(@_); | |||||||||
| 183 | |||||||||||
| 184 | 1 | return $self; | |||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | # ----------------------------------------------------------------------------- | ||||||||||
| 188 | |||||||||||
| 189 | =head2 Objektmethoden | ||||||||||
| 190 | |||||||||||
| 191 | =head3 js() - Generiere JavaScript-Code | ||||||||||
| 192 | |||||||||||
| 193 | =head4 Synopsis | ||||||||||
| 194 | |||||||||||
| 195 | $js = $obj->%METHOD; | ||||||||||
| 196 | $js = $obj->%METHOD(@keyVal); | ||||||||||
| 197 | |||||||||||
| 198 | =head4 Description | ||||||||||
| 199 | |||||||||||
| 200 | Generiere den JavaScript-Code eines Kontext-Menüs und liefere | ||||||||||
| 201 | diesen zurück. | ||||||||||
| 202 | |||||||||||
| 203 | =cut | ||||||||||
| 204 | |||||||||||
| 205 | # ----------------------------------------------------------------------------- | ||||||||||
| 206 | |||||||||||
| 207 | 1 | ▶ | sub js { | ||||||||
|
Subroutine
js: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 208 | 1 | my $this = shift; | |||||||||
| 209 | # @_: @keyVal | ||||||||||
| 210 | |||||||||||
| 211 | 1 | ▶ | my $self = ref $this? $this: $this->new(@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | |||||||||||
| 213 | # Objektattribute | ||||||||||
| 214 | |||||||||||
| 215 | 1 | my ($className,$selector,$trigger) = | |||||||||
| 216 | $self->get(qw/className selector trigger/); | ||||||||||
| 217 | |||||||||||
| 218 | # Prüfe Attributwerte | ||||||||||
| 219 | |||||||||||
| 220 | 1 | my $a = Quiq::Assert->new; | |||||||||
| 221 | 1 | $a->isNotNull($selector,-name=>'selector'); | |||||||||
| 222 | |||||||||||
| 223 | # JSON-Generator | ||||||||||
| 224 | 1 | my $j = Quiq::Json::Code->new; | |||||||||
| 225 | |||||||||||
| 226 | # Generiere JavaScript-Code | ||||||||||
| 227 | |||||||||||
| 228 | 1 | return '$.contextMenu('.$j->o( | |||||||||
| 229 | className => $className, | ||||||||||
| 230 | selector => $selector, | ||||||||||
| 231 | trigger => $trigger, | ||||||||||
| 232 | build => $j->c(q~ | ||||||||||
| 233 | function(ej,ev) { | ||||||||||
| 234 | var options; | ||||||||||
| 235 | |||||||||||
| 236 | $.ajax({ | ||||||||||
| 237 | type: 'GET', | ||||||||||
| 238 | url: ej.attr('href'), | ||||||||||
| 239 | async: false, | ||||||||||
| 240 | beforeSend: function () { | ||||||||||
| 241 | $('body').css('cursor','wait'); | ||||||||||
| 242 | }, | ||||||||||
| 243 | complete: function () { | ||||||||||
| 244 | $('body').css('cursor','default'); | ||||||||||
| 245 | }, | ||||||||||
| 246 | success: function (data,textStatus,jqXHR) { | ||||||||||
| 247 | // Wir bekommen die Items in einem Array geliefert, | ||||||||||
| 248 | // damit die Reihenfolge wohldefiniert ist. Hier | ||||||||||
| 249 | // wandeln wir das Array in ein Objekt, wie | ||||||||||
| 250 | // das ContexMenu-Plugin es erwartet. | ||||||||||
| 251 | var items = {}; | ||||||||||
| 252 | for (var i = 0; i < data.length; i += 2) { | ||||||||||
| 253 | items[data[i]] = data[i+1]; | ||||||||||
| 254 | } | ||||||||||
| 255 | options = { | ||||||||||
| 256 | items: items, | ||||||||||
| 257 | callback: function(key,options) { | ||||||||||
| 258 | var item = items[key]; | ||||||||||
| 259 | if (item.target) | ||||||||||
| 260 | window.open(item.url,item.target); | ||||||||||
| 261 | else | ||||||||||
| 262 | document.location = item.url; | ||||||||||
| 263 | }, | ||||||||||
| 264 | }; | ||||||||||
| 265 | }, | ||||||||||
| 266 | error: function () { | ||||||||||
| 267 | alert('ERROR: AJAX Request failed'); | ||||||||||
| 268 | }, | ||||||||||
| 269 | }); | ||||||||||
| 270 | |||||||||||
| 271 | return options; | ||||||||||
| 272 | } | ||||||||||
| 273 | ~), | ||||||||||
| 274 | ).');' | ||||||||||
| 275 | } | ||||||||||
| 276 | |||||||||||
| 277 | # ----------------------------------------------------------------------------- | ||||||||||
| 278 | |||||||||||
| 279 | =head1 VERSION | ||||||||||
| 280 | |||||||||||
| 281 | 1.240 | ||||||||||
| 282 | |||||||||||
| 283 | =head1 AUTHOR | ||||||||||
| 284 | |||||||||||
| 285 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 286 | |||||||||||
| 287 | =head1 COPYRIGHT | ||||||||||
| 288 | |||||||||||
| 289 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 290 | |||||||||||
| 291 | =head1 LICENSE | ||||||||||
| 292 | |||||||||||
| 293 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 294 | it under the same terms as Perl itself. | ||||||||||
| 295 | |||||||||||
| 296 | =cut | ||||||||||
| 297 | |||||||||||
| 298 | # ----------------------------------------------------------------------------- | ||||||||||
| 299 | |||||||||||
| 300 | 1 | 1; | |||||||||
| 301 | |||||||||||
| 302 | # eof | ||||||||||