~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/editor/ui/dialogs/content/EdAdvancedEdit.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Netscape Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/NPL/
 
6
 *
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 *
 
12
 * The Original Code is Mozilla Communicator client code, released
 
13
 * March 31, 1998.
 
14
 *
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation. Portions created by Netscape are
 
17
 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
 
18
 * Rights Reserved.
 
19
 *
 
20
 * Contributor(s):
 
21
 *    Ben "Count XULula" Goodger
 
22
 *    Charles Manske (cmanske@netscape.com)
 
23
 *    Neil Rashbrook (neil@parkwaycc.co.uk)
 
24
 */
 
25
 
 
26
/**************         GLOBALS         **************/
 
27
var gElement    = null; // handle to actual element edited
 
28
 
 
29
var HTMLAttrs   = [];   // html attributes
 
30
var CSSAttrs    = [];   // css attributes
 
31
var JSEAttrs    = [];   // js events
 
32
 
 
33
var HTMLRAttrs  = [];   // removed html attributes
 
34
var JSERAttrs   = [];   // removed js events
 
35
 
 
36
/* Set false to allow changing selection in tree
 
37
   without doing "onselect" handler actions
 
38
*/
 
39
var gDoOnSelectTree = true;
 
40
var gUpdateTreeValue = true;
 
41
 
 
42
/************** INITIALISATION && SETUP **************/
 
43
 
 
44
/**
 
45
 * function   : void Startup();
 
46
 * parameters : none
 
47
 * returns    : none
 
48
 * desc.      : startup and initialisation, prepares dialog.
 
49
 **/
 
50
function Startup()
 
51
{
 
52
  var editor = GetCurrentEditor();
 
53
 
 
54
  // Element to edit is passed in
 
55
  if (!editor || !window.arguments[1])
 
56
  {
 
57
    dump("Advanced Edit: No editor or element to edit not supplied\n");
 
58
    window.close();
 
59
    return;
 
60
  }
 
61
  // This is the return value for the parent,
 
62
  // who only needs to know if OK was clicked
 
63
  window.opener.AdvancedEditOK = false;
 
64
 
 
65
  // The actual element edited (not a copy!)
 
66
  gElement = window.arguments[1];
 
67
 
 
68
  // place the tag name in the header
 
69
  var tagLabel = document.getElementById("tagLabel");
 
70
  tagLabel.setAttribute("value", ("<" + gElement.localName + ">"));
 
71
 
 
72
  // Create dialog object to store controls for easy access
 
73
  gDialog.AddHTMLAttributeNameInput  = document.getElementById("AddHTMLAttributeNameInput");
 
74
 
 
75
  // We use a <deck> to switch between editable menulist and textbox
 
76
  gDialog.AddHTMLAttributeValueDeck     = document.getElementById("AddHTMLAttributeValueDeck");
 
77
  gDialog.AddHTMLAttributeValueMenulist = document.getElementById("AddHTMLAttributeValueMenulist");
 
78
  gDialog.AddHTMLAttributeValueTextbox  = document.getElementById("AddHTMLAttributeValueTextbox");
 
79
  gDialog.AddHTMLAttributeValueInput    = gDialog.AddHTMLAttributeValueTextbox;
 
80
 
 
81
  gDialog.AddHTMLAttributeTree          = document.getElementById("HTMLATree");
 
82
  gDialog.AddCSSAttributeNameInput      = document.getElementById("AddCSSAttributeNameInput");
 
83
  gDialog.AddCSSAttributeValueInput     = document.getElementById("AddCSSAttributeValueInput");
 
84
  gDialog.AddCSSAttributeTree           = document.getElementById("CSSATree");
 
85
  gDialog.AddJSEAttributeNameList       = document.getElementById("AddJSEAttributeNameList");
 
86
  gDialog.AddJSEAttributeValueInput     = document.getElementById("AddJSEAttributeValueInput");
 
87
  gDialog.AddJSEAttributeTree           = document.getElementById("JSEATree");
 
88
  gDialog.okButton                      = document.documentElement.getButton("accept");
 
89
 
 
90
  // build the attribute trees
 
91
  BuildHTMLAttributeTable();
 
92
  BuildCSSAttributeTable();
 
93
  BuildJSEAttributeTable();
 
94
  
 
95
  // Build attribute name arrays for menulists
 
96
  BuildJSEAttributeNameList();
 
97
  BuildHTMLAttributeNameList();
 
98
  // No menulists for CSS panel (yet)
 
99
 
 
100
  // Set focus to Name editable menulist in HTML panel
 
101
  SetTextboxFocus(gDialog.AddHTMLAttributeNameInput);
 
102
 
 
103
  // size the dialog properly
 
104
  window.sizeToContent();
 
105
 
 
106
  SetWindowLocation();
 
107
}
 
