home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / ServicePaneHelper.jsm < prev    next >
Text File  |  2012-06-08  |  7KB  |  221 lines

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2010 POTI, Inc.
  7.  * http://www.songbirdnest.com
  8.  *
  9.  * This file may be licensed under the terms of of the
  10.  * GNU General Public License Version 2 (the ``GPL'').
  11.  *
  12.  * Software distributed under the License is distributed
  13.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  14.  * express or implied. See the GPL for the specific language
  15.  * governing rights and limitations.
  16.  *
  17.  * You should have received a copy of the GPL along with this
  18.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  19.  * or write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  *
  22.  *=END SONGBIRD GPL
  23.  */
  24.  
  25. EXPORTED_SYMBOLS = [ "ServicePaneHelper" ];
  26.  
  27. /**
  28.  * \file ServicePaneHelper.jsm
  29.  * \brief helper functions for working with the service pane
  30.  */
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Cr = Components.results;
  35. const Ce = Components.Exception;
  36. const Cu = Components.utils;
  37.  
  38. /**
  39.  * \brief Helper function to retrieve visible badges for a service pane node.
  40.  *
  41.  * \param node sbIServicePaneNode to check
  42.  * \return array of badge IDs (empty if no badges visible)
  43.  */
  44. function getBadgesForNode(node)
  45. {
  46.   let attr = node.getAttribute("badges");
  47.   if (attr)
  48.     return attr.split(/\s+/);
  49.   else
  50.     return [];
  51. }
  52.  
  53. /**
  54.  * \brief counter used to generate unique IDs for badges where no ID is
  55.  *        supplied.
  56.  */
  57. var badgeIndex = 0;
  58.  
  59. /**
  60.  * \class ServicePaneBadge
  61.  *
  62.  * This is a tear-off object, only created when badge data needs to be
  63.  * manipulated. It doesn't store any data, real data is stored in node
  64.  * attributes.
  65.  *
  66.  * \param node node that the badge is tied to (becomes the node property
  67.  *        of the object)
  68.  * \param id badge ID (becomes the id property of the object)
  69.  */
  70. function ServicePaneBadge(node, id) {
  71.   if (!id)
  72.     throw "Badge ID is a mandatory parameter";
  73.   if (id && /\s/.test(id))
  74.     throw "Spaces are not allowed in badge ID";
  75.  
  76.   this.__defineGetter__("node", function() node);
  77.   this.__defineGetter__("id", function() id);
  78. }
  79. ServicePaneBadge.prototype = {
  80.   /**
  81.    * \brief Retrieves and changes the displayed label of the badge.
  82.    */
  83.   get label() {
  84.     return this.node.getAttribute("badge_" + this.id + "_label");
  85.   },
  86.   set label(value) {
  87.     this.node.setAttribute("badge_" + this.id + "_label", value);
  88.     return value;
  89.   },
  90.  
  91.   /**
  92.    * \brief Retrieves and changes the image URL to be displayed for the badge.
  93.    */
  94.   get image() {
  95.     return this.node.getAttribute("badge_" + this.id + "_image");
  96.   },
  97.   set image(value) {
  98.     this.node.setAttribute("badge_" + this.id + "_image", value);
  99.     return value;
  100.   },
  101.  
  102.   /**
  103.    * \brief Retrieves and changes the visibility of the badge.
  104.    *
  105.    * Changing this property to true will have the same result as calling
  106.    * append(). Changing it to false will make the badge invisible, its
  107.    * attributes will not be cleared however.
  108.    */
  109.   get visible() {
  110.     let badges = getBadgesForNode(this.node);
  111.     return (badges.indexOf(this.id) >= 0);
  112.   },
  113.   set visible(val) {
  114.     let badges = getBadgesForNode(this.node);
  115.     let currentIndex = badges.indexOf(this.id);
  116.     if (val) {
  117.       if (currentIndex < 0)
  118.         this.append();
  119.     }
  120.     else {
  121.       if (currentIndex >= 0) {
  122.         badges.splice(currentIndex, 1);
  123.         this.node.setAttribute("badges", badges.length ? badges.join(" ") : null);
  124.       }
  125.     }
  126.     return false;
  127.   },
  128.  
  129.   /**
  130.    * \brief Makes the current badge visible as the last badge of the node. Will
  131.    *        move the badge if it is already visible.
  132.    */
  133.   append: function()
  134.   {
  135.     this.insertBefore(null);
  136.   },
  137.  
  138.   /**
  139.    * \brief Makes the current badge visible by inserting it before another
  140.    *        badge. Will move the badge if it is already visible.
  141.    */
  142.   insertBefore: function(id) {
  143.     if (id && /\s/.test(id))
  144.       throw "No spaces allowed in badge ID";
  145.  
  146.     let badges = getBadgesForNode(this.node);
  147.  
  148.     let currentIndex = badges.indexOf(this.id);
  149.     if (currentIndex >= 0)
  150.       badges.splice(currentIndex, 1);
  151.  
  152.     let newIndex = (id ? badges.indexOf(id) : -1);
  153.     if (newIndex < 0)
  154.       newIndex = badges.length;
  155.     badges.splice(newIndex, 0, this.id);
  156.  
  157.     this.node.setAttribute("badges", badges.join(" "));
  158.   },
  159.  
  160.   /**
  161.    * \brief Removes the current badge and all node attributes associated with
  162.    *        it.
  163.    *
  164.    * Note: this will also unset the properties of the current badge object. If
  165.    * you only want to hide the badge use badge.visible = false.
  166.    */
  167.   remove: function() {
  168.     this.visible = false;
  169.     this.node.removeAttribute("badge_" + this.id + "_label");
  170.     this.node.removeAttribute("badge_" + this.id + "_image");
  171.   }
  172. };
  173.  
  174. var ServicePaneHelper = {
  175.   /**
  176.    * \brief Creates a new ServicePaneBadge object to help manipulate a badge
  177.    *        on a particular service pane node.
  178.    *
  179.    * The object returned is a temporary tear-off object, references to is
  180.    * should be released one it is no longer needed to allow garbage collection
  181.    * to pick it up. Note that two subsequent calls to getBadge() will not return
  182.    * the same object even though all properties will return the same values.
  183.    *
  184.    * If the badge doesn't exist yet a new one will be created. In this case a
  185.    * call to badge.append() or badge.insertBefore() is necessary to make it
  186.    * visible.
  187.    *
  188.    * \param node node that the badge is tied to
  189.    * \param id (optional) ID of the badge. If missing
  190.    *        an ID will be generated automatically. Supplied ID should not
  191.    *        contain any spaces or NUL characters.
  192.    * \return ServicePaneBadge object for the badge
  193.    */
  194.   getBadge: function(node, badgeID) {
  195.     if (!badgeID) {
  196.       // Generate badge ID automatically if none given, use NUL character to
  197.       // avoid clashes with user-supplied IDs
  198.       badgeID = "badge\x00" + badgeIndex++;
  199.     } else if (/\x00/.test(badgeID)) {
  200.       // NUL characters are reserved for generated IDs
  201.       throw "No NUL characters allowed in badge ID";
  202.     }
  203.  
  204.     return new ServicePaneBadge(node, badgeID);
  205.   },
  206.  
  207.   /**
  208.    * \brief Retrieves all visible badges of a node.
  209.    *
  210.    * The order in which the badges are returned is the order in which they
  211.    * should be displayed.
  212.    *
  213.    * \param node the node for which the badges should be retrieved
  214.    * \return iterator returning ServicePaneBadge objects
  215.    */
  216.   getAllBadges: function(node) {
  217.     let badges = getBadgesForNode(node);
  218.     return (new ServicePaneBadge(node, badgeID) for each (badgeID in badges));
  219.   }
  220. };
  221.