BugMonkey: Running Javascript in the different case modes (view/edit/reply)


Follow
 

If you add javascript to the page and just call it, it will run on every page-load. If you want to limit to just case edit, or assign, or another, use this code:

$(function(){

    // if we're not on the case page, don't do anything
    if (!$('#bugviewContainer').length) return;

    var myFunction = function(sCommand) {
        // you can put a conditional in here if you only want to
        // run your code for certain modes by looking at sCommand.
        // the possible values are:
        // load, view, edit, assign, reply, forward, resolve,
        // close, reactivate, reopen, new

        // on initial page load, sCommand will be 'load'
        // on viewing a case after canceling an AJAXy action, e.g.
        // canceling an edit, sCommand will be 'view'
        // on page load of the new case page, sCommand will be 'new'
    };

    if ($('#sEventEdit').length > 0)
    {
        myFunction('new');
    }
    else if ($('#sEventReply').length > 0)
    {
        myFunction('reply');
    }
    else if ($('#sEventForward').length > 0)
    {
        myFunction('forward');
    }
    else {
        myFunction('load');
    }

    // run it when the view changes and pass in the new view:
    $(window).on('BugViewChange', function(e, data) {
        myFunction(data.sCommand); 
    });

});

To figure out what commands are executing, enter this as the first line inside of myFunction():

console.log(sCommand);

If your code (myFunction) is not executing on some or all types of page-loads, you can add a delay:

$(function(){

    // if we're not on the case page, don't do anything
    if (!$('#bugviewContainer').length) return;

    var myFunction = function(sCommand) {
        // you can put a conditional in here if you only want to
        // run your code for certain modes by looking at sCommand.
        // the possible values are:
        // load, view, edit, assign, reply, forward, resolve,
        // close, reactivate, reopen, new

        // on initial page load, sCommand will be 'load'
        // on viewing a case after canceling an AJAXy action, e.g.
        // canceling an edit, sCommand will be 'view'
        // on page load of the new case page, sCommand will be 'new'

        // run after a delay (in milliseconds)
        setTimeout(doSomeStuff, 100);
    };

    var doSomeStuff = function() {
        // do your work here
    }

    if ($('#sEventEdit').length > 0)
    {
        myFunction('new');
    }
    else if ($('#sEventReply').length > 0)
    {
        myFunction('reply');
    }
    else if ($('#sEventForward').length > 0)
    {
        myFunction('forward');
    }
    else {
        myFunction('load');
    }

    // run it when the view changes and pass in the new view:
    $(window).on('BugViewChange', function(e, data) {
        myFunction(data.sCommand); 
    });

});