home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / chrome / extension.jar / content / application / Sharing.js < prev   
Encoding:
Text File  |  2010-02-10  |  5.1 KB  |  153 lines

  1. Glydo.Sharing = Prototype.Class.create({
  2.     initialize: function(application) {
  3.         this.application = application;
  4.         this.localizedStrings = new Glydo.Utils.LocalizedBundle(document);
  5.         this.url = null;
  6.         this.title = null;
  7.         this.buildPopups();
  8.     },
  9.  
  10.     buildPopups: function() {
  11.         var doc = this.application.window.document;
  12.         var elems = doc.getElementsByClassName("glydo-sharing-popup");
  13.         for (var i = 0; i < elems.length; ++i) {
  14.             var elem = elems[i];
  15.             var kind = elem.getAttribute("rel");
  16.             this.buildPopup(doc,elem,kind);
  17.         }
  18.     },
  19.     
  20.     buildPopup: function(doc,root,kind) {
  21.         var nServices = Glydo.Sharing.GLOBAL_SETTINGS.services.length;
  22.         for (var i = 0; i < nServices; ++i) {
  23.             var service = Glydo.Sharing.GLOBAL_SETTINGS.services[i];
  24.             this.addItemToPopup(doc,root,service,kind);
  25.         }
  26.         this.addItemToPopup(doc,root,null,kind);
  27.     },
  28.  
  29.     addItemToPopup: function(doc,root,service,kind) {
  30.         var item = doc.createElement('menuitem');
  31.         item.className = "menuitem-iconic glydo-sharing-item glydo-sharing-" + (service ? "item-" + service : "more");
  32.         item.setAttribute("label",this.localizedStrings.get("sharing." + (service ? "item." + service : "more") + ".label"));
  33.         item.addEventListener("command",Prototype.F.bind(Prototype.F.curry(this.share,service,kind),this),false);
  34.         root.appendChild(item);
  35.     },
  36.     
  37.     onMenu: function(event) {
  38.         if (event.type == "popupshowing") {
  39.             var popupNode = document.popupNode;
  40.             if (!popupNode) {
  41.                 return false;
  42.             }
  43.             if (popupNode.rec) {
  44.                 this.application.detachedPopupsManager.onContextMenu(event);
  45.             } else {
  46.                 var doc = this.application.window.content.document;
  47.                 if (doc) {
  48.                     var url = doc.documentURI;
  49.                     if (!Prototype.S.startsWith(url,"http://") && !Prototype.S.startsWith(url,"https://")) {
  50.                         Glydo.Utils.promptService().alert(null,this.localizedStrings.get("sharing.blocked_page.title"),this.localizedStrings.get("sharing.blocked_page.message"));
  51.                         return false;
  52.                     }
  53.                 }
  54.             }
  55.             return true;
  56.         }
  57.     },
  58.     
  59.     share: function(service,kind) {
  60.         var url;
  61.         var title;
  62.         if (kind == "recommendation") {
  63.             var popupNode = this.application.getContextRecommendationLink();
  64.             if (!popupNode) {
  65.                 return;
  66.             }
  67.             var rec = popupNode.rec;
  68.             url = popupNode.href;
  69.             title = Glydo.Utils.unescapeHTMLIfNecessary(popupNode.rec.title);
  70.             if (rec) {
  71.                 var rek = Glydo.Utils.getAncestorOrSelfAttributeOrProperty(popupNode,'realEstateKind');
  72.                 var rep = Glydo.Utils.getAncestorOrSelfAggregateAttributeOrProperty(popupNode,'realEstatePosition');
  73.                 Glydo.LOGGING_DB.logRecAction(rec.recSetId,rec.id,"SHARE",service,rek,rep);
  74.             }
  75.         } else if (kind == "current"){
  76.             var doc = this.application.window.content.document;
  77.             if (!doc) {
  78.                 return;
  79.             }
  80.             url = url || doc.documentURI;
  81.             title = title || doc.title;
  82.  
  83.             if (!Prototype.S.startsWith(url,"http://") && !Prototype.S.startsWith(url,"https://")) {
  84.                 Glydo.Utils.promptService().alert(null,this.localizedStrings.get("sharing.blocked_page.title"),this.localizedStrings.get("sharing.blocked_page.message"));
  85.                 return;
  86.             }
  87.             Glydo.LOGGING_DB.logUserEvent("application.sharing.current",
  88.                     "share", { service: service }, null);
  89.         } else if (kind == "app"){
  90.             url = "http://www.glydo.com/";
  91.             Glydo.LOGGING_DB.logUserEvent("application.sharing.app",
  92.                     "share", { service: service }, null);
  93.         } else {
  94.             return;
  95.         }
  96.         if (url) {
  97.             var template = this.getTemplate(service,kind);
  98.             var title = this.getTitle(title,service,kind);
  99.             Glydo.Sharing.share(url,title,service,template);
  100.         }
  101.     },
  102.     
  103.     getTemplate: function(service,kind) {
  104.         if (!service) {
  105.             return null;
  106.         }
  107.         var ret = this.localizedStrings.get('sharing.' + kind + '.item.'+service+'.template');
  108.         ret = ret || this.localizedStrings.get('sharing.' + kind + '.defaults.template');
  109.         return ret;
  110.     },
  111.     
  112.     getTitle: function(title,service,kind) {
  113.         if (!service) {
  114.             return null;
  115.         }
  116.         if (!title) {
  117.             return this.localizedStrings.get('sharing.' + kind + '.defaults.notitle');
  118.         }
  119.         var ret = this.localizedStrings.get('sharing.' + kind + '.item.'+service+'.title',[title]);
  120.         ret = ret || this.localizedStrings.get('sharing.' + kind + '.defaults.title',[title]);
  121.         return ret;
  122.     }
  123.  
  124. });
  125.  
  126. // Global sharing function
  127. Glydo.Sharing.share = function(url,title,service,template) {
  128.     if (!url) {
  129.         return;
  130.     }
  131.     var encURL = encodeURIComponent(url);
  132.     var shareURL = Glydo.Sharing.GLOBAL_SETTINGS.baseShareURL + "&pub=" + Glydo.Sharing.GLOBAL_SETTINGS.encAccount + "&url=" + encURL;
  133.     if (service) {
  134.         var encService = encodeURIComponent(service);
  135.         shareURL +=  "&s=" + encService;
  136.     }
  137.     if (title !== null && title !== undefined) {
  138.         var encTitle = encodeURIComponent(title);
  139.         shareURL += "&title=" + encTitle;
  140.     }
  141.     if (template) {
  142.         var encTmpl = encodeURIComponent(template);
  143.         shareURL += "&template=" + encTmpl;
  144.     }
  145.     Glydo.WindowUtils.goToURL(window,shareURL,'new-tab');
  146. }
  147.  
  148. //FIXME: Templates should be localized
  149. Glydo.Sharing.GLOBAL_SETTINGS = ({
  150.     services: ['twitter','facebook','myspace','bebo','buzz','digg','delicious','friendfeed','google','live'],
  151.     encAccount: "glydo",
  152.     baseShareURL: "http://www.addthis.com/bookmark.php?v=250"
  153. });