{% set mod = constant('BB_URL') ~ 'bb-modules/Filemanager' %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <script src="{{mod}}/src/js/jquery.js" type="text/javascript"></script>
  <title>{{ info.basename }}</title>
  <style type="text/css" media="screen">
    body {
        overflow: hidden;
        margin: 0;
        padding: 0;
    }
    .claer { clear: both }
    #editor { 
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0px;
        margin: 0;
        padding: 0;
    }
    #asset-info {
        color: #fff;
        position: fixed;
        top: 10px;
        right: 20px;
        text-shadow: 0px 2px 3px #666;
        background: #000;
        padding: 5px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 5px;
        z-index: 1;
        display: none;
    }
  </style>
</head>

<body>
    
<pre id="editor">{{ file_content|raw }}</pre>
<span id="asset-info"></span>

<script src="{{ mod }}/src/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="{{ mod }}/src/ace/theme-monokai.js" type="text/javascript" charset="utf-8"></script>
<script src="{{ mod }}/src/ace/{{js}}" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){

    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/monokai");

    /*
    editor.getSession().on('change', function(e){
        console.log('changed');
    });
    */
    var openfile = '{{file}}';
    var canon = require('pilot/canon')
	
	canon.addCommand({
	    name: 'myCommand',
	    bindKey: {
	        win: 'Ctrl-S',
	        mac: 'Command-S',
	        sender: 'editor'
	    },
	    exec: function(env, args, request) {
	    	var data = editor.getSession().getValue();
	    	saveFile(openfile,data);
	    }
	});
	
	canon.addCommand({
	    name: 'myCommand',
	    bindKey: {
	        win: 'Ctrl-Shift-S',
	        mac: 'Command-Shift-S',
	        sender: 'editor'
	    },
	    exec: function(env, args, request) {
			var data = editor.getSession().getValue();
	    	saveFile(openfile,data);
	    }
	});
	
	var JavaScriptMode = require("{{mode}}").Mode;
	editor.getSession().setMode(new JavaScriptMode());
	editor.setShowPrintMargin(false);
    
    $('#save-button').click(function(){
        var data = editor.getSession().getValue();
        saveFile(openfile,data);
        return false;
    });
});

function saveFile(path,data){	
    $('#asset-info').text('Saving file').fadeIn();
	$.post('{{ constant("BB_URL_API") }}admin/filemanager/save_file',{path: path, data:data},function(d){
		if(d.result){
			$('#asset-info').text('Your file has been saved.').delay(5000).fadeOut('slow');
		} else {
            $('#asset-info').text('Unable to save file. Check file permissions.').delay(5000).fadeOut('slow');
        }
	});
}

</script>

</body>
</html>