PageRenderTime 59ms CodeModel.GetById 12ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/content/customizeCharset.js

http://github.com/zpao/v8monkey
JavaScript | 304 lines | 229 code | 68 blank | 7 comment | 48 complexity | 191a9148b39b30a50592e94dac8df0f8 MD5 | raw file
  1//get prefInt services
  2
  3var availCharsetDict     = [];
  4var prefBranch           = null; //Preferences Interface
  5var pref_string_title    = "";
  6var pref_string_content  = "";
  7var pref_string_object   = null;
  8
  9function Init()
 10{
 11  var applicationArea = "";
 12
 13  if ("arguments" in window && window.arguments[0])
 14    applicationArea = window.arguments[0];
 15
 16  prefBranch = Components.classes["@mozilla.org/preferences-service;1"];
 17
 18  if (prefBranch) {
 19    prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch);
 20
 21    if (applicationArea.indexOf("mail") != -1) {
 22      pref_string_title = "intl.charsetmenu.mailedit";
 23    } else {
 24    //default is the browser
 25      pref_string_title = "intl.charsetmenu.browser.static";
 26    }
 27
 28    pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString)
 29    pref_string_content = pref_string_object.data;
 30
 31    AddRemoveLatin1('add');
 32  }
 33
 34  LoadAvailableCharSets();
 35  LoadActiveCharSets();
 36}
 37
 38
 39function readRDFString(aDS,aRes,aProp) 
 40{
 41  var n = aDS.GetTarget(aRes, aProp, true);
 42  if (n)
 43    return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
 44  else 
 45    return "";
 46}
 47
 48
 49function LoadAvailableCharSets()
 50{
 51  try {
 52    var available_charsets_listbox = document.getElementById('available_charsets');
 53    var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
 54    var kNC_Root = rdf.GetResource("NC:DecodersRoot");
 55    var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
 56    var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
 57    var rdfContainer =
 58      Components.classes["@mozilla.org/rdf/container;1"]
 59                .createInstance(Components.interfaces.nsIRDFContainer);
 60
 61    rdfContainer.Init(rdfDataSource, kNC_Root);
 62    var availableCharsets = rdfContainer.GetElements();
 63    var charset;
 64
 65    for (var i = 0; i < rdfContainer.GetCount(); i++) {
 66      charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
 67      availCharsetDict[i] = new Array(2);
 68      availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
 69      availCharsetDict[i][1] = charset.Value;
 70
 71      AddListItem(document,
 72                  available_charsets_listbox,
 73                  availCharsetDict[i][1],
 74                  availCharsetDict[i][0]);
 75    }
 76  }
 77  catch (e) {}
 78}
 79
 80
 81function GetCharSetTitle(id)
 82{
 83  if (availCharsetDict) {
 84    for (var j = 0; j < availCharsetDict.length; j++) {
 85      if (availCharsetDict[j][1] == id) {
 86        return availCharsetDict[j][0];
 87      }
 88    }
 89  }
 90  return '';
 91}
 92
 93function AddRemoveLatin1(action)
 94{
 95  var arrayOfPrefs = [];
 96  arrayOfPrefs = pref_string_content.split(', ');
 97
 98  if (arrayOfPrefs.length > 0) {
 99    for (var i = 0; i < arrayOfPrefs.length; i++) {
100      if (arrayOfPrefs[i] == 'ISO-8859-1') {
101        if (action == 'remove') {
102          arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
103          arrayOfPrefs.length = arrayOfPrefs.length - 1;
104        }
105
106        pref_string_content = arrayOfPrefs.join(', ');
107        return;
108      }
109    }
110
111    if (action == 'add') {
112      arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
113      pref_string_content = arrayOfPrefs.join(', ');
114    }
115  }
116}
117
118
119function LoadActiveCharSets()
120{
121  var active_charsets = document.getElementById('active_charsets');
122  var arrayOfPrefs = [];
123  var str;
124  var tit;
125
126  arrayOfPrefs = pref_string_content.split(', ');
127
128  if (arrayOfPrefs.length > 0) {
129    for (var i = 0; i < arrayOfPrefs.length; i++) {
130      str = arrayOfPrefs[i];
131      tit = GetCharSetTitle(str);
132      if (str && tit)
133        AddListItem(document, active_charsets, str, tit);
134    }
135  }
136}
137
138function enable_save()
139{
140  var save_button = document.documentElement.getButton("accept");
141  save_button.removeAttribute('disabled');
142}
143
144
145function update_buttons()
146{
147  var available_charsets = document.getElementById('available_charsets');
148  var active_charsets = document.getElementById('active_charsets');
149  var remove_button = document.getElementById('remove_button');
150  var add_button = document.getElementById('add_button');
151  var up_button = document.getElementById('up_button');
152  var down_button = document.getElementById('down_button');
153
154  var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
155  remove_button.disabled = !activeCharsetSelected;
156
157  if (activeCharsetSelected) {
158    up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
159    down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
160  }
161  else {
162    up_button.disabled = true;
163    down_button.disabled = true;
164  }
165
166  add_button.disabled = (available_charsets.selectedItems.length == 0);
167}
168
169
170
171function AddAvailableCharset()
172{
173  var active_charsets = document.getElementById('active_charsets');
174  var available_charsets = document.getElementById('available_charsets');
175
176  for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
177  {
178    var selItem =  available_charsets.selectedItems[nodeIndex];
179    
180    var charsetname  = selItem.label;
181    var charsetid = selItem.id;
182    var already_active = false;
183
184    for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
185      if (charsetid == item.id) {
186        already_active = true;
187        break;
188      }//if
189
190    }//for
191
192    if (already_active == false) {
193      AddListItem(document, active_charsets, charsetid, charsetname);
194    }//if
195
196  }//for
197
198  available_charsets.clearSelection();
199  enable_save();
200
201} //AddAvailableCharset
202
203
204
205function RemoveActiveCharset()
206{
207  var listbox = document.getElementById('active_charsets');
208  var numSelectedItems = listbox.selectedItems.length;
209
210  for (var count = 0; count < numSelectedItems; count ++) {
211    listbox.removeChild(listbox.selectedItems[0]);
212  }
213
214  listbox.clearSelection();
215
216  enable_save();
217} //RemoveActiveCharset
218
219
220
221function Save()
222{
223  // Iterate through the 'active charsets  tree to collect the charsets
224  // that the user has chosen.
225
226  var active_charsets = document.getElementById('active_charsets');
227
228  var charsetid    = "";
229  var num_charsets = 0;
230  var pref_string_content = '';
231
232  for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
233    charsetid = item.id;
234
235    if (charsetid.length > 1) {
236      num_charsets++;
237
238      //separate >1 charsets by commas
239      if (num_charsets > 1)
240        pref_string_content = pref_string_content + ", " + charsetid;
241      else
242        pref_string_content = charsetid;
243    }
244  }
245
246  try
247  {
248    if (prefBranch) {
249      pref_string_object.data = pref_string_content;
250      prefBranch.setComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString, pref_string_object);
251      window.close();
252    }
253  }
254  catch(ex) {
255    confirm('exception' + ex);
256  }
257  return true;
258} //Save
259
260
261function MoveUp() {
262  var listbox = document.getElementById('active_charsets');
263  if (listbox.selectedItems.length == 1) {
264    var selected = listbox.selectedItems[0];
265    var before = selected.previousSibling
266    if (before) {
267      listbox.insertBefore(selected, before);
268      listbox.selectItem(selected);
269      listbox.ensureElementIsVisible(selected);
270    }
271  }
272
273  enable_save();
274} //MoveUp
275
276
277
278function MoveDown() {
279  var listbox = document.getElementById('active_charsets');
280  if (listbox.selectedItems.length == 1) {
281    var selected = listbox.selectedItems[0];
282    if (selected.nextSibling) {
283      if (selected.nextSibling.nextSibling)
284        listbox.insertBefore(selected, selected.nextSibling.nextSibling);
285      else
286        selected.parentNode.appendChild(selected);
287      listbox.selectItem(selected);
288    }
289  }
290
291  enable_save();
292} //MoveDown
293
294function AddListItem(doc, listbox, ID, UIstring)
295{
296  // Create a treerow for the new item
297  var item = doc.createElement('listitem');
298
299  // Copy over the attributes
300  item.setAttribute('label', UIstring);
301  item.setAttribute('id', ID);
302
303  listbox.appendChild(item);
304}