You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.9 KiB

1 year ago
  1. /**
  2. * form_utils.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2017 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. var themeBaseURL = tinyMCEPopup.editor.baseURI.toAbsolute('themes/' + tinyMCEPopup.getParam("theme"));
  11. function getColorPickerHTML(id, target_form_element) {
  12. var h = "", dom = tinyMCEPopup.dom;
  13. if (label = dom.select('label[for=' + target_form_element + ']')[0]) {
  14. label.id = label.id || dom.uniqueId();
  15. }
  16. 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">';
  17. 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>';
  18. return h;
  19. }
  20. function updateColor(img_id, form_element_id) {
  21. document.getElementById(img_id).style.backgroundColor = document.forms[0].elements[form_element_id].value;
  22. }
  23. function setBrowserDisabled(id, state) {
  24. var img = document.getElementById(id);
  25. var lnk = document.getElementById(id + "_link");
  26. if (lnk) {
  27. if (state) {
  28. lnk.setAttribute("realhref", lnk.getAttribute("href"));
  29. lnk.removeAttribute("href");
  30. tinyMCEPopup.dom.addClass(img, 'disabled');
  31. } else {
  32. if (lnk.getAttribute("realhref")) {
  33. lnk.setAttribute("href", lnk.getAttribute("realhref"));
  34. }
  35. tinyMCEPopup.dom.removeClass(img, 'disabled');
  36. }
  37. }
  38. }
  39. function getBrowserHTML(id, target_form_element, type, prefix) {
  40. var option = prefix + "_" + type + "_browser_callback", cb, html;
  41. cb = tinyMCEPopup.getParam(option, tinyMCEPopup.getParam("file_browser_callback"));
  42. if (!cb) {
  43. return "";
  44. }
  45. html = "";
  46. html += '<a id="' + id + '_link" href="javascript:openBrowser(\'' + id + '\',\'' + target_form_element + '\', \'' + type + '\',\'' + option + '\');" onmousedown="return false;" class="browse">';
  47. html += '<span id="' + id + '" title="' + tinyMCEPopup.getLang('browse') + '">&nbsp;</span></a>';
  48. return html;
  49. }
  50. function openBrowser(img_id, target_form_element, type, option) {
  51. var img = document.getElementById(img_id);
  52. if (img.className != "mceButtonDisabled") {
  53. tinyMCEPopup.openBrowser(target_form_element, type, option);
  54. }
  55. }
  56. function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
  57. if (!form_obj || !form_obj.elements[field_name]) {
  58. return;
  59. }
  60. if (!value) {
  61. value = "";
  62. }
  63. var sel = form_obj.elements[field_name];
  64. var found = false;
  65. for (var i = 0; i < sel.options.length; i++) {
  66. var option = sel.options[i];
  67. if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
  68. option.selected = true;
  69. found = true;
  70. } else {
  71. option.selected = false;
  72. }
  73. }
  74. if (!found && add_custom && value != '') {
  75. var option = new Option(value, value);
  76. option.selected = true;
  77. sel.options[sel.options.length] = option;
  78. sel.selectedIndex = sel.options.length - 1;
  79. }
  80. return found;
  81. }
  82. function getSelectValue(form_obj, field_name) {
  83. var elm = form_obj.elements[field_name];
  84. if (elm == null || elm.options == null || elm.selectedIndex === -1) {
  85. return "";
  86. }
  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 = tinyMCEPopup.getParam('theme_advanced_styles', false);
  98. styles = tinyMCEPopup.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. /*tinymce.each(tinyMCEPopup.editor.dom.getClasses(), function(o) {
  111. styleSelectElm.options[styleSelectElm.length] = new Option(o.title || o['class'], o['class']);
  112. });*/
  113. }
  114. }
  115. function isVisible(element_id) {
  116. var elm = document.getElementById(element_id);
  117. return elm && elm.style.display != "none";
  118. }
  119. function convertRGBToHex(col) {
  120. var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
  121. var rgb = col.replace(re, "$1,$2,$3").split(',');
  122. if (rgb.length == 3) {
  123. r = parseInt(rgb[0]).toString(16);
  124. g = parseInt(rgb[1]).toString(16);
  125. b = parseInt(rgb[2]).toString(16);
  126. r = r.length == 1 ? '0' + r : r;
  127. g = g.length == 1 ? '0' + g : g;
  128. b = b.length == 1 ? '0' + b : b;
  129. return "#" + r + g + b;
  130. }
  131. return col;
  132. }
  133. function convertHexToRGB(col) {
  134. if (col.indexOf('#') != -1) {
  135. col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
  136. r = parseInt(col.substring(0, 2), 16);
  137. g = parseInt(col.substring(2, 4), 16);
  138. b = parseInt(col.substring(4, 6), 16);
  139. return "rgb(" + r + "," + g + "," + b + ")";
  140. }
  141. return col;
  142. }
  143. function trimSize(size) {
  144. return size.replace(/([0-9\.]+)(px|%|in|cm|mm|em|ex|pt|pc)/i, '$1$2');
  145. }
  146. function getCSSSize(size) {
  147. size = trimSize(size);
  148. if (size == "") {
  149. return "";
  150. }
  151. // Add px
  152. if (/^[0-9]+$/.test(size)) {
  153. size += 'px';
  154. }
  155. // Sanity check, IE doesn't like broken values
  156. else if (!(/^[0-9\.]+(px|%|in|cm|mm|em|ex|pt|pc)$/i.test(size))) {
  157. return "";
  158. }
  159. return size;
  160. }
  161. function getStyle(elm, attrib, style) {
  162. var val = tinyMCEPopup.dom.getAttrib(elm, attrib);
  163. if (val != '') {
  164. return '' + val;
  165. }
  166. if (typeof (style) == 'undefined') {
  167. style = attrib;
  168. }
  169. return tinyMCEPopup.dom.getStyle(elm, style);
  170. }