opening attachments in a new window

General discussion of Mantis.

Moderators: Developer, Contributor

didds
Posts: 23
Joined: 18 Jan 2017, 16:24

opening attachments in a new window

Post by didds »

mantisbt 2.5.0
Apache/2.4.6
CentOS Linux release 7.4.1708 (Core)

newly provisioned version of 2.5.0

Image and PDF attachments to a call are opened in the same window if subsequently clicked on, requiring a return to the original call if updates are needed.

is there a simple tweak that will permit opening in a separate window?

(I did search but the only hit I found was for a very old version of Mantis 1.2.3)

cheers

didds
didds
Posts: 23
Joined: 18 Jan 2017, 16:24

Re: opening attachments in a new window

Post by didds »

Having done some more tests...

* a text file gets the option to save or open it (separate dialogue box)
* a png file gets opened in the same window
* a pdf file gets opened in the same window

what is the config_inc.php variable that affects this? It wold make more sense to have all attachments provided with the option to "open or save" rather than just open in a browser window

cheers

didds
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: opening attachments in a new window

Post by atrol »

Copy the following piece of code (defaults from config_defaults_inc.php) to your config_inc.php and adjust to your needs

Code: Select all

/**
 * Specifies the maximum size (in bytes) below which an attachment is
 * previewed in the bug view pages.
 * To disable the previewing of attachments, set max size to 0.
 * @global integer $g_preview_attachments_inline_max_size
 */
$g_preview_attachments_inline_max_size = 256 * 1024;

/**
 * Extensions for text files that can be expanded inline.
 * @global array $g_preview_text_extensions
 */
$g_preview_text_extensions = array(
	'', 'txt', 'diff', 'patch'
);

/**
 * Extensions for images that can be expanded inline.
 * @global array $g_preview_image_extensions
 */
$g_preview_image_extensions = array(
	'bmp', 'png', 'gif', 'jpg', 'jpeg'
);
Please use Search before posting and read the Manual
zoziano
Posts: 1
Joined: 10 Oct 2018, 00:01

Re: opening attachments in a new window

Post by zoziano »

it dosent working for me
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: opening attachments in a new window

Post by cas »

so be more precise, what does not work and actually what happens ?
DanielLeeeee
Posts: 1
Joined: 11 Oct 2018, 08:53

Re: opening attachments in a new window

Post by DanielLeeeee »

have just the same issue
martin.fernau
Posts: 5
Joined: 19 Nov 2019, 10:54

Re: opening attachments in a new window

Post by martin.fernau »

cas wrote: 10 Oct 2018, 09:34 so be more precise, what does not work and actually what happens ?
What the OP means is that attachments like a PDF should be opened in a new Window/Tab instead of overriding the currently opened topic of mantis.
If I read a topic in mantis and see an attached pdf I'll click on it to read the content. Currently it openes in the same window/tab as the currently viewed mantis topic. My intuitive action is to close the window/tab after finally reading the pdf which leads to the problem that the mantis topic itself is closed which wan't my intention.
For me such files should be opened in new window/tab so that I can close the pdf like any other pages and returning to the mantis topic.

I think mantis should just add a target=”_blank” to the attachment links
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: opening attachments in a new window

Post by atrol »

@martin.fernau not sure you are aware that
a) there is a standard functionality of most browsers, where you can hold Ctrl key while clicking a link to open n a new tab and Shift key to open in a new window
b) there is option $g_html_make_links https://mantisbt.org/docs/master/en-US/ ... onfig.html
Please use Search before posting and read the Manual
martin.fernau
Posts: 5
Joined: 19 Nov 2019, 10:54

Re: opening attachments in a new window

Post by martin.fernau »

