annotate media/js/tiny_mce/utils/form_utils.js @ 286:72fd300685d5

For #95. You can now make posts with no text in the body if you have attachments. And now if you create a new topic with an attachment, and the POST fails (say you forgot the topic title), we will now re-attach attachments. Also fixed a bug in the smiley code that would arise if it was asked to markup an empty string.
author Brian Neal <bgneal@gmail.com>
date Sat, 23 Oct 2010 20:19:46 +0000
parents 149c3567fec1
children
rev   line source
bgneal@45 1 /**
bgneal@183 2 * form_utils.js
bgneal@45 3 *
bgneal@183 4 * Copyright 2009, Moxiecode Systems AB
bgneal@183 5 * Released under LGPL License.
bgneal@45 6 *
bgneal@183 7 * License: http://tinymce.moxiecode.com/license
bgneal@183 8 * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@45 9 */
bgneal@45 10
bgneal@45 11 var themeBaseURL = tinyMCEPopup.editor.baseURI.toAbsolute('themes/' + tinyMCEPopup.getParam("theme"));
bgneal@45 12
bgneal@45 13 function getColorPickerHTML(id, target_form_element) {
bgneal@45 14 var h = "";
bgneal@45 15
bgneal@45 16 h += '<a id="' + id + '_link" href="javascript:;" onclick="tinyMCEPopup.pickColor(event,\'' + target_form_element +'\');" onmousedown="return false;" class="pickcolor">';
bgneal@45 17 h += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '">&nbsp;</span></a>';
bgneal@45 18
bgneal@45 19 return h;
bgneal@45 20 }
bgneal@45 21
bgneal@45 22 function updateColor(img_id, form_element_id) {
bgneal@45 23 document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
bgneal@45 24 }
bgneal@45 25
bgneal@45 26 function setBrowserDisabled(id, state) {
bgneal@45 27 var img = document.getElementById(id);
bgneal@45 28 var lnk = document.getElementById(id + "_link");
bgneal@45 29
bgneal@45 30 if (lnk) {
bgneal@45 31 if (state) {
bgneal@45 32 lnk.setAttribute("realhref", lnk.getAttribute("href"));
bgneal@45 33 lnk.removeAttribute("href");
bgneal@45 34 tinyMCEPopup.dom.addClass(img, 'disabled');
bgneal@45 35 } else {
bgneal@45 36 if (lnk.getAttribute("realhref"))
bgneal@45 37 lnk.setAttribute("href", lnk.getAttribute("realhref"));
bgneal@45 38
bgneal@45 39 tinyMCEPopup.dom.removeClass(img, 'disabled');
bgneal@45 40 }
bgneal@45 41 }
bgneal@45 42 }
bgneal@45 43
bgneal@45 44 function getBrowserHTML(id, target_form_element, type, prefix) {
bgneal@45 45 var option = prefix + "_" + type + "_browser_callback", cb, html;
bgneal@45 46
bgneal@45 47 cb = tinyMCEPopup.getParam(option, tinyMCEPopup.getParam("file_browser_callback"));
bgneal@45 48
bgneal@45 49 if (!cb)
bgneal@45 50 return "";
bgneal@45 51
bgneal@45 52 html = "";
bgneal@45 53 html += '<a id="' + id + '_link" href="javascript:openBrowser(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;" class="browse">';
bgneal@45 54 html += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '">&nbsp;</span></a>';
bgneal@45 55
bgneal@45 56 return html;
bgneal@45 57 }
bgneal@45 58
bgneal@45 59 function openBrowser(img_id, target_form_element, type, option) {
bgneal@45 60 var img = document.getElementById(img_id);
bgneal@45 61
bgneal@45 62 if (img.className != "mceButtonDisabled")
bgneal@45 63 tinyMCEPopup.openBrowser(target_form_element, type, option);
bgneal@45 64 }
bgneal@45 65
bgneal@45 66 function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
bgneal@45 67 if (!form_obj || !form_obj.elements[field_name])
bgneal@45 68 return;
bgneal@45 69
bgneal@45 70 var sel = form_obj.elements[field_name];
bgneal@45 71
bgneal@45 72 var found = false;
bgneal@45 73 for (var i=0; i<sel.options.length; i++) {
bgneal@45 74 var option = sel.options[i];
bgneal@45 75
bgneal@45 76 if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
bgneal@45 77 option.selected = true;
bgneal@45 78 found = true;
bgneal@45 79 } else
bgneal@45 80 option.selected = false;
bgneal@45 81 }
bgneal@45 82
bgneal@45 83 if (!found && add_custom && value != '') {
bgneal@45 84 var option = new Option(value, value);
bgneal@45 85 option.selected = true;
bgneal@45 86 sel.options[sel.options.length] = option;
bgneal@45 87 sel.selectedIndex = sel.options.length - 1;
bgneal@45 88 }
bgneal@45 89
bgneal@45 90 return found;
bgneal@45 91 }
bgneal@45 92
bgneal@45 93 function getSelectValue(form_obj, field_name) {
bgneal@45 94 var elm = form_obj.elements[field_name];
bgneal@45 95
bgneal@183 96 if (elm == null || elm.options == null || elm.selectedIndex === -1)
bgneal@45 97 return "";
bgneal@45 98
bgneal@45 99 return elm.options[elm.selectedIndex].value;
bgneal@45 100 }
bgneal@45 101
bgneal@45 102 function addSelectValue(form_obj, field_name, name, value) {
bgneal@45 103 var s = form_obj.elements[field_name];
bgneal@45 104 var o = new Option(name, value);
bgneal@45 105 s.options[s.options.length] = o;
bgneal@45 106 }
bgneal@45 107
bgneal@45 108 function addClassesToList(list_id, specific_option) {
bgneal@45 109 // Setup class droplist
bgneal@45 110 var styleSelectElm = document.getElementById(list_id);
bgneal@45 111 var styles = tinyMCEPopup.getParam('theme_advanced_styles', false);
bgneal@45 112 styles = tinyMCEPopup.getParam(specific_option, styles);
bgneal@45 113
bgneal@45 114 if (styles) {
bgneal@45 115 var stylesAr = styles.split(';');
bgneal@45 116
bgneal@45 117 for (var i=0; i<stylesAr.length; i++) {
bgneal@45 118 if (stylesAr != "") {
bgneal@45 119 var key, value;
bgneal@45 120
bgneal@45 121 key = stylesAr[i].split('=')[0];
bgneal@45 122 value = stylesAr[i].split('=')[1];
bgneal@45 123
bgneal@45 124 styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
bgneal@45 125 }
bgneal@45 126 }
bgneal@45 127 } else {
bgneal@45 128 tinymce.each(tinyMCEPopup.editor.dom.getClasses(), function(o) {
bgneal@45 129 styleSelectElm.options[styleSelectElm.length] = new Option(o.title || o['class'], o['class']);
bgneal@45 130 });
bgneal@45 131 }
bgneal@45 132 }
bgneal@45 133
bgneal@45 134 function isVisible(element_id) {
bgneal@45 135 var elm = document.getElementById(element_id);
bgneal@45 136
bgneal@45 137 return elm && elm.style.display != "none";
bgneal@45 138 }
bgneal@45 139
bgneal@45 140 function convertRGBToHex(col) {
bgneal@45 141 var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
bgneal@45 142
bgneal@45 143 var rgb = col.replace(re, "$1,$2,$3").split(',');
bgneal@45 144 if (rgb.length == 3) {
bgneal@45 145 r = parseInt(rgb[0]).toString(16);
bgneal@45 146 g = parseInt(rgb[1]).toString(16);
bgneal@45 147 b = parseInt(rgb[2]).toString(16);
bgneal@45 148
bgneal@45 149 r = r.length == 1 ? '0' + r : r;
bgneal@45 150 g = g.length == 1 ? '0' + g : g;
bgneal@45 151 b = b.length == 1 ? '0' + b : b;
bgneal@45 152
bgneal@45 153 return "#" + r + g + b;
bgneal@45 154 }
bgneal@45 155
bgneal@45 156 return col;
bgneal@45 157 }
bgneal@45 158
bgneal@45 159 function convertHexToRGB(col) {
bgneal@45 160 if (col.indexOf('#') != -1) {
bgneal@45 161 col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
bgneal@45 162
bgneal@45 163 r = parseInt(col.substring(0, 2), 16);
bgneal@45 164 g = parseInt(col.substring(2, 4), 16);
bgneal@45 165 b = parseInt(col.substring(4, 6), 16);
bgneal@45 166
bgneal@45 167 return "rgb(" + r + "," + g + "," + b + ")";
bgneal@45 168 }
bgneal@45 169
bgneal@45 170 return col;
bgneal@45 171 }
bgneal@45 172
bgneal@45 173 function trimSize(size) {
bgneal@45 174 return size.replace(/([0-9\.]+)px|(%|in|cm|mm|em|ex|pt|pc)/, '$1$2');
bgneal@45 175 }
bgneal@45 176
bgneal@45 177 function getCSSSize(size) {
bgneal@45 178 size = trimSize(size);
bgneal@45 179
bgneal@45 180 if (size == "")
bgneal@45 181 return "";
bgneal@45 182
bgneal@45 183 // Add px
bgneal@45 184 if (/^[0-9]+$/.test(size))
bgneal@45 185 size += 'px';
bgneal@45 186
bgneal@45 187 return size;
bgneal@45 188 }
bgneal@45 189
bgneal@45 190 function getStyle(elm, attrib, style) {
bgneal@45 191 var val = tinyMCEPopup.dom.getAttrib(elm, attrib);
bgneal@45 192
bgneal@45 193 if (val != '')
bgneal@45 194 return '' + val;
bgneal@45 195
bgneal@45 196 if (typeof(style) == 'undefined')
bgneal@45 197 style = attrib;
bgneal@45 198
bgneal@45 199 return tinyMCEPopup.dom.getStyle(elm, style);
bgneal@45 200 }