home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2011 November
/
CHIP_2011_11.iso
/
Programy
/
Narzedzia
/
AIMP2
/
aimp_2.61.583.exe
/
$TEMP
/
YandexPackSetup.msi
/
fil3B505E1A6DD1B3D1298587D22DA6EEBD
< prev
next >
Wrap
Text File
|
2010-07-12
|
15KB
|
555 lines
if (!Ya.YaTranslate) {
Ya.YaTranslate = {
get tooltip() {
if (!this._tooltip) {
let tooltip;
let appInfo = Ya.nsIYa.AppInfo;
if (appInfo.OS.isWindows) {
tooltip = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "panel");
tooltip.setAttribute("noautofocus", "true");
tooltip.setAttribute("norestorefocus", "false");
tooltip.setAttribute("level", "top");
if (appInfo.browser.isGreaterThenFx30)
tooltip.setAttribute("yaShadow", "true");
} else {
tooltip = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "tooltip");
tooltip.setAttribute("noautohide", "true");
}
tooltip.setAttribute("yaIsLinux", appInfo.OS.isLinux);
tooltip.style.MozBinding = "url('chrome://yasearch/content/translate/balloon.xbl#balloon')";
document.getElementById("mainPopupSet").appendChild(tooltip);
tooltip.tooltipController = this;
this._tooltip = tooltip;
}
return this._tooltip;
},
get tooltipHTML() {
return this._tooltipHTML || (this._tooltipHTML = document.getElementById("aHTMLTooltip"));
},
get contextMenu() {
return this._contextMenu || (this._contextMenu = document.getElementById("contentAreaContextMenu"));
},
get locale() {
return this._locale || (this._locale = Ya.stringBundleSet.getString("locale.lang") || "ru");
},
get content() {
return this._content || (this._content = document.getElementById("main-window"));
},
get contentIsActive() {
return this.content.getAttribute("active") === "true";
},
INTERVAL_MIN: 500,
INTERVAL_MAX: 10000,
get intervalShow() {
return this._intervalShow || (this._intervalShow =
Math.min(Math.max(this.INTERVAL_MIN, this.Prefs.get(this.INTERVAL_SHOW_PREF, this.INTERVAL_MAX)), 10000));
},
ENABLE_PREF: "general.ui.translate.enabled",
REVERSE_PREF: "general.ui.translate.reverse",
FROM_PREF: "general.ui.translate.from",
TO_PREF: "general.ui.translate.to",
INTERVAL_SHOW_PREF: "general.ui.translate.interval.show",
showTimeout: null,
request: null,
balloon: null,
_lastScrollEventTime: 0,
_cache: {
MAX_CACHED_COUNT: 256,
_cachedBallons: [],
clear: function() {
this._cachedBallons = [];
},
_balloonId: function(balloon) {
return [balloon.from, balloon.to, balloon.text.toLowerCase()].join("|");
},
storeBalloon: function(balloon) {
let cached = {};
for (let name in {from: 0, to: 0, text: 0, url: 0, translations: 0})
cached[name] = balloon[name];
cached.balloonId = this._balloonId(balloon);
if (this._cachedBallons.unshift(cached) > this.MAX_CACHED_COUNT)
this._cachedBallons.splice(-this.MAX_CACHED_COUNT/2);
},
restoreBalloon: function(balloon) {
let cached;
let balloonId = this._balloonId(balloon);
this._cachedBallons.some(function(aBalloon) {
return (balloonId === aBalloon.balloonId) && (cached = aBalloon);
});
if (cached)
for (let name in {url: 0, translations: 0})
balloon[name] = cached[name];
return balloon;
}
},
isPopupOpen: function(aPopupNode) {
return aPopupNode && (["open", "showing"].indexOf(aPopupNode.state) > -1);
},
init: function() {
Components.utils.import("resource://yasearch/Preferences.jsm", this);
this.onEnableChanged();
this.onReverseChanged();
this.onDirectionChanged();
this.Prefs.observe(this.ENABLE_PREF, this.onEnableChanged, this);
this.Prefs.observe(this.REVERSE_PREF, this.onReverseChanged, this);
this.Prefs.observe(this.FROM_PREF, this.onDirectionChanged, this);
this.Prefs.observe(this.TO_PREF, this.onDirectionChanged, this);
YaProgressListener.addListener(this);
},
destroy: function() {
this.clearShowTimeout();
YaProgressListener.removeListener(this);
this.Prefs.ignore(this.ENABLE_PREF, this.onEnableChanged, this);
this.Prefs.ignore(this.REVERSE_PREF, this.onReverseChanged, this);
this.Prefs.ignore(this.FROM_PREF, this.onDirectionChanged, this);
this.Prefs.ignore(this.TO_PREF, this.onDirectionChanged, this);
if (this.request) {
this.request.abort();
this.request = null;
}
this.hideBalloon();
if (this._tooltip) {
this._tooltip.tooltipController = null;
this._tooltip = null;
}
this._tooltipHTML = null;
this._contextMenu = null;
this._content = null;
this._cache.clear();
Ya.YaTranslate = null;
},
_enable: false,
set enable(value) {
this._enable = !!value;
this.enableShowEvents(value);
},
get enable() {
return this._enable;
},
_available: false,
set available(value) {
this._available = !!value;
this.onEnableChanged();
},
get available() {
return this._available;
},
showEventsStatus: false,
enableShowEvents: function(aEnabled) {
if (aEnabled == this.showEventsStatus)
return;
this.showEventsStatus = aEnabled;
let content = this.content;
let events = ["mousemove", "mouseout", "scroll"];
for (let i = events.length; i--;) {
if (aEnabled)
content.addEventListener(events[i], this, false, true);
else
content.removeEventListener(events[i], this, false, true);
}
},
isUserPage: function(event) {
try {
return !!(event.view && event.view.top === gBrowser.mCurrentBrowser.contentWindow);
} catch(e) {}
return false;
},
isCyrillic: function(aString) {
return !!(aString && /^[\u0400-\u04ff\-]+$/.test(aString));
},
translateIt: function(balloon) {
if (
this.isPopupOpen(this.tooltip) &&
this.balloon &&
this.balloon.text == balloon.text &&
this.balloon.nodeRef === balloon.nodeRef &&
this.balloon.begin == balloon.begin
)
return true;
var cyrillic = this.isCyrillic(balloon.text);
var wrong = ((this.direction.from == "ru") != cyrillic);
if (wrong && !this.reverse)
return false;
if (wrong) {
balloon.from = this.direction.to;
balloon.to = this.direction.from;
}
else {
balloon.from = this.direction.from;
balloon.to = this.direction.to;
}
if (balloon.from == "ru" && balloon.to == "it")
return false;
balloon = this._cache.restoreBalloon(balloon);
this.balloon = balloon;
if (balloon.translations) {
this.showBalloon(balloon);
} else {
if (this.request)
this.request.abort();
this.request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
var target = this.request.QueryInterface(Ci.nsIDOMEventTarget);
target.addEventListener("load", this, false);
target.addEventListener("error", this, false);
this.request.open("GET", "http://export.yandex.ru/bar/translate.xml" +
"?text=" + encodeURIComponent(balloon.text) +
"&from=" + balloon.from +
"&to=" + balloon.to +
"&locale=" + this.locale,
true);
this.request.send(null);
}
return true;
},
response: function(event) {
let request = event.target;
let status = request.status;
if (this.request === request)
this.request = null;
if (status < 200 || status > 399)
return;
let xml = Ya.nsIYa.safeE4Xml(request.responseXML, null, "translate");
if (!xml || parseInt(xml.error.@status, 10) != 0)
return;
let balloon = this.balloon;
["from", "to", "url"]
.forEach(function(elName) {
let el = xml[elName][0];
balloon[elName] = el ? el.toString() : "";
});
balloon.translations = [];
for each (let translate in xml.translations.translation) {
let word = translate.@word.toString();
let part = {};
let parts = [];
for each (let variant in translate.variant) {
let partOfSpeech = variant.@class.toString();
if (parts.indexOf(partOfSpeech) == -1) {
parts.push(partOfSpeech);
part[partOfSpeech] = [];
}
let variantStr = variant.toString();
if (part[partOfSpeech].indexOf(variantStr) == -1)
part[partOfSpeech].push(variantStr);
}
if (parts.length) {
balloon.translations.push({
word: word,
parts: parts,
part: part
});
}
}
this._cache.storeBalloon(balloon);
if (!request.doNotShow)
this.showBalloon(balloon);
},
clearShowTimeout: function() {
if (this.showTimeout) {
clearTimeout(this.showTimeout);
this.showTimeout = null;
}
},
doNotShowBalloonNow: function() {
this.clearShowTimeout();
if (this.request)
this.request.doNotShow = true;
},
showBalloon: function(balloon) {
if (!(balloon.translations && balloon.translations.length) ||
this.isPopupOpen(this.contextMenu) ||
balloon.contentIsActive != this.contentIsActive) {
this.hideBalloon();
return;
}
if (this.isPopupOpen(this.tooltipHTML))
this.tooltipHTML.hidePopup();
let tooltip = this.tooltip;
tooltip.hidePopup();
tooltip.from = balloon.from;
tooltip.to = balloon.to;
tooltip.url = balloon.url;
tooltip.translations = balloon.translations;
tooltip.openPopupAtScreen(balloon.x, balloon.y, false);
},
hideBalloon: function() {
if (this._tooltip)
this._tooltip.hidePopup();
if (this.balloon && this.balloon.nodeRef)
this.balloon.nodeRef = null;
},
onEnableChanged: function() {
if (!this.Prefs)
return;
if (this.isPopupOpen(this.tooltip))
this.hideBalloon();
this.enable = this.available && this.Prefs.get(this.ENABLE_PREF, false);
},
onReverseChanged: function() {
if (!this.Prefs)
return;
this.reverse = this.Prefs.get(this.REVERSE_PREF, false);
},
onDirectionChanged: function() {
this.direction = {
from: this.Prefs.get(this.FROM_PREF, "en"),
to: this.Prefs.get(this.TO_PREF, "ru")
};
},
observe: function(aSubject) {
switch (aSubject) {
case "pagehide":
let [, browser] = arguments;
if (browser !== gBrowser.mCurrentBrowser)
return;
case "onWindowLocationChange":
this.doNotShowBalloonNow();
this.hideBalloon();
break;
}
},
handleEvent: function(event) {
switch (event.type) {
case "mousemove":
this.clearShowTimeout();
this.showTimeout = setTimeout(this.onMouseHoverWithoutContext, this.intervalShow,
this, event, event.rangeParent, event.rangeOffset);
break;
case "mouseout":
if (!this.isPopupOpen(this.tooltip))
this.doNotShowBalloonNow();
break;
case "scroll":
var timeNow = Date.now();
if (timeNow - this._lastScrollEventTime > 200) {
this._lastScrollEventTime = timeNow;
this.doNotShowBalloonNow();
this.hideBalloon();
}
break;
case "load":
this.response(event);
break;
case "error":
break;
}
},
_getNodeWeakReference: function(aNode) {
try {
return (aNode instanceof Ci.nsISupportsWeakReference) ? aNode.GetWeakReference() : null;
} catch (e) {}
return null;
},
_checkForNewNode: function(aRangeParent, aRangeOffset) {
if (!this.isPopupOpen(this.tooltip) || this.tooltip.contains(aRangeParent))
return false;
let balloon = this.balloon;
if (balloon && balloon.nodeRef) {
if (aRangeOffset < balloon.begin || aRangeOffset > balloon.end)
return true;
if (balloon.nodeRef !== this._getNodeWeakReference(aRangeParent.parentNode))
return true;
}
return false;
},
checkForNewNode: function(aRangeParent, aRangeOffset) {
if (this._checkForNewNode(aRangeParent, aRangeOffset))
this.hideBalloon();
},
onMouseHoverWithoutContext: function(aSelf, aEvent, aNode, aOffset) {
aSelf.clearShowTimeout();
if (aSelf.isUserPage(aEvent) && !aSelf.onMouseHover(aEvent, aNode, aOffset))
aSelf.hideBalloon();
},
onMouseHover: function(event, node, offset) {
if (!(node && node.nodeType === Node.TEXT_NODE))
return false;
try {
var nodeDocument = node.ownerDocument;
var parent = nodeDocument.elementFromPoint(event.clientX, event.clientY);
} catch (e) {}
if (!(parent && node.parentNode === parent))
return false;
function length(s, reverse) {
if (reverse)
s = s.split("").reverse().join("");
return (s.match(/^[\u0041-\u005a\u0061-\u007a\u00c0-\u1fff\-\'\u2019]+/) || [""])[0].length;
}
var text = node.nodeValue;
var left = text.substr(0, offset),
right = text.substr(offset);
var begin = offset - length(left, true);
var end = offset + length(right, false);
if (end - begin <= 0)
text = "";
else
text = text.substr(begin, end - begin);
if (!text || /(^\-)|(\-$)|(\-{2,})|([\'\u2019]{2,})/.test(text))
return false;
/* fx37
if (isGreaterThenFx36) {// Bug 396392
let nodeRange = nodeDocument.createRange();
nodeRange.setStart(node, begin);
nodeRange.setEnd(node, end);
if ("getBoundingClientRect" in nodeRange) {
let nodeBCR = nodeRange.getBoundingClientRect();
if (nodeBCR.left > event.clientX || nodeBCR.right < event.clientX ||
nodeBCR.top > event.clientY || nodeBCR.bottom < event.clientY)
return false;
}
}
*/
return this.translateIt({
text: text,
x: event.screenX,
y: event.screenY,
nodeRef: this._getNodeWeakReference(parent),
begin: begin,
end: end,
contentIsActive: this.contentIsActive
});
}
}
window.addEventListener("load", function(aLoadEvent) {
aLoadEvent.currentTarget.removeEventListener("load", arguments.callee, false);
aLoadEvent.currentTarget.addEventListener("unload", function(aUnloadEvent) {
aUnloadEvent.currentTarget.removeEventListener("unload", arguments.callee, false);
Ya.YaTranslate.destroy();
}, false);
Ya.YaTranslate.init();
}, false);
}