* Upgraded TinyMCE to version 2.0RC3. This fixes a conflict with
[citadel.git] / webcit / tiny_mce / utils / form_utils.js
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * Various form utilitiy functions.
7  *
8  * @author Moxiecode
9  * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
10  */
11
12 function renderColorPicker(id, target_form_element) {
13         var html = "";
14
15         html += '<a id="' + id + '_link" href="javascript:void(0);" onkeydown="pickColor(event,\'' + target_form_element +'\');" onmousedown="pickColor(event,\'' + target_form_element +'\');return false;">';
16         html += '<img id="' + id + '" src="../../themes/advanced/images/color.gif"';
17         html += ' onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');"';
18         html += ' onmouseout="tinyMCE.restoreClass(this);"';
19         html += ' onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');"';
20         html += ' width="20" height="16" border="0" title="' + tinyMCE.getLang('lang_browse') + '"';
21         html += ' class="mceButtonNormal" alt="' + tinyMCE.getLang('lang_browse') + '" /></a>';
22
23         document.write(html);
24 }
25
26 function pickColor(e, target_form_element) {
27         if ((e.keyCode == 32 || e.keyCode == 13) || e.type == "mousedown")
28                 tinyMCEPopup.pickColor(e, target_form_element);
29 }
30
31 function updateColor(img_id, form_element_id) {
32         document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
33 }
34
35 function setBrowserDisabled(id, state) {
36         var img = document.getElementById(id);
37         var lnk = document.getElementById(id + "_link");
38
39         if (lnk) {
40                 if (state) {
41                         lnk.setAttribute("realhref", lnk.getAttribute("href"));
42                         lnk.removeAttribute("href");
43                         tinyMCE.switchClass(img, 'mceButtonDisabled', true);
44                 } else {
45                         lnk.setAttribute("href", lnk.getAttribute("realhref"));
46                         tinyMCE.switchClass(img, 'mceButtonNormal', false);
47                 }
48         }
49 }
50
51 function renderBrowser(id, target_form_element, type, prefix) {
52         var option = prefix + "_" + type + "_browser_callback";
53         var cb = tinyMCE.getParam(option, tinyMCE.getParam("file_browser_callback"));
54         if (cb == null)
55                 return;
56
57         var html = "";
58
59         html += '<a id="' + id + '_link" href="javascript:openBrower(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;">';
60         html += '<img id="' + id + '" src="../../themes/advanced/images/browse.gif"';
61         html += ' onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');"';
62         html += ' onmouseout="tinyMCE.restoreClass(this);"';
63         html += ' onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');"';
64         html += ' width="20" height="18" border="0" title="' + tinyMCE.getLang('lang_browse') + '"';
65         html += ' class="mceButtonNormal" alt="' + tinyMCE.getLang('lang_browse') + '" /></a>';
66
67         document.write(html);
68 }
69
70 function openBrower(img_id, target_form_element, type, option) {
71         var img = document.getElementById(img_id);
72
73         if (img.className != "mceButtonDisabled")
74                 tinyMCEPopup.openBrowser(target_form_element, type, option);
75 }
76
77 function selectByValue(form_obj, field_name, value, add_custom) {
78         if (!form_obj || !form_obj.elements[field_name])
79                 return;
80
81         var sel = form_obj.elements[field_name];
82
83         var found = false;
84         for (var i=0; i<sel.options.length; i++) {
85                 var option = sel.options[i];
86
87                 if (option.value == value) {
88                         option.selected = true;
89                         found = true;
90                 } else
91                         option.selected = false;
92         }
93
94         if (!found && add_custom && value != '') {
95                 var option = new Option('Value: ' + value, value);
96                 option.selected = true;
97                 sel.options[sel.options.length] = option;
98         }
99
100         return found;
101 }
102
103 function getSelectValue(form_obj, field_name) {
104         var elm = form_obj.elements[field_name];
105
106         if (elm == null || elm.options == null)
107                 return "";
108
109         return elm.options[elm.selectedIndex].value;
110 }
111
112 function addClassesToList(list_id, specific_option) {
113         // Setup class droplist
114         var styleSelectElm = document.getElementById(list_id);
115         var styles = tinyMCE.getParam('theme_advanced_styles', false);
116         styles = tinyMCE.getParam(specific_option, styles);
117
118         if (styles) {
119                 var stylesAr = styles.split(';');
120
121                 for (var i=0; i<stylesAr.length; i++) {
122                         if (stylesAr != "") {
123                                 var key, value;
124
125                                 key = stylesAr[i].split('=')[0];
126                                 value = stylesAr[i].split('=')[1];
127
128                                 styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
129                         }
130                 }
131         } else {
132                 // Use auto impored classes
133                 var csses = tinyMCE.getCSSClasses(tinyMCE.getWindowArg('editor_id'));
134                 for (var i=0; i<csses.length; i++)
135                         styleSelectElm.options[styleSelectElm.length] = new Option(csses[i], csses[i]);
136         }
137 }
138
139 function isVisible(element_id) {
140         var elm = document.getElementById(element_id);
141
142         return elm && elm.style.display != "none";
143 }
144
145 function convertRGBToHex(col) {
146         var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
147
148         var rgb = col.replace(re, "$1,$2,$3").split(',');
149         if (rgb.length == 3) {
150                 r = parseInt(rgb[0]).toString(16);
151                 g = parseInt(rgb[1]).toString(16);
152                 b = parseInt(rgb[2]).toString(16);
153
154                 r = r.length == 1 ? '0' + r : r;
155                 g = g.length == 1 ? '0' + g : g;
156                 b = b.length == 1 ? '0' + b : b;
157
158                 return "#" + r + g + b;
159         }
160
161         return col;
162 }
163
164 function convertHexToRGB(col) {
165         if (col.indexOf('#') != -1) {
166                 col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
167
168                 r = parseInt(col.substring(0, 2), 16);
169                 g = parseInt(col.substring(2, 4), 16);
170                 b = parseInt(col.substring(4, 6), 16);
171
172                 return "rgb(" + r + "," + g + "," + b + ")";
173         }
174
175         return col;
176 }
177
178 function trimSize(size) {
179         return size.replace(new RegExp('[^0-9%]', 'gi'), '');
180 }
181
182 function getCSSSize(size) {
183         size = trimSize(size);
184
185         if (size == "")
186                 return "";
187
188         return size.indexOf('%') != -1 ? size : size + "px";
189 }
190
191 function getStyle(elm, attrib, style) {
192         var val = tinyMCE.getAttrib(elm, attrib);
193
194         if (val != '')
195                 return '' + val;
196
197         if (typeof(style) == 'undefined')
198                 style = attrib;
199
200         val = eval('elm.style.' + style);
201
202         return val == null ? '' : '' + val;
203 }