<commit_before>
import api.Program;
import haxe.remoting.HttpAsyncConnection;
import js.Browser;
import js.codemirror.CodeMirror;
import js.JQuery;

using js.bootstrap.Button;
using Lambda;
using StringTools;

class Editor {

	var cnx : HttpAsyncConnection;
	
	var program : Program;
	var output : Output;
	
	var gateway : String;
	
	var form : JQuery;
	var haxeSource : CodeMirror;
	var jsSource : CodeMirror;
	var runner : JQuery;
	var messages : JQuery;
	var compileBtn : JQuery;
  var libs : JQuery;
  var targets : JQuery;
  var stage : JQuery;
  var jsTab : JQuery;

  var markers : Array<MarkedText>;
  var lineHandles : Array<LineHandle>;

  var completions : Array<String>;
  var completionIndex : Int;

	public function new(){
    markers = [];
    lineHandles = [];

		CodeMirror.commands.autocomplete = autocomplete;
    CodeMirror.commands.compile = function(_) compile();
    CodeMirror.commands.togglefullscreen = toggleFullscreenSource;

  	haxeSource = CodeMirror.fromTextArea( cast new JQuery("textarea[name='hx-source']")[0] , {
			mode : "haxe",
			//theme : "default",
			lineWrapping : true,
			lineNumbers : true,
			extraKeys : {
				"Ctrl-Space" : "autocomplete",
        "Ctrl-Enter" : "compile",
        "F8" : "compile",
        "F5" : "compile",
        "F11" : "togglefullscreen"
			},
      onChange : onChange
		} );

   
		jsSource = CodeMirror.fromTextArea( cast new JQuery("textarea[name='js-source']")[0] , {
			mode : "javascript",
			//theme : "default",
			lineWrapping : true,
			lineNumbers : true,
			readOnly : true
		} );
		
		runner = new JQuery("iframe[name='js-run']");
		messages = new JQuery(".messages");
		compileBtn = new JQuery(".compile-btn");
    libs = new JQuery("#hx-options-form .hx-libs");
    targets = new JQuery("#hx-options-form .hx-targets");
    stage = new JQuery(".js-output .js-canvas");
    jsTab = new JQuery("a[href='#js-source']");

    new JQuery(".link-btn").bind("click", function(e){
      var _this = new JQuery(e.target);
      if( _this.attr('href') == "#" ){
        e.preventDefault();
      }
    });

    new JQuery(".fullscreen-btn").bind("click" , toggleFullscreenRunner);
      
		new JQuery("body").bind("keyup", onKey );

		new JQuery("a[data-toggle='tab']").bind( "shown", function(e){
			jsSource.refresh();
      haxeSource.refresh();
		});

    targets.delegate("input[name='target']" , "change" , onTarget );
		
		compileBtn.bind( "click" , compile );

		var apiRoot = new JQuery("body").data("api");
		cnx = HttpAsyncConnection.urlConnect(apiRoot+"/compiler");

    program = {
      uid : null,
      main : {
        name : "Test",
        source : haxeSource.getValue()
      },
      target : SWF( "test", 11 ),
      libs : new Array()
    };

    initLibs();

    setTarget( api.Program.Target.JS( "test" ) );

		var uid = Browser.window.location.hash;
		if (uid.length > 0){
      uid = uid.substr(1);
  		cnx.Compiler.getProgram.call([uid], onProgram);
    }
  }

