import Component from 'can-component';
import DefineMap from 'can-define/map/';
import view from './donejs-xterm.stache';

import './donejs-xterm.less';
import 'xterm/dist/xterm.css';
import Terminal from 'xterm/dist/xterm';
import 'xterm/dist/addons/fit/';

export const Xterm = DefineMap.extend({
  fit: 'string',
  prompt: 'string',
  api: {
    type: '*',
    value: function () {
      return new Terminal();
    }
  }
});

export default Component.extend({
  tag: 'donejs-xterm',
  ViewModel: Xterm,
  view,
  events: {
    inserted(el) {
      if (System.isPlatform("window")) {
        var vm = this.viewModel;
        var api = vm.api;
        api.open(el.childNodes[0], false);
        if (vm.fit) {
          api.fit();
        }

        api.prompt = function () {
          api.write('\r\n' + vm.prompt);
        };
        api.write(vm.prompt);

        api.on('key', function (key, ev) {
          var printable = (
            !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
          );

          if (ev.keyCode === 13) {
            api.prompt();
          } else if (ev.keyCode === 8) {
            // Do not delete the prompt
            if (api.x > vm.prompt.length) {
              api.write('\b \b');
            }
          } else if (printable) {
            api.write(key);
          }
        });
      }
    }
  }
});

@@ -331,19 +331,76 @@
 ing',%0A  
-api
+doFit: function() %7B%0A    this.terminal.fit();%0A  %7D,%0A  terminal
 : %7B%0A    
@@ -685,21 +685,32 @@
 var 
-api = vm.api;
+terminal = vm.terminal;%0A
 %0A   
@@ -714,19 +714,24 @@
         
-api
+terminal
 .open(el
@@ -754,16 +754,17 @@
 false);%0A
+%0A
         
@@ -791,13 +791,14 @@
     
-api.f
+vm.doF
 it()
@@ -814,27 +814,32 @@
  %7D%0A%0A        
-api
+terminal
 .prompt = fu
@@ -852,35 +852,40 @@
  () %7B%0A          
-api
+terminal
 .write('%5Cr%5Cn' + 
@@ -911,27 +911,32 @@
  %7D;%0A        
-api
+terminal
 .write(vm.pr
@@ -951,19 +951,24 @@
         
-api
+terminal
 .on('key
@@ -1152,19 +1152,24 @@
         
-api
+terminal
 .prompt(
@@ -1268,19 +1268,24 @@
     if (
-api
+terminal
 .x %3E vm.
@@ -1311,27 +1311,32 @@
             
-api
+terminal
 .write('%5Cb %5C
@@ -1404,11 +1404,16 @@
     
-api
+terminal
 .wri

Add doFit function