Question about common js file (folder :js)

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Question about common js file (folder :js)

Post by Amin »

Hello evreybody,

I would like to understand how the code who exist on common.js is executed ?
How all functions of this file are called ?

I have looked on all support document and also on all php file and no idea

If someone have an idea, I will appreciate his help
Thank you
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Question about common js file (folder :js)

Post by atrol »

This can't be explained in a few sentences.
You have to learn the basics of HTML, CSS, JavaScript and jQuery to understand how it works.

I didn't have a deeper look, maybe this is a useful starting point https://www.w3schools.com/
Please use Search before posting and read the Manual
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Re: Question about common js file (folder :js)

Post by Amin »

Hello Atrol,

I understand a big part of code, and my question is not really that.

I would like to know how this code is executed on mantis, when he is called ?

This file is called common, it's common for all php file or only specefic file ?


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

Re: Question about common js file (folder :js)

Post by atrol »

This code gets executed on all pages where function html_head_javascript is called, so I would say it's executed on nearly all pages.
Please use Search before posting and read the Manual
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Re: Question about common js file (folder :js)

Post by Amin »

So if I add some lines of code on the common js file in order to calculate automaticly the value of a custom field normaly the code it will be excuted on the page where the fields are used..

I know is not the best solution because if update mantis I will loose the modification...
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Question about common js file (folder :js)

Post by atrol »

Instead of changing existing code, you could write a plugin that uses event EVENT_LAYOUT_RESOURCES to include your own JavaScript file.
Please use Search before posting and read the Manual
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Re: Question about common js file (folder :js)

Post by Amin »

Exactly that I want to do later, but before I'm trying to add lines of code directly on the common file. Once my code is working I will try to export it on a plugin.

This is my code I have add it at the buttom of the file after the last bracket

$(document).addEventListener("DOMContentLoaded", function()
{
var f1 = document.getElementById("field1");
f1.addEventListener("blur",function() {
document.getElementById("field2").value=f1.value-5;
});
});

But Nothing is passing, I have no idea which kind of mistake I have do,
I have also created my 2 customs field1 and field2
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Question about common js file (folder :js)

Post by atrol »

Maybe your browser uses the cached version of common.js
Please use Search before posting and read the Manual
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Re: Question about common js file (folder :js)

Post by Amin »

I have deleted cache just now and still Nothing, maybe my code is not correct
Amin
Posts: 26
Joined: 08 Oct 2018, 15:42

Re: Question about common js file (folder :js)

Post by Amin »

I have try like you said to create an plugin using Event_layout_resources event like you see here

I create a folder called calculate_custom_field_value
Inside this folder :
/calculate_custom_field_value.php
/files/calculate.js

******************************************************************calculate_custom_field_value.PHP************************************************
<?php
class calculate_custom_field_valuePlugin extends MantisPlugin {
function register() {

$this->name = ' Calculate Custom field value 2';
$this->description = 'Calculate Custom field value.';
$this->version = '1.0.0';

$this->requires = array(

'MantisCore' => '2.0.0',
);
$this->author = '';
$this->contact = '';
$this->url = '';

}

function hooks() {
return array(
'EVENT_LAYOUT_RESOURCES' => 'resources',
);
}
/***Create the resource link*/
function resources( $p_event ) {
return '<script type="text/javascript" src="' . plugin_file( 'calculate.js' ) . '"></script>';
}
}
**********************************
*************************************calculate.js**********************************************************
$(document).addEventListener("DOMContentLoaded", function()
{
var f1 = document.getElementById("field1");
f1.addEventListener("blur",function() {
document.getElementById("field2").value=f1.value-5;
});
});
-******************************************************************************
I think the plugin structure code is good but I'm not sur that my good are right because it dosen't work
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Question about common js file (folder :js)

Post by atrol »

Don't expect more support from my side.
The time I can invest for Mantis is pretty limited and I focus on supporting users in Mantis related topics.
I don't have that much time to look at / install / debug 3rd party code.

The only general advice I can give is to use the browser developer tools to check if your code is loaded, if there are any JavaScript errors or warnings in browser console, if you are selecting the right element ids, ...
Please use Search before posting and read the Manual
Post Reply