/static/scripts/tiny_mce/utils/form_utils.js

http://n23.googlecode.com/ · JavaScript · 199 lines · 139 code · 50 blank · 10 comment · 45 complexity · b591942219a32ee0ca36347cfbeb5db9 MD5 · raw file

  1. /**
  2. * $Id: form_utils.js 673 2008-03-06 13:26:20Z spocke $
  3. *
  4. * Various form utilitiy functions.
  5. *
  6. * @author Moxiecode
  7. * @copyright Copyright Š 2004-2008, Moxiecode Systems AB, All rights reserved.
  8. */
  9. var themeBaseURL = tinyMCEPopup.editor.baseURI.toAbsolute('themes/' + tinyMCEPopup.getParam("theme"));
  10. function getColorPickerHTML(id, target_form_element) {
  11. var h = "";
  12. h += '<a id="' + id + '_link" href="javascript:;" onclick="tinyMCEPopup.pickColor(event,\'' + target_form_element +'\');" onmousedown="return false;" class="pickcolor">';
  13. h += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '"></span></a>';
  14. return h;
  15. }
  16. function updateColor(img_id, form_element_id) {
  17. document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
  18. }
  19. function setBrowserDisabled(id, state) {
  20. var img = document.getElementById(id);
  21. var lnk = document.getElementById(id + "_link");
  22. if (lnk) {
  23. if (state) {
  24. lnk.setAttribute("realhref", lnk.getAttribute("href"));
  25. lnk.removeAttribute("href");
  26. tinyMCEPopup.dom.addClass(img, 'disabled');
  27. } else {
  28. if (lnk.getAttribute("realhref"))
  29. lnk.setAttribute("href", lnk.getAttribute("realhref"));
  30. tinyMCEPopup.dom.removeClass(img, 'disabled');
  31. }
  32. }
  33. }
  34. function getBrowserHTML(id, target_form_element, type, prefix) {
  35. var option = prefix + "_" + type + "_browser_callback", cb, html;
  36. cb = tinyMCEPopup.getParam(option, tinyMCEPopup.getParam("file_browser_callback"));
  37. if (!cb)
  38. return "";
  39. html = "";
  40. html += '<a id="' + id + '_link" href="javascript:openBrowser(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;" class="browse">';
  41. html += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '"></span></a>';
  42. return html;
  43. }
  44. function openBrowser(img_id, target_form_element, type, option) {
  45. var img = document.getElementById(img_id);
  46. if (img.className != "mceButtonDisabled")
  47. tinyMCEPopup.openBrowser(target_form_element, type, option);
  48. }
  49. function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
  50. if (!form_obj || !form_obj.elements[field_name])
  51. return;
  52. var sel = form_obj.elements[field_name];
  53. var found = false;
  54. for (var i=0; i<sel.options.length; i++) {
  55. var option = sel.options[i];
  56. if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
  57. option.selected = true;
  58. found = true;
  59. } else
  60. option.selected = false;
  61. }
  62. if (!found && add_custom && value != '') {
  63. var option = new Option(value, value);
  64. option.selected = true;
  65. sel.options[sel.options.length] = option;
  66. sel.selectedIndex = sel.options.length - 1;
  67. }
  68. return found;
  69. }
  70. function getSelectValue(form_obj, field_name) {
  71. var elm = form_obj.elements[field_name];
  72. if (elm == null || elm.options == null)
  73. return "";
  74. return elm.options[elm.selectedIndex].value;
  75. }
  76. function addSelectValue(form_obj, field_name, name, value) {
  77. var s = form_obj.elements[field_name];
  78. var o = new Option(name, value);
  79. s.options[s.options.length] = o;
  80. }
  81. function addClassesToList(list_id, specific_option) {
  82. // Setup class droplist
  83. var styleSelectElm = document.getElementById(list_id);
  84. var styles = tinyMCEPopup.getParam('theme_advanced_styles', false);
  85. styles = tinyMCEPopup.getParam(specific_option, styles);
  86. if (styles) {
  87. var stylesAr = styles.split(';');
  88. for (var i=0; i<stylesAr.length; i++) {
  89. if (stylesAr != "") {
  90. var key, value;
  91. key = stylesAr[i].split('=')[0];
  92. value = stylesAr[i].split('=')[1];
  93. styleSelectElm.options[styleSelectElm.length] = new Option(key, value);
  94. }
  95. }
  96. } else {
  97. tinymce.each(tinyMCEPopup.editor.dom.getClasses(), function(o) {
  98. styleSelectElm.options[styleSelectElm.length] = new Option(o.title || o['class'], o['class']);
  99. });
  100. }
  101. }
  102. function isVisible(element_id) {
  103. var elm = document.getElementById(element_id);
  104. return elm && elm.style.display != "none";
  105. }
  106. function convertRGBToHex(col) {
  107. var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
  108. var rgb = col.replace(re, "$1,$2,$3").split(',');
  109. if (rgb.length == 3) {
  110. r = parseInt(rgb[0]).toString(16);
  111. g = parseInt(rgb[1]).toString(16);
  112. b = parseInt(rgb[2]).toString(16);
  113. r = r.length == 1 ? '0' + r : r;
  114. g = g.length == 1 ? '0' + g : g;
  115. b = b.length == 1 ? '0' + b : b;
  116. return "#" + r + g + b;
  117. }
  118. return col;
  119. }
  120. function convertHexToRGB(col) {
  121. if (col.indexOf('#') != -1) {
  122. col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
  123. r = parseInt(col.substring(0, 2), 16);
  124. g = parseInt(col.substring(2, 4), 16);
  125. b = parseInt(col.substring(4, 6), 16);
  126. return "rgb(" + r + "," + g + "," + b + ")";
  127. }
  128. return col;
  129. }
  130. function trimSize(size) {
  131. return size.replace(/([0-9\.]+)px|(%|in|cm|mm|em|ex|pt|pc)/, '$1$2');
  132. }
  133. function getCSSSize(size) {
  134. size = trimSize(size);
  135. if (size == "")
  136. return "";
  137. // Add px
  138. if (/^[0-9]+$/.test(size))
  139. size += 'px';
  140. return size;
  141. }
  142. function getStyle(elm, attrib, style) {
  143. var val = tinyMCEPopup.dom.getAttrib(elm, attrib);
  144. if (val != '')
  145. return '' + val;
  146. if (typeof(style) == 'undefined')
  147. style = attrib;
  148. return tinyMCEPopup.dom.getStyle(elm, style);
  149. }