| 1 | package HTML::DragAndDrop; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 13 | # Do not simply export all your public functions/methods/constants. | ||
| 14 | |||
| 15 | # This allows declaration use HTML::DragAndDrop ':all'; | ||
| 16 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 17 | # will save memory. | ||
| 18 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 19 | |||
| 20 | ) ] ); | ||
| 21 | |||
| 22 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 23 | |||
| 24 | our @EXPORT = qw( | ||
| 25 | |||
| 26 | ); | ||
| 27 | |||
| 28 | our $VERSION = '0.01'; | ||
| 29 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | our $module = "DragDrop.pm"; | ||
| 31 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 0 | ▶ | use CGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 33 | |||
| 34 | |||
| 35 | # Preloaded methods go here. | ||
| 36 | sub new { | ||
| 37 | my ($class, %arg) = @_; | ||
| 38 | my $self = {}; | ||
| 39 | $self->{javascript_dir} = $arg{javascript_dir} || '/'; | ||
| 40 | $self->{javascript_dir} .= "/" unless ($self->{javascript_dir} =~ m:/$:); | ||
| 41 | $self->{tooltip} = $arg{tooltip}; | ||
| 42 | $self->{dragables} = {}; | ||
| 43 | bless($self, $class); | ||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub add_dragable($%) { | ||
| 48 | my ($self, %args) = @_; | ||
| 49 | $self->{dragables}->{$args{name}} = \%args; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub output_html($) { | ||
| 53 | my ($self) = @_; | ||
| 54 | # The order in which we parse the objects, is not the order in which | ||
| 55 | # we need to output them | ||
| 56 | my $output = ''; | ||
| 57 | my $script_include_output; | ||
| 58 | my $dragables_output; | ||
| 59 | my $javascript = $self->{javascript_dir} . 'wz_dragdrop.js'; | ||
| 60 | $script_include_output .= | ||
| 61 | qq(\n<script type="text/javascript" src="$javascript"></script>\n); | ||
| 62 | |||
| 63 | foreach my $d (keys %{$self->{dragables}}) { | ||
| 64 | |||
| 65 | my $name = $self->{dragables}->{$d}->{name}; | ||
| 66 | my $width = $self->{dragables}->{$d}->{width}; | ||
| 67 | my $height = $self->{dragables}->{$d}->{height}; | ||
| 68 | my $class = $self->{dragables}->{$d}->{class} || 'dragable'; | ||
| 69 | |||
| 70 | my $copy = $self->{dragables}->{$d}->{copy}; | ||
| 71 | $copy = 0 if (!$copy); | ||
| 72 | for (0 .. $copy) { | ||
| 73 | my $left = $self->{dragables}->{$d}->{left}; | ||
| 74 | my $top = $self->{dragables}->{$d}->{top}; | ||
| 75 | my $use_name; | ||
| 76 | if ($_ == 0) { | ||
| 77 | $use_name = $name if ($_ == 0); | ||
| 78 | } else { | ||
| 79 | $use_name = $name . 'div' . $_; | ||
| 80 | } | ||
| 81 | my $tooltip; | ||
| 82 | my $tooltip_output = ''; | ||
| 83 | if ($self->{tooltip}) { | ||
| 84 | $tooltip = $self->{dragables}->{$d}->{tooltip}; | ||
| 85 | if ($tooltip) { | ||
| 86 | $tooltip =~ s/'/\\'/g; | ||
| 87 | $tooltip_output = qq(onmouseover="return escape('$tooltip');"); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | if ($self->{dragables}->{$d}->{src}) { | ||
| 91 | # Image | ||
| 92 | my $src = $self->{dragables}->{$d}->{src}; | ||
| 93 | $dragables_output .= qq(<img name="$use_name" src="$src" width="$width" height="$height" style="position: absolute; left: $left; top: $top;" class="$class" $tooltip_output alt="$use_name" />\n); | ||
| 94 | } elsif ($self->{dragables}->{$d}->{content}) { | ||
| 95 | # A div? | ||
| 96 | my $content = $self->{dragables}->{$d}->{content}; | ||
| 97 | $dragables_output .= qq(<div class="$class" id="${use_name}" style="position: absolute; left: $left; top: $top; width: $width; height: $height;" $tooltip_output>$content</div>\n); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | $output .= $script_include_output; | ||
| 103 | $output .= $dragables_output; | ||
| 104 | return $output; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub output_script($) { | ||
| 108 | my ($self) = @_; | ||
| 109 | my $dragables; | ||
| 110 | my $output = ''; | ||
| 111 | |||
| 112 | foreach my $d (keys %{$self->{dragables}}) { | ||
| 113 | my $name = $self->{dragables}->{$d}->{name}; | ||
| 114 | my $features = $self->{dragables}->{$d}->{features}; | ||
| 115 | $features = '+' . $features unless($features =~ m/^\+/ || $features =~ m/^\s*$/); | ||
| 116 | my $src = $self->{dragables}->{$d}->{src}; | ||
| 117 | my $copy = $self->{dragables}->{$d}->{copy}; | ||
| 118 | my $to_add = qq("$name"); | ||
| 119 | if ($src && $copy) { | ||
| 120 | $to_add .= "+COPY+$copy"; | ||
| 121 | } | ||
| 122 | $to_add .= $features if ($features); | ||
| 123 | $dragables .= $to_add . ','; | ||
| 124 | } | ||
| 125 | $dragables =~ s/,$//; | ||
| 126 | |||
| 127 | if ($dragables) { | ||
| 128 | $output .= qq(<script language="Javascript" type="text/javascript">\n); | ||
| 129 | $output .= "SET_DHTML($dragables);\n"; | ||
| 130 | $output .= qq(</script>\n); | ||
| 131 | |||
| 132 | } | ||
| 133 | if ($self->{tooltip}) { | ||
| 134 | my $javascript = $self->{javascript_dir} . 'wz_tooltip.js'; | ||
| 135 | $output .= qq(<script type="text/javascript" src="$javascript"></script>\n); | ||
| 136 | } | ||
| 137 | return $output; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | __END__ | ||