ServicePaneHelper.jsm
Go to the documentation of this file.
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 
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 
45 {
46  let attr = node.getAttribute("badges");
47  if (attr)
48  return attr.split(/\s+/);
49  else
50  return [];
51 }
52 
57 var badgeIndex = 0;
58 
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 = {
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 
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 
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 
133  append: function()
134  {
135  this.insertBefore(null);
136  },
137 
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 
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 
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 
216  getAllBadges: function(node) {
217  let badges = getBadgesForNode(node);
218  return (new ServicePaneBadge(node, badgeID) for each (badgeID in badges));
219  }
220 };
function ServicePaneBadge(node, id)
EXPORTED_SYMBOLS
menuItem id
Definition: FeedWriter.js:971
const Ce
const Cr
var badgeIndex
counter used to generate unique IDs for badges where no ID is supplied.
function getBadgesForNode(node)
Helper function to retrieve visible badges for a service pane node.
this _contentSandbox label
Definition: FeedWriter.js:814
gTestRoot append("_tests")
const Ci
this _dialogInput val(dateText)
const Cu
return null
Definition: FeedWriter.js:1143
let node
countRef value
Definition: FeedWriter.js:1423
__defineGetter__("Application", function(){delete this.Application;return this.Application=Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);})
const Cc
var ServicePaneHelper