<?xml version='1.0'?>
<!DOCTYPE bindings [
<!ENTITY % settings SYSTEM "/native/fx/opinions.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="opinions.css"/>
        </resources>

        <content>
            <toolbarbutton anonid="opinions-button" class="chromeclass-toolbar-additional toolbarbutton-1 label2" label="&opinions.name;" tooltip="opinions-tooltip" type="menu-button" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" yb-icon-overlay="false">
                <menupopup anonid="opinions-menupopup">
                    <menuitem disabled="true" label="&opinions.refreshData.label;" yb-widget-command="refreshData"/>
                    <menuseparator/>
                    <menuitem disabled="true" label="&opinions.link.showCommnets.label;" yb-widget-command="gotoComments"/>
                    <menuseparator/>
                    <menuitem disabled="true" label="&opinions.link.addComment.label;" yb-widget-command="gotoAddComment"/>
                </menupopup>
            </toolbarbutton>
            <tooltip disabled="true" id="opinions-tooltip" noautohide="true" orient="vertical" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"/>
        </content>

        <implementation implements="nsIObserver">
            <field name="API">(this.module.API)</field>
            <field name="mWindowListener">this.API.Browser.getWindowListener(window)</field>
            <field name="title">"&opinions.name;"</field>

            <field name="_button" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","opinions-button");]]></field>

            <field name="_menu" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","opinions-menupopup");]]></field>

            <field name="_currentOpinionsData">null</field>
            <field name="_opinionsWindow">null</field>
            <field name="_isOpinionsAlwaysOpened">false</field>

            <field name="mLoadingTimestamp">0</field>
            <field name="mLoadingTimerId"/>

            <property name="loading">
                <getter><![CDATA[return this._loading;]]></getter>
                <setter><![CDATA[this._loading = val;
if (this.mLoadingTimerId)
clearTimeout(this.mLoadingTimerId);
if (val)
{
this._button.setAttribute("loading",val);
this.mLoadingTimestamp = Date.now() + 700;
}
else
{
this.mLoadingTimerId = setTimeout(function (me) {
me._button && me._button.setAttribute("loading",val);
}
,Math.max(0,this.mLoadingTimestamp - Date.now()),this);
}
]]></setter>
            </property>

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

            <property name="disabled" onget="return this._disabled;" onset="this._disabled = val; this._setDisabled(val)"/>

            <method name="_setDisabled">
                <parameter name="val"/>
                <body><![CDATA[this._setBtnDisabled(val);]]></body>
            </method>

            <constructor><![CDATA[var browser = gBrowser.mCurrentBrowser;
this.API.Settings.observeChanges(this);
this.module.sendBarNavigRequest(browser);
var url = browser.currentURI.spec;
var data = this.module.getDataForURL(url);
this.module.updateToolbaritemData(browser,url,data);
this.mWindowListener.addListener("WindowLocationChange",this);]]></constructor>

            <method name="opinionsDestroy">
                <body><![CDATA[this.mWindowListener.removeListener("WindowLocationChange",this);
this.API.Settings.ignoreChanges(this);
this._currentOpinionsData = null;
this._opinionsWindow = null;
var strDlgID = this.module._getDialogID(window);
this.module.dlgman.closeOpinionsDialog(strDlgID);]]></body>
            </method>

            <method name="observe">
                <parameter name="subject"/>
                <parameter name="topic"/>
                <parameter name="data"/>
                <body><![CDATA[switch (topic) {
case "WindowLocationChange":
let tab = data.tab;
let strDlgID = this.module._getDialogID(window);
if (this._isOpinionsAlwaysOpened || this._getTabData(tab,"opinionsOpened"))
{
let URI = data.uri;
let strURL = this.module._getURL(URI);
this._opinionsWindow = this.module.dlgman.openOpinionsDialog(this.module,strDlgID,strURL,this._getTabData(tab,"yScrollPos") || 0,this._onDialogEvent.bind(this),true);
}
else
{
this.module.dlgman.closeOpinionsDialog(strDlgID);
}

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

            <method name="_onDialogEvent">
                <parameter name="event"/>
                <body><![CDATA[switch (event.type) {
case "scroll":
{
let tab = this.mWindowListener.currentTab;
this._setTabData(tab,"yScrollPos",event.data);
break;
}

case "mode":
{
this._isOpinionsAlwaysOpened = event.data;
break;
}

case "close":
{
if (this._isOpinionsAlwaysOpened)
{
Array.forEach(this.module._getCurWinTabs(window),function (tab) {
this._removeTabData(tab,"opinionsOpened");
this._removeTabData(tab,"yScrollPos");
}
,this);
this._isOpinionsAlwaysOpened = false;
}
else
{
if (this.mWindowListener)
{
let tab = this.mWindowListener.currentTab;
this._removeTabData(tab,"opinionsOpened");
this._removeTabData(tab,"yScrollPos");
}

}

this._opinionsWindow = null;
break;
}

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

            <method name="onSettingChange">
                <parameter name="settingName"/>
                <parameter name="newValue"/>
                <body><![CDATA[if (settingName == "showValue" && ! this.disabled)
{
this._refreshValue();
}
]]></body>
            </method>

            <method name="setOpinionsCount">
                <parameter name="data"/>
                <body><![CDATA[this._currentOpinionsData = data;
if (data.buttonState !== this.module.barNavigDataProvider.DATA_STATE_REQUEST)
{
this.loading = false;
}

this._setButtonOpinionsCount(data);
var disabled = false;
if (! parseInt(this.getAttribute("yb-second-label"),10))
{
let currentURI = gBrowser.currentURI;
disabled = ! (currentURI && /^https?$/.test(currentURI.scheme));
}

this.disabled = disabled;
var tab = this.mWindowListener.currentTab;
if (this._isOpinionsAlwaysOpened || this._getTabData(tab,"opinionsOpened"))
{
if (data.windowState === data.STATES.UNKNOWN)
this.module.requestOpinionsListTimed(tab);
if (this._opinionsWindow && this._opinionsWindow.OpinionsDialog)
{
this._opinionsWindow.OpinionsDialog.setData(data);
}

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

            <method name="refreshData">
                <body><![CDATA[this.loading = true;
this.module.sendBarNavigRequest(gBrowser.mCurrentBrowser,true);]]></body>
            </method>

            <method name="_setButtonOpinionsCount">
                <parameter name="data"/>
                <body><![CDATA[var yaIconAttr = 0;
if (data.count)
{
yaIconAttr = 1;
[0, 100, 1000, 10000].every(function (value) {
return data.count > value ? ! ! yaIconAttr++ : false;
}
);
}

this._button.setAttribute("yaIcon",yaIconAttr);
this._button.setAttribute("yb-second-label-off",! data.count || ! this.API.Settings.getValue("showValue"));
this._button.setAttribute("yb-second-label",data.count);]]></body>
            </method>

            <method name="_setBtnDisabled">
                <parameter name="val"/>
                <body><![CDATA[if (val)
this._button.setAttribute("yaIcon",0);
this._button.setAttribute("yb-second-label-off",val || ! this.API.Settings.getValue("showValue"));]]></body>
            </method>

            <method name="_refreshValue">
                <body><![CDATA[this._button.setAttribute("yb-second-label-off",! this.API.Settings.getValue("showValue"));]]></body>
            </method>

            
            <method name="_setTabData">
                <parameter name="tab"/>
                <parameter name="key"/>
                <parameter name="value"/>
                <body><![CDATA[this.module._setTabData(window,tab,key,value);]]></body>
            </method>

            <method name="_getTabData">
                <parameter name="tab"/>
                <parameter name="key"/>
                <body><![CDATA[return this.module._getTabData(window,tab,key);]]></body>
            </method>

            <method name="_removeTabData">
                <parameter name="tab"/>
                <parameter name="key"/>
                <body><![CDATA[this.module._removeTabData(window,tab,key);]]></body>
            </method>

            <method name="_onButtonClick">
                <parameter name="event"/>
                <body><![CDATA[if (this._getTabData(this.mWindowListener.currentTab,"opinionsOpened"))
{
let strDlgID = this.module._getDialogID(window);
this.module.dlgman.closeOpinionsDialog(strDlgID);
}
else
{
this._openDialog(event);
}
]]></body>
            </method>

            <method name="_openDialog">
                <parameter name="event"/>
                <body><![CDATA[var strDlgID = this.module._getDialogID(window);
var strURL = this.module._getURL(this.mWindowListener.currentURI);
this.module._logger.debug("openDialog:" + strDlgID);
this._opinionsWindow = this.module.dlgman.openOpinionsDialog(this.module,strDlgID,strURL,0,this._onDialogEvent.bind(this));
this._setTabData(this.mWindowListener.currentTab,"opinionsOpened",true);]]></body>
            </method>

        </implementation>

        <handlers>
            
            <handler event="command"><![CDATA[if (event.originalTarget.localName != "toolbarbutton")
{
let cmdName = event.originalTarget.getAttribute("yb-widget-command");
if (cmdName in this)
this[cmdName]();
if (cmdName in this.module)
this.module[cmdName](event,this);
}
]]></handler>

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

            <handler event="popupshowing"><![CDATA[var menu = this._menu;
if (event.originalTarget !== menu)
return;
var disabled = this.disabled;
Array.forEach(menu.getElementsByTagName("menuitem"),function _setItemDisabled(item) item.setAttribute("disabled",disabled));]]></handler>
        </handlers>
    </binding>

    <binding extends="chrome://global/content/bindings/popup.xml#tooltip" id="opinions-tooltip">
        <content>
            <vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
                <description style="text-align:center; font-weight:bold" value="&opinions.win.title;"/>
            </vbox>
            <deck anonid="data" selectedIndex="0" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
                <description style="text-align:center" value="(...&opinions.nodata.label;...)"/>
                <hbox pack="center">
                    <label style="margin:0" value="&opinions.count.label;: "/>
                    <label anonid="count" style="margin:0" value=""/>
                </hbox>
            </deck>
        </content>

        <implementation>
            <field name="_deck" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","data");]]></field>

            <field name="_count" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","count");]]></field>

            <method name="setOpinionsCount">
                <parameter name="data"/>
                <body><![CDATA[this._deck.selectedIndex = ! data || ! data.count ? 0 : 1;
this._count.setAttribute("value",data && data.count);]]></body>
            </method>
        </implementation>

        <handlers>
            <handler event="popupshowing"><![CDATA[var parent = document.getBindingParent(event.target.triggerNode.parentNode);
if (parent && "_currentOpinionsData" in parent)
this.setOpinionsCount(parent._currentOpinionsData);]]></handler>
        </handlers>
    </binding>
</bindings>
