Ticket #79 (closed defect: fixed)

Opened 9 months ago

Last modified 9 months ago

javascript needs cleanup in edit_project.tpl.html

Reported by: anonymous Owned by: florian
Priority: minor Milestone: 0.5
Component: other Version:
Keywords: Cc:

Description

the template edit_project_tpl.html contains a javascript function which I don't quite understand, but the code looks like it might be broken. Here is the existing code:

    function setSelCompany() {

    newcpn1 = document.getElementById("newcpn");
    field = document.getElementById('s_cpn_id');
    newval = field.options[field.selectedIndex].value;

    if (newval == -10) {
        document.getElementById('new_cpn').value = 1;
        newcpn1.style.display = 'block';
        document.getElementById("cpn_name").focus();
    } else {
        document.getElementById('new_cpn').value = 0;
        newcpn1.style.display = 'none';
    }
}

Looks like you omitted the "_" from the string "newcpn" in the first line of the function. Put that back and it simplifies things:

    function setSelCompany() {

    newcpn1 = document.getElementById("new_cpn");
    field = document.getElementById('s_cpn_id');
    newval = field.options[field.selectedIndex].value;

    if (newval == -10) {
        newcpn1.value = 1;
        newcpn1.style.display = 'block';
        document.getElementById("cpn_name").focus();
    } else {
        newcpn1.value = 0;
        newcpn1.style.display = 'none';
    }
}

I noticed this because the function is called even when it is not defined, in the event that you are editing an existing project. It should be possible to remove the JS at the end when prj_id is equal to 0 as is done in the first JS declaration:

{if ($prjinfo.prj_id == 0)}
{literal}
<script type="text/javascript">
  setSelCompany();
  document.getElementById("prj_title").focus();
</script>
{/literal}
{/if}

Attachments

Change History

Changed 9 months ago by richcowan

whoops -- that was me

Changed 9 months ago by richcowan

looks like the last JS does something useful so we should just suppress one of the 2 JS lines by wrapping just that:

{if ($prjinfo.prj_id == 0)}
  setSelCompany();
{/if}

Changed 9 months ago by richcowan

best solution: move the call to the JS function setSelCompany();

to line 24 so it falls inside the first conditional block.

Changed 9 months ago by florian

  • status changed from new to closed
  • resolution set to fixed
  • summary changed from javascript needs cleanup in edit_prjoect_tpl.html to javascript needs cleanup in edit_project.tpl.html

"newcpn" is referring to the id of the div element that comes later in the code (line 45). "new_cpn" is referring to the id of the hidden input field (line 30).

The "newcpn" div displays the "new company" form, the "new_cpn" field contains the id of the new company. I admit that the similarity of their name is confusing.

As far as I know, I cannot move setSelCompany() to line 24 because at that time the page has not been rendered yet; the calls within that function would not work consistently across all browsers. Therefore I have moved the function declaration to the bottom of the file, and put setSelCompany() within that block.

I have committed the changes as revision r937.

Changed 9 months ago by richcowan

great! thanks.

Add/Change #79 (javascript needs cleanup in edit_project.tpl.html)

Author



Change Properties
<Author field>
Action
as closed
 
Note: See TracTickets for help on using tickets.