<?xml version="1.0"?>

<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (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.mozilla.org/MPL/
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is Mozilla XForms support.
   -
   - The Initial Developer of the Original Code is
   - Novell, Inc.
   - Portions created by the Initial Developer are Copyright (C) 2005
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -  Allan Beaufour <abeaufour@novell.com>
   -  Olli Pettay <Olli.Pettay@helsinki.fi>
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the GPL or the LGPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->

<!--
  This file implements the "abstract" UI classes for XForms controls. They
  all have "pure virtual" functions that they expect to be implemented by
  concrete application and returned in the getElementControl() call. An example
  is the controls for XHTML in xforms-xhtml.xml.
-->

<bindings id="xformsBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:xforms="http://www.w3.org/2002/xforms"
          xmlns:mozType="http://www.mozilla.org/projects/xforms/2005/type"
          xmlns:lazy="http://www.mozilla.org/projects/xforms/2005/lazy">


  <!-- LAZY INSTANCE MODEL -->
  <binding id="xforms-lazy-instance">
    <content><xforms:instance lazy:lazy="true"/><children/></content>
  </binding>


  <!-- BASE BINDINGS
    The next bindings are used as a base for xforms controls implementations.
  -->

  <!-- The binding provides utils interface and interface for anonymous content
    work.
  -->
  <binding id="xformswidget-general">
    <implementation>

    <!-- utils interface -->
      <property name="XFORMS_NS" readonly="true">
        <getter>
          return "http://www.w3.org/2002/xforms";
        </getter>
      </property>

      <property name="MOZTYPE_NS" readonly="true">
        <getter>
          return "http://www.mozilla.org/projects/xforms/2005/type";
        </getter>
      </property>

      <property name="XHTML_NS" readonly="true"
                onget="return 'http://www.w3.org/1999/xhtml';"/>

      <!-- Dispatch UI Event to the control itself -->
      <method name="dispatchDOMUIEvent">
        <parameter name="aType"/>
        <body>
          var ev = document.createEvent("UIEvents");
          ev.initUIEvent(aType, true, true, window, 1);
          this.dispatchEvent(ev);
          return true;
        </body>
      </method>

      <!--
        Dispatch an _XForms notification event_ to a node.
        See http://www.w3.org/TR/xforms/slice4.html#evt-notify
      -->
      <method name="dispatchXFormsNotificationEvent">
        <parameter name="aEventName"/>
        <parameter name="aTarget"/>
        <body>
          var ev = document.createEvent("Events");
          ev.initEvent(aEventName, true, false);
          aTarget.dispatchEvent(ev);
          return true;
        </body>
      </method>

    <!-- interface for anonymous content work -->

      <!--
        Return the native control widget that is the object with access for
        visual representation of this xforms control.
      -->
      <property name="control" readonly="true">
        <getter>
          if (!this._control)
            this._control = this.getControlElement();
          return this._control;
        </getter>
      </property>

      <!--
        Each base binding for xforms widget assumes successors bindings will
        override the method.
      -->
      <method name="getControlElement">
        <body>
          return null;
        </body>
      </method>

    <!-- private -->
      <destructor>
        this._control = null;
      </destructor>

      <field name="_control">null</field>
    </implementation>
  </binding>


  <!-- Base binding is for xforms controls that are able to bind to instance
    data.
  -->
  <binding id="xformswidget-accessors" extends="#xformswidget-general">
    <implementation>
    <!-- interface -->
      <property name="accessors" readonly="true">
        <getter>
          <![CDATA[
          if (!this._accessors && this.delegate)
            this._accessors = this.delegate.getXFormsAccessors();
          return this._accessors;
          ]]>
        </getter>
      </property>

      <property name="stringValue" readonly="true">
        <getter>
          var value = this.accessors.getValue();
          return value != null ? value : "";
        </getter>
      </property>

    <!-- private -->
      <constructor>
        if (!this.hasAttributeNS(this.MOZTYPE_NS, "deferredrefresh"))
          this.delegate.widgetAttached();
      </constructor>

      <destructor>
        this._delegate = null;
        this._accessors = null;
      </destructor>

      <property name="delegate" readonly="true">
        <getter>
          if (!this._delegate) {
            this._delegate =
              this.QueryInterface(Components.interfaces.nsIXFormsDelegate);
          }
          return this._delegate;
        </getter>
      </property>

      <field name="_delegate">null</field>
      <field name="_accessors">null</field>
    </implementation>
  </binding>


  <!--
    Base binding is for xforms controls that implement nsIXFormsUIWidget
    interface.
  -->
  <binding id="xformswidget-base" extends="#xformswidget-accessors">
    <implementation implements="nsIXFormsUIWidget">

    <!-- nsIXFormsUIWidget interface
      Following methods are called from XTF part of XForms implementation and
      thease methods should be overrided by successors bindings.
    -->

      <!-- Method is called when bound node was changed. -->
      <method name="refresh">
        <body>
          return true;
        </body>
      </method>

      <!-- Method is called when control should be disabled. -->
      <method name="disable">
        <parameter name="aDisable"/>
        <body>
          return true;
        </body>
      </method>

      <!-- Method is called when control should be focused -->
      <method name="focus">
        <body>
          return false;
        </body>
      </method>

      <!-- Method is called when accesskey associated with the control is
        activated. -->
      <method name="performAccesskey">
        <body>
          this.focus();
        </body>
      </method>

      <!-- Method is called to get current value of xforms element.

        Here the method is implemented for those xforms elements that can't be
        used to change the bound instance value.  For example, xforms:output.
        The rest of the xforms elements should override this implementation.
        For example, xforms:trigger returns empty string since it actually
        hasn't value even if it has a bound.
      -->
      <method name="getCurrentValue">
        <body>
          return this.accessors.getValue();
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <DEFAULT>
    The output widget assumes successors bindings implement getElementControl()
    method what returns the object:
    {
      set value(); // set "string" value
    }
  -->
  <binding id="xformswidget-output-base"
           extends="chrome://xforms/content/xforms.xml#xformswidget-base">
    <implementation implements="nsIXFormsUIWidget, nsIAccessibleProvider">
      <property name="accessibleType" readonly="true">
        <getter>
          return Components.interfaces.nsIAccessibleProvider.XFormsOuput;
        </getter>
      </property>

      <method name="refresh">
        <body>
          this.control.value = this.stringValue;
          return true;
        </body>
      </method>

      <constructor>
        this.setMediatype();
      </constructor>

      <method name="setMediatype">
        <body>
          if (this.hasAttribute("mediatype")) {
            this.setAttributeNS(this.MOZTYPE_NS, "mediatype",
                                this.getAttribute("mediatype"));
          }
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="DOMAttrModified">
      <![CDATA[
        if (event.attrName == "mediatype" &&
            event.relatedNode.namespaceURI == "") {
          this.setMediatype();
        }
      ]]>
      </handler>
    </handlers>
  </binding>


  <!-- LABEL: <DEFAULT>
    The label widget assumes successors bindings implement getElementControl()
    method what returns the object:
    {
      set value(); // set "string" value
      get textValue() // return text representing label value
      get nodeValue() // return document fragment representing label value
    }
  -->
  <binding id="xformswidget-label-base"
           extends="chrome://xforms/content/xforms.xml#xformswidget-base">

    <implementation implements="nsIXFormsUIWidget, nsIAccessibleProvider">
      <property name="accessibleType" readonly="true">
        <getter>
          return Components.interfaces.nsIAccessibleProvider.XFormsLabel;
        </getter>
      </property>

      <method name="refresh">
        <body>
          // the label control hides/unhides anonymous content based on the
          // the value we set it to.  The control assumes that if the value
          // comes from a binding or linking attribute that the value we are
          // setting it to will be null
          var externalValue = this.hasAttribute("ref") ||
                              this.hasAttribute("bind") ||
                              this.hasAttribute("src");

          if (externalValue) {
            this.control.value = this.accessors.getValue();
          } else {
            this.control.value = null;
          }
          return true;
        </body>
      </method>

      <property name="textValue" readonly="true"
                onget="return this.control.textValue;"/>

      <property name="nodeValue" readonly="true"
                onget="return this.control.nodeValue;"/>

      <!-- Advance focus to parent form control -->
      <method name="advanceFocusToParent">
        <body>
          var parent = this.parentNode;
          if (parent.namespaceURI != this.XFORMS_NS)
            return;

          switch (parent.localName) {
            case "input":
              // XXX: probably we should check base type (see a bug 316691)
              switch (parent.getAttribute("type")) {
                case "http://www.w3.org/2001/XMLSchema#boolean":
                  var value = parent.accessors.getValue();
                  if (value == "true" || value == "1") {
                    parent.accessors.setValue("false");
                  } else {
                    parent.accessors.setValue("true");
                  }
              }
            case "secret":
            case "textarea":
            case "output":
            case "upload":
            case "range":
            case "trigger":
            case "submit":
            case "select":
            case "select1":
              parent.focus();
              break;
          }
        </body>
      </method>

    </implementation>

    <handlers>
      <handler event="click">
        this.advanceFocusToParent();
      </handler>
    </handlers>
  </binding>


  <!-- LABEL: <ACCESKEY SUPPORT>
    The label widget assumes successors bindings implement getElementControl()
    method what returns the object:
    {
      set value(); // set "string" value
    }
  -->
  <binding id="xformswidget-label-accesskey-base"
           extends="#xformswidget-label-base">

    <implementation>
      <method name="formatWithAccesskey">
        <parameter name="aAccesskey"/>
        <parameter name="aTextNode"/>
        <body>
          var text = aTextNode.nodeValue;
          var location = text.indexOf(aAccesskey);
          var span = this.ownerDocument.createElementNS(this.XHTML_NS, "span");

          if (location > -1) {
            // We create a range around the character we want and surround
            // it with an html:span.
            var range = this.ownerDocument.createRange();
            range.setStart(aTextNode, location);
            range.setEnd(aTextNode, location + 1);

            span.setAttribute("class", "xf-accesskey-inline");
            range.surroundContents(span);
          } else {
            // If we didn't find the accesskey, append it to the end.
            span.setAttribute("class", "xf-accesskey-appended");
            span.textContent = aAccesskey;
            if (aTextNode.nextSibling) {
              aTextNode.parentNode.insertBefore(span,
                                                aTextNode.nextSibling);
            } else {
              aTextNode.parentNode.appendChild(span);
            }
          }
        </body>
      </method>
    </implementation>
  </binding>


  <!-- TRIGGER: <DEFAULT>
    The trigger widget assumes successors bindings implement getElementControl()
    method what returns the object:
    {
      set disabled(); // set disabled state
      focus(); // set focus
      activate(); // activates trigger
    }
  -->
  <binding id="xformswidget-trigger-base"
           extends="chrome://xforms/content/xforms.xml#xformswidget-base">

    <implementation implements="nsIXFormsUIWidget, nsIAccessibleProvider">
      <property name="accessibleType" readonly="true">
        <getter>
          return Components.interfaces.nsIAccessibleProvider.XFormsTrigger;
        </getter>
      </property>

      <method name="disable">
        <parameter name="aDisable"/>
        <body>
          this.control.disabled = aDisable;
        </body>
      </method>

      <method name="focus">
        <body>
          this.control.focus();
          return true;
        </body>
      </method>

      <method name="performAccesskey">
        <body>
          this.control.activate();
        </body>
      </method>

      <method name="getCurrentValue">
        <body>
          return "";
        </body>
      </method>
    </implementation>
  </binding>


  <!-- TRIGGER: MINIMAL
    We don't need in any special base binding for trigger[appearance="minimal"]
    widget. All successors bindings should be extended from base binding for
    default trigger widget.
  -->


  <!-- CASE: <DEFAULT> -->
  <binding id="xformswidget-case-base" extends="#xformswidget-general">
    <implementation implements="nsIXFormsCaseUIElement">
      <constructor>
        this.QueryInterface(Components.interfaces.nsIXFormsCaseElement).
          widgetAttached();
      </constructor>

      <property name="control" readonly="true">
        <getter>
          if (!this._control)
            this._control = this.getControlElement();
          return this._control;
        </getter>
      </property>

      <method name="caseSelected">
        <parameter name="aEnable"/>
        <body>
          this.control.selected = aEnable;
          return true;
        </body>
      </method>
    </implementation>
  </binding>


  <!-- ALERT: <DEFAULT> -->
  <binding id="xformswidget-alert-base"
           extends="chrome://xforms/content/xforms.xml#xformswidget-base">

    <implementation>
      <method name="refresh">
        <body>
        <![CDATA[
          var useInlineValue = !this.accessors.hasBoundNode() &&
            !this.hasAttribute('src');
          this.control.setValue(useInlineValue, this.stringValue);
        ]]></body>
      </method>
    </implementation>
  </binding>


  <!-- UPLOAD: <DEFAULT>
    The upload widget assumes successors bindings implement getElementControl()
    method what returns the object:
    {
      set value(); // set "string" value
      set readonly(); // set readonly state
      focus(); // set focus
    }
  -->
  <binding id="xformswidget-upload-base"
           extends="#xformswidget-base">

    <implementation implements="nsIXFormsUploadUIElement, nsIAccessibleProvider">
      <property name="accessibleType" readonly="true">
        <getter>
          return Components.interfaces.nsIAccessibleProvider.XFormsContainer;
        </getter>
      </property>

      <method name="refresh">
        <body>
          this.control.readonly = this.accessors.isReadonly();
        </body>
      </method>

      <method name="focus">
        <body>
          this.control.focus();
          return true;
        </body>
      </method>

      <method name="performAccesskey">
        <body>
          this.control.activate();
        </body>
      </method>

      <method name="getCurrentValue">
        <body>
          return this.control.value;
        </body>
      </method>

      <property name="uploadElement" readonly="true">
        <getter>
          if (!this._uploadElement) {
            this._uploadElement =
              this.QueryInterface(Components.interfaces.nsIXFormsUploadElement);
          }
          return this._uploadElement;
        </getter>
      </property>
      <field name="_uploadElement">null</field>

      <method name="setFieldText">
        <parameter name="aString"/>
        <body>
          this.control.value = aString;
        </body>
      </method>
    </implementation>
  </binding>


  <!-- UPLOAD: DISABLED -->
  <binding id="xformswidget-upload-disabled-base"
           extends="#xformswidget-upload-base">
    <implementation>
      <method name="getControlElement">
        <body>
          return {
            get value(){ return ""; },
            set value(val){},
            set readonly(val){},
            focus: function(){}
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- REPEAT -->
  <binding id="xformswidget-repeat-base" extends="#xformswidget-accessors">

    <implementation implements="nsIXFormsRepeatUIElement">
      <property name="anonymousRepeatContent" readonly="true">
        <getter>
          if (!this._anonymousRepeatContent) {
            this._anonymousRepeatContent =
              document.getAnonymousElementByAttribute(this, "anonid", "insertion");
          }
          return this._anonymousRepeatContent;

        </getter>
      </property>
      <field name="_anonymousRepeatContent">null</field>
    </implementation>
  </binding>

  <!-- ATTRIBUTE-BASED REPEAT -->
  <binding id="xformswidget-attr-repeat">
    <content>
      <xforms:repeat xbl:inherits="model=xforms:repeat-model,bind=xforms:repeat-bind,nodeset=xforms:repeat-nodeset,startindex=xforms:repeat-startindex,number=xforms:repeat-number"
                     mozType:attrBased="true">
        <children/>
      </xforms:repeat>
    </content>
  </binding>


  <!-- MEDIATYPE -->
  <binding id="xformswidget-mediatype" extends="#xformswidget-base">
    <implementation implements="nsIXFormsUIWidget">
      <method name="refresh">
        <body>
          this.setMediatype();
        </body>
      </method>

      <method name="setMediatype">
        <body>
        <![CDATA[
          var parent = this.parentNode;
          if (parent.namespaceURI == this.XFORMS_NS &&
              parent.localName == "output" &&
              !parent.hasAttribute("mediatype")) {
            parent.setAttributeNS(this.MOZTYPE_NS, "mediatype",
                                  this.stringValue);
          }
        ]]>
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="DOMCharacterDataModified">
        this.setMediatype();
      </handler>
    </handlers>
  </binding>

</bindings>
