<!-- Copyright 2008 Orbitz WorldWide

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<head>
 <link rel='stylesheet' type='text/css' href='../content/css/cli.css'>
 <link rel='stylesheet' type='text/css' href='../content/css/default.css'>
 <script src="../content/js/window/prototype.js" type="text/javascript"></script>
 <script src="../content/js/window/effects.js" type="text/javascript"></script>
 <script src="../content/js/window/window.js" type="text/javascript"></script>
 <script src="../content/js/window/window_effects.js" type="text/javascript"></script>
 <script src="../content/js/scriptaculous/scriptaculous.js?load=controls,builder"></script>
 <script src="../content/js/cli.js" type="text/javascript"></script>
</head>

<script type="text/javascript">
var URL = "../cli/eval";
{% if user.is_authenticated %}
var graphitePrompt = "{{user.username}}@graphite>";
{% else %}
var graphitePrompt = "graphite>";
{% endif %}

function sendCommand () {
  var params = Form.serialize('inputForm') + '&_salt=' + (new Date()).valueOf();
  var cmd = $('commandInput').value;
  $('commandInput').value = ""
  $('output').innerHTML += graphitePrompt + cmd + "<br/>";
  if (cmd == '') { return false; }
  $('commandInput').disabled = true;
  $('input').style.visibility = 'hidden';
  var req = new Ajax.Request(URL, {method: 'get', parameters: params, onComplete: handleResponse, onException: handleException});
  return false;
}

function restorePrompt() {
  $('input').style.visibility = 'visible';
  $('commandInput').disabled = false;
  $('commandInput').blur();
  $('commandInput').focus();
  scrollBy(0,1000);
}

function handleResponse (req) {
  eval(req.responseText);
  restorePrompt();
}

function handleException (req, exc) {
  $('output').innerHTML += '<font color="red">AJAX Exception ' + exc.name + ': ' + exc.message + '</font><br/>';
  restorePrompt();
}

function changeImage (win, url) {
  var img_id = win.getId().replace('_win','_img');
  var resizeCode = "var w=Windows.getWindow('" + win.getId() + "');w.setSize(this.width,this.height);"
  var newCenter = Builder.node( 'center', [Builder.node('img', {id: img_id, src: url, onload: resizeCode} )] );
  var content = win.getContent();
  var oldCenter = content.getElementsByTagName('center')[0];
  var oldImage = oldCenter.getElementsByTagName('img')[0];
  oldCenter.removeChild( oldImage );
  content.removeChild( oldCenter );
  content.appendChild( newCenter );
}

resizeObserver = {
  onEndResize: function(eventName, win) {
    var img_id = win.getId().replace('_win','_img');
    var img = $(img_id);
    if (!img) { return; }
    var url = img.src;
    if (url.indexOf('?') == -1) { return; }
    var size = win.getSize();
    if (url.indexOf('&height=') != -1) {
      url = url.replace(/&height=[0-9]+/, '&height=' + size['height']);
    } else {
      url = url + '&height=' + size['height'];
    }
    if (url.indexOf('&width=') != -1) {
      url = url.replace(/&width=[0-9]+/, '&width=' + size['width']);
    } else {
      url = url + '&width=' + size['width'];
    }
    changeImage(win, url);
  }
}
Windows.addObserver(resizeObserver);

</script>

</head>

<body class='mybody' onload="$('commandInput').focus();" onclick="$('commandInput').focus();">

<div id="output"></div>
<div id="input">
 <form id='inputForm' action='null' method='get' onsubmit="sendCommand(); return false">
  {% comment %}
   The line below is ugly as hell but it makes the prompt look better to not have a space after it.
  {% endcomment %}
  {% if user.is_authenticated %}{{user.username}}@graphite&gt;{% else %}graphite&gt;{% endif %}<input type='text' id='commandInput' name='commandInput' class='commandEntry' size='130' autocomplete='off'>
  <div id='completerDiv' class='autocomplete'></div>
  <script type="text/javascript">
    function chooseTarget(element) {
      if (window.completer.drawMode()) {
        window.completer.hide();
        window.completer.active = false;
      } else if (window.completer.historyMode()) {
        var txt = element.childNodes[0].nodeValue;
        $('commandInput').value = txt;
      }
    }
    window.completer = new GraphiteCompleter( 'commandInput',
                                              'completerDiv',
                                              '../cli/autocomplete/',
                                              {
                                                method: 'get',
                                                paramName: 'path',
                                                //updateElement: chooseTarget,
                                                tokens: [',']
                                              }
    );
  </script>
 </form>
</div>

<script type="text/javascript">
if ("{{ command }}") {
  $('commandInput').value = "{{command}}";
  sendCommand();
}
</script>
