]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/utils/form_utils.js
Wordpress 2.3.2-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / utils / form_utils.js
1 /**
2  * $Id: form_utils.js 162 2007-01-03 16:16:52Z spocke $
3  *
4  * Various form utilitiy functions.
5  *
6  * @author Moxiecode
7  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
8  */
9
10 var themeBaseURL = tinyMCE.baseURL + '/themes/' + tinyMCE.getParam("theme");
11
12 function getColorPickerHTML(id, target_form_element) {
13         var h = "";
14
15         h += '<a id="' + id + '_link" href="javascript:void(0);" onkeydown="pickColor(event,\'' + target_form_element +'\');" onmousedown="pickColor(event,\'' + target_form_element +'\');return false;">';
16         h += '<img id="' + id + '" src="' + themeBaseURL + '/images/color.gif"';
17         h += ' onmouseover="this.className=\'mceButtonOver\'"';
18         h += ' onmouseout="this.className=\'mceButtonNormal\'"';
19         h += ' onmousedown="this.className=\'mceButtonDown\'"';
20         h += ' width="20" height="16" border="0" title="' + tinyMCE.getLang('lang_browse') + '"';
21         h += ' class="mceButtonNormal" alt="' + tinyMCE.getLang('lang_browse') + '" /></a>';
22
23         return h;
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 getBrowserHTML(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="' + themeBaseURL + '/images/browse.gif"';
61         html += ' onmouseover="this.className=\'mceButtonOver\';"';
62         html += ' onmouseout="this.className=\'mceButtonNormal\';"';
63         html += ' onmousedown="this.className=\'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         return 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, ignore_case) {
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 || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
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);
96                 option.selected = true;
97                 sel.options[sel.options.length] = option;
98                 sel.selectedIndex = sel.options.length - 1;
99         }
100
101         return found;
102 }
103
104 function getSelectValue(form_obj, field_name) {
105         var elm = form_obj.elements[field_name];
106
107         if (elm == null || elm.options == null)
108                 return "";
109
110         return elm.options[elm.selectedIndex].value;
111 }
112
113 function addSelectValue(form_obj, field_name, name, value) {
114         var s = form_obj.elements[field_name];
115         var o = new Option(name, value);
116         s.options[s.options.length] = o;
117 }
118
119 function addClassesToList(list_id, specific_option) {
120         // Setup class droplist
121         var styleSelectElm = document.getElementById(list_id);
122         var styles = tinyMCE.getParam('theme_advanced_styles', false);
123         styles = tinyMCE.getParam(specific_option, styles);
124
125         if (styles) {
126                 var stylesAr = styles.split(';');
127
128                 for (var i=0; i<stylesAr.length; i++) {
129                         if (stylesAr != "") {
130                                 var key, value;
131
132                                 key = stylesAr[i].split('=')[0];
133                                 value = stylesAr[i].split('=')[1];
134
135                                 styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
136                         }
137                 }
138         } else {
139                 // Use auto impored classes
140                 var csses = tinyMCE.getCSSClasses(tinyMCE.getWindowArg('editor_id'));
141                 for (var i=0; i<csses.length; i++)
142                         styleSelectElm.options[styleSelectElm.length] = new Option(csses[i], csses[i]);
143         }
144 }
145
146 function isVisible(element_id) {
147         var elm = document.getElementById(element_id);
148
149         return elm && elm.style.display != "none";
150 }
151
152 function convertRGBToHex(col) {
153         var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
154
155         var rgb = col.replace(re, "$1,$2,$3").split(',');
156         if (rgb.length == 3) {
157                 r = parseInt(rgb[0]).toString(16);
158                 g = parseInt(rgb[1]).toString(16);
159                 b = parseInt(rgb[2]).toString(16);
160
161                 r = r.length == 1 ? '0' + r : r;
162                 g = g.length == 1 ? '0' + g : g;
163                 b = b.length == 1 ? '0' + b : b;
164
165                 return "#" + r + g + b;
166         }
167
168         return col;
169 }
170
171 function convertHexToRGB(col) {
172         if (col.indexOf('#') != -1) {
173                 col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
174
175                 r = parseInt(col.substring(0, 2), 16);
176                 g = parseInt(col.substring(2, 4), 16);
177                 b = parseInt(col.substring(4, 6), 16);
178
179                 return "rgb(" + r + "," + g + "," + b + ")";
180         }
181
182         return col;
183 }
184
185 function trimSize(size) {
186         return size.replace(new RegExp('[^0-9%]', 'gi'), '');
187 }
188
189 function getCSSSize(size) {
190         size = trimSize(size);
191
192         if (size == "")
193                 return "";
194
195         return size.indexOf('%') != -1 ? size : size + "px";
196 }
197
198 function getStyle(elm, attrib, style) {
199         var val = tinyMCE.getAttrib(elm, attrib);
200
201         if (val != '')
202                 return '' + val;
203
204         if (typeof(style) == 'undefined')
205                 style = attrib;
206
207         val = eval('elm.style.' + style);
208
209         return val == null ? '' : '' + val;
210 }