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

    <binding extends="../bindings/buttons.xbl#ycounter-widget" id="widget">
        <resources>
            <stylesheet src="yaru.css"/>
        </resources>

        <content>
            <toolbarbutton anonid="yaru-button" class="chromeclass-toolbar-additional toolbarbutton-1 label2 ycounter-button" label="&yaru.label;" noauth-tooltip-text="&yaru.noauthtooltiptext;" tooltip="yaru-tooltip" tooltip-text="&yaru.tooltiptext;" type="menu-button" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" yb-icon-overlay="true" yb-second-tooltip-text="&noauth.label;">
                <menupopup anonid="yaru-menupopup">
                    <menuitem label="&yaru.reloadCounter.label;" oncommand="refreshData(event)"/>
                    <menuseparator/>
                    <menuitem label="&yaru.replies.unread.label;" yb-widget-command="gotoUnread"/>
                    <menuitem label="&yaru.whats_new.label;" yb-widget-command="gotoFriends"/>
                    <menuitem label="&yaru.post.text.label;" yb-widget-command="postText"/>
                    <menuitem label="&yaru.post.photo.label;" yb-widget-command="postImage"/>
                    <menuitem label="&yaru.post.status.label;" yb-widget-command="postStatus"/>
                    <menuitem label="&yaru.post.sendPage.label;" yb-widget-command="sharePage"/>
                </menupopup>
            </toolbarbutton>

            
            <xul:menuitem accesskey="&yaru.send.accesskey;" anonid="yaru-share-menu" class="menuitem-iconic menuitem-with-favicon" hidden="true" image="/native/fx/yaru/yaru.png" label="&yaru.send.page.label;" yaruLabel_ShareImage="&yaru.send.image.label;" yaruLabel_SharePage="&yaru.send.page.label;" yaruLabel_ShareSelection="&yaru.send.selected.label;" yb-widget-command="shareContent"/>
        </content>

        <implementation implements="nsIDOMEventListener">
            <field name="title">"&yaru.label;"</field>

            <constructor><![CDATA[if (this._constructed)
return;
this._constructed = true;
this._appendContextMenu();]]></constructor>

            <method name="yaruDestroy">
                <body><![CDATA[if (! this._constructed)
return;
this._constructed = false;
this._removeContextMenu();]]></body>
            </method>

            
            <method name="handleEvent">
                <parameter name="event"/>
                <body><![CDATA[switch (event.type) {
case "popupshowing":
if (this._shareMenu && event.target === this._pageContextMenu)
this._updateShareMenu();
break;
case "command":
this.module.shareContent(event.originalTarget.yaruShareData,event);
break;
}
]]></body>
            </method>

            

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

            <field name="_shareMenuSrc" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","yaru-share-menu");]]></field>

            <field name="_shareMenu">null</field>

            <property name="_pageContextMenu" onget="return document.getElementById('contentAreaContextMenu');" readonly="true"/>

            <method name="_appendContextMenu">
                <body><![CDATA[var pageContextMenu = this._pageContextMenu;
if (! pageContextMenu)
return;
this._shareMenu = pageContextMenu.ownerDocument.importNode(this._shareMenuSrc,true);
var imageURL = this.module.API.Package.resolvePath(this._shareMenu.getAttribute("image"));
this._shareMenu.setAttribute("image",imageURL);
var pivotElement = document.getElementById("frame-sep") || document.getElementById("context-sep-viewbgimage");
if (pivotElement && pivotElement.parentNode == pageContextMenu)
pageContextMenu.insertBefore(this._shareMenu,pivotElement); else
pageContextMenu.appendChild(this._shareMenu);
this._shareMenu.addEventListener("command",this,false);
pageContextMenu.addEventListener("popupshowing",this,false);]]></body>
            </method>

            <method name="_removeContextMenu">
                <body><![CDATA[if (! this._shareMenu)
return;
this._shareMenu.removeEventListener("command",this,false);
var parentMenu = this._shareMenu.parentNode;
if (parentMenu)
parentMenu.removeChild(this._shareMenu);
this._shareMenu = null;
if (this._pageContextMenu)
this._pageContextMenu.removeEventListener("popupshowing",this,false);]]></body>
            </method>

            <method name="_updateShareMenu">
                <body><![CDATA[var imageDescr = this._getContextImageData();
var shareData = {
contentType: imageDescr ? "ShareImage" : gContextMenu.isTextSelected ? "ShareSelection" : "SharePage",
image: imageDescr};
this._shareMenu.setAttribute("label",this._shareMenu.getAttribute("yaruLabel_" + shareData.contentType));
this._shareMenu.yaruShareData = shareData;
var cxtMenuTarget = gContextMenu.target;
var cxtPageDoc = cxtMenuTarget && cxtMenuTarget.ownerDocument;
var hideShareMenu = ! cxtPageDoc || /^(chrome|file|about):$/.test(cxtPageDoc.location.protocol);
this._shareMenu.setAttribute("hidden",hideShareMenu);]]></body>
            </method>

            <method name="_getContextImageData">
                <body><![CDATA[var docPopupNode = document.popupNode;
if (docPopupNode && docPopupNode.nodeType == docPopupNode.ELEMENT_NODE && docPopupNode instanceof Ci.nsIImageLoadingContent && docPopupNode.currentURI)
{
let imageURI = docPopupNode.currentURI;
if (/file|https?/.test(imageURI.scheme))
{
let altText = docPopupNode.getAttribute("alt") || "";
let titleText = docPopupNode.getAttribute("title") || "";
if (altText == imageURI.spec)
altText = "";
if (titleText == imageURI.spec)
titleText = "";
let imageName = titleText.length > altText.length ? titleText : altText;
if (imageName.length > 1000)
imageName = imageName.substr(0,1000);
return {
imageURL: imageURI.spec,
imageName: imageName};
}

}

return null;]]></body>
            </method>
        </implementation>
    </binding>
</bindings>
