| 1 | package Bio::Graphics::Browser2::Render::SnapshotManager; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak cluck); | ||
| 6 | use CGI qw(:standard escape start_table end_table); | ||
| 7 | |||
| 8 | use constant JS => '/gbrowse2/js'; | ||
| 9 | use constant ANNOTATION_EDIT_ROWS => 25; | ||
| 10 | use constant ANNOTATION_EDIT_COLS => 100; | ||
| 11 | use constant MAXIMUM_EDITABLE_UPLOAD => 1_000_000; # bytes | ||
| 12 | use constant DEBUG => 0; | ||
| 13 | |||
| 14 | use constant HAVE_SVG => eval "require GD::SVG; 1"; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my $render = shift; | ||
| 19 | return bless {render=>$render}, $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub render { shift->{render} } | ||
| 23 | |||
| 24 | sub render_snapshots_listing { | ||
| 25 | my $self = shift; | ||
| 26 | my $render = shift || $self->render; | ||
| 27 | my $source = $render->data_source->name; | ||
| 28 | my $settings = $render->state; | ||
| 29 | my $snapshots = $render->session->snapshots; | ||
| 30 | my @snapshot_keys = keys %$snapshots; | ||
| 31 | my @sortedSnapshots = sort @snapshot_keys; | ||
| 32 | |||
| 33 | my ($imageURL,$base,$s,$timeStamp,$escapedKey); | ||
| 34 | |||
| 35 | my $buttons = $render->data_source->globals->button_url; | ||
| 36 | my $deleteSnapshotPath = "$buttons/snap_trash.png"; | ||
| 37 | my $sendSnapshotPath = "$buttons/snap_share.png"; | ||
| 38 | my $mailSnapshotPath = "$buttons/snap_mail.png"; | ||
| 39 | my $closeImage = "$buttons/ex.png"; | ||
| 40 | my $nameHeading = $render->translate('SNAPSHOT_FORM'); | ||
| 41 | my $timeStampHeading = $render->translate('TIMESTAMP'); | ||
| 42 | my $url = $render->globals->gbrowse_url(); | ||
| 43 | |||
| 44 | # creating the snapshot banner | ||
| 45 | my $html = div({-id => "Snapshot_banner",}, | ||
| 46 | h1({-id=>'snapshot_section_title'},$render->translate('SNAPSHOT_SELECT')), | ||
| 47 | input({-type => "button", -name => "Save Snapshot", -value => "Save Snapshot", | ||
| 48 | -onClick => '$(\'save_snapshot_2\').show(); $(\'snapshot_name_2\').select();',}), | ||
| 49 | div({-id => 'save_snapshot_2', | ||
| 50 | -style=>'display:none', | ||
| 51 | }, | ||
| 52 | div({ | ||
| 53 | -id=>'snapshot_form_2',}, | ||
| 54 | input({ | ||
| 55 | -type => "text", | ||
| 56 | -name => "snapshot_name", | ||
| 57 | -id => "snapshot_name_2", | ||
| 58 | -style => "width:180px;", | ||
| 59 | -maxlength => "50", | ||
| 60 | -value => $render->translate('SNAPSHOT_FORM'), | ||
| 61 | -onDblclick => "this.value='';" | ||
| 62 | }), | ||
| 63 | input({ -type => "button", | ||
| 64 | -name => "Save", | ||
| 65 | -value => "Save", | ||
| 66 | -onclick => '$(\'save_snapshot_2\').hide(); this.style.zIndex = \'0\'; Controller.saveSnapshot(true);', | ||
| 67 | -style => 'margin-left:35px;',}), | ||
| 68 | input({ -type => "button", | ||
| 69 | -name => "Cancel", | ||
| 70 | -value => "Cancel", | ||
| 71 | -onclick => '$(\'save_snapshot_2\').hide(); this.style.zIndex = \'0\'',}), | ||
| 72 | )), | ||
| 73 | div({-id => "enlarge_image", | ||
| 74 | -style => 'display:none', | ||
| 75 | }, | ||
| 76 | img({-id => 'large_snapshot_close', | ||
| 77 | -onclick=> '$(\'enlarge_image\').hide(); Box.prototype.greyout(false);', -src => "$closeImage"},), | ||
| 78 | img({-id => 'large_snapshot', -width=>'1000', -height => 'auto'}) | ||
| 79 | ) | ||
| 80 | ); | ||
| 81 | |||
| 82 | $html .= div({-id=>'headingRow',-style=>"height:20px;background-color:#F0E68C"}, | ||
| 83 | h1({-style => "left:100px;width:180px"},$nameHeading), | ||
| 84 | h1({-style => "left:550px;width:300px;bottom:30px"},$timeStampHeading), | ||
| 85 | ); | ||
| 86 | |||
| 87 | my $innerHTML = ''; | ||
| 88 | for my $snapshot_name (@sortedSnapshots) { | ||
| 89 | next unless $snapshot_name && $snapshot_name =~ /\S/; | ||
| 90 | |||
| 91 | $timeStamp = $snapshots->{$snapshot_name}{session_time}; | ||
| 92 | $imageURL = $snapshots->{$snapshot_name}{data}{image_url}; | ||
| 93 | ($base,$s) = $render->globals->gbrowse_base; | ||
| 94 | ($escapedKey = $snapshot_name) =~ s/(['"])/\\$1/g; | ||
| 95 | my $readable_name = CGI::unescape($snapshot_name); # ugly, but easier to fix here than where the real bug is | ||
| 96 | |||
| 97 | warn "time = $timeStamp" if DEBUG; | ||
| 98 | |||
| 99 | # Creating the snapshots table with all the snapshot features | ||
| 100 | my $set_snapshot = span({-id=>"set_$snapshot_name"}, input({ -type => "button", | ||
| 101 | -class => "snapshot_button", | ||
| 102 | -name => "set", | ||
| 103 | -value => "Load", | ||
| 104 | -onClick => "Controller.setSnapshot('$escapedKey')", | ||
| 105 | })); | ||
| 106 | my $delete_snapshot = span({-id=>"kill_$snapshot_name"}, img({ -src => $deleteSnapshotPath, | ||
| 107 | -id => "kill", | ||
| 108 | -onClick => "Controller.killSnapshot('$escapedKey')", | ||
| 109 | -title => 'Delete', | ||
| 110 | -class => 'snapshot_icon', | ||
| 111 | })); | ||
| 112 | my $send_snapshot = span({-id=>"send_$snapshot_name"}, img({ -src => $sendSnapshotPath, | ||
| 113 | -id => "send", | ||
| 114 | -onClick => "Controller.sendSnapshot('$escapedKey')", | ||
| 115 | -title => 'Share', | ||
| 116 | -class => 'snapshot_icon', | ||
| 117 | })); | ||
| 118 | |||
| 119 | my $mail_snapshot = $self->render->globals->smtp_enabled ? ( | ||
| 120 | span({-id=>"mail_$snapshot_name"}, img({ -src => $mailSnapshotPath, | ||
| 121 | -id => "mail", | ||
| 122 | -onClick => "\$('mail_snapshot_$escapedKey').style.display='block';" | ||
| 123 | . "Controller.pushForward('$escapedKey')", | ||
| 124 | -title => 'Email', | ||
| 125 | -class => 'snapshot_icon', | ||
| 126 | }))) | ||
| 127 | : ''; | ||
| 128 | |||
| 129 | my $send_snapshot_dialog = div({-id =>"send_snapshot_$escapedKey", | ||
| 130 | -class =>"send_snapshot", | ||
| 131 | -style => 'display:none', | ||
| 132 | }, | ||
| 133 | |||
| 134 | div({-id => "send_snapshot_contents"}, | ||
| 135 | div({-id=>'send_snapshot_box'}, | ||
| 136 | b({-id => "snapshot_header_$escapedKey",},"Send Snapshot: " . (substr $escapedKey, 5) . ""),), | ||
| 137 | |||
| 138 | p({-id => "snapshot_message_$escapedKey", | ||
| 139 | -class=>'snapshot_message'},"To reload this snapshot at any time, use the following link: "), | ||
| 140 | |||
| 141 | textarea({-id => "send_snapshot_url_$escapedKey", -class=>'snapshot_url'},""), | ||
| 142 | input({ -type => "button", | ||
| 143 | -name => "OK", | ||
| 144 | -value => "OK", | ||
| 145 | -onclick => '$(\'' . "send_snapshot_$escapedKey" . '\').hide(); this.style.zIndex = \'0\'',}), | ||
| 146 | )); | ||
| 147 | |||
| 148 | my $mail_snapshot_dialog = div({-id =>"mail_snapshot_$escapedKey", | ||
| 149 | -class =>"mail_snapshot"}, | ||
| 150 | div( | ||
| 151 | {-id => "mail_snapshot_contents"}, | ||
| 152 | div({-class=>'mail_snapshot_box'}, | ||
| 153 | b({-id => "mail_snap_header_$escapedKey",},"Mail Snapshot: " . (substr $escapedKey, 5) . ""),), | ||
| 154 | p({-id => "mail_snap_message_$escapedKey", -style => "font-size: small;"}, | ||
| 155 | "Enter the email of the person you wish to send the snapshot to: ",), | ||
| 156 | input({-type => "text", -id => "email_$escapedKey", -style => 'width:280px;',},"",), | ||
| 157 | input({ -type => "button", | ||
| 158 | -name => "Mail", | ||
| 159 | -value => "Send", | ||
| 160 | -onclick => "Controller.mailSnapshot('$escapedKey')",}), | ||
| 161 | input({ -type => "button", | ||
| 162 | -name => "Cancel", | ||
| 163 | -value => "Cancel", | ||
| 164 | -onclick => '$(\'' . "mail_snapshot_$escapedKey" . '\').hide(); this.style.zIndex = \'0\'',}), | ||
| 165 | )); | ||
| 166 | my $snapshot_image = span({-class=>'snapshot_image_frame'},span({-class => "snapshot_names"}, (substr $readable_name, 5))). | ||
| 167 | span({-class => "timestamps"},$timeStamp, | ||
| 168 | img({-src => $imageURL, -width=>"50",-height=>"30",-class=>'snapshot_image', | ||
| 169 | -onclick => 'Controller.enlarge_image(' . "'${imageURL}'" . '); $(' . "'$escapedKey'" . ').style.zIndex = \'0\';', | ||
| 170 | -onmouseover => "this.style.width='550px'; this.style.height='auto';Controller.pushForward('$escapedKey');this.style.zIndex='1000'", | ||
| 171 | -onmouseout => "this.style.width='50px'; this.style.height='30px';this.style.zIndex='0'"})); | ||
| 172 | |||
| 173 | |||
| 174 | $innerHTML .= | ||
| 175 | div({ | ||
| 176 | -class=>"draggable snapshot_table_entry", | ||
| 177 | -id=>$snapshot_name || 'snapshotname'}, | ||
| 178 | $set_snapshot, | ||
| 179 | $delete_snapshot, | ||
| 180 | $send_snapshot, | ||
| 181 | $mail_snapshot, | ||
| 182 | $send_snapshot_dialog, | ||
| 183 | $mail_snapshot_dialog, | ||
| 184 | $snapshot_image); | ||
| 185 | } | ||
| 186 | $html .= div({-id=>'snapshotTable'},$innerHTML); | ||
| 187 | return $html; | ||
| 188 | } | ||
| 189 | |||
| 190 | # *** Function to render the snapshot title. | ||
| 191 | sub render_title { | ||
| 192 | my $self = shift; | ||
| 193 | my $render = $self->render; | ||
| 194 | my $currentSnapshot = $render->translate('CURRENT_SNAPSHOT'); | ||
| 195 | my $settings = $render->state; | ||
| 196 | |||
| 197 | if ($settings->{snapshot_active}){ | ||
| 198 | return h1({-id=>'snapshot_page_title',-class=>'normal'},""); | ||
| 199 | } else { | ||
| 200 | return h1({-id=>'snapshot_page_title',-class=>'normal', -style=>'display: none;'},"");} | ||
| 201 | } | ||
| 202 | |||
| 203 | sub snapshot_form { | ||
| 204 | my $self = shift; | ||
| 205 | # %% Adding form %% | ||
| 206 | return div({ | ||
| 207 | -id=>'snapshot_form',}, | ||
| 208 | input({-type => "text", | ||
| 209 | -name => "snapshot_name", | ||
| 210 | -id => "snapshot_name", | ||
| 211 | -style => "width:180px", | ||
| 212 | -maxlength => "50", | ||
| 213 | -value => $self->render->translate('SNAPSHOT_FORM'), | ||
| 214 | -onDblclick => "this.value='';", | ||
| 215 | }) | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | |||
| 219 | # *** Render select snapshots. **** | ||
| 220 | sub render_snapshots_section { | ||
| 221 | my $self = shift; | ||
| 222 | my $render = $self->render; | ||
| 223 | my $userdata = $render->user_tracks; | ||
| 224 | my $html = $render->is_admin ? h2({-style=>'font-style:italic;background-color:yellow'}, | ||
| 225 | $render->translate('ADMIN_MODE_WARNING')) | ||
| 226 | : ""; | ||
| 227 | $html .= div({-id => "snapshots_page"}, $self->render_snapshots_listing()); | ||
| 228 | return div({-style => 'margin: 1em;'}, $html); | ||
| 229 | } | ||
| 230 | |||
| 231 | # *** Render the save session button *** | ||
| 232 | sub render_select_saveSession { | ||
| 233 | my $self = shift; | ||
| 234 | my $title = $self->render->translate('SAVE_SNAPSHOT'); | ||
| 235 | return button({-name=>$title, | ||
| 236 | -onClick => '$(\'save_snapshot\').show(); $(\'snapshot_name\').select();', | ||
| 237 | }, | ||
| 238 | ); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub render_select_loadSession { | ||
| 242 | my $self = shift; | ||
| 243 | my $title = $self->render->translate('LOAD_SNAPSHOT'); | ||
| 244 | return button({-name=>$title, | ||
| 245 | -onClick => "Controller.select_tab('snapshots_page');", | ||
| 246 | }, | ||
| 247 | ); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub snapshot_options { | ||
| 251 | my $self = shift; | ||
| 252 | my $snapshot_form = div({-id=>'snapshot_form'},$self->snapshot_form()); | ||
| 253 | my $saveSessionButton = span({-id=>'unsessionbutton'},$self->render_select_saveSession()); | ||
| 254 | my $restoreSessionButton = span({-id=>'loadbutton'}, $self->render_select_loadSession()); | ||
| 255 | my $saveSessionStyle = "position:fixed;left;width:184px;height:50px;background:whitesmoke;z-index:1; border:2px solid gray;display:none; padding: 5px;"; | ||
| 256 | my $save_prompt = div({-id => 'save_snapshot',-style=>"$saveSessionStyle"}, | ||
| 257 | $snapshot_form, | ||
| 258 | input({ -type => "button", | ||
| 259 | -name => "Save", | ||
| 260 | -value => "Save", | ||
| 261 | -onclick => '$(\'save_snapshot\').hide(); this.style.zIndex = \'0\'; Controller.saveSnapshot();'}), | ||
| 262 | input({ -type => "button", | ||
| 263 | -name => "Cancel", | ||
| 264 | -value => "Cancel", | ||
| 265 | -onclick => '$(\'save_snapshot\').hide(); this.style.zIndex = \'0\'',}), | ||
| 266 | ),; | ||
| 267 | |||
| 268 | return div({-id => 'snapshot_options'},$saveSessionButton . $save_prompt . $restoreSessionButton); | ||
| 269 | } | ||
| 270 | |||
| 271 | 1; |