<?xml version='1.0'?>
<!DOCTYPE bindings [
<!ENTITY % settings SYSTEM "/native/fx/login.dtd">
%settings;
]>
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding extends="//toolkit/native/bindings/widget.xml#widget" id="widget">
        <resources>
            <stylesheet src="login.css"/>
        </resources>

        <content>
            <toolbarbutton anonid="login-button" class="chromeclass-toolbar-additional toolbarbutton-1 label2 yauth-button" label="&login.enter.label;" noauth-tooltip-text="&login.tooltiptext;" tooltip="yb-login-tooltip" type="menu-button" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" yb-icon-overlay="true">
                <menupopup anonid="login-menupopup">
                    <menuitem anonid="login-menuitem-switch" label="&login.switch.label;" oncommand="_switchUsers();"/>
                    <menuitem anonid="login-menuitem-logout" label="&login.logout.label;" oncommand="_logoutAll();"/>
                </menupopup>
            </toolbarbutton>
            <tooltip id="yb-login-tooltip" noautohide="true" onpopupshowing="event.stopPropagation(); return _onTooltipShowing(this);" orient="vertical" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"/>
        </content>

        <implementation>
            <field name="ID">this.getAttribute("id")</field>
            <field name="yauth">(this.module.dauth)</field>
            <field name="API">(this.module.API)</field>
            <field name="title">"&login.name;"</field>

            <field name="_button" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","login-button");]]></field>
            <field name="_menu" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","login-menupopup");]]></field>
            <field name="_switch" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","login-menuitem-switch");]]></field>
            <field name="_logout" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","login-menuitem-logout");]]></field>

            <property name="logoutProcess" onget="return this._logoutProcess" onset="this._logoutProcess = val; this._menu.disabled = val; this._button.setAttribute('loading', val);"/>
            <property name="loginProcess" onget="return this._loginProcess" onset="this._loginProcess = val; this._menu.disabled = val; this._button.setAttribute('loading', val);"/>

            <constructor><![CDATA[this.yauth.addListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
this.yauth.addListener("login-state-changed",this);
this._button.setAttribute("yb-authorized",this.yauth.authorized);]]></constructor>

            <method name="destroy">
                <body><![CDATA[this.yauth.removeListener(this.yauth.EVENTS.AUTH_STATE_CHANGED,this);
this.yauth.removeListener("login-state-changed",this);]]></body>
            </method>

            
            <method name="observe">
                <parameter name="subject"/>
                <parameter name="topic"/>
                <parameter name="data"/>
                <body><![CDATA[switch (topic) {
case this.yauth.EVENTS.AUTH_STATE_CHANGED:
if (! this.yauth.authorized && this.logoutProcess)
{
this.logoutProcess = false;
}

this._button.setAttribute("yb-authorized",this.yauth.authorized);
break;
case "login-state-changed":
if (! this.logoutProcess)
{
switch (data.state) {
case this.yauth.authdefs.LOGIN_STATES.REQUEST:
this._menu.disabled = true;
break;
default:
this.loginProcess = false;
break;
}

}

break;
}
]]></body>
            </method>

            <method name="updateMenu">
                <parameter name="menuItems"/>
                <body><![CDATA[var len = this._menu.childNodes.length;
for (let i = len - 3;i >= 0;i--) {
let child = this._menu.childNodes[i];
this._menu.removeChild(child);
}

len = menuItems.length;
for (let i = 0;i < len;i++) {
let child = menuItems[i];
this._menu.insertBefore(child,this._switch);
}
]]></body>
            </method>

            <method name="login">
                <parameter name="strUsername"/>
                <parameter name="strPwd"/>
                <body><![CDATA[this.loginProcess = true;
try {
this.module.login(strUsername,strPwd);
}
catch (ex) {
this.loginProcess = false;
}
]]></body>
            </method>

            <method name="_switchUsers">
                <body><![CDATA[this.module.switchUsers({
target: this},this);]]></body>
            </method>

            <method name="_logoutAll">
                <body><![CDATA[this.module.logoutAll((function () {
this.logoutProcess = true;
}
).bind(this));]]></body>
            </method>

            <method name="_onTooltipShowing">
                <parameter name="tooltip"/>
                <body><![CDATA[if (document.tooltipNode === this._button)
return false;
while (tooltip.firstChild) tooltip.removeChild(tooltip.firstChild);
var labels = this.yauth.users.map(function (u) u.fullName);
if (! labels.length)
labels = [this._button.getAttribute("noauth-tooltip-text")];
labels.forEach(function (labelValue) {
var label = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","label");
label.setAttribute("value",labelValue);
tooltip.appendChild(label);
}
);]]></body>
            </method>
        </implementation>

        <handlers>
            <handler event="click"><![CDATA[if (event.button === 2)
return;
if (event.originalTarget.localName == "toolbarbutton")
this._menu.showPopup();]]></handler>

            <handler event="popupshowing"><![CDATA[var authorized = this.yauth.authorized;
if (authorized)
{
let menu = this.module._buildMenu();
this.updateMenu(menu);
}

if (this._menu.disabled)
return false;
if (event.originalTarget == this._menu && ! authorized)
{
this.module.onButtonClick(event,this);
return false;
}

return true;]]></handler>
        </handlers>
    </binding>
</bindings>