  function fullscreen(){
     untyped __js__("var el = window.document.documentElement;
            var rfs = el.requestFullScreen
                || el.webkitRequestFullScreen
                || el.mozRequestFullScreen;
              rfs.call(el); ");
    
  }

  function toggleFullscreenRunner(e : JqEvent){
    var _this = new JQuery(e.target);
    e.preventDefault();
    if( _this.attr('href') != "#" ){
      new JQuery("body").addClass("fullscreen-runner");
      fullscreen();
    }
  }

  function toggleFullscreenSource(_){
    new JQuery("body").toggleClass("fullscreen-source");
    haxeSource.refresh();
    fullscreen();
  }

  function onTarget(e : JqEvent){
    var cb = new JQuery( e.target );
    var name = cb.val();
    var target = switch( name ){
      case "SWF" : api.Program.Target.SWF('test',11);
      case _ : api.Program.Target.JS('test');
    }
    setTarget(target);
  }

  function setTarget( target : api.Program.Target ){
    program.target = target;
    libs.find(".controls").hide();
    
    var sel :String;
    
    switch( target ){
      case JS(_): 
        sel = "JS";
        jsTab.fadeIn();

      case SWF(_,_) : 
        sel = "SWF";
        jsTab.hide();
    }

    var radio = new JQuery( 'input[name=\'target\'][value=\'$sel\']' );
    radio.attr( "checked" ,"checked" );

    libs.find("."+sel+"-libs").fadeIn();
  }

  function initLibs(){
    for( t in Type.getEnumConstructs(api.Program.Target) ){
      var el = libs.find("."+t+"-libs");
      var libs : Array<Libs.LibConf> = Libs.getLibsConfig(t);
      var def : Array<String> = Libs.getDefaultLibs(t);
	  if (def == null) def = [];
      for( l in libs ){

        el.append(
            '<label class="checkbox"><input class="lib" type="checkbox" value="${l.name}"' 
          + (Lambda.has(def, l.name) ? "checked='checked'" : "") 
          + ' /> ${l.name}'
          + "<span class='help-inline'><a href='" + (l.help == null ? "http://lib.haxe.org/p/" + l.name : l.help) 
          + "' target='_blank'><i class='icon-question-sign'></i></a></span>"
          + "</label>"
          );
    
      }
    }
  }

	//function onProgram(p:{p:Program, o:Output})<commit_msg>minor<commit_after>
import api.Program;
import haxe.remoting.HttpAsyncConnection;
import js.Browser;
import js.codemirror.CodeMirror;
import js.JQuery;

using js.bootstrap.Button;
using Lambda;
using StringTools;

class Editor {

	var cnx : HttpAsyncConnection;
	
	var program : Program;
	var output : Output;
	
	var gateway : String;
	
	var form : JQuery;
	var haxeSource : CodeMirror;
	var jsSource : CodeMirror;
	var runner : JQuery;
	var messages : JQuery;
	var compileBtn : JQuery;
  var libs : JQuery;
  var targets : JQuery;
  var stage : JQuery;
  var jsTab : JQuery;

  var markers : Array<MarkedText>;
  var lineHandles : Array<LineHandle>;

  var completions : Array<String>;
  var completionIndex : Int;

	public function new(){
    markers = [];
    lineHandles = [];

		CodeMirror.commands.autocomplete = autocomplete;
    CodeMirror.commands.compile = function(_) compile();
    CodeMirror.commands.togglefullscreen = toggleFullscreenSource;

  	haxeSource = CodeMirror.fromTextArea( cast new JQuery("textarea[name='hx-source']")[0] , {
			mode : "haxe",
			//theme : "default",
			lineWrapping : true,
			lineNumbers : true,
			extraKeys : {
				"Ctrl-Space" : "autocomplete",
        "Ctrl-Enter" : "compile",
        "F8" : "compile",
        "F5" : "compile",
        "F11" : "togglefullscreen"
			},
      onChange : onChange
		} );

   
		jsSource = CodeMirror.fromTextArea( cast new JQuery("textarea[name='js-source']")[0] , {
			mode : "javascript",
			//theme : "default",
			lineWrapping : true,
			lineNumbers : true,
			readOnly : true
		} );
		
		runner = new JQuery("iframe[name='js-run']");
		messages = new JQuery(".messages");
		compileBtn = new JQuery(".compile-btn");
    libs = new JQuery("#hx-options-form .hx-libs");
    targets = new JQuery("#hx-options-form .hx-targets");
    stage = new JQuery(".js-output .js-canvas");
    jsTab = new JQuery("a[href='#js-source']");

    new JQuery(".link-btn").bind("click", function(e){
      var _this = new JQuery(e.target);
      if( _this.attr('href') == "#" ){
        e.preventDefault();
      }
    });

    new JQuery(".fullscreen-btn").bind("click" , toggleFullscreenRunner);
      
		new JQuery("body").bind("keyup", onKey );

		new JQuery("a[data-toggle='tab']").bind( "shown", function(e){
			jsSource.refresh();
      haxeSource.refresh();
		});

    targets.delegate("input[name='target']" , "change" , onTarget );
		
		compileBtn.bind( "click" , compile );

		var apiRoot = new JQuery("body").data("api");
		cnx = HttpAsyncConnection.urlConnect(apiRoot+"/compiler");

    program = {
      uid : null,
      main : {
        name : "Test",
        source : haxeSource.getValue()
      },
      target : SWF( "test", 11 ),
      libs : new Array()
    };

    initLibs();

    setTarget( api.Program.Target.JS( "test" ) );

		var uid = Browser.window.location.hash;
		if (uid.length > 0){
      uid = uid.substr(1);
  		cnx.Compiler.getProgram.call([uid], onProgram);
    }
  }

  function fullscreen(){
     untyped __js__("var el = window.document.documentElement;
            var rfs = el.requestFullScreen
                || el.webkitRequestFullScreen
                || el.mozRequestFullScreen;
              rfs.call(el); ");
    
  }

  function toggleFullscreenRunner(e : JqEvent){
    var _this = new JQuery(e.target);
    e.preventDefault();
    if( _this.attr('href') != "#" ){
      new JQuery("body").addClass("fullscreen-runner");
      fullscreen();
    }
  }

  function toggleFullscreenSource(_){
    new JQuery("body").toggleClass("fullscreen-source");
    haxeSource.refresh();
    fullscreen();
  }

  function onTarget(e : JqEvent){
    var cb = new JQuery( e.target );
    var name = cb.val();
    var target = switch( name ){
      case "SWF" : api.Program.Target.SWF('test',11);
      case _ : api.Program.Target.JS('test');
    }
    setTarget(target);
  }

  function setTarget( target : api.Program.Target ){
    program.target = target;
    libs.find(".controls").hide();
    
    var sel :String = Type.enumConstructor(target);
    
    switch( target ){
      case JS(_): 
        jsTab.fadeIn();

      case SWF(_,_) : 
        jsTab.hide();
    }

    var radio = new JQuery( 'input[name=\'target\'][value=\'$sel\']' );
    radio.attr( "checked" ,"checked" );

    libs.find("."+sel+"-libs").fadeIn();
  }

  function initLibs(){
    for( t in Type.getEnumConstructs(api.Program.Target) ){
      var el = libs.find("."+t+"-libs");
      var libs : Array<Libs.LibConf> = Libs.getLibsConfig(t);
      var def : Array<String> = Libs.getDefaultLibs(t);
	  if (def == null) def = [];
      for( l in libs ){

        el.append(
            '<label class="checkbox"><input class="lib" type="checkbox" value="${l.name}"' 
          + (Lambda.has(def, l.name) ? "checked='checked'" : "") 
          + ' /> ${l.name}'
          + "<span class='help-inline'><a href='" + (l.help == null ? "http://lib.haxe.org/p/" + l.name : l.help) 
          + "' target='_blank'><i class='icon-question-sign'></i></a></span>"
          + "</label>"
          );
    
      }
    }
  }

	//function onProgram(p:{p:Program, o:Output})
{'fraction_non_alphanumeric': 0.16981132075471697, 'fraction_numerical': 0.002334176230305388, 'mean_word_length': 2.7447195921340133, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6365291', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3376, 'n_words': 686}