108
 
 
109
/**
 
110
 * function   : bool onAccept ( void );
 
111
 * parameters : none
 
112
 * returns    : boolean true to close the window
 
113
 * desc.      : event handler for ok button
 
114
 **/
 
115
function onAccept()
 
116
{
 
117
  var editor = GetCurrentEditor();
 
118
  editor.beginTransaction();
 
119
  try {
 
120
    // Update our gElement attributes
 
121
    UpdateHTMLAttributes();
 
122
    UpdateCSSAttributes();
 
123
    UpdateJSEAttributes();
 
124
  } catch(ex) {
 
125
    dump(ex);
 
126
  }
 
127
  editor.endTransaction();
 
128
 
 
129
  window.opener.AdvancedEditOK = true;
 
130
  SaveWindowLocation();
 
131
 
 
132
  return true; // do close the window
 
133
}
 
134
 
 
135
// Helpers for removing and setting attributes
 
136
// Use editor transactions if modifying the element already in the document
 
137
// (Temporary element from a property dialog won't have a parent node)
 
138
function doRemoveAttribute(attrib)
 
139
{
 
140
  try {
 
141
    var editor = GetCurrentEditor();
 
142
    if (gElement.parentNode)
 
143
      editor.removeAttribute(gElement, attrib);
 
144
    else
 
145
      gElement.removeAttribute(attrib);
 
146
  } catch(ex) {}
 
147
}
 
148
 
 
149
function doSetAttribute(attrib, value)
 
150
{
 
151
  try {
 
152
    var editor = GetCurrentEditor();
 
153
    if (gElement.parentNode)
 
154
      editor.setAttribute(gElement, attrib, value);
 
155
    else
 
156
      gElement.setAttribute(attrib, value);
 
157
  } catch(ex) {}
 
158
}
 
159
 
 
160
/**
 
161
 * function   : bool CheckAttributeNameSimilarity ( string attName, array attArray );
 
162
 * parameters : attribute to look for, array of current attributes
 
163
 * returns    : true if attribute already exists, false if it does not
 
164
 * desc.      : checks to see if any other attributes by the same name as the arg supplied
 
165
 *              already exist.
 
166
 **/
 
167
function CheckAttributeNameSimilarity(attName, attArray)
 
168
{
 
169
  for (var i = 0; i < attArray.length; i++)
 
170
  {
 
171
    if (attName.toLowerCase() == attArray[i].toLowerCase())
 
172
      return true;
 
173
  }
 
174
  return false;
 
175
}
 
176
 
 
177
/**
 
178
 * function   : bool UpdateExistingAttribute ( string attName, string attValue, string treeChildrenId );
 
179
 * parameters : attribute to look for, new value, ID of <treeChildren> node in XUL tree
 
180
 * returns    : true if attribute already exists in tree, false if it does not
 
181
 * desc.      : checks to see if any other attributes by the same name as the arg supplied
 
182
 *              already exist while setting the associated value if different from current value
 
183
 **/
 
184
function UpdateExistingAttribute( attName, attValue, treeChildrenId )
 
185
{
 
186
  var treeChildren = document.getElementById(treeChildrenId);
 
187
  if (!treeChildren)
 
188
    return false;
 
189
 
 
190
  var name;
 
191
  var i;
 
192
  attName = TrimString(attName).toLowerCase();
 
193
  attValue = TrimString(attValue);
 
194
 
 
195
  for (i = 0; i < treeChildren.childNodes.length; i++)
 
196
  {
 
197
    var item = treeChildren.childNodes[i];
 
198
    name = GetTreeItemAttributeStr(item);
 
199
    if (name.toLowerCase() == attName)
 
200
    {
 
201
      // Set the text in the "value' column treecell
 
202
      SetTreeItemValueStr(item, attValue);
 
203
 
 
204
      // Select item just changed, 
 
205
      //  but don't trigger the tree's onSelect handler
 
206
      gDoOnSelectTree = false;
 
207
      try {
 
208
        selectTreeItem(treeChildren, item);
 
209
      } catch (e) {}
 
210
      gDoOnSelectTree = true;
 
211
 
 
212
      return true;
 
213
    }
 
214
  }
 
215
  return false;
 
216
}
 
217
 
 
218
/**
 
219
 * function   : string GetAndSelectExistingAttributeValue ( string attName, string treeChildrenId );
 
220
 * parameters : attribute to look for, ID of <treeChildren> node in XUL tree
 
221
 * returns    : value in from the tree or empty string if name not found
 
222
 **/
 
223
function GetAndSelectExistingAttributeValue( attName, treeChildrenId )
 
