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 / filA12A566DFAB875FFAD5B9F3849A4A1C0 < prev    next >
Text File  |  2010-07-12  |  14KB  |  366 lines

  1. (function() {
  2.  
  3. var APP_NAME = "yasearch";
  4.  
  5. function OverlayControllerF() {
  6.     this._barCore = Components.classes["@yandex.ru/custombarcore;" + this._appName].getService().wrappedJSObject;
  7.     this._application = this._barCore.application;
  8.     this._id = this._application.getNewControllerID();
  9.     this._logger = this._barCore.Log4Moz.repository.getLogger(this._appName + ".OverlayController");
  10.     
  11.     window.addEventListener("load", this, false);
  12.     
  13.     this._init();
  14. }
  15.  
  16. OverlayControllerF.prototype = {
  17.     get id() {
  18.         return this._id;
  19.     },
  20.     
  21.     get application() {
  22.         return this._application;
  23.     },
  24.     
  25.     get guiBuilder() {
  26.         return this._guiBuilder;
  27.     },
  28.     
  29.     get windowEngine() {
  30.         return this._wndEngine;
  31.     },
  32.     
  33.     navigate: function wndCtrl_navigate(url, target, windowWidth, windowHeight, yandexAction, widgetProto) {
  34.         this._application.navigate(window, url, target, windowWidth, windowHeight, yandexAction, widgetProto);
  35.     },
  36.     
  37.     addWidgetItem: function wndCtrl_addWidgetItem(protoID, settingsMap, relativeTo, placeAfter, dontPersist) {
  38.         var widgetProto = this._application.widgetLibrary.getWidgetProto(protoID);
  39.         var paletteItemID;
  40.         var spawnedWidgets = widgetProto.spawnedWidgets;
  41.         var WIID = (widgetProto.isUnique && spawnedWidgets.length > 0) ? spawnedWidgets[0].id : 0;
  42.         if (this._wndEngine.hasWidget(WIID)) {
  43.             paletteItemID = this._wndEngine.getToolbarElement(WIID).getAttribute("id");
  44.         }
  45.         else {
  46.             let palette = this._toolbox.palette;
  47.             paletteItemID = this._application.overlayProvider.compileWidgetItemId(widgetProto.id, 0);
  48.             if (!palette.getElementsByAttribute("id", paletteItemID).length) {
  49.                 this._logger.debug("Adding new item to palette " + paletteItemID);
  50.                 let paletteItem = this._application.overlayProvider.makePaletteItem(document, widgetProto, 0);
  51.                 palette.appendChild(paletteItem);
  52.             }
  53.         }
  54.         
  55.         var beforeElement;
  56.         if (relativeTo) {
  57.             let relativeToElement;
  58.             
  59.             if (relativeTo instanceof Components.interfaces.nsIDOMNode)
  60.                 relativeToElement = relativeTo;
  61.             else
  62.                 if (relativeTo instanceof this._barCore.Lib.XB._Parser.Unit.WidgetInstance)
  63.                     relativeToElement = this._wndEngine.getToolbarElement(relativeTo.id);
  64.                 else
  65.                     throw new TypeError("relativeTo must be either a WidgetInstance or nsIDOMNode");
  66.             
  67.             beforeElement = placeAfter? relativeToElement.nextSibling: relativeToElement;
  68.         }
  69.         
  70.         var destToolbar = beforeElement? beforeElement.parentNode: this._appToolbar;
  71.         
  72.         let toolbarItem = document.getElementById(paletteItemID);
  73.         if (toolbarItem) {
  74.             let sourceToolbar = toolbarItem.parentNode;
  75.             if (beforeElement)
  76.                 destToolbar.insertBefore(toolbarItem, beforeElement);
  77.             else
  78.                 destToolbar.appendChild(toolbarItem);
  79.             
  80.             if (!dontPersist && sourceToolbar !== destToolbar)
  81.                 this._persistToolbarSet(sourceToolbar);
  82.         } else {
  83.             toolbarItem = destToolbar.insertItem(paletteItemID, beforeElement);
  84.             
  85.             if (settingsMap) {
  86.                 let widget = this._wndEngine.getWidget(toolbarItem.instanceID);
  87.                 for (let settingName in settingsMap)
  88.                     try {
  89.                         widget.applySetting(settingName, settingsMap[settingName]);
  90.                     }
  91.                     catch (e) {
  92.                         this._logger.warn("Could not apply widget setting '" + settingName + "'");
  93.                     }
  94.             }
  95.         }
  96.         
  97.         if (!dontPersist)
  98.             this._persistToolbarSet(destToolbar);
  99.         
  100.         return toolbarItem;
  101.     },
  102.     
  103.     removeItem: function wndCtrl_removeWidgetItem(DOMElementOrWidgetInstanceID, dontPersist) {
  104.         var widgetElement;
  105.         if (DOMElementOrWidgetInstanceID instanceof Components.interfaces.nsIDOMElement)
  106.             widgetElement = DOMElementOrWidgetInstanceID;
  107.         else if (typeof DOMElementOrWidgetInstanceID == "string")
  108.             widgetElement = this._wndEngine.getToolbarElement(DOMElementOrWidgetInstanceID);
  109.         else
  110.             throw new TypeError("Widget DOM element or instance ID string required");
  111.         
  112.         var toolbar = widgetElement.parentNode;
  113.         var palette = this._toolbox.palette;
  114.         
  115.         if (widgetElement.isXBWidget) {
  116.             if (widgetElement.isUnique) {
  117.                 let paletteItem = this._application.overlayProvider
  118.                     .makePaletteItem(document, widgetElement.prototype, 0);
  119.                 
  120.                 if (!palette.getElementsByAttribute("id", paletteItem.id).length)
  121.                     palette.appendChild(paletteItem);
  122.             }
  123.             
  124.             widgetElement.kill();
  125.             toolbar.removeChild(widgetElement);
  126.             
  127.         } else {
  128.             widgetElement = toolbar.removeChild(widgetElement);
  129.             if (["toolbarseparator", "toolbarspacer", "toolbarspring"].indexOf(widgetElement.localName) == -1) {
  130.                 if (!palette.getElementsByAttribute("id", widgetElement.id).length)
  131.                     palette.appendChild(widgetElement);
  132.             }
  133.         }
  134.         
  135.         if (!dontPersist)
  136.             this._persistToolbarSet(toolbar);
  137.     },
  138.     
  139.     removeWidgetsOfPackage: function wndCtrl_removeWidgetsOfPackage(packageID) {
  140.         for each (let toolbarItem in this.getAppWidgetItems(packageID, undefined))
  141.             this.removeItem(toolbarItem);
  142.     },
  143.     
  144.     removeWidgetsOfProto: function wndCtrl_removeWidgetsOfProto(protoID) {
  145.         for each (let toolbarItem in this.getAppWidgetItems(undefined, protoID))
  146.             this.removeItem(toolbarItem);
  147.     },
  148.     
  149.     removeItemById: function wndCtrl_removeItemById(aDOMElementId, aDontPersist) {
  150.         this.removeItem(document.getElementById(aDOMElementId), aDontPersist);
  151.     },
  152.     
  153.     setupWidget: function wndCtrl_setupWidget(widgetInstanceID) {
  154.         const winType = this._appName + ":Preferences";
  155.         
  156.         let prefWindow = this._barCore.Lib.misc.getTopWindowOfType(winType);
  157.         if (prefWindow) {
  158.             prefWindow.focus();
  159.             prefWindow.Preferences.selectWidget(widgetInstanceID);
  160.         } else {
  161.             let instantApply = this._barCore.Preferences.get("browser.preferences.instantApply", false);
  162.             let features = "chrome,titlebar,toolbar,centerscreen,resizable," + (instantApply ? "dialog=no" : "modal");
  163.             prefWindow = window.openDialog("chrome://" + this._appName + "/content/custombar/preferences/preferences.xul",
  164.                                            winType, features, "prefpane-customize", widgetInstanceID);
  165.         }
  166.     },
  167.     
  168.     clearToolbar: function wndCtrl_clearToolbar() {
  169.         Array.slice(this._appToolbar.childNodes)
  170.             .forEach(function(item) {
  171.                 if (!this.isPermanetToolbarItem(item))
  172.                     this.removeItem(item, true);
  173.             }, this);
  174.     },
  175.     
  176.     updatePalette: function wndCtrl_updatePalette(newPackagesIDs) {
  177.         var palette = this._toolbox.palette;
  178.         
  179.         let items = palette.getElementsByTagName("toolbaritem");
  180.         for (let i = items.length; i--;) {
  181.             let item = items.item(i);
  182.             if (item.getAttribute("xb-app") == this._application.name) {
  183.                 let protoID = item.getAttribute("xb-proto-id");
  184.                 let [packageID, ] = this._application.widgetLibrary.parseWidgetProtoID(protoID);
  185.                 let isNewPackage = newPackagesIDs.indexOf(packageID) >= 0;
  186.                 if (!this._application.widgetLibrary.isKnownWidget(protoID) || isNewPackage)
  187.                     item.parentNode.removeChild(item);
  188.             }
  189.         }
  190.         
  191.         for each (let packageId in newPackagesIDs) {
  192.             for each (let widgetProto in this._application.widgetLibrary.getWidgetProtos(packageId)) {
  193.                 if (document.getElementsByAttribute("xb-proto-id", widgetProto.id).length)
  194.                     continue;
  195.                 let widgetItem = this._application.overlayProvider.makePaletteItem(document, widgetProto, 0);
  196.                 palette.appendChild(widgetItem);
  197.             }
  198.         }
  199.     },
  200.     
  201.     deleteObsoleteWidgetItems: function wndCtrl_deleteObsoleteWidgetItems() {
  202.         var toolbars = {__proto__: null};
  203.         for each (let widgetItem in this.getAppWidgetItems()) {
  204.             if (this._application.widgetLibrary.isKnownWidget(widgetItem.prototypeID)) continue;
  205.             let toolbar = widgetItem.parentNode;
  206.             toolbars[toolbar.id] = toolbar;
  207.             widgetItem.kill();
  208.             widgetItem.parentNode.removeChild(widgetItem);
  209.         }
  210.         for each (let toolbar in toolbars)
  211.             this._persistToolbarSet(toolbar);
  212.     },
  213.     
  214.     isPermanetToolbarItem: function(node) {
  215.         let ids = [
  216.             "yasearch-search",
  217.             "yasearch-container",
  218.             "yasearch-bloggers",
  219.             "yasearch-translate"
  220.         ];
  221.         
  222.         let protoIds = [
  223.             "http://bar.yandex.ru/packages/yandexbar#town"
  224.         ];
  225.         
  226.         if (node.id && ids.indexOf(node.id) != -1)
  227.             return true;
  228.         
  229.         if (node.isXBWidget && (node.application == this._application) && protoIds.indexOf(node.prototypeID) != -1)
  230.             return true;
  231.         
  232.         let nodeLocalName = node.localName;
  233.         
  234.         if (["toolbarseparator", "toolbarspacer", "toolbarspring"].indexOf(nodeLocalName) != -1) {
  235.             let previousSiblingLocalName = node.previousSibling ? node.previousSibling.localName : null;
  236.             if (nodeLocalName != previousSiblingLocalName)
  237.                 return true;
  238.         }
  239.         
  240.         return false;
  241.     },
  242.     
  243.     getToolbarSpring: function() {
  244.         var item = this._appToolbar.firstChild;
  245.         while (item) {
  246.             if (item.localName == "toolbarspring")
  247.                 return item;
  248.             item = item.nextSibling;
  249.         }
  250.         
  251.         return this._appToolbar.insertItem("spring");
  252.     },
  253.     
  254.     setWidgets: function wndCtrl_setWidgets(widgetsInfo, placement) {
  255.         if (placement == "replace")
  256.             this.clearToolbar();
  257.         
  258.         var relativeElement,
  259.             placeAfter = (placement == "after");
  260.         
  261.         if (placeAfter) {
  262.             widgetsInfo.reverse();
  263.             relativeElement = this._appToolbar.lastChild;
  264.         } else {
  265.             relativeElement = this.getToolbarSpring();
  266.         }
  267.         
  268.         for each (let info in widgetsInfo)
  269.             this.addWidgetItem(info.protoID, info.settings, relativeElement, placeAfter);
  270.         
  271.         this._persistToolbarSet(this._appToolbar);
  272.     },
  273.     
  274.     handleXBAction: function wndCtrl_handleXBAction(WIID, command) {
  275.         this._logger.debug("handleXBAction " + [WIID, command]);
  276.         switch(command) {
  277.             case "reload-package":
  278.                 this._application.restartWidgets(this._wndEngine.getWidget(WIID).prototype.unit.unitPackage.id);
  279.                 break;
  280.         }
  281.     },
  282.     
  283.     handleEvent: function wndCtrl_handleEvent(event) {
  284.         switch (event.type) {
  285.             case "load":
  286.                 this._onOverlayLoaded(event);
  287.                 break;
  288.             case "unload":
  289.                 this._onOverlayUnload(event);
  290.                 break;
  291.         }
  292.     },
  293.     
  294.     switchWidgets: function wndCtrl_switchWidgets(packageID, on) {
  295.         for each (let widgetItem in this.getAppWidgetItems(packageID, undefined))
  296.             try {
  297.                 widgetItem.switchOn(on);
  298.             }
  299.             catch (e) {
  300.                 this._logger.error("Could not switch widget. " + this._application.core.Lib.misc.formatError(e));
  301.             }
  302.     },
  303.     
  304.     getAppWidgetItems: function wndCtrl_getAppWidgetItems(packageID, protoID, knownIDs) {
  305.         var items = this._toolbox.getElementsByTagName("toolbaritem");
  306.         var result = [];
  307.         for (let i = 0, len = items.length; i < len; i++) {
  308.             let item = items[i];
  309.             if (item.isXBWidget && (item.application == this._application)
  310.                 && (!protoID || item.prototypeID == protoID)
  311.                 && (!packageID || item.prototypeID.indexOf(packageID) == 0)
  312.                 || (knownIDs && knownIDs.indexOf(item.id) != -1) )
  313.                 result.push(item);
  314.         }
  315.         return result;
  316.     },
  317.     
  318.     _id: 0,
  319.     _appName: APP_NAME,
  320.     _barCore: null,
  321.     _application: null,
  322.     _logger: null,
  323.     _wndEngine: null,
  324.     _guiBuilder: null,
  325.     
  326.     _init: function() {
  327.         this._wndEngine = new this._barCore.Lib.XB.WindowEngine(this);
  328.         this._guiBuilder = new this._barCore.Lib.XB.UI.Builder(this._wndEngine, this);
  329.         
  330.         this._guiBuilder.registerExtActionsHelper(this);
  331.     },
  332.     
  333.     _onOverlayLoaded: function wndCtrl_onOverlayLoaded(loadEvent) {
  334.         this._logger.info("Overlay loaded");
  335.         
  336.         loadEvent.currentTarget.removeEventListener("load", this, false);
  337.         loadEvent.currentTarget.addEventListener("unload", this, false);
  338.         
  339.         this._application.onNewNavigatorReady(this);
  340.     },
  341.     
  342.     _onOverlayUnload: function wndCtrl_onOverlayUnload(unloadEvent) {
  343.         this._logger.info("Overlay unloads");
  344.         try {
  345.             unloadEvent.currentTarget.removeEventListener("unload", this, false);
  346.         }
  347.         finally {
  348.         }
  349.     },
  350.     
  351.     _persistToolbarSet: function wndCtrl_persistToolbarSet(toolbar) {
  352.         toolbar.setAttribute("currentset", toolbar.currentSet);
  353.         document.persist(toolbar.id, "currentset");
  354.     },
  355.     
  356.     get _toolbox() {
  357.         return document.getElementById("navigator-toolbox");
  358.     },
  359.     
  360.     get _appToolbar() {
  361.         return document.getElementById(this._application.name + "-bar");
  362.     }
  363. };
  364.  
  365. window[APP_NAME + "OverlayController"] = new OverlayControllerF();
  366. })();