A tumblelog CMS built on AJAX, PHP and MySQL.

form_utils.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. var themeBaseURL = tinyMCE.baseURL + '/themes/' + tinyMCE.getParam("theme");
  10. function getColorPickerHTML(id, target_form_element) {
  11. var h = "";
  12. h += '<a id="' + id + '_link" href="javascript:void(0);" onkeydown="pickColor(event,\'' + target_form_element +'\');" onmousedown="pickColor(event,\'' + target_form_element +'\');return false;">';
  13. h += '<img id="' + id + '" src="' + themeBaseURL + '/images/color.gif"';
  14. h += ' onmouseover="this.className=\'mceButtonOver\'"';
  15. h += ' onmouseout="this.className=\'mceButtonNormal\'"';
  16. h += ' onmousedown="this.className=\'mceButtonDown\'"';
  17. h += ' width="20" height="16" border="0" title="' + tinyMCE.getLang('lang_browse') + '"';
  18. h += ' class="mceButtonNormal" alt="' + tinyMCE.getLang('lang_browse') + '" /></a>';
  19. return h;
  20. }
  21. function pickColor(e, target_form_element) {
  22. if ((e.keyCode == 32 || e.keyCode == 13) || e.type == "mousedown")
  23. tinyMCEPopup.pickColor(e, target_form_element);
  24. }
  25. function updateColor(img_id, form_element_id) {
  26. document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
  27. }
  28. function setBrowserDisabled(id, state) {
  29. var img = document.getElementById(id);
  30. var lnk = document.getElementById(id + "_link");
  31. if (lnk) {
  32. if (state) {
  33. lnk.setAttribute("realhref", lnk.getAttribute("href"));
  34. lnk.removeAttribute("href");
  35. tinyMCE.switchClass(img, 'mceButtonDisabled', true);
  36. } else {
  37. lnk.setAttribute("href", lnk.getAttribute("realhref"));
  38. tinyMCE.switchClass(img, 'mceButtonNormal', false);
  39. }
  40. }
  41. }
  42. function getBrowserHTML(id, target_form_element, type, prefix) {
  43. var option = prefix + "_" + type + "_browser_callback";
  44. var cb = tinyMCE.getParam(option, tinyMCE.getParam("file_browser_callback"));
  45. if (cb == null)
  46. return "";
  47. var html = "";
  48. html += '<a id="' + id + '_link" href="javascript:openBrower(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;">';
  49. html += '<img id="' + id + '" src="' + themeBaseURL + '/images/browse.gif"';
  50. html += ' onmouseover="this.className=\'mceButtonOver\';"';
  51. html += ' onmouseout="this.className=\'mceButtonNormal\';"';
  52. html += ' onmousedown="this.className=\'mceButtonDown\';"';
  53. html += ' width="20" height="18" border="0" title="' + tinyMCE.getLang('lang_browse') + '"';
  54. html += ' class="mceButtonNormal" alt="' + tinyMCE.getLang('lang_browse') + '" /></a>';
  55. return html;
  56. }
  57. function openBrower(img_id, target_form_element, type, option) {
  58. var img = document.getElementById(img_id);
  59. if (img.className != "mceButtonDisabled")
  60. tinyMCEPopup.openBrowser(target_form_element, type, option);
  61. }
  62. function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
  63. if (!form_obj || !form_obj.elements[field_name])
  64. return;
  65. var sel = form_obj.elements[field_name];
  66. var found = false;
  67. for (var i=0; i<sel.options.length; i++) {
  68. var option = sel.options[i];
  69. if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
  70. option.selected = true;
  71. found = true;
  72. } else
  73. option.selected = false;
  74. }
  75. if (!found && add_custom && value != '') {
  76. var option = new Option(value, value);
  77. option.selected = true;
  78. sel.options[sel.options.length] = option;
  79. sel.selectedIndex = sel.options.length - 1;
  80. }
  81. return found;
  82. }
  83. function getSelectValue(form_obj, field_name) {
  84. var elm = form_obj.elements[field_name];
  85. if (elm == null || elm.options == null)
  86. return "";
  87. return elm.options[elm.selectedIndex].value;
  88. }
  89. function addSelectValue(form_obj, field_name, name, value) {
  90. var s = form_obj.elements[field_name];
  91. var o = new Option(name, value);
  92. s.options[s.options.length] = o;
  93. }
  94. function addClassesToList(list_id, specific_option) {
  95. // Setup class droplist
  96. var styleSelectElm = document.getElementById(list_id);
  97. var styles = tinyMCE.getParam('theme_advanced_styles', false);
  98. styles = tinyMCE.getParam(specific_option, styles);
  99. if (styles) {
  100. var stylesAr = styles.split(';');
  101. for (var i=0; i<stylesAr.length; i++) {
  102. if (stylesAr != "") {
  103. var key, value;
  104. key = stylesAr[i].split('=')[0];
  105. value = stylesAr[i].split('=')[1];
  106. styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
  107. }
  108. }
  109. } else {
  110. // Use auto impored classes
  111. var csses = tinyMCE.getCSSClasses(tinyMCE.getWindowArg('editor_id'));
  112. for (var i=0; i<csses.length; i++)
  113. styleSelectElm.options[styleSelectElm.length] = new Option(csses[i], csses[i]);
  114. }
  115. }
  116. function isVisible(element_id) {
  117. var elm = document.getElementById(element_id);
  118. return elm && elm.style.display != "none";
  119. }
  120. function convertRGBToHex(col) {
  121. var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
  122. var rgb = col.replace(re, "$1,$2,$3").split(',');
  123. if (rgb.length == 3) {
  124. r = parseInt(rgb[0]).toString(16);
  125. g = parseInt(rgb[1]).toString(16);
  126. b = parseInt(rgb[2]).toString(16);
  127. r = r.length == 1 ? '0' + r : r;
  128. g = g.length == 1 ? '0' + g : g;
  129. b = b.length == 1 ? '0' + b : b;
  130. return "#" + r + g + b;
  131. }
  132. return col;
  133. }
  134. function convertHexToRGB(col) {
  135. if (col.indexOf('#') != -1) {
  136. col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
  137. r = parseInt(col.substring(0, 2), 16);
  138. g = parseInt(col.substring(2, 4), 16);
  139. b = parseInt(col.substring(4, 6), 16);
  140. return "rgb(" + r + "," + g + "," + b + ")";
  141. }
  142. return col;
  143. }
  144. function trimSize(size) {
  145. return size.replace(new RegExp('[^0-9%]', 'gi'), '');
  146. }
  147. function getCSSSize(size) {
  148. size = trimSize(size);
  149. if (size == "")
  150. return "";
  151. return size.indexOf('%') != -1 ? size : size + "px";
  152. }
  153. function getStyle(elm, attrib, style) {
  154. var val = tinyMCE.getAttrib(elm, attrib);
  155. if (val != '')
  156. return '' + val;
  157. if (typeof(style) == 'undefined')
  158. style = attrib;
  159. val = eval('elm.style.' + style);
  160. return val == null ? '' : '' + val;
  161. }