<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bindings SYSTEM "/native/fx/pagetranslator.dtd"><bindings xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <binding id="button">
        <resources>
            <stylesheet src="container.css"/>
        </resources>

        <content>
            <xul:label>&pagetranslator.translate;</xul:label>
        </content>

        <implementation implements="nsIObserver, nsIDOMEventListener">
            <field name="api" readonly="true">
                this.module.api
            </field>

            <field name="mWindowMediator" readonly="true">
                Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
            </field>

            <field name="locale" readonly="true">
                this.api.Environment.addon.locale
            </field>

            <field name="translator" readonly="true">
                this.module.translator
            </field>

            <field name="isShowing">
                false
            </field>

            <field name="MAX_SHOW_TOOLTIP" readonly="true">
                5
            </field>

            <property name="showTooltipSetting">
                <getter>return this.api.Settings.getValue("show-tooltip");</getter>
                <setter>return this.api.Settings.setValue("show-tooltip",val);</setter>
            </property>

            <property name="isPageSecure">
                <getter><![CDATA[var doc = gBrowser.mCurrentBrowser.contentDocument;
if (doc.location.protocol === "https:")
return true;
return false;]]></getter>
            </property>

            <field name="wListener" readonly="true">
                this.api.Browser.getWindowListener(window)
            </field>

            <constructor><![CDATA[this.init();]]></constructor>

            <desctructor><![CDATA[this.finalize();]]></desctructor>

            <method name="init">
                <body><![CDATA[this.wListener.addListener("PageShow",this);
this.wListener.addListener("PageStateStart",this);
gBrowser.tabContainer.addEventListener("TabSelect",this,false);
gBrowser.addEventListener("PageTranslator_onReset",this,false);]]></body>
            </method>

            <method name="finalize">
                <body><![CDATA[this.wListener.removeListener("PageShow",this);
this.wListener.removeListener("PageStateStart",this);
gBrowser.tabContainer.removeEventListener("TabSelect",this,false);
gBrowser.removeEventListener("PageTranslator_onReset",this,false);
for (let i = 0, length = gBrowser.browsers.length;i < length;i++) {
let browser = gBrowser.browsers[i];
this._removeNotification(browser);
this._removeTooltip(browser);
}

;]]></body>
            </method>

            
            <method name="observe">
                <parameter name="aSubject"/>
                <parameter name="aTopic"/>
                <parameter name="aData"/>
                <body><![CDATA[switch (aTopic) {
case "PageShow":
this.removePageTranslatorData(aData.tab);
this._checkState();
break;
case "PageStateStart":
this.removePageTranslatorData(aData.tab);
this.hide();
this._removeNotification();
break;
default:
break;
}
]]></body>
            </method>

            <method name="handleEvent">
                <parameter name="aEvent"/>
                <body><![CDATA[switch (aEvent.type) {
case "TabSelect":
setTimeout(function (me) me._checkState(),0,this);
break;
case "PageTranslator_onReset":
this._checkState();
break;
default:
break;
}
]]></body>
            </method>

            <method name="translate">
                <parameter name="lang"/>
                <body><![CDATA[if (this.isPageSecure)
{
return;
}

this._setTranslatedView();
this.setPageTranslatorData(null,true);
var pageTranslatorData = this.getPageTranslatorData();
var doc = gBrowser.mCurrentBrowser.contentDocument;
lang = lang || pageTranslatorData.lang;
var notification = this._createNotification(lang,gBrowser.mCurrentBrowser);
this.translator.translate(doc,lang);]]></body>
            </method>

            <method name="_checkState">
                <body><![CDATA[if (this.isPageSecure)
{
this.hide();
this._removeNotification();
return;
}

var href = gBrowser.mCurrentBrowser.contentDocument.location.href;
if (href.indexOf("yafd:tabs") === 0)
{
this.hide();
this._removeNotification();
return;
}

var pageTranslatorData = this.getPageTranslatorData();
var lang = pageTranslatorData.lang;
if (! pageTranslatorData.isDetermined)
{
let doc = gBrowser.mCurrentBrowser.contentDocument;
lang = this.translator.detectLang(doc);
this.setPageTranslatorData(lang,pageTranslatorData.isActive);
pageTranslatorData.isDetermined = true;
}

if (pageTranslatorData.isActive)
{
this._setTranslatedView();
this.show();
}
else
{
this._unsetTranslatedView();
this._removeNotification();
if (! lang || this.translator.matchLang(lang) || ! this.translator.getLangPair(lang,this.translator.language))
{
this.hide();
}
else
{
this.show();
if (+ this.showTooltipSetting < this.MAX_SHOW_TOOLTIP)
{
this._createTooltip();
}

;
}

}
]]></body>
            </method>

            <method name="setPageTranslatorData">
                <parameter name="lang"/>
                <parameter name="isActive"/>
                <parameter name="tab"/>
                <body><![CDATA[tab = tab || gBrowser.mCurrentBrowser;
var pageTranslatorData = this.wListener.getTabData(tab,"pageTranslatorData");
if (lang)
pageTranslatorData.lang = lang;
pageTranslatorData.isActive = isActive;]]></body>
            </method>

            <method name="getPageTranslatorData">
                <parameter name="tab"/>
                <body><![CDATA[tab = tab || gBrowser.mCurrentBrowser;
return this.wListener.getTabData(tab,"pageTranslatorData");]]></body>
            </method>

            <method name="removePageTranslatorData">
                <parameter name="tab"/>
                <body><![CDATA[tab = tab || gBrowser.mCurrentBrowser;
this.wListener.removeTabData(tab,"pageTranslatorData");]]></body>
            </method>

            <method name="show">
                <body><![CDATA[this.style.setProperty("display","-moz-box","");
this.isShowing = true;]]></body>
            </method>

            <method name="hide">
                <body><![CDATA[this.style.setProperty("display","none","");
this.isShowing = false;
this._removeTooltip();]]></body>
            </method>

            <method name="revert">
                <body><![CDATA[var doc = gBrowser.mCurrentBrowser.contentDocument;
var pageTranslatorData = this.getPageTranslatorData();
this.setPageTranslatorData(null,false);
this.translator.revert(doc);
this._checkState();]]></body>
            </method>

            <method name="_setTranslatedView">
                <body><![CDATA[this.setAttribute("translated",true);]]></body>
            </method>

            <method name="_unsetTranslatedView">
                <body><![CDATA[this.removeAttribute("translated");]]></body>
            </method>

            <method name="_createNotification">
                <parameter name="lang"/>
                <parameter name="tab"/>
                <body><![CDATA[var notificationBox = gBrowser.getNotificationBox(tab);
var notification = notificationBox.getNotificationWithValue("pagetranslator-notification");
if (! notification)
{
notification = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","notification");
notificationBox.appendNotification("","pagetranslator-notification","",notificationBox.PRIORITY_INFO_MEDIUM,[]);
notification = notificationBox.getNotificationWithValue("pagetranslator-notification");
notification.setAttribute("lang",lang);
notification.setAttribute("anonid","pagetranslator-notification");
notification.browser = tab || gBrowser.mCurrentBrowser;
}
else
{
notificationBox._showNotification(notification,true);
}

notification.pageTranslator = this;
return notification;]]></body>
            </method>

            <method name="_removeNotification">
                <parameter name="tab"/>
                <body><![CDATA[var notificationBox = gBrowser.getNotificationBox(tab);
var notification = notificationBox.getNotificationWithValue("pagetranslator-notification");
if (notification)
{
notification.remove(true);
}
]]>
                </body>
            </method>

            <method name="_createTooltip">
                <body><![CDATA[var notificationBox = gBrowser.getNotificationBox();
var notification = notificationBox.getNotificationWithValue("pagetranslator-tooltip");
if (! notification)
{
notificationBox.appendNotification("","pagetranslator-tooltip","",notificationBox.PRIORITY_INFO_HIGH,[]);
notification = notificationBox.getNotificationWithValue("pagetranslator-tooltip");
notification.setAttribute("anonid","pagetranslator-tooltip");
notification.remove = this._removeAllTooltips.bind(this);
this.showTooltipSetting++;
}
else
{
notificationBox._showNotification(notification,true);
}

;
return notification;]]></body>
            </method>

            <method name="_removeTooltip">
                <parameter name="browser"/>
                <body><![CDATA[browser = browser || gBrowser.mCurrentBrowser;
var tabbrowser = browser.ownerDocument.defaultView.gBrowser;
var notificationBox = tabbrowser.getNotificationBox(browser);
var notification = notificationBox.getNotificationWithValue("pagetranslator-tooltip");
if (notification)
{
notificationBox.removeNotification(notification,true);
}

;]]></body>
            </method>

            <method name="_removeAllTooltips">
                <body><![CDATA[var enumerator = this.mWindowMediator.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let tabbrowser = enumerator.getNext().gBrowser;
tabbrowser.browsers.forEach(function (browser) {
this._removeTooltip(browser);
}
,this);
}

this.showTooltipSetting = "disabled";]]></body>
            </method>

        </implementation>

        <handlers>
            <handler button="0" event="click"><![CDATA[var pageTranslatorData = this.getPageTranslatorData();
if (pageTranslatorData.isActive)
{
this.revert();
}
else
{
this.translate();
this.api.Statistics.logCustomAction(1);
this._removeAllTooltips();
}
]]></handler>
        </handlers>
    </binding>

</bindings>
