!import
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 Label Properties Dialog.
15 *
16 * The Initial Developer of the Original Code is
17 * Neil Rashbrook.
18 * Portions created by the Initial Developer are Copyright (C) 2001
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ***** END LICENSE BLOCK ***** */
36
37 var labelElement;
38
39 // dialog initialization code
40
Startup
41 function Startup()
42 {
43 var editor = GetCurrentEditor();
44 if (!editor)
45 {
46 dump("Failed to get active editor!\n");
47 window.close();
48 return;
49 }
50
51 gDialog.editText = document.getElementById("EditText");
52 gDialog.labelText = document.getElementById("LabelText");
53 gDialog.labelFor = document.getElementById("LabelFor");
54 gDialog.labelAccessKey = document.getElementById("LabelAccessKey");
55
56 labelElement = window.arguments[0];
57
58 // Make a copy to use for AdvancedEdit
59 globalElement = labelElement.cloneNode(false);
60
61 InitDialog();
62
63 var range = editor.document.createRange();
64 range.selectNode(labelElement);
65 gDialog.labelText.value = range.toString();
66
67 if (/</.test(labelElement.innerHTML))
68 {
69 gDialog.editText.checked = false;
70 gDialog.editText.disabled = false;
71 gDialog.labelText.disabled = true;
72 gDialog.editText.addEventListener("command", onEditText, false);
73 SetTextboxFocus(gDialog.labelFor);
74 }
75 else
76 SetTextboxFocus(gDialog.labelText);
77
78 SetWindowLocation();
79 }
80
InitDialog
81 function InitDialog()
82 {
83 gDialog.labelFor.value = globalElement.getAttribute("for");
84 gDialog.labelAccessKey.value = globalElement.getAttribute("accesskey");
85 }
86
onEditText
87 function onEditText()
88 {
89 gDialog.editText.removeEventListener("command", onEditText, false);
90 AlertWithTitle(GetString("Alert"), GetString("EditTextWarning"));
91 }
92
RemoveLabel
93 function RemoveLabel()
94 {
95 RemoveContainer(labelElement);
96 SaveWindowLocation();
97 window.close();
98 }
99
ValidateData
100 function ValidateData()
101 {
102 if (gDialog.labelFor.value)
103 globalElement.setAttribute("for", gDialog.labelFor.value);
104 else
105 globalElement.removeAttribute("for");
106 if (gDialog.labelAccessKey.value)
107 globalElement.setAttribute("accesskey", gDialog.labelAccessKey.value);
108 else
109 globalElement.removeAttribute("accesskey");
110 return true;
111 }
112
onAccept
113 function onAccept()
114 {
115 // All values are valid - copy to actual element in doc
116 ValidateData();
117
118 var editor = GetCurrentEditor();
119
120 editor.beginTransaction();
121
122 try {
123 if (gDialog.editText.checked)
124 {
125 editor.setShouldTxnSetSelection(false);
126
127 while (labelElement.firstChild)
128 editor.deleteNode(labelElement.firstChild);
129 if (gDialog.labelText.value)
130 editor.insertNode(editor.document.createTextNode(gDialog.labelText.value), labelElement, 0);
131
132 editor.setShouldTxnSetSelection(true);
133 }
134
135 editor.cloneAttributes(labelElement, globalElement);
136 } catch(e) {}
137
138 editor.endTransaction();
139
140 SaveWindowLocation();
141
142 return true;
143 }