!import
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is the new tag dialog
17 *
18 * The Initial Developer of the Original Code is
19 * David Bienvenu.
20 * Portions created by the Initial Developer are Copyright (C) 2006
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * David Bienvenu <bienvenu@nventure.com>
25 * Karsten Düsterloh <mnyromyr@tprac.de>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 var dialog;
42
43 /**
44 * Pass in keyToEdit as a window argument to turn this dialog into an edit
45 * tag dialog.
46 */
onLoad
47 function onLoad()
48 {
49 var arguments = window.arguments[0];
50
51 dialog = {};
52
53 dialog.OKButton = document.documentElement.getButton("accept");
54 dialog.nameField = document.getElementById("name");
55 dialog.nameField.focus();
56
57 // call this when OK is pressed
58 dialog.okCallback = arguments.okCallback;
59 if (arguments.keyToEdit)
60 initializeForEditing(arguments.keyToEdit);
61
62 moveToAlertPosition();
63 doEnabling();
64 }
65
66 /**
67 * Turn the new tag dialog into an edit existing tag dialog
68 */
initializeForEditing
69 function initializeForEditing(aTagKey)
70 {
71 dialog.editTagKey = aTagKey;
72
73 // Change the title of the dialog
74 var messengerBundle = document.getElementById("bundle_messenger");
75 document.title = messengerBundle.getString("editTagTitle");
76
77 // override the OK button
78 document.documentElement.setAttribute("ondialogaccept", "return onOKEditTag();");
79
80 // extract the color and name for the current tag
81 var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"]
82 .getService(Components.interfaces.nsIMsgTagService);
83 document.getElementById("tagColorPicker").color = tagService.getColorForKey(aTagKey);
84 dialog.nameField.value = tagService.getTagForKey(aTagKey);
85 }
86
87 /**
88 * on OK handler for editing a new tag.
89 */
onOKEditTag
90 function onOKEditTag()
91 {
92 var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"]
93 .getService(Components.interfaces.nsIMsgTagService);
94 // get the tag name of the current key we are editing
95 var existingTagName = tagService.getTagForKey(dialog.editTagKey);
96
97 // it's ok if the name didn't change
98 if (existingTagName != dialog.nameField.value)
99 {
100 // don't let the user edit a tag to the name of another existing tag
101 if (tagService.getKeyForTag(dialog.nameField.value))
102 {
103 alertForExistingTag();
104 return false; // abort the OK
105 }
106
107 tagService.setTagForKey(dialog.editTagKey, dialog.nameField.value);
108 }
109
110 tagService.setColorForKey(dialog.editTagKey, document.getElementById("tagColorPicker").color);
111 return dialog.okCallback();
112 }
113
114 /**
115 * on OK handler for creating a new tag. Alerts the user if a tag with
116 * the name already exists.
117 */
onOKNewTag
118 function onOKNewTag()
119 {
120 var name = dialog.nameField.value;
121
122 var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"].getService(Components.interfaces.nsIMsgTagService);
123
124 if (tagService.getKeyForTag(name))
125 {
126 alertForExistingTag();
127 return false;
128 }
129 else
130 return dialog.okCallback(name, document.getElementById("tagColorPicker").color);
131 }
132
133 /**
134 * Alerts the user that they are trying to create a tag with a name that
135 * already exists.
136 */
alertForExistingTag
137 function alertForExistingTag()
138 {
139 var messengerBundle = document.getElementById("bundle_messenger");
140 var alertText = messengerBundle.getString("tagExists");
141 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
142 .getService(Components.interfaces.nsIPromptService);
143 promptService.alert(window, document.title, alertText);
144 }
145
doEnabling
146 function doEnabling()
147 {
148 dialog.OKButton.disabled = !dialog.nameField.value;
149 }