1'use strict'; 2 3var ProgressBar = require('./progressBar.js'); 4 5function showError($el, text) { 6 if (typeof text !== 'string') { 7 text = text.text(); 8 } 9 10 $el.siblings('.error-msg').html(text); 11 highlightError($el, true); 12} 13 14function highlightError($el, b) { 15 $el.toggleClass('error', b); 16} 17 18function isValidField($el) { 19 var isRequired = $el.data('required'); 20 var type = $el.data('ls_type'); 21 var $item = $el.find('.sc_value'); 22 var val = null; 23 24 highlightError($item, false); 25 26 if (['image', 'video'].indexOf(type) > -1) { 27 if (($item.prop('files')) && ($item.prop('files').length)) { 28 val = $item.prop('files')[0]; 29 } 30 31 var oldVal = $el.find('.sc_value_old').val(); 32 33 if ((isRequired) && (!val) && (!oldVal)) { 34 showError($item, I18n.t('error.validator.required')); 35 return false; 36 } 37 38 var maxSize = (type === 'image') ? 5 : 100; 39 40 if ((val) && (val.size > (maxSize * 1024 * 1024))) { 41 showError($item, I18n.t('error.validator.client.file_max_size_lim', {max_size: maxSize})); 42 return false; 43 } 44 } else { 45 val = ($item.val() || '').trim(); 46 if ((!val) && (isRequired)) { 47 showError($item, I18n.t('error.validator.required')); 48 return false; 49 } 50 } 51 52 return true; 53} 54 55function isValid($fields) { 56 var hasError = false; 57 58 $fields.each(function(i, e) { 59 if (!isValidField($(e))) { 60 hasError = true; 61 } 62 }); 63 64 return !hasError; 65} 66 67module.exports = { 68 init: function () { 69 $(document).on('change', '.sc_value', function(e) { 70 e.preventDefault(); 71 72 var $item = $(this).closest('.form-item'); 73 var lsType = $item.data('ls_type'); 74 75 if (lsType === 'text') { 76 return; 77 } 78 79 $item.find('.sc_value_old').val(''); 80 }); 81 82 $(document).on('submit', 'form.subscribe-comment-form', function(e) { 83 e.preventDefault(); 84 85 var $form = $('form.subscribe-comment-form'); 86 var projectId = $form.find('[name="project_id"]').val(); 87 var levelId = $form.find('[name="level_id"]').val(); 88 var $fields = $form.find('.form-item'); 89 90 if ((!isValid($fields)) || (!$fields.length)) { 91 return; 92 } 93 94 var formData = new FormData(); 95 formData.append('level_id', levelId); 96 var cntItems = 0; 97 98 $fields.each(function(i, e) { 99 var $inp = $(e).find('.sc_value'); 100 101 if ($inp.prop('readonly')) { 102 return; 103 } 104 105 var lsId = parseInt($(e).data('ls_id')) || 0; 106 var lsType = $(e).data('ls_type'); 107 var scValue = null; 108 109 if (lsType === 'text') { 110 scValue = $inp.val(); 111 if ($inp.data('sc_value') === scValue) { 112 return; 113 } 114 115 formData.append('fields['+ lsId +'][text]', scValue); 116 } else { 117 scValue = $inp.prop('files')[0]; 118 if (scValue) { 119 formData.append('fields['+ lsId +'][file]', scValue); 120 } else { 121 return; 122 } 123 } 124 125 cntItems++; 126 }); 127 128 if (!cntItems) { 129 return; 130 } 131 132 var _id = ProgressBar.show(); 133 134 $.ajax({ 135 url: '/project/' + projectId + '/subscribe-comment/', 136 type: 'POST', 137 data: formData, 138 processData: false, 139 contentType: false, 140 dataType: 'json', 141 success: function(result) { 142 ProgressBar.hide(_id); 143 144 if (result.error) { 145 var err = I18n.t('error.try_again'); 146 147 if (result.error.message) { 148 err = result.error.message; 149 } 150 151 alert(err); 152 return; 153 } 154 155 if (parseInt($form.data('hide_after_save'))) { 156 $form.hide(); 157 $('.subscribe-comment-saved').show(); 158 } else { 159 $.toast({ 160 text: I18n.t('common.msg.comment_sub_save_ok'), 161 icon: 'success', 162 position: 'top-right', 163 loader: false, 164 afterHidden: 6000 165 }); 166 167 window.location.reload(); 168 } 169 } 170 }); 171 }); 172 173 174 }, 175};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.