!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Communicator client code, released
15 * March 31, 1998.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-1999
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 //Cancel() is in EdDialogCommon.js
39 var gTableElement = null;
40 var gRows;
41 var gColumns;
42 var gActiveEditor;
43
44 // dialog initialization code
Startup
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
45 function Startup()
46 {
47 gActiveEditor = GetCurrentTableEditor();
48 if (!gActiveEditor)
49 {
50 dump("Failed to get active editor!\n");
51 window.close();
52 return;
53 }
54
55 try {
56 gTableElement = gActiveEditor.createElementWithDefaults("table");
57 } catch (e) {}
58
59 if(!gTableElement)
60 {
61 dump("Failed to create a new table!\n");
62 window.close();
63 return;
64 }
65 gDialog.rowsInput = document.getElementById("rowsInput");
66 gDialog.columnsInput = document.getElementById("columnsInput");
67 gDialog.widthInput = document.getElementById("widthInput");
68 gDialog.borderInput = document.getElementById("borderInput");
69 gDialog.widthPixelOrPercentMenulist = document.getElementById("widthPixelOrPercentMenulist");
70 gDialog.OkButton = document.documentElement.getButton("accept");
71
72 // Make a copy to use for AdvancedEdit
73 globalElement = gTableElement.cloneNode(false);
74 try {
75 if (GetPrefs().getBoolPref("editor.use_css") && IsHTMLEditor()
76 && !(gActiveEditor.flags & Components.interfaces.nsIPlaintextEditor.eEditorMailMask))
77 {
78 // only for Composer and not for htmlmail
79 globalElement.setAttribute("style", "text-align: left;");
80 }
81 } catch (e) {}
82
83 // Initialize all widgets with image attributes
84 InitDialog();
85
86 // Set initial number to 2 rows, 2 columns:
87 // Note, these are not attributes on the table,
88 // so don't put them in InitDialog(),
89 // else the user's values will be trashed when they use
90 // the Advanced Edit dialog
91 gDialog.rowsInput.value = 2;
92 gDialog.columnsInput.value = 2;
93
94 // If no default value on the width, set to 100%
95 if (gDialog.widthInput.value.length == 0)
96 {
97 gDialog.widthInput.value = "100";
98 gDialog.widthPixelOrPercentMenulist.selectedIndex = 1;
99 }
100
101 SetTextboxFocusById("rowsInput");
102
103 SetWindowLocation();
104 }
105
106 // Set dialog widgets with attribute data
107 // We get them from globalElement copy so this can be used
108 // by AdvancedEdit(), which is shared by all property dialogs
InitDialog
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
109 function InitDialog()
110 {
111 // Get default attributes set on the created table:
112 // Get the width attribute of the element, stripping out "%"
113 // This sets contents of menu combobox list
114 // 2nd param = null: Use current selection to find if parent is table cell or window
115 gDialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, null, "width", "widthPixelOrPercentMenulist", gPercent);
116 gDialog.borderInput.value = globalElement.getAttribute("border");
117 }
118
ChangeRowOrColumn
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
119 function ChangeRowOrColumn(id)
120 {
121 // Allow only integers
122 forceInteger(id);
123
124 // Enable OK only if both rows and columns have a value > 0
125 var enable = gDialog.rowsInput.value.length > 0 &&
126 gDialog.rowsInput.value > 0 &&
127 gDialog.columnsInput.value.length > 0 &&
128 gDialog.columnsInput.value > 0;
129
130 SetElementEnabled(gDialog.OkButton, enable);
131 SetElementEnabledById("AdvancedEditButton1", enable);
132 }
133
134
135 // Get and validate data from widgets.
136 // Set attributes on globalElement so they can be accessed by AdvancedEdit()
ValidateData
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
137 function ValidateData()
138 {
139 gRows = ValidateNumber(gDialog.rowsInput, null, 1, gMaxRows, null, null, true)
140 if (gValidationError)
141 return false;
142
143 gColumns = ValidateNumber(gDialog.columnsInput, null, 1, gMaxColumns, null, null, true)
144 if (gValidationError)
145 return false;
146
147 // Set attributes: NOTE: These may be empty strings (last param = false)
148 ValidateNumber(gDialog.borderInput, null, 0, gMaxPixels, globalElement, "border", false);
149 // TODO: Deal with "BORDER" without value issue
150 if (gValidationError) return false;
151
152 ValidateNumber(gDialog.widthInput, gDialog.widthPixelOrPercentMenulist,
153 1, gMaxTableSize, globalElement, "width", false);
154 if (gValidationError)
155 return false;
156
157 return true;
158 }
159
160
onAccept
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
161 function onAccept()
162 {
163 if (ValidateData())
164 {
165 gActiveEditor.beginTransaction();
166 try {
167 gActiveEditor.cloneAttributes(gTableElement, globalElement);
168
169 // Create necessary rows and cells for the table
170 var tableBody = gActiveEditor.createElementWithDefaults("tbody");
171 if (tableBody)
172 {
173 gTableElement.appendChild(tableBody);
174
175 // Create necessary rows and cells for the table
176 for (var i = 0; i < gRows; i++)
177 {
178 var newRow = gActiveEditor.createElementWithDefaults("tr");
179 if (newRow)
180 {
181 tableBody.appendChild(newRow);
182 for (var j = 0; j < gColumns; j++)
183 {
184 var newCell = gActiveEditor.createElementWithDefaults("td");
185 if (newCell)
186 {
187 newRow.appendChild(newCell);
188 }
189 }
190 }
191 }
192 }
193 // Detect when entire cells are selected:
194 // Get number of cells selected
195 var tagNameObj = { value: "" };
196 var countObj = { value: 0 };
197 var element = gActiveEditor.getSelectedOrParentTableElement(tagNameObj, countObj);
198 var deletePlaceholder = false;
199
200 if (tagNameObj.value == "table")
201 {
202 //Replace entire selected table with new table, so delete the table
203 gActiveEditor.deleteTable();
204 }
205 else if (tagNameObj.value == "td")
206 {
207 if (countObj.value >= 1)
208 {
209 if (countObj.value > 1)
210 {
211 // Assume user wants to replace a block of
212 // contiguous cells with a table, so
213 // join the selected cells
214 gActiveEditor.joinTableCells(false);
215
216 // Get the cell everything was merged into
217 element = gActiveEditor.getFirstSelectedCell();
218
219 // Collapse selection into just that cell
220 gActiveEditor.selection.collapse(element,0);
221 }
222
223 if (element)
224 {
225 // Empty just the contents of the cell
226 gActiveEditor.deleteTableCellContents();
227
228 // Collapse selection to start of empty cell...
229 gActiveEditor.selection.collapse(element,0);
230 // ...but it will contain a <br> placeholder
231 deletePlaceholder = true;
232 }
233 }
234 }
235
236 // true means delete selection when inserting
237 gActiveEditor.insertElementAtSelection(gTableElement, true);
238
239 if (deletePlaceholder && gTableElement && gTableElement.nextSibling)
240 {
241 // Delete the placeholder <br>
242 gActiveEditor.deleteNode(gTableElement.nextSibling);
243 }
244 } catch (e) {}
245
246 gActiveEditor.endTransaction();
247
248 SaveWindowLocation();
249 return true;
250 }
251 return false;
252 }