<html>
    <head>
        <meta charset="utf-8">
	<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <style>
			.ui-autocomplete-category {
			font-weight: bold;
			padding: .2em .4em;
			margin: .8em 0 .2em;
			line-height: 1.5;
		}
    
    
    </style>
    </head>

    <body>
	sdsdds
	{% block content %}
	{% endblock %}
	

        
    <!--END-->

<script>
     
$(function() 
{

    
    // global  variable for code base search
    var code_base_view = [];
    var code_base_models = [];
    var code_base_tasks = [];
    var code_base_urls = [];
    var code_base_ajaxcalls = [];
    var code_base_htmlToViews = [];
    var code_base_URLinHTML = [];
    var current_view =''
    
    $.getJSON('code_base_info/',function(data){
		
		$.each(data['views'], function(key,val){	
			$.each(val,function(index,value){
				var dict1 = {};
				dict1['label'] = value;
				dict1['category'] = key;
				code_base_view.push(dict1)
			})
			console.log(code_base_view)
		});
		$.each(data['models'],function(key,val){	
			$.each(val,function(index,value){
				var dict1 = {};
				dict1['label'] = value;
				dict1['category'] = key;
				code_base_models.push(dict1)
			})
		});		
		$.each(data['tasks'], function(key,val){	
			$.each(val,function(index,value){
				var dict1 = {};
				dict1['label'] = value;
				dict1['category'] = key;
				code_base_tasks.push(dict1)
			})
		});
		$.each(data['urls'], function(key,val){	
				var dict1 = {};
				dict1['label'] = val;
				dict1['category'] = 'urls';
				code_base_urls.push(dict1)
		    
		});
		//~ $.each(data['ajaxcalls'], function(key,val){
		    //~ $.each(val,function(index,value){	
				//~ var dict1 = {};
				//~ dict1['label'] = value;
				//~ dict1['category'] = key;
				//~ code_base_ajaxcalls.push(dict1)
		    //~ })
		   //~ 
		//~ });
		$.each(data['views_html_mapper'],function(key,val){
		    
		    $.each(val,function(key2,value){
				var dict1 = {};
				dict1['label'] = value['views']+" - "+ value['html'];
				dict1['category'] = key;
				code_base_htmlToViews.push(dict1)
		    })

		    
		})
		$.each(data['ajaxcalls'],function(key,val){
		    console.log(key,val)
		    $.each(val,function(key2,value){
				console.log(key2,value)
				var dict1 = {};
				dict1['label'] = value['url'];
				dict1['category'] = value['file_name'];
				code_base_ajaxcalls.push(dict1)
		    })
 
		})
		console.log(code_base_ajaxcalls)
    });
	
    $('body').on('click','.find_function',function(){
	    $('#trial').html('')
	    $( "#code_base_search" ).val('')
	    $('.find_function').removeClass('active')
	    $(this).addClass('active');
	    current_view = $(this).attr('arrayname')
	    $("#code_base_search" ).attr('arrayname', $(this).attr('arrayname'))
	    $( "#code_base_search" ).catcomplete({
		    delay: 0,
		    source: eval($("#code_base_search" ).attr('arrayname'))
	    })
	    
    });
    
    
    $('#ajax_calls_search').on('keyup',function(){
	$('#trial2').html('')
	var url = $(this).val()
	
	$.each(code_base_ajaxcalls,function(a,b){
	    
	    $.each(b,function(c,d){
		
		if( url==d ){ 
		    
		    //~ alert()
		    $('#trial2').append('<li>'+code_base_ajaxcalls[a]['category']+'</li>')
		     
		    
		}
		
	    })
	    
	    
	    
	})
	
    })
	//~ console.log(eval($("#code_base_search" ).attr('arrayname')))




   
    $('body').on('click','.model_details',function(){
	    app_name = $(this).attr('app_name')
	    model_name = $(this).attr('model_name')
	  
	  $.ajax({
		    url:'/get/model_details/',
		    type:'POST',
		    data:{
		    app_name:app_name,
		    model_name:model_name
		    
		},
		    success:function(json){
		$('#trial2').html('')
		var list = ''
		$.each(json['code'],function(a,b){
		    //~ console.log(b)
		    list = list + b
		    //~ list = list + '\n'
		    //~ $('#trial2').append(b)
		    //~ $('#trial2').append('<br>')
		    
		    console.log(list)
		    
		})
		//~ list = list.toString()
		$('#trial2').text(list)
		    
		
		
		}  
	      
	      
	  })
	  
      }) 
	
    $.widget( "custom.catcomplete", $.ui.autocomplete, {
	_create: function() {
	  this._super();
	  this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
	},
	_renderMenu: function( ul, items) {
	  var that = this,
	    currentCategory = "";
	  $('#trial').html('')
	  $.each( items, function( index, item ) {
	    
	    var li;
	    if ( item.category != currentCategory ) {
	      //~ ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
	      currentCategory = item.category;
	      $('#trial').append('<li class="ui-autocomplete-category">#'+item.category+'</li>')
	      
	    }
	    
	    //~ li = that._renderItemData( ul, item );
	    if ( item.category ) {
		
	      //~ li.attr( "aria-label", item.category + " : " + item.label );
	      if( $("#code_base_search" ).attr('arrayname') == 'code_base_models' ){
		    $('#trial').append('<li class="model_details" app_name="'+item.category+'" model_name="'+item.label+'" >'+item.label+'</li>')
	      }
	      else{
		  $('#trial').append('<li class="sd">'+item.label+'</li>')
	      }
		
		
	    }
	  });
	}
      });


    })
</script>




</body>
</html>
