home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 March / Chip_2011.03_CD.iso / Tools / yabar.msi / fil79A9DC82B1FC7D845EA9EE47B9880F8D < prev    next >
Encoding:
Text File  |  2010-04-16  |  6.9 KB  |  205 lines

  1. function YaSubSize2Content() {
  2.   try {
  3.     window.sizeToContent();
  4.     window.resizeTo(window.outerWidth, window.outerHeight + 1);//fx bug
  5.   } catch(e) {}
  6. }
  7.  
  8. var YaSubPrefs = {
  9.   accepted: false,
  10.   
  11.   observerService: Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService),
  12.   
  13.   resizeWindow: function() {
  14.     YaSubSize2Content();
  15.   },
  16.   
  17.   savedShowCityLabel: true,
  18.   
  19.   get currentShowCityLabel() {
  20.     return _YaPrefs.nsIYa.prefs.showCityLabel;
  21.   },
  22.   
  23.   set currentShowCityLabel(val) {
  24.     _YaPrefs.nsIYa.prefs.showCityLabel = val;
  25.     return this.currentShowCityLabel;
  26.   },
  27.   
  28.   autoLabelAppendix: _YaPrefs.stringBundle.getString("CityAuto"),
  29.   unknownCityLabel:  _YaPrefs.stringBundle.getString("CityUnknown"),
  30.   
  31.   autoCityElements: {},
  32.   
  33.   changeState: function(aState, aId) {
  34.     var observer = _YaPrefs.nsIYa.yaCity._observers[aId];
  35.     if (observer && observer.nodes)
  36.       for each (var node in observer.nodes)
  37.         node.setAttribute("yaCustomizedVisible", aState);
  38.   },
  39.   
  40.   observe: function(aSubject, aTopic, aState) {
  41.     var observer = _YaPrefs.nsIYa.yaCity._observers[aState];
  42.     var props = _YaPrefs.nsIYa.yaCity.getCityProps(observer.nodes[0].id);
  43.     var chk = document.getElementById("yasearch-city-" + props.buttonId + "-node");
  44.     
  45.     if (!chk)
  46.       return;
  47.     
  48.     if (aTopic == "Ya-City-Data-Changed") {
  49.       chk.setAttribute("label", this._getLabelFromCityProps(props));
  50.     } else if (aTopic == "Ya-City-Item-OnRemove") {
  51.       chk.parentNode.removeChild(chk);
  52.     }
  53.     
  54.     YaSubSize2Content();
  55.   },
  56.   
  57.   _getLabelFromCityProps: function(aProps) {
  58.     let label = aProps.label;
  59.     if (aProps.cityId == "")
  60.       label += " " + this.autoLabelAppendix;
  61.     
  62.     if (label == "")
  63.       label = this.unknownCityLabel;
  64.     
  65.     return label;
  66.   },
  67.   
  68.   changeLabelFromCityWindow: function(aId, aLabel) {
  69.     var chk = document.getElementById(aId + "-node");
  70.     if (!chk)
  71.       return;
  72.     
  73.     if (!aLabel) {
  74.       let props = _YaPrefs.nsIYa.yaCity.getCityProps(aId);
  75.       aLabel = this._getLabelFromCityProps(props);
  76.     }
  77.     
  78.     chk.setAttribute("label", aLabel);
  79.     YaSubSize2Content();
  80.   },
  81.   
  82.   beforeAccept: function() {
  83.     this.accepted = true;
  84.     return true;
  85.   },
  86.   
  87.   beforeUnload: function() {
  88.     this.observerService.removeObserver(this, "Ya-City-Data-Changed");
  89.     this.observerService.removeObserver(this, "Ya-City-Item-OnRemove");
  90.     
  91.     if (!this.accepted) {
  92.       for each (var chk in document.getElementById("yasearch.exists.city.buttons").getElementsByTagName("checkbox")) {
  93.         if (chk.hasAttribute) {
  94.           var id = chk.id.replace(/^yasearch\-city\-/, "").replace(/\-node$/, "");
  95.           if (!chk.hasAttribute("visibleOnEnter")) {
  96.             for each (var node in _YaPrefs.nsIYa.yaCity._observers[id].nodes)
  97.               node.parentNode.removeChild(node);
  98.           } else {
  99.             this.changeState(true, id);
  100.           }
  101.         }
  102.       }
  103.       
  104.       if (this.currentShowCityLabel != this.savedShowCityLabel)
  105.         this.currentShowCityLabel = this.savedShowCityLabel;
  106.     } else {
  107.       var checkedBoxes = document.getElementById("yasearch.exists.city.buttons").getElementsByAttribute("checked", "true");
  108.       if (!checkedBoxes[0])
  109.         _YaPrefs.changeElementState("yasearch-city", false);
  110.     }
  111.     
  112.     _YaPrefs.nsIYa.yaCity.updateObservers();
  113.   },
  114.   
  115.   init: function() {
  116.     this.savedShowCityLabel = this.currentShowCityLabel;
  117.     
  118.     this.observerService.addObserver(this, "Ya-City-Data-Changed", false);
  119.     this.observerService.addObserver(this, "Ya-City-Item-OnRemove", false);
  120.     
  121.     var listBox = document.getElementById("yasearch.exists.city.buttons");
  122.     while (listBox.firstChild)
  123.       listBox.removeChild(listBox.firstChild);
  124.     
  125.     var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  126.     
  127.     for each (var observer in _YaPrefs.nsIYa.yaCity._observers) {
  128.       var node = observer.nodes[0];
  129.       if (node.getAttribute("yaCustomizedVisible") == "true") {
  130.         var props = _YaPrefs.nsIYa.yaCity.getCityProps(node.id);
  131.         var label = props.label;
  132.         
  133.         if (props.cityId == "")
  134.           label += " " + this.autoLabelAppendix;
  135.         
  136.         if (props.labelAlias)
  137.           label = "(" + props.labelAlias + ") " + label;
  138.         
  139.         if (label == "")
  140.           label = this.unknownCityLabel;
  141.         
  142.         var chk = document.createElementNS(XULNS, "checkbox");
  143.         chk.setAttribute("id", "yasearch-city-" + props.buttonId + "-node");
  144.         chk.setAttribute("label", label);
  145.         chk.setAttribute("checked", "true");
  146.         chk.setAttribute("visibleOnEnter", "true");
  147.         chk.setAttribute("oncommand", "YaSubPrefs.changeState(this.checked, '" + props.buttonId + "');");
  148.         listBox.appendChild(chk);
  149.       }
  150.     }
  151.   },
  152.   
  153.   addCityButton: function() {
  154.     var defaultSet = _YaPrefs.gYaToolbar.getAttribute("defaultset");
  155.     
  156.     var pid = "yasearch-city";
  157.     var item = _YaPrefs.customizedElements[pid];
  158.     
  159.     for each (var browser in _YaPrefs.browsers) {
  160.       var yaBar = browser.document.getElementById("yasearch-bar");
  161.       
  162.       var el = item.prev,
  163.           found = false;
  164.       
  165.       while (el && !found) {
  166.         found = yaBar.getElementsByAttribute("_yaPID", el.pid)[0] || false;
  167.         el = el.prev;
  168.       }
  169.       
  170.       if (found && found.nextSibling) {
  171.         var prevFromDefault = new RegExp("([^,]*)," + pid).exec(defaultSet)[1];
  172.         
  173.         if (prevFromDefault && (prevFromDefault == "spring" || prevFromDefault == "separator")) {
  174.           prevFromDefault = "toolbar" + prevFromDefault;
  175.           while (found.nextSibling && found.nextSibling.localName == prevFromDefault)
  176.             found = found.nextSibling;
  177.         }
  178.       }
  179.       
  180.       var beforeElt = found ? (found.nextSibling ? found.nextSibling : false) :
  181.                               (yaBar.firstChild ? yaBar.firstChild : false);
  182.       
  183.       var [id, clone] = _YaPrefs.nsIYa.yaOverlay.insertToolbarItem("yasearch-city", beforeElt, browser);
  184.       if (!clone)
  185.         continue;
  186.       
  187.       clone.setAttribute("yaCustomizedVisible", true);
  188.       clone.setAttribute("_yaPID", pid);
  189.       item.toolbarItems.push(clone);
  190.     }
  191.     
  192.     var chk = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "checkbox");
  193.     chk.setAttribute("label", this.autoLabelAppendix);
  194.     chk.setAttribute("checked", "true");
  195.     chk.setAttribute("id", id + "-node");
  196.     chk.setAttribute("oncommand", "YaSubPrefs.changeState(this.checked, '" + id.replace(/^yasearch\-city\-/, "") + "');");
  197.     
  198.     document.getElementById("yasearch.exists.city.buttons").appendChild(chk);
  199.     
  200.     window.openDialog(YaUtils.CHROME_CONTENT + "city/city-settings.xul", "",
  201.                       "centerscreen,chrome,dialog,resizable,dependent", id, true);
  202.     
  203.     setTimeout("YaSubSize2Content",100);
  204.   }
  205. };