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