<?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>
   -  Alexander Surkov <surkov@dc.baikal.ru>
   -
   - 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 contains xforms controls implementation for XHTML. All controls
  are inherited from interface bindings realized in xforms.xml.
-->

<!DOCTYPE bindings [
<!ENTITY % xformsDTD SYSTEM "chrome://xforms/locale/xforms.dtd">
  %xformsDTD;
]>

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


  <!-- OUTPUT: <DEFAULT>
    @note - xf:output can support tab navigation
  -->
  <binding id="xformswidget-output"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <html:span anonid="control"
                 class="xf-value"
                 xbl:inherits="tabindex">
        <children/>
      </html:span>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
             __proto__: this.ownerDocument.
               getAnonymousElementByAttribute(this, 'anonid', 'control'),

            set value(val) {
              this.firstChild.data = val ? val : "";
            }
          };
        </body>
      </method>

    </implementation>
  </binding>


  <!-- OUTPUT: <DATE, APPEARANCE='FULL'> -->
  <binding id="xformswidget-output-date-full"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <html:span mozType:calendar="true" anonid="control" readonly="true"/>
      <children/>
    </content>
    <implementation>
      <method name="getControlElement">
        <body>
          return this.ownerDocument.
            getAnonymousElementByAttribute(this, "anonid", "control");
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <MEDIATYPE="image/*", TYPE="xsd:anyURI"> -->
  <binding id="xformswidget-output-mediatype-anyURI"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <html:img class="xf-value" anonid="control"
                xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "control"),

            set value(val) {
              // We have to use a timeout since set value can be hit during the
              // output's constructor.  There are always potential problems
              // changing the DOM during the constructor.  For example, if we
              // don't use a timeout then setting the img's @src won't work on
              // FF 1.5 or FF 2.  So we'll wait a tick before changing it here.
              setTimeout(function(img, val) {img.src = val;}, 0, this, val);
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <MEDIATYPE="image/*", TYPE="xsd:base64Binary"> -->
  <binding id="xformswidget-output-mediatype-base64Binary"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <html:img class="xf-value" anonid="control"
                xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "control"),

            set value(val) {
              // We have to use a timeout since set value can be hit during the
              // output's constructor.  There are always potential problems
              // changing the DOM during the constructor.  For example, if we
              // don't use a timeout then setting the img's @src won't work on
              // FF 1.5 or FF 2.  So we'll wait a tick before changing it here.
              setTimeout(function(img, val) {img.src = "data:[image/*][;base64]," + val;}, 0, this, val);
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <MEDIATYPE="text/html", TYPE="application/xhtml+xml"> -->
  <binding id="xformswidget-output-mediatype-texthtml"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <span xmlns="http://www.w3.org/1999/xhtml"
            class="xf-value" anonid="control"
            xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
        <![CDATA[
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "control"),

            set value(val) {
              this.innerHTML = val;
            }
          };
        ]]>
        </body>
      </method>
    </implementation>
  </binding>


  <!-- LABEL: <DEFAULT>
    @note - xf:label can support tab navigation
  -->
  <binding id="xformswidget-label"
           extends="chrome://xforms/content/xforms.xml#xformswidget-label-base">
    <content>
      <html:span anonid="implicitcontent"
                 xbl:inherits="tabindex"/>
      <html:span anonid="explicitcontent"
                 xbl:inherits="tabindex"><children/></html:span>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            _implicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", 'implicitcontent'),
            _explicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'explicitcontent'),
            __proto__: this,

            set value(val) {
              if (val != null) {
                this._explicitContent.style.display = 'none';
                this._implicitContent.style.removeProperty('display');
                this._implicitContent.textContent = val;
              } else {
                this._explicitContent.style.removeProperty('display');
                this._implicitContent.style.display = 'none';
                this._implicitContent.textContent = '';
              }
            },

            get textValue() {
              if (this._explicitContent.style.display == 'none')
                return this._implicitContent.textContent;
              return this.textContent;
            },
            get nodeValue() {
              var fragment = this.ownerDocument.createDocumentFragment();

              var container = null;
              if (this._explicitContent.style.display == 'none')
                container = this._implicitContent;
              else
                container = this;

              for (var node = container.firstChild; node; node = node.nextSibling) {
                fragment.appendChild(node.cloneNode(true));
              }
              return fragment;
            }
          };
        </body>
      </method>

    </implementation>
  </binding>


  <!-- LABEL: <ACCESKEY SUPPORT> -->
  <binding id="xformswidget-label-accesskey"
           extends="chrome://xforms/content/xforms.xml#xformswidget-label-accesskey-base">
    <content>
      <html:span anonid="implicitcontent"
                 xbl:inherits="tabindex"/>
      <html:span anonid="explicitcontent"
                 xbl:inherits="tabindex"><children/></html:span>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
        <![CDATA[
          return {
            _labelControl: this,
            _implicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'implicitcontent'),
            _explicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'explicitcontent'),

            _ownerDocument: this.ownerDocument,

            set value(val) {
              var textnode = null;

              if (val != null) {
                this._explicitContent.style.display = 'none';
                this._implicitContent.style.display = 'inline';
                this._implicitContent.textContent = val;
                textnode = this._implicitContent.firstChild;
              } else {
                // XXX: How should we handle underlining if label contains
                // non-text nodes (see bug 370483)?
                var underline = true;
                for (var node = this._labelControl.firstChild; node; node = node.nextSibling) {
                  if (node.nodeType == Node.ELEMENT_NODE) {
                    underline = false;
                    break;
                  }
                }
                if (!underline) {
                  this._explicitContent.style.display = 'inline';
                  this._implicitContent.style.display = 'none';
                  this._implicitContent.textContent = '';
                  return;
                }

                this._explicitContent.style.display = 'none';
                this._implicitContent.style.display = 'inline';
                this._implicitContent.textContent = this._labelControl.textContent;
                textnode = this._implicitContent.firstChild;
              }

              var accesskey = this._labelControl.parentNode.getAttribute("accesskey");
              if (accesskey.length == 1 && textnode)
                this._labelControl.formatWithAccesskey(accesskey, textnode);
            }
          };
        ]]>
        </body>
      </method>
    </implementation>
  </binding>


  <!-- TRIGGER: <DEFAULT> -->
  <binding id="xformswidget-trigger"
           extends="chrome://xforms/content/xforms.xml#xformswidget-trigger-base">
    <content>
      <html:button anonid="control"
                   xbl:inherits="tabindex">
        <children/>
      </html:button>
    </content>

    <implementation>
      <method name="getControlElement">
      <body>
        return {
          __proto__: this.ownerDocument.
            getAnonymousElementByAttribute(this, 'anonid', 'control'),

          set disabled(val) {
            if (val) {
              this.setAttribute('disabled', 'disabled');
            } else {
              this.removeAttribute('disabled');
            }
          },
          activate: function triggerControlElmActivate() {
            this.focus();
            this.click();
          }
        };
      </body>
      </method>
    </implementation>

    <handlers>
      <handler event="focus" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusIn");
        }
      </handler>

      <handler event="blur" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusOut");
        }
      </handler>

    </handlers>
  </binding>


  <!-- TRIGGER: MINIMAL -->
  <binding id="xformswidget-trigger-minimal"
            extends="chrome://xforms/content/xforms.xml#xformswidget-trigger-base">
    <content>
      <html:span tabindex="0" anonid="control"
                 xbl:inherits="tabindex">
        <children/>
      </html:span>
    </content>

    <implementation>
      <method name="getControlElement">
      <body>
        return {
          __proto__: this.ownerDocument.
            getAnonymousElementByAttribute(this, 'anonid', 'control'),
          trigger: this,

          set disabled(val) {
            this.isDisabled = val;
          },
          activate: function triggerControlElmActivate(aNoFocus) {
            if (!this.isDisabled) {
              if (!aNoFocus)
                this.trigger.focus();
              this.trigger.dispatchDOMUIEvent("DOMActivate");
            }
          },

          isDisabled: false
        };
      </body>
      </method>

    </implementation>

    <handlers>
      <handler event="click" action="this.control.activate(true);"/>

      <handler event="focus" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusIn");
        }
      </handler>

      <handler event="blur" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusOut");
        }
      </handler>

    </handlers>
  </binding>


  <!-- CASE -->
  <binding id="xformswidget-case"
           extends="chrome://xforms/content/xforms.xml#xformswidget-case-base">
    <content>
      <html:div anonid="container" class="-moz-xforms-case-container">
        <children/>
      </html:div>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'container'),

            set selected(val) {
              this.style.display = val ? "inherit" : "none";
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- ALERT: <DEFAULT> -->
  <binding id="xformswidget-alert"
           extends="chrome://xforms/content/xforms.xml#xformswidget-alert-base">
    <content role="alert">
      <html:div anonid="container" class="-moz-xforms-message-container">
        <html:div anonid="inlineData" style="display: inherit;">
          <children/>
        </html:div>
        <html:div anonid="bindingData" style="display: inherit;"/>
      </html:div>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            inlineData: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "inlineData"),
            bindingData: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "bindingData"),
            container: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "container"),

            setValue: function setValue(aUseInlineValue, aValue) {
              if (aUseInlineValue) {
                this.bindingData.style.display = "none";
                this.inlineData.style.display = "inherit";
              } else {
                this.bindingData.textContent = aValue;
                this.inlineData.style.display = "none";
                this.bindingData.style.display = "inherit";
              }
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- HINT: <DEFAULT> -->
  <binding id="xformswidget-hint"
           extends="#xformswidget-alert">
    <implementation implements="nsIXFormsEphemeralMessageUI">
      <method name="show">
        <parameter name="aPosX"/>
        <parameter name="aPosY"/>
        <body>
          this.control.container.style.visibility = "visible";
          this.control.container.style.display = "inherit";
          this.control.container.style.left = aPosX + "px";
          this.control.container.style.top = aPosY + "px";
        </body>
      </method>

      <method name="hide">
        <body>
          this.control.container.style.left = "0px";
          this.control.container.style.top = "0px";
          this.control.container.style.visibility = "hidden";
          this.control.container.style.display = "none";
        </body>
      </method>
    </implementation>
  </binding>


  <!-- MESSAGE: EPHEMERAL: <DEFAULT> -->
  <binding id="xformswidget-ephemeral-message"
           extends="#xformswidget-hint">

    <implementation>
      <constructor>
        // xforms:message[level="ephemeral"] element should fire accessibility
        // alert event when it appears, so we add role='alert'
        // attribute.
        this.control.container.setAttribute("role", "alert");
      </constructor>
    </implementation>
  </binding>


  <!-- UPLOAD: <DEFAULT> -->
  <binding id="xformswidget-upload"
           extends="chrome://xforms/content/xforms.xml#xformswidget-upload-base">
    <content>
      <children includes="label"/>
      <html:input anonid="text_control" class="xf-value" readonly="readonly"
                  tabindex="-1"/>
      <html:button anonid="browse_button"
                   xbl:inherits="tabindex">
        &xforms.upload.browsetext;
      </html:button>
      <html:button anonid="clear_button"
                   xbl:inherits="tabindex">
        &xforms.upload.cleartext;
      </html:button>
      <children/>
    </content>

    <implementation implements="nsIXFormsUIWidget, nsIXFormsUploadUIElement">
      <method name="getControlElement">
        <body>
          return {
            get value() {
              return this._textControl.value;
            },
            set value(val) {
              this._textControl.value = val;
            },
            set readonly(val) {
              if (val) {
                this._browseButton.setAttribute('disabled', 'disabled');
                this._clearButton.setAttribute('disabled', 'disabled');
              } else {
                this._browseButton.removeAttribute('disabled');
                this._clearButton.removeAttribute('disabled');
              }
            },
            focus: function() {
              this._browseButton.focus();
            },
            activate: function uploadControlElmActivate() {
              this._browseButton.focus();
              this._upload.uploadElement.pickFile();
            },

            _browseButton: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'browse_button'),
            _clearButton: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'clear_button'),
            _textControl: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'text_control'),
            _upload: this
          };
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="click">
        switch (event.originalTarget.getAttribute("anonid")) {
        case "browse_button":
          this.uploadElement.pickFile();
          break;
        case "clear_button":
          this.uploadElement.clearFile();
          break;
        }
      </handler>

      <handler event="keypress" keycode="VK_RETURN">
        switch (event.originalTarget.getAttribute("anonid")) {
        case "browse_button":
        case "clear_button":
          this.dispatchDOMUIEvent('DOMActivate');
          break;
        }
      </handler>
    </handlers>
  </binding>


  <!-- UPLOAD: DISABLED -->
  <binding id="xformswidget-upload-disabled"
           extends="chrome://xforms/content/xforms.xml#xformswidget-upload-disabled-base">
    <content>
      <children includes="label"/>
      <html:input readonly="readonly" class="xf-value"/>
      <html:button disabled="disabled">
        &xforms.upload.browsetext;
      </html:button>
      <html:button disabled="disabled">
        &xforms.upload.cleartext;
      </html:button>
      <children/>
    </content>
  </binding>


  <!-- REPEAT -->
  <binding id="xformswidget-repeat"
           extends="chrome://xforms/content/xforms.xml#xformswidget-repeat-base">
    <content>
      <html:div style="display:none;">
        <children/>
      </html:div>
      <html:div anonid="insertion"/>
    </content>
  </binding>

</bindings>
