OLD | NEW |
(Empty) | |
| 1 /* Copyright 2016 The Chromium Authors. All Rights Reserved. |
| 2 * |
| 3 * Use of this source code is governed by a BSD-style |
| 4 * license that can be found in the LICENSE file or at |
| 5 * https://developers.google.com/open-source/licenses/bsd |
| 6 */ |
| 7 |
| 8 /** |
| 9 * This file contains JS code for editing fields and field definitions. |
| 10 */ |
| 11 |
| 12 var TKR_fieldNameXmlHttp; |
| 13 |
| 14 |
| 15 /** |
| 16 * Function that communicates with the server. |
| 17 * @param {string} projectName Current project name. |
| 18 * @param {string} fieldName The proposed field name. |
| 19 * @param {string} token security token. |
| 20 */ |
| 21 function TKR_checkFieldNameOnServer(projectName, fieldName, token) { |
| 22 var url = ('/p/' + projectName + '/fields/checkName' + |
| 23 '?field=' + encodeURIComponent(fieldName) + |
| 24 '&token=' + token); |
| 25 TKR_fieldNameXmlHttp = XH_XmlHttpCreate(); |
| 26 XH_XmlHttpGET(TKR_fieldNameXmlHttp, url, TKR_fieldNameCallback); |
| 27 } |
| 28 |
| 29 /** |
| 30 * The communication with the server has made some progress. If it is |
| 31 * done, then process the response. |
| 32 */ |
| 33 function TKR_fieldNameCallback() { |
| 34 if (TKR_fieldNameXmlHttp.readyState == 4) { |
| 35 if (TKR_fieldNameXmlHttp.status == 200) { |
| 36 TKR_gotFieldNameFeed(TKR_fieldNameXmlHttp); |
| 37 } |
| 38 } |
| 39 } |
| 40 |
| 41 |
| 42 /** |
| 43 * Function that evaluates the server response and sets the error message. |
| 44 * @param {object} xhr AJAX response with JSON text. |
| 45 */ |
| 46 function TKR_gotFieldNameFeed(xhr) { |
| 47 var json_data = null; |
| 48 try { |
| 49 json_data = CS_parseJSON(xhr); |
| 50 } |
| 51 catch (e) { |
| 52 return; |
| 53 } |
| 54 var errorMessage = json_data['error_message']; |
| 55 $('fieldnamefeedback').innerText = errorMessage; |
| 56 |
| 57 var choicesLines = []; |
| 58 if (json_data['choices'].length > 0) { |
| 59 for (var i = 0; i < json_data['choices'].length; i++) { |
| 60 choicesLines.push( |
| 61 json_data['choices'][i]['name'] + ' = ' + |
| 62 json_data['choices'][i]['doc']); |
| 63 } |
| 64 $('choices').innerText = choicesLines.join('\n'); |
| 65 $('field_type').value = 'enum_type'; |
| 66 $('choices_row').style.display = ''; |
| 67 enableOtherTypeOptions(true); |
| 68 } else { |
| 69 enableOtherTypeOptions(false); |
| 70 } |
| 71 |
| 72 $('submit_btn').disabled = errorMessage ? 'disabled' : ''; |
| 73 } |
| 74 |
| 75 |
| 76 function enableOtherTypeOptions(disabled) { |
| 77 var type_option_el = $('field_type').firstChild; |
| 78 while (type_option_el) { |
| 79 if (type_option_el.tagName == 'OPTION') { |
| 80 if (type_option_el.value != 'enum_type') { |
| 81 type_option_el.disabled = disabled ? 'disabled' : ''; |
| 82 } |
| 83 } |
| 84 type_option_el = type_option_el.nextSibling; |
| 85 } |
| 86 } |
OLD | NEW |