
<public:component tagName="codearea" literalContent="true" urn="q42:components:code-area" supportsEditMode="true">
  <public:defaults viewlinkContent="true" viewInheritStyle="false" canHaveHTML="false" tabStop="true" />
  
  <public:attach event="oncontentready" onevent="setTimeout(initComponent, 0);" />
  <public:attach event="onkeydown" onevent="componentKeyPress();" />
  <public:attach event="onfocus" onevent="componentFocus();" />
  
  <public:property name="formattedCode" get="getFormattedCode" />
  <public:property name="value" get="getCode" put="setCode" />
  <public:property name="selection" get="getSelection" />
  <public:property name="readOnly" get="getReadOnly" put="setReadOnly" />
  
  <!-- misschien moeten deze straks weg; als we onze subscribeOnKey al hebben? -->
  <public:event name="onkeydown" id="pekeydown" />
  <public:event name="onkeyup" id="pekeyup" />
  <public:event name="onkeypress" id="pekeypress" />
  
  <public:event name="onload" id="peload" />
  <public:event name="onactionupdate" id="peactionupdate" />
  
  <public:method name="subscribeOnKey" />

  <public:method name="createCodeRange" />
  <public:method name="getLineByNumber" />
  <public:method name="findNext" />
  <public:method name="findPrevious" />
  <public:method name="findAll" />
  
  <public:method name="getAction" />
  <public:method name="createAction" />

  <script language="javascript">
    var comp, w;
var loaded = false;
var initCode;
var initReadOnly;

/*
  CodeRange createCodeRange();
  
  Creates a new CodeRange object.
  
  Public method
*/
function createCodeRange()
{
  return new w.CodeRange();
};

function getLineByNumber(number)
{  
  return w.codeArea.getLineByNumber(number);
};

function findNext(s, flags)
{
  return w.codeArea.findNext(s, flags);
};

function findPrevious(s, flags)
{
  return w.codeArea.findPrevious(s, flags);
};

function findAll(s, flags)
{
  return w.codeArea.findAll(s, flags);
};

function subscribeOnKey(condition, callback)
{
  return w.codeArea.subscribeOnKey(condition, callback);
};

function getAction(name)
{
  return w.actions[name];
};

function createAction(_perform, options)
{
  return w.codeArea.createAction(_perform, options);
};

/*
  string getFormattedCode();
  
  Returns a string containing the code, with HTML formatting.
  
  Used by the .formattedCode property [read-only]
*/
function getFormattedCode()
{
  return loaded ? w.codeArea.canvas.innerHTML : element.innerHTML;
};

/*
  string getCode();
  
  Returns a string containing the code.
  
  Used by the .code property.
*/
function getCode()
{
  return loaded ? w.codeArea.getValue() : initCode;
};

/*
  void getCode(
    string value
  );
  
  Sets a string containing new code.
  
  Used by the .code property.
*/
function setCode(value)
{
  if (!loaded)
    initCode = value;
  else
    w.codeArea.setValue(value);
};

function getReadOnly()
{
  return loaded ? w.codeArea.getReadOnly() : initReadOnly;
}

function setReadOnly(value)
{
  if (!loaded)
    initReadOnly = value;
  else
    w.codeArea.setReadOnly(value);
}

/*
  CodeRange getSelection();
  
  weet nog niet wat voor object hieruit komt..
*/
function getSelection()
{
  //return loaded && w.selection.range;
  return loaded && w.codeArea.getSelection();
};

/*
  void initComponent();
  
  Initialization code for this component instance.
  Triggered by the oncontentready event.
*/
function initComponent()
{
  // Check whether iframe element has a contentWindow
  // if there are other HTC components loading the contentWindow of the iframe is of type 'unknown'
  // we should wait until it is present
  if (document.body.getElementsByTagName('iframe')[0] && (typeof(document.body.getElementsByTagName('iframe')[0].contentWindow) == 'unknown'))
  {
    setTimeout(initComponent, 0);
    return;
  }

  w = document.body.firstChild.contentWindow;
  
  w.element = element;
  w.parentDoc = document;

  if (typeof(initCode) == "undefined")
    initCode = element.innerHTML;
  
  if (typeof(initReadOnly) == "undefined")
    initReadOnly = element.readOnly;
  
  if (initCode.substring(0, 2) == '\r\n' || initCode.substring(0, 2) == '\n\r')
      initCode = initCode.substring(2);
  else if (initCode.substring(0, 1) == '\n' || initCode.substring(0, 1) == '\r')
      initCode == initCode.substring(1);

  if (w.notifyLoaded)
    // iframe init was before this
    notifyLoaded();
  else
    // iframe init has not yet run
    w.notifyLoaded = notifyLoaded;
  
  element.ownerDocument.body.attachEvent('onkeydown', function()
  {
//    window.status = ('keydown ' + event.keyCode + '  -  ' + (new Date)*1);
  });
};

function notifyLoaded()
{
  loaded = true;
  w.codeArea.setValue(initCode);
  w.codeArea.setReadOnly(initReadOnly);
  
  w.codeArea.fireEvent("load");
}

function componentKeyPress()
{
};

function componentFocus()
{
  if (!w)
    return;
  w.document.body.setActive();
  w.document.focus();
};


/*

  CodeRange Interface:
  
  .select()
  .mark(string class)
  .unmark(string class)
  
  .text/.code
  .htmlText/.formattedCode
  
  .boundingLeft/Top/Width/Height
  
  .collapse()
  .compareEndPoints()
  .duplicate()
  .execCommand()
  .expand()
  
  
*/
  </script>

</public:component>

<body>
  <iframe tabindex="-1" src="codearea.iframe.html" style="border:none;width:100%;height:100%"></iframe>
</body>

