"use strict";
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
var OpinionsDialog = {
get params() {
return window.yaWinArguments;
}
,
get api() {
return this.params.api;
}
,
onDialogLoad: function OpinionsDialog_onDialogLoad() {
document.getElementById("opinions-content").setAttribute("selectedIndex",2);
document.addEventListener("link",this._onLink,false);
document.documentElement.setAttribute("mode",window.mode ? window.mode : "bottom");
this.params.opinionsModule.requestOpinionsListTimed(window.opener.gBrowser.mCurrentBrowser);
this.setData(this.params.opinionsModule.getDataForURL(this.params.targetURL));
}
,
onDialogUnload: function OpinionsDialog_onDialogUnload() {
document.removeEventListener("link",this._onLink,false);
document.documentElement.savedPosition = {
x: window.screenX,
y: window.screenY};
this._fireEvent("close");
delete this.__stringBundle;
}
,
setData: function OpinionsDialog_setData(aData) {
if (aData.windowState === aData.STATES.UNKNOWN || aData.windowState === aData.STATES.TIMED_REQUEST)
{
document.getElementById("opinions-content").setAttribute("selectedIndex",0);
return;
}

if (aData.windowState === aData.STATES.ERROR)
{
document.getElementById("opinions-content").setAttribute("selectedIndex",3);
return;
}

var fragment = aData.content;
var container = document.getElementById("opinions-content-2");
if (fragment)
{
if (container.firstChild)
{
container.removeChild(container.firstChild);
}

container.appendChild(fragment);
if (container.firstChild)
{
let dateNow = new Date();
Array.forEach(container.firstChild.getElementsByTagName("label"),function (label) {
if (label.getAttribute("class") == "ya-blogger-time")
{
let postDate = this._getDateDiffString(dateNow,this._getDateFromString(label.getAttribute("value")));
if (postDate)
label.setAttribute("value",postDate);
}

}
,this);
Array.forEach(container.firstChild.getElementsByTagName("description"),function (desc) {
var node = desc.firstChild;
if (node && node.nodeName == "#text")
node.nodeValue = node.nodeValue.replace(/([\/\-&\?\.])/g,"$1\u200b").replace(/(\S{15})(\S{15})/g,"$1\u200b$2");
}
);
}

}

var selIndex = fragment ? 1 : 2;
var content = document.getElementById("opinions-content");
if (content.selectedIndex != selIndex)
content.setAttribute("selectedIndex",selIndex);
var scroll = document.getElementById("opinions-content-scroll");
if (scroll)
{
scroll.addEventListener("scroll",this._onScroll.bind(this),false);
setTimeout(function (me) {
if ("scrollByPixels" in scroll)
scroll.scrollByPixels(me.params.scrollPos || 0);
}
,500,this);
}

}
,
openSupportLink: function OpinionsDialog_openSupportLink(aEvent) {
this.api.Controls.navigateBrowser({
url: aEvent.target.getAttribute("href"),
target: "new tab"});
}
,
_onLink: function OpinionsDialog__onLink(event) {
var strURL = event.originalTarget.getAttribute("yahref");
if (! strURL)
return;
var nAction = event.originalTarget.getAttribute("yaLinkAction");
if (nAction)
{
OpinionsDialog.api.Statistics.logShortAction(nAction);
}

OpinionsDialog.api.Controls.navigateBrowser({
target: "new tab",
unsafeURL: strURL});
}
,
_onScroll: function OpinionsDialog__onScroll(evt) {
var scroll = document.getElementById("opinions-content-scroll");
var yPos = {
};
scroll.scrollBoxObject.getPosition({
},yPos);
this._fireEvent("scroll",yPos.value);
}
,
hboxPopupShowing: function OpinionsDialog_hboxPopupShowing(tooltip) {
var url = this._getUrlValue(document.tooltipNode);
return url ? tooltip.firstChild.setAttribute("value",url) : false;
}
,
changeMode: function OpinionsDialog_changeMode() {
var oldMode = document.documentElement.getAttribute("mode");
var newMode = oldMode == "top" ? "bottom" : "top";
document.documentElement.setAttribute("mode",newMode);
window.mode = newMode;
this._fireEvent("mode",newMode == "top");
}
,
hide: function OpinionsDialog_hide() {
window.close();
}
,
refresh: function OpinionsDialog_refresh() {
this.params.opinionsModule.sendBarNavigRequest(window.opener.gBrowser.mCurrentBrowser,true);
this.setData(this.params.opinionsModule.getDataForURL(this.params.targetURL));
}
,
_fireEvent: function OpinionsDialog__fireEvent(strType, data) {
var clb = this.params.onEvent;
if (clb)
clb({
type: strType,
data: data,
openerWindow: window.opener});
}
,
_getUrlValue: function OpinionsDialog__getUrlValue(aNode) {
var node = aNode;
while (node && "hasAttribute" in node) {
if (node.hasAttribute("yahref"))
return node.getAttribute("yahref");
node = node.parentNode;
}

return null;
}
,
_getDateDiffString: function OpinionsDialog__getDateDiffString(crtDate, date) {
var diff = crtDate.getTime() - date.getTime();
if (diff < 0)
return null;
var res = [], strType = "year";
var ds = 86400000;
if (diff < ds)
strType = "today";
if (crtDate.getDate() != date.getDate())
strType = "yesterday";
if (diff >= ds * 2)
strType = "month";
if (crtDate.getYear() != date.getYear())
strType = "year";
switch (strType) {
case "year":
res = [date.getDate(), this._strArrMonths[date.getMonth()], date.getFullYear()];
break;
case "month":
res = [date.getDate(), this._strArrMonths[date.getMonth()]];
res.push(this._getFormatedTime(date));
break;
case "today":
res = [this._stringBundle.get("today"), this._getFormatedTime(date)];
break;
case "yesterday":
res = [this._stringBundle.get("yesterday"), this._getFormatedTime(date)];
break;
}

return res.join(" ");
}
,
_getDateFromString: function OpinionsDialog__getDateFromString(strDate) {
return strDate.match(/(\d{1,2})\.(\d{1,2})\.(\d{4})(?:\s+(\d{1,2})\:(\d{1,2}))?/) ? new Date(RegExp.$3, RegExp.$2 * 1 - 1, RegExp.$1, RegExp.$4, RegExp.$5) : null;
}
,
_getFormatedTime: function OpinionsDialog__getFormatedTime(date) {
return [date.getHours(), ("0" + date.getMinutes()).slice(- 2)].join(":");
}
,
get _strArrMonths() {
var months = this._stringBundle.get("dateMonths").toLowerCase().split(",");
delete this._strArrMonths;
this.__defineGetter__("_strArrMonths",function _strArrMonths() months);
return months;
}
,
__stringBundle: null,
get _stringBundle() {
return this.__stringBundle || (this.__stringBundle = this.api.Localization.createStringBundle("/native/fx/time.properties"));
}
};