atrol wrote: 05 Mar 2020, 17:26 @martin.fernau not sure you are aware that
a) there is a standard functionality of most browsers, where you can hold Ctrl key while clicking a link to open n a new tab and Shift key to open in a new window
b) there is option $g_html_make_links https://mantisbt.org/docs/master/en-US/ ... onfig.html
Sure, I'm aware of this functionality and I regularly use it on other websites if I want to open links to other sites into a new tab.
Please try to understand that we're not talking about "links to other sites". We're talking about attachments and thus the $g_html_make_links configuration does not work here (I even set it to $g_html_make_links = LINKS_NEW_WINDOW).
To be even more precise take a look at my screenshot.
If I click an attachment - for me it would be intuitive that this attachment is opened in a new tab and not "over" the currently viewed bug entry. I often get asked by my colleagues if we're able to change the current behavior to let the attachments open in a new tab. This is a confirmation for me that the current behavior "opening attachments in the active tab" isn't really intuitive.
Attachments
Mantis_example.png
Mantis_example.png (310.39 KiB) Viewed 14131 times
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: opening attachments in a new window

Post by atrol »

I understand that this is a good solution for your users and your workflow.
I don't like it that much, as I have opened sometimes quite a lot of tabs, get distracted and later it's good to have a "Back" button.

There is some discussion around PDF display, e.g. search the web for something like "open pdf in new tab"
Using target=“_blank“ is at least questionable, check https://css-tricks.com/use-target_blank/ https://www.media-affin.de/blog/gruende ... rget-blank

As always, if there is not a single right way to do it, we would need a user specific configuration option in Mantis.
If I understand right, the option must also be file type specific, as you might want this behavior for PDF, but not JPG as inline preview of JPG is quite convenient.

Independant from that, there can't be an implementation that works for all.
There are browsers that are
- not able to display PDF themselves, but need an external viewer.
- able to display PDF, but there are configuration options to change it (e.g. display, download, open external viewer)
- able to display PDF, but they don't support your special PDF the right way

Introducing an option that covers all these aspects is something where "normal" users without technical background will struggle in configuring it.
Maybe you have an idea for a simpler approach?

You could
- open a feature request at www.mantisbt.org/bugs, maybe someone in the community (you?) is interested / able in implementing it
- change the original Mantis source the way you need it (if you are using Git, you could maintain your own branch, making it easy to change again when upgrading to a newer version)
- write a plugin that uses JavaScript to tweak the links the way you want it.
- as you seem to use Chrome, you could configure Chrome to open an external viewer
- ...
Please use Search before posting and read the Manual
martin.fernau
Posts: 5
Joined: 19 Nov 2019, 10:54

Re: opening attachments in a new window

Post by martin.fernau »

Thanks for your detailed answer atrol. I understand your point of view in this manner and you're right that having an option of all and everything might be a bit of too much..

I'll see how I can solve this 'problem' for my purposes in a way that don't need a code change of mantis itself.
alex-od
Posts: 2
Joined: 04 Mar 2021, 14:24

Re: opening attachments in a new window

Post by alex-od »

Hi,

If I understand the request correctly then it can be implemented in a couple strings of code:

Please find the

Code: Select all

core/print_api.php
in the mantis directory.

Then please find

Code: Select all

