.buttons
	button.save-button(disabled) Сохранить
.editor
script(src="/cm/lib/codemirror.js")
script.
	$(function () {
		var b = $('.___id___-app-block').find.bind($('.___id___-app-block'));
		var height = $(window).height() - 120;
		$.get('/cm/lib/codemirror.css', function (content) {
			$('head').append($('<style>' + content + '.CodeMirror-scroll {height: ' + height + 'px; overflow-y: auto; overflow-x: auto;}</style>'));
			os.slot('text.getContent', {
				filename: '#{filename}'
			}, function (content) {
				var editor = CodeMirror(b('.editor')[0], {
					value: content,
					lineNumbers: true,
					autofocus: true,
					viewportMargin: Infinity,
					lineWrapping: true
				});
				editor.getWrapperElement().style.height = height + 'px';
				editor.on('change', function () {
					$('.save-button').removeAttr('disabled');
				});
				b('.save-button').click(function () {
					save();
				});
				window.editor = editor;
				$(document).keydown(function (event) {
					if (!editor.hasFocus()) { return; }
					if (event.which === 83 && event.ctrlKey) {
						event.stopPropagation();
						event.preventDefault();
						b('.save-button').click();
					}
				});
				
				function save() {
					var content = editor.getValue();
					os.slot('text.saveFile', {
						filename: '#{filename}',
						content: content
					}, function () {
						b('.save-button').attr('disabled', 'disabled');
					});
				}
			});
		});		
		
	});