224
{
 
225
  if (!attName)
 
226
    return "";
 
227
 
 
228
  var treeChildren = document.getElementById(treeChildrenId);
 
229
  var name;
 
230
  var i;
 
231
 
 
232
  for (i = 0; i < treeChildren.childNodes.length; i++)
 
233
  {
 
234
    var item = treeChildren.childNodes[i];
 
235
    name = GetTreeItemAttributeStr(item);
 
236
    if (name.toLowerCase() == attName.toLowerCase())
 
237
    {
 
238
      // Select item in the tree
 
239
      //  but don't trigger the tree's onSelect handler
 
240
      gDoOnSelectTree = false;
 
241
      try {
 
242
        selectTreeItem(treeChildren, item);
 
243
      } catch (e) {}
 
244
      gDoOnSelectTree = true;
 
245
 
 
246
      // Get the text in the "value' column treecell
 
247
      return GetTreeItemValueStr(item);
 
248
    }
 
249
  }
 
250
 
 
251
  // Attribute doesn't exist in tree, so remove selection
 
252
  gDoOnSelectTree = false;
 
253
  try {
 
254
    treeChildren.parentNode.treeBoxObject.selection.clearSelection();
 
255
  } catch (e) {}
 
256
  gDoOnSelectTree = true;
 
257
 
 
258
  return "";
 
259
}
 
260
 
 
261
/* Tree structure: 
 
262
  <treeItem>
 
263
    <treeRow>
 
264
      <treeCell> // Name Cell
 
265
      <treeCell  // Value Cell
 
266
*/
 
267
function GetTreeItemAttributeStr(treeItem)
 
268
{
 
269
  if (treeItem)
 
270
    return TrimString(treeItem.firstChild.firstChild.getAttribute("label"));
 
271
 
 
272
  return "";
 
273
}
 
274
 
 
275
function GetTreeItemValueStr(treeItem)
 
276
{
 
277
  if (treeItem)
 
278
    return TrimString(treeItem.firstChild.lastChild.getAttribute("label"));
 
279
 
 
280
  return "";
 
281
}
 
282
 
 
283
function SetTreeItemValueStr(treeItem, value)
 
284
{
 
285
  if (treeItem && GetTreeItemValueStr(treeItem) != value)
 
286
    treeItem.firstChild.lastChild.setAttribute("label", value);
 
287
}
 
288
 
 
289
function IsNotTreeHeader(treeCell)
 
290
{
 
291
  if (treeCell)
 
292
    return (treeCell.parentNode.parentNode.nodeName != "treehead");
 
293
 
 
294
  return false;
 
295
}
 
296
 
 
297
function RemoveNameFromAttArray(attName, attArray)
 
298
{
 
299
  for (var i=0; i < attArray.length; i++)
 
300
  {
 
301
    if (attName.toLowerCase() == attArray[i].toLowerCase())
 
302
    {
 
303
      // Remove 1 array item
 
304
      attArray.splice(i,1);
 
305
      break;
 
306
    }
 
307
  }
 
308
}
 
309
 
 
310
// adds a generalised treeitem.
 
311
function AddTreeItem ( name, value, treeChildrenId, attArray )
 
312
{
 
313
  attArray[attArray.length] = name;
 
314
  var treeChildren    = document.getElementById ( treeChildrenId );
 
315
  var treeitem    = document.createElementNS ( XUL_NS, "treeitem" );
 
316
  var treerow     = document.createElementNS ( XUL_NS, "treerow" );
 
317
 
 
318
  var attrCell    = document.createElementNS ( XUL_NS, "treecell" );
 
319
  attrCell.setAttribute( "class", "propertylist" );
 
320
  attrCell.setAttribute( "label", name );
 
321
 
 
322
  var valueCell    = document.createElementNS ( XUL_NS, "treecell" );
 
323
  valueCell.setAttribute( "class", "propertylist" );
 
324
  valueCell.setAttribute( "label", value );
 
325
 
 
326
  treerow.appendChild ( attrCell );
 
327
  treerow.appendChild ( valueCell );
 
328
  treeitem.appendChild ( treerow );
 
329
  treeChildren.appendChild ( treeitem );
 
330
 
 
331
  // Select item just added,
 
332
  //  but suppress calling the onSelect handler
 
333
  gDoOnSelectTree = false;
 
334
  try {
 
335
    selectTreeItem(treeChildren, treeitem);
 
336
  } catch (e) {}
 
337
  gDoOnSelectTree = true;
 
338
 
 
339
  return treeitem;
 
340
}
 
341
 
 
342
function doHelpButton()
 
343
{
 
344
  openHelp("advanced_property_editor");
 
345
  return true;
 
346
}
 
347
 
 
348
function selectTreeItem(treeChildren, item)
 
349
{
 
350
  var index = treeChildren.parentNode.contentView.getIndexOfItem(item);
 
351
  treeChildren.parentNode.treeBoxObject.selection.select(index);
 
352
}
 
353
 
 
354
function getSelectedItem(tree)
 
355
{
 
356
  if (tree.treeBoxObject.selection.count == 1)
 
357
    return tree.contentView.getItemAtIndex(tree.currentIndex);
 
358
  else
 
359
    return null;
 
360
}