function print_bug_attachment_header( array $p_attachment, $p_security_token ) {
In this function it's possible to add target="_blank" to the attachment links:

Code: Select all

echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
full function code:

Code: Select all

function print_bug_attachment_header( array $p_attachment, $p_security_token ) {
	if( $p_attachment['exists'] ) {
		if( $p_attachment['can_download'] ) {
			echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
		}
		print_file_icon( $p_attachment['display_name'] );
		if( $p_attachment['can_download'] ) {
			echo '</a>';
		}
		echo lang_get( 'word_separator' );
		if( $p_attachment['can_download'] ) {
			echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
		}
		echo string_display_line( $p_attachment['display_name'] );
		if( $p_attachment['can_download'] ) {
			echo '</a>';
		}

		echo lang_get( 'word_separator' ) . '(' . number_format( $p_attachment['size'] ) . lang_get( 'word_separator' ) . lang_get( 'bytes' ) . ')';
		event_signal( 'EVENT_VIEW_BUG_ATTACHMENT', array( $p_attachment ) );
	} else {
		print_file_icon( $p_attachment['display_name'] );
		echo lang_get( 'word_separator' ) . '<s>' . string_display_line( $p_attachment['display_name'] ) . '</s>' . lang_get( 'word_separator' ) . '(' . lang_get( 'attachment_missing' ) . ')';
	}

	if( $p_attachment['can_delete'] ) {
		echo '<a class="noprint red zoom-130 pull-right" href="bug_file_delete.php?file_id=' . $p_attachment['id'] .
			form_security_param( 'bug_file_delete', $p_security_token ) . '">
			<i class="1 ace-icon fa fa-trash-o bigger-115"></i></a>';
	}
}
Of course it would be better to create a special configuration parameter for this, but currently it's just raw and simple solution yet.
mtikuman
Posts: 24
Joined: 18 Nov 2020, 17:02

Re: opening attachments in a new window

Post by mtikuman »

alex-od wrote: 04 Mar 2021, 14:36 Hi,

If I understand the request correctly then it can be implemented in a couple strings of code:

Please find the

Code: Select all

core/print_api.php
in the mantis directory.

Then please find

Code: Select all

function print_bug_attachment_header( array $p_attachment, $p_security_token ) {
In this function it's possible to add target="_blank" to the attachment links:

Code: Select all

echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
full function code:

Code: Select all

function print_bug_attachment_header( array $p_attachment, $p_security_token ) {
	if( $p_attachment['exists'] ) {
		if( $p_attachment['can_download'] ) {
			echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
		}
		print_file_icon( $p_attachment['display_name'] );
		if( $p_attachment['can_download'] ) {
			echo '</a>';
		}
		echo lang_get( 'word_separator' );
		if( $p_attachment['can_download'] ) {
			echo '<a href="' . string_attribute( $p_attachment['download_url'] ) . '" target="_blank">';
		}
		echo string_display_line( $p_attachment['display_name'] );
		if( $p_attachment['can_download'] ) {
			echo '</a>';
		}

		echo lang_get( 'word_separator' ) . '(' . number_format( $p_attachment['size'] ) . lang_get( 'word_separator' ) . lang_get( 'bytes' ) . ')';
		event_signal( 'EVENT_VIEW_BUG_ATTACHMENT', array( $p_attachment ) );
	} else {
		print_file_icon( $p_attachment['display_name'] );
		echo lang_get( 'word_separator' ) . '<s>' . string_display_line( $p_attachment['display_name'] ) . '</s>' . lang_get( 'word_separator' ) . '(' . lang_get( 'attachment_missing' ) . ')';
	}

	if( $p_attachment['can_delete'] ) {
		echo '<a class="noprint red zoom-130 pull-right" href="bug_file_delete.php?file_id=' . $p_attachment['id'] .
			form_security_param( 'bug_file_delete', $p_security_token ) . '">
			<i class="1 ace-icon fa fa-trash-o bigger-115"></i></a>';
	}
}
Of course it would be better to create a special configuration parameter for this, but currently it's just raw and simple solution yet.
Hi,
Thanks for this code.
It works perfectly for images, but for a pdf document it downloads it instead of opening it in a new window.
Is there any way to tweak it to do as desired?
Kind regards.
alex-od
Posts: 2
Joined: 04 Mar 2021, 14:24

Re: opening attachments in a new window

Post by alex-od »

Hi,

I'm not sure but it it looks like the problem is not with the code but with browser settings. For example for me PDF domcuments are opening perfectly in Google Chrome by default but for example DOC files are downloading because there is no way to open them in the browser.

Could you please check if your browser is able to open PDF files?
mtikuman
Posts: 24
Joined: 18 Nov 2020, 17:02

Re: opening attachments in a new window

Post by mtikuman »

alex-od wrote: 18 May 2021, 12:21 Hi,

I'm not sure but it it looks like the problem is not with the code but with browser settings. For example for me PDF documents are opening perfectly in Google Chrome by default but for example DOC files are downloading because there is no way to open them in the browser.

Could you please check if your browser is able to open PDF files?
My browser is able to, but funnily enough after updating Mantis to 2.25.1 and adding the code, it now works. I dunno if my previous install had an issue, but I'm happy enough now.
Thanks!
Post Reply