annotate static/js/tiny_mce/utils/form_utils.js @ 905:be233ba7ca31

Reworked registration process. Previous one proved too challenging for some humans. Hopefully made it simpler but still unusual to confuse bots. Increased test coverage also.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Mar 2015 11:06:07 -0500
parents 6c182ceb7147
children
rev   line source
bgneal@312 1 /**
bgneal@312 2 * form_utils.js
bgneal@312 3 *
bgneal@312 4 * Copyright 2009, Moxiecode Systems AB
bgneal@312 5 * Released under LGPL License.
bgneal@312 6 *
bgneal@312 7 * License: http://tinymce.moxiecode.com/license
bgneal@312 8 * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@312 9 */
bgneal@312 10
bgneal@312 11 var themeBaseURL = tinyMCEPopup.editor.baseURI.toAbsolute('themes/' + tinyMCEPopup.getParam("theme"));
bgneal@312 12
bgneal@312 13 function getColorPickerHTML(id, target_form_element) {
bgneal@442 14 var h = "", dom = tinyMCEPopup.dom;
bgneal@312 15
bgneal@442 16 if (label = dom.select('label[for=' + target_form_element + ']')[0]) {
bgneal@442 17 label.id = label.id || dom.uniqueId();
bgneal@442 18 }
bgneal@442 19
bgneal@442 20 h += '<a role="button" aria-labelledby="' + id + '_label" id="' + id + '_link" href="javascript:;" onclick="tinyMCEPopup.pickColor(event,\'' + target_form_element +'\');" onmousedown="return false;" class="pickcolor">';
bgneal@442 21 h += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '">&nbsp;<span id="' + id + '_label" class="mceVoiceLabel mceIconOnly" style="display:none;">' + tinyMCEPopup.getLang('browse') + '</span></span></a>';
bgneal@312 22
bgneal@312 23 return h;
bgneal@312 24 }
bgneal@312 25
bgneal@312 26 function updateColor(img_id, form_element_id) {
bgneal@312 27 document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
bgneal@312 28 }
bgneal@312 29
bgneal@312 30 function setBrowserDisabled(id, state) {
bgneal@312 31 var img = document.getElementById(id);
bgneal@312 32 var lnk = document.getElementById(id + "_link");
bgneal@312 33
bgneal@312 34 if (lnk) {
bgneal@312 35 if (state) {
bgneal@312 36 lnk.setAttribute("realhref", lnk.getAttribute("href"));
bgneal@312 37 lnk.removeAttribute("href");
bgneal@312 38 tinyMCEPopup.dom.addClass(img, 'disabled');
bgneal@312 39 } else {
bgneal@312 40 if (lnk.getAttribute("realhref"))
bgneal@312 41 lnk.setAttribute("href", lnk.getAttribute("realhref"));
bgneal@312 42
bgneal@312 43 tinyMCEPopup.dom.removeClass(img, 'disabled');
bgneal@312 44 }
bgneal@312 45 }
bgneal@312 46 }
bgneal@312 47
bgneal@312 48 function getBrowserHTML(id, target_form_element, type, prefix) {
bgneal@312 49 var option = prefix + "_" + type + "_browser_callback", cb, html;
bgneal@312 50
bgneal@312 51 cb = tinyMCEPopup.getParam(option, tinyMCEPopup.getParam("file_browser_callback"));
bgneal@312 52
bgneal@312 53 if (!cb)
bgneal@312 54 return "";
bgneal@312 55
bgneal@312 56 html = "";
bgneal@312 57 html += '<a id="' + id + '_link" href="javascript:openBrowser(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;" class="browse">';
bgneal@312 58 html += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '">&nbsp;</span></a>';
bgneal@312 59
bgneal@312 60 return html;
bgneal@312 61 }
bgneal@312 62
bgneal@312 63 function openBrowser(img_id, target_form_element, type, option) {
bgneal@312 64 var img = document.getElementById(img_id);
bgneal@312 65
bgneal@312 66 if (img.className != "mceButtonDisabled")
bgneal@312 67 tinyMCEPopup.openBrowser(target_form_element, type, option);
bgneal@312 68 }
bgneal@312 69
bgneal@312 70 function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
bgneal@312 71 if (!form_obj || !form_obj.elements[field_name])
bgneal@312 72 return;
bgneal@312 73
bgneal@442 74 if (!value)
bgneal@442 75 value = "";
bgneal@442 76
bgneal@312 77 var sel = form_obj.elements[field_name];
bgneal@312 78
bgneal@312 79 var found = false;
bgneal@312 80 for (var i=0; i<sel.options.length; i++) {
bgneal@312 81 var option = sel.options[i];
bgneal@312 82
bgneal@312 83 if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
bgneal@312 84 option.selected = true;
bgneal@312 85 found = true;
bgneal@312 86 } else
bgneal@312 87 option.selected = false;
bgneal@312 88 }
bgneal@312 89
bgneal@312 90 if (!found && add_custom && value != '') {
bgneal@312 91 var option = new Option(value, value);
bgneal@312 92 option.selected = true;
bgneal@312 93 sel.options[sel.options.length] = option;
bgneal@312 94 sel.selectedIndex = sel.options.length - 1;
bgneal@312 95 }
bgneal@312 96
bgneal@312 97 return found;
bgneal@312 98 }
bgneal@312 99
bgneal@312 100 function getSelectValue(form_obj, field_name) {
bgneal@312 101 var elm = form_obj.elements[field_name];
bgneal@312 102
bgneal@312 103 if (elm == null || elm.options == null || elm.selectedIndex === -1)
bgneal@312 104 return "";
bgneal@312 105
bgneal@312 106 return elm.options[elm.selectedIndex].value;
bgneal@312 107 }
bgneal@312 108
bgneal@312 109 function addSelectValue(form_obj, field_name, name, value) {
bgneal@312 110 var s = form_obj.elements[field_name];
bgneal@312 111 var o = new Option(name, value);
bgneal@312 112 s.options[s.options.length] = o;
bgneal@312 113 }
bgneal@312 114
bgneal@312 115 function addClassesToList(list_id, specific_option) {
bgneal@312 116 // Setup class droplist
bgneal@312 117 var styleSelectElm = document.getElementById(list_id);
bgneal@312 118 var styles = tinyMCEPopup.getParam('theme_advanced_styles', false);
bgneal@312 119 styles = tinyMCEPopup.getParam(specific_option, styles);
bgneal@312 120
bgneal@312 121 if (styles) {
bgneal@312 122 var stylesAr = styles.split(';');
bgneal@312 123
bgneal@312 124 for (var i=0; i<stylesAr.length; i++) {
bgneal@312 125 if (stylesAr != "") {
bgneal@312 126 var key, value;
bgneal@312 127
bgneal@312 128 key = stylesAr[i].split('=')[0];
bgneal@312 129 value = stylesAr[i].split('=')[1];
bgneal@312 130
bgneal@312 131 styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
bgneal@312 132 }
bgneal@312 133 }
bgneal@312 134 } else {
bgneal@312 135 tinymce.each(tinyMCEPopup.editor.dom.getClasses(), function(o) {
bgneal@312 136 styleSelectElm.options[styleSelectElm.length] = new Option(o.title || o['class'], o['class']);
bgneal@312 137 });
bgneal@312 138 }
bgneal@312 139 }
bgneal@312 140
bgneal@312 141 function isVisible(element_id) {
bgneal@312 142 var elm = document.getElementById(element_id);
bgneal@312 143
bgneal@312 144 return elm && elm.style.display != "none";
bgneal@312 145 }
bgneal@312 146
bgneal@312 147 function convertRGBToHex(col) {
bgneal@312 148 var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
bgneal@312 149
bgneal@312 150 var rgb = col.replace(re, "$1,$2,$3").split(',');
bgneal@312 151 if (rgb.length == 3) {
bgneal@312 152 r = parseInt(rgb[0]).toString(16);
bgneal@312 153 g = parseInt(rgb[1]).toString(16);
bgneal@312 154 b = parseInt(rgb[2]).toString(16);
bgneal@312 155
bgneal@312 156 r = r.length == 1 ? '0' + r : r;
bgneal@312 157 g = g.length == 1 ? '0' + g : g;
bgneal@312 158 b = b.length == 1 ? '0' + b : b;
bgneal@312 159
bgneal@312 160 return "#" + r + g + b;
bgneal@312 161 }
bgneal@312 162
bgneal@312 163 return col;
bgneal@312 164 }
bgneal@312 165
bgneal@312 166 function convertHexToRGB(col) {
bgneal@312 167 if (col.indexOf('#') != -1) {
bgneal@312 168 col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
bgneal@312 169
bgneal@312 170 r = parseInt(col.substring(0, 2), 16);
bgneal@312 171 g = parseInt(col.substring(2, 4), 16);
bgneal@312 172 b = parseInt(col.substring(4, 6), 16);
bgneal@312 173
bgneal@312 174 return "rgb(" + r + "," + g + "," + b + ")";
bgneal@312 175 }
bgneal@312 176
bgneal@312 177 return col;
bgneal@312 178 }
bgneal@312 179
bgneal@312 180 function trimSize(size) {
bgneal@442 181 return size.replace(/([0-9\.]+)(px|%|in|cm|mm|em|ex|pt|pc)/i, '$1$2');
bgneal@312 182 }
bgneal@312 183
bgneal@312 184 function getCSSSize(size) {
bgneal@312 185 size = trimSize(size);
bgneal@312 186
bgneal@312 187 if (size == "")
bgneal@312 188 return "";
bgneal@312 189
bgneal@312 190 // Add px
bgneal@312 191 if (/^[0-9]+$/.test(size))
bgneal@312 192 size += 'px';
bgneal@442 193 // Sanity check, IE doesn't like broken values
bgneal@442 194 else if (!(/^[0-9\.]+(px|%|in|cm|mm|em|ex|pt|pc)$/i.test(size)))
bgneal@442 195 return "";
bgneal@312 196
bgneal@312 197 return size;
bgneal@312 198 }
bgneal@312 199
bgneal@312 200 function getStyle(elm, attrib, style) {
bgneal@312 201 var val = tinyMCEPopup.dom.getAttrib(elm, attrib);
bgneal@312 202
bgneal@312 203 if (val != '')
bgneal@312 204 return '' + val;
bgneal@312 205
bgneal@312 206 if (typeof(style) == 'undefined')
bgneal@312 207 style = attrib;
bgneal@312 208
bgneal@312 209 return tinyMCEPopup.dom.getStyle(elm, style);
bgneal@312 210 }