"use strict";
const EXPORTED_SYMBOLS = ["core", "resources"];
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const resources = {
browser: {
styles: ["/native/fx/town/browser.css"]}};
const WIDGET_NAME = "http://bar.yandex.ru/packages/yandexbar#town";
const core = {
init: function TownWidget_init(api) {
this._api = api;
this._pkgRootURL = this.API.Package.resolvePath("/");
}
,
finalize: function TownWidget_finalize() {
this._pkgRootURL = null;
this._api = null;
}
,
buildWidget: function TownWidget_buildWidget(WIID, toolbarItem) {
toolbarItem.setAttribute("yb-native-widget-name",WIDGET_NAME);
toolbarItem.setAttribute("yb-native-widget-wiid",WIID);
toolbarItem.module = this;
this._createInstSlices(WIID);
}
,
destroyWidget: function TownWidget_destroyWidget(WIID, toolbarItem) {
if ("wdgtxDestructor" in toolbarItem)
toolbarItem.wdgtxDestructor();
delete toolbarItem.module;
}
,
onNoMoreInstProjections: function TownWidget_onNoMoreInstProjections(WIID) {
this._destroyInstSlices(WIID);
}
,
get API() {
return this._api;
}
,
get defaultTownIconPath() {
return this._pkgRootURL + "icons/weather.png";
}
,
get DEFAULT_WEATHER_URL() {
delete this.DEFAULT_WEATHER_URL;
return this.DEFAULT_WEATHER_URL = this._api.Localization.createStringBundle("/urls/town.properties").get("weatherURL");
}
,
get DEFAULT_TRAFFIC_URL() {
delete this.DEFAULT_TRAFFIC_URL;
return this.DEFAULT_TRAFFIC_URL = this._api.Localization.createStringBundle("/urls/town.properties").get("mapsURL");
}
,
Export: {
_data: Object.create(null),
getDataFor: function TownWidget_Export_getDataFor(WIID) {
var url = core.makeURLForWIID(WIID);
var urlExportData = this.isDataDefinedForURL(url) ? this._data[url] : null;
return urlExportData && urlExportData.refs[WIID] ? urlExportData.data : null;
}
,
setDataFor: function TownWidget_Export_setDataFor(WIID, aData) {
var url = core.makeURLForWIID(WIID);
if (typeof this._data[url] === "undefined")
{
this._initExportEntryFor(url);
}

var urlCacheEntry = this._data[url];
urlCacheEntry.refs[WIID] = true;
urlCacheEntry.data = this._makeValuesFromXML(aData);
this._data[url] = urlCacheEntry;
}
,
resetDataFor: function TownWidget_Export_resetDataFor(WIID) {
var url = core.makeURLForWIID(WIID);
if (typeof this._data[url] !== "undefined")
{
let urlCacheEntry = this._data[url];
urlCacheEntry.refs[WIID] = false;
}

}
,
subscribeWidget: function TownWidget_Export_subscribeWidget(WIID, aURL) {
var url = core.makeURLForWIID(WIID);
if (typeof this._data[url] === "undefined")
{
this._initExportEntryFor(url);
}

this._data[url].refs[WIID] = true;
}
,
isDataDefinedForURL: function TownWidget_Export_isDataDefinedForURL(aURL) {
return this._data[aURL] && this._data[aURL].data;
}
,
_initExportEntryFor: function TownWidget_Export__initExportEntry(url) {
this._data[url] = {
refs: Object.create(null),
data: null};
}
,
_makeValuesFromXML: function TownWidget_Export__makeValuesFromXML(aData) {
var values = Object.create(null);
var xmlData = aData;
var titleElement = xmlData && xmlData.querySelector("info > region > title");
values.title = titleElement && titleElement.textContent || "";
var weatherElement = xmlData && xmlData.querySelector("info > weather");
var weatherIcon = weatherElement && weatherElement.querySelector("day > day_part > image");
weatherIcon = weatherIcon && weatherIcon.textContent || "";
weatherIcon = weatherIcon.replace("http://weather.yandex.ru/i/","").replace(".png","");
weatherIcon = this._weatherImagesHash[weatherIcon];
weatherIcon = weatherIcon ? core.API.Package.resolvePath("/weather/i-" + weatherIcon + ".png") : core.defaultTownIconPath;
var temperature = weatherElement && weatherElement.querySelector("day > day_part > temperature");
temperature = temperature && temperature.textContent || "";
if (core._brandId == "tb")
temperature = temperature.replace(/\s?\+\s?/,"");
var weatherURL = weatherElement && weatherElement.querySelector("url");
weatherURL = weatherURL && weatherURL.textContent || "";
values.weather = {
temperature: temperature,
icon: weatherIcon,
url: weatherURL};
var trafficElement = xmlData && xmlData.querySelector("info > traffic");
if (trafficElement)
{
let trafficLevel = trafficElement.querySelector("level");
trafficLevel = trafficLevel && trafficLevel.textContent || "";
let trafficIcon = trafficElement.querySelector("icon");
trafficIcon = trafficIcon && trafficIcon.textContent || "";
trafficIcon = trafficIcon && core.API.Package.resolvePath("traffic/" + trafficIcon + ".png");
let trafficURL = trafficElement.querySelector("url");
trafficURL = trafficURL && trafficURL.textContent || "";
values.traffic = {
level: trafficLevel,
icon: trafficIcon,
url: trafficURL};
}

aData.values = values;
return aData;
}
,
_weatherImagesHash: {
__proto__: null,
"1": "rain-summer",
"n1": "rain-summer",
"4": "rain-summer",
"n4": "rain-summer",
"2": "snow",
"n2": "snow",
"5": "cloud",
"16": "cloud",
"6": "ch",
"7": "sun",
"9": "rain-ch-summer",
"14": "rain-ch-summer",
"10": "rain-ch2-summer",
"11": "cloud-rain",
"15": "cloud-rain",
"12": "ch-snow",
"13": "cloud-snow",
"n5": "night-cloud",
"n6": "night-cloud",
"n9": "night-cloud-rain",
"n10": "night-cloud-rain",
"n11": "night-cloud-rain",
"n14": "night-cloud-rain",
"n15": "night-cloud-rain",
"n12": "night-cloud-snow",
"n13": "night-cloud-snow",
"n7": "night",
"n16": "night",
"3": "rain-grad",
"n3": "rain-grad",
"8": "thund-rain",
"n8": "thund-rain",
"17": "snowstorm",
"n17": "snowstorm",
"18": "fog",
"n18": "fog"}},
weatherSlices: {
},
trafficSlices: {
},
updateSlicesURLs: function TownWidget_updateSlicesURLs(WIID) {
this.weatherSlices[WIID].url = this._makeWeatherSliceURL(WIID);
this.trafficSlices[WIID].url = this._makeTrafficSliceURL(WIID);
}
,
recreateInstSlice: function TownWidget_recreateInstSlice(aSliceType, WIID) {
this._destroyInstSlice(aSliceType,WIID);
this._createInstSlice(aSliceType,WIID);
}
,
_makeWeatherSliceURL: function TownWidget__makeWeatherSliceURL(WIID) {
var regionID = this.API.Settings.getValue("id",WIID) || "";
return this._pkgRootURL + "native/fx/town/slice-w/weather.html?region=" + regionID;
}
,
_makeTrafficSliceURL: function TownWidget__makeTrafficSliceURL(WIID) {
var regionID = this.API.Settings.getValue("id",WIID) || "";
return this._pkgRootURL + "native/fx/town/slice-t/traffic.html?region=" + regionID;
}
,
_createInstSlice: function TownWidget__createInstSlice(aType, WIID) {
switch (aType) {
case "weather":
if (! this.weatherSlices[WIID])
this.weatherSlices[WIID] = this.API.Controls.createSlice({
url: this._makeWeatherSliceURL(WIID)},WIID);
break;
case "traffic":
if (! this.trafficSlices[WIID])
this.trafficSlices[WIID] = this.API.Controls.createSlice({
url: this._makeTrafficSliceURL(WIID)},WIID);
break;
}

}
,
_destroyInstSlice: function TownWidget__destroyInstSlice(aType, WIID) {
this._api.logger.debug("Destroying \"" + aType + "\" slice for WIID " + WIID);
switch (aType) {
case "weather":
let weatherSlice = this.weatherSlices[WIID];
if (weatherSlice)
{
weatherSlice.destroy();
delete this.weatherSlices[WIID];
}

break;
case "traffic":
let trafficSlice = this.trafficSlices[WIID];
if (trafficSlice)
{
trafficSlice.destroy();
delete this.trafficSlices[WIID];
}

break;
}

}
,
_createInstSlices: function TownWidget__createInstSlices(WIID) {
this._createInstSlice("weather",WIID);
this._createInstSlice("traffic",WIID);
}
,
_destroyInstSlices: function TownWidget__destroyInstSlices(WIID) {
this._destroyInstSlice("weather",WIID);
this._destroyInstSlice("traffic",WIID);
}
,
_getAllToolbarItemsForURL: function TownWidget__getAllToolbarItemsForURL(aURL) {
var toolbaritems = [];
var allWidgetItems = this._api.Controls.getAllWidgetItems();
for (let i = allWidgetItems.length;i--;) {
let widget = allWidgetItems[i];
let url = this.makeURLForWIID(widget.getAttribute("yb-native-widget-wiid"));
if (url !== aURL)
continue;
toolbaritems.push(widget);
}

return toolbaritems;
}
,
updateSliceData: function TownWidget_updateSliceData(aURL) {
var region = aURL.match(/region=(\d+)/);
region = region && region[0] || "";
for each(let weatherSlice in this.weatherSlices) {
let weatherRegion = weatherSlice.url.match(/region=(\d+)/);
weatherRegion = weatherRegion && weatherRegion[0] || "";
if (weatherRegion === region)
weatherSlice.notify("update");
}

for each(let trafficSlice in this.trafficSlices) {
let trafficRegion = trafficSlice.url.match(/region=(\d+)/);
trafficRegion = trafficRegion && trafficRegion[0] || "";
if (trafficRegion === region)
trafficSlice.notify("update");
}

}
,
updateToolbaritemData: function TownWidget_updateToolbaritemData(aURL, aData) {
this._getAllToolbarItemsForURL(aURL).forEach(function (el) {
if ("updateData" in el)
el.updateData(aData);
}
);
}
,
switchToolbaritemThrobber: function TownWidget_switchToolbaritemThrobber(aURL, aEnable) {
this._getAllToolbarItemsForURL(aURL).forEach(function (el) {
if ("loadingData" in el)
el.loadingData = aEnable;
}
);
}
,
get _reginfoURL() {
var exportURL = this._api.Localization.createStringBundle("/urls/common.properties").get("exportURL");
delete this._reginfoURL;
return this._reginfoURL = exportURL + "bar/reginfo.xml" + "?lang=" + this.API.Environment.addon.locale.language + "&brid=" + this._brandId + "&region=";
}
,
makeURLForWIID: function TownWidget_makeURLForWIID(WIID) {
var id = this.API.Settings.getValue("id",WIID) || "";
return this._reginfoURL + id;
}
,
__brandId: null,
get _brandId() {
if (this.__brandId !== null)
return this.__brandId;
var listener = {
observeServiceEvent: function () {

}
};
var service = this.API.Services.obtainService("ru.yandex.custombar.branding","package",listener);
try {
this.__brandId = service && service.getBrandID() || "";
}
 finally {
this.API.Services.releaseService("ru.yandex.custombar.branding","package",listener);
}

return this.__brandId;
}
};
