How to keep sub menu open if there a item is selected

How to keep sub menu open if there a item is selected

Hi, I am having trouble with the mtree.js plugin. 
I am trying to keep the current parent menu open if a child is selected. however i need it to collapse if another menu item on the same level is selected. which is defined on the following code on line 12.

  1. // mtree.js
  2. // Requires jquery.js and velocity.js (optional but recommended).
  3. // Copy the below function, add to your JS, and simply add a list <ul class=mtree> ... </ul>
  4. ;(function ($, window, document, undefined) {
  5.   
  6.   // Only apply if mtree list exists
  7.   if($('ul.mtree').length) { 
  8.   
  9.     
  10.   // Settings
  11.   var collapsed = true; // Start with collapsed menu (only level 1 items visible)
  12.   var close_same_level = true; // Close elements on same level when opening new node.
  13.   var duration = 200; // Animation duration should be tweaked according to easing.
  14.   var listAnim = true; // Animate separate list items on open/close element (velocity.js only).
  15.   var easing = 'easeOutQuart'; // Velocity.js only, defaults to 'swing' with jquery animation.
  16.     
  17.   
  18.   // Set initial styles 
  19.   $('.mtree ul').css({'overflow':'hidden', 'height': (collapsed) ? 0 : 'auto', 'display': (collapsed) ? 'none' : 'block' });
  20.   
  21.   // Get node elements, and add classes for styling
  22.   var node = $('.mtree li:has(ul)');  
  23.   node.each(function(index, val) {
  24.     $(this).children(':first-child').css('cursor', 'pointer')
  25.     $(this).addClass('mtree-node mtree-' + ((collapsed) ? 'closed' : 'open'));
  26.     $(this).children('ul').addClass('mtree-level-' + ($(this).parentsUntil($('ul.mtree'), 'ul').length + 1));
  27.   });
  28.   
  29.   // Set mtree-active class on list items for last opened element
  30.   $('.mtree li > *:first-child').on('click.mtree-active', function(e){
  31.     if($(this).parent().hasClass('mtree-closed')) {
  32.       $('.mtree-active').not($(this).parent()).removeClass('mtree-active');
  33.       $(this).parent().addClass('mtree-active');
  34.     } else if($(this).parent().hasClass('mtree-open')){
  35.       $(this).parent().removeClass('mtree-active'); 
  36.     } else {
  37.       $('.mtree-active').not($(this).parent()).removeClass('mtree-active');
  38.       $(this).parent().toggleClass('mtree-active'); 
  39.     }
  40.   });

  41.   // Set node click elements, preferably <a> but node links can be <span> also
  42.   node.children(':first-child').on('click.mtree', function(e){
  43.     
  44.     // element vars
  45.     var el = $(this).parent().children('ul').first();
  46.     var isOpen = $(this).parent().hasClass('mtree-open');
  47.     
  48.     // close other elements on same level if opening 
  49.     if((close_same_level || $('.csl').hasClass('active')) && !isOpen) {
  50.       var close_items = $(this).closest('ul').children('.mtree-open').not($(this).parent()).children('ul');
  51.       
  52.       // Velocity.js
  53.       if($.Velocity) {
  54.         close_items.velocity({
  55.           height: 0
  56.         }, {
  57.           duration: duration,
  58.           easing: easing,
  59.           display: 'none',
  60.           delay: 100,
  61.           complete: function(){
  62.             setNodeClass($(this).parent(), true)
  63.           }
  64.         });
  65.         
  66.       // jQuery fallback
  67.       } else {
  68.         close_items.delay(100).slideToggle(duration, function(){
  69.           setNodeClass($(this).parent(), true);
  70.         });
  71.       }
  72.     }
  73.     
  74.     // force auto height of element so actual height can be extracted
  75.     el.css({'height': 'auto'}); 
  76.     
  77.     // listAnim: animate child elements when opening
  78.     if(!isOpen && $.Velocity && listAnim) el.find(' > li, li.mtree-open > ul > li').css({'opacity':0}).velocity('stop').velocity('list');
  79.     
  80.     // Velocity.js animate element
  81.     if($.Velocity) {
  82.       el.velocity('stop').velocity({
  83.         //translateZ: 0, // optional hardware-acceleration is automatic on mobile
  84.         height: isOpen ? [0, el.outerHeight()] : [el.outerHeight(), 0]
  85.       },{
  86.         queue: false,
  87.         duration: duration,
  88.         easing: easing,
  89.         display: isOpen ? 'none' : 'block',
  90.         begin: setNodeClass($(this).parent(), isOpen),
  91.         complete: function(){
  92.           if(!isOpen) $(this).css('height', 'auto');
  93.         }
  94.       });
  95.     
  96.     // jQuery fallback animate element
  97.     } else {
  98.       setNodeClass($(this).parent(), isOpen);
  99.       el.slideToggle(duration);
  100.     }
  101.     
  102.     // We can't have nodes as links unfortunately
  103.     e.preventDefault();
  104.   });
  105.   
  106.   // Function for updating node class
  107.   function setNodeClass(el, isOpen) {
  108.     if(isOpen) {
  109.       el.removeClass('mtree-open').addClass('mtree-closed');
  110.     } else {
  111.       el.removeClass('mtree-closed').addClass('mtree-open');
  112.     }
  113.   }
  114.   
  115.   // List animation sequence
  116.   if($.Velocity && listAnim) {
  117.     $.Velocity.Sequences.list = function (element, options, index, size) {
  118.       $.Velocity.animate(element, { 
  119.         opacity: [1,0],
  120.         translateY: [0, -(index+1)]
  121.       }, {
  122.         delay: index*(duration/size/2),
  123.         duration: duration,
  124.         easing: easing
  125.       });
  126.     };
  127.   }
  128.     
  129.     // Fade in mtree after classes are added.
  130.     // Useful if you have set collapsed = true or applied styles that change the structure so the menu doesn't jump between states after the function executes.
  131.     if($('.mtree').css('opacity') == 0) {
  132.       if($.Velocity) {
  133.         $('.mtree').css('opacity', 1).children().css('opacity', 0).velocity('list');
  134.       } else {
  135.         $('.mtree').show(200);
  136.       }
  137.     }
  138.   }
  139. }(jQuery, this, this.document));

I have shamefully hacked and added this code to almost make it 

  1. $('.current-menu-parent').removeClass('mtree-closed').addClass('mtree-open mtree-active block');
  2.   
  3.   

  4.   $('.current-menu-parent').click(function(){
  5.       $(this).next('.sub-menu').slideUp();
  6.       $(this).toggleClass('block');
  7.       
  8.   });

 
Would be very grateful for any help or advice 

Thanks!