1(function($,undefined){ 2 "use strict"; 3 let defaults = {}; 4 let methods = { 5 init:function(params){ 6 let options = $.extend(true,{},defaults,params); 7 8 if(!this.data('apiForm')){ 9 this.data('apiForm',options); 10 11 let $form = $(this); 12 let inputs = '.api_field'; //.api_field:not(:disabled) 13 14 function onValidateFields(){ 15 let errors = false; 16 17 let $row = $(this).closest('.api_row'); 18 let $isMultiple = $(this).closest('.api_row_multiple').length; 19 let value = this.value; 20 21 if($isMultiple){ 22 if($row.hasClass('api_row_multiple_string') || $row.hasClass('api_row_multiple_file')){ 23 value = null; 24 $row.find('.api_field').each(function(indx,element){ 25 if($(element).val().length) 26 value = $(element).val(); 27 }); 28 } 29 else{ 30 let $checked = $row.find('.api_field:checked'); 31 value = !$checked.length ? null : $checked.val(); 32 } 33 } 34 35 if(!value){ 36 37 if($row.hasClass('api_row_required')){ 38 errors = true; 39 $row.addClass('api_row_invalid'); 40 $row.find('.api_field_msg').show(); 41 } 42 else{ 43 //$row.addClass('api_selected'); 44 } 45 } 46 else{ 47 $row.removeClass('api_row_invalid'); 48 $row.find('.api_field_msg').hide(); 49 } 50 51 return errors; 52 } 53 54 $form.on('click change touch keyup',inputs,function(e){ 55 onValidateFields.call(this); 56 }); 57 58 $form.on('focus',inputs,function(e){ 59 e.stopPropagation(); 60 61 $form.find(inputs).each(function(){ 62 let $row = $(this).closest('.api_row'); 63 let $isMultiple = $(this).closest('.api_row_multiple').length; 64 let value = this.value; 65 66 //Remove focus 67 $row.removeClass('api_input_focus'); 68 $row.find('.api_input_focus').removeClass('api_input_focus'); 69 70 //Remove selected 71 if($isMultiple){ 72 if(!$row.find('.api_field:checked').length) 73 value = null; 74 } 75 76 if(!value && !$row.hasClass('api_row_required') && !$row.hasClass('api_row_invalid')){ 77 $row.removeClass('api_selected'); 78 } 79 }); 80 81 $(this).closest('.api_row').addClass('api_input_focus api_selected'); 82 }); 83 84 $form.on('click',inputs,function(e){ 85 e.stopPropagation(); 86 87 let $row = $(this).closest('.api_row'); 88 let $isMultiple = $(this).closest('.api_row_multiple').length; 89 let $isTransform = $(this).closest('.api_row_transform').length; 90 91 if($isMultiple && !$isTransform){ 92 $row.find('.api_field:checked').closest('label').addClass('api_input_focus').siblings('label').removeClass('api_input_focus'); 93 } 94 else{ 95 $row.addClass('api_input_focus api_selected'); 96 } 97 }); 98 99 $(document).on('click',function(e){ 100 e.stopPropagation(); 101 102 $form.find(inputs).each(function(){ 103 let $row = $(this).closest('.api_row'); 104 let $isMultiple = $(this).closest('.api_row_multiple').length; 105 let value = this.value; 106 107 if(!$(e.target).closest($row).length){ 108 109 //Remove focus 110 $row.removeClass('api_input_focus'); 111 $row.find('.api_input_focus').removeClass('api_input_focus'); 112 113 //Remove selected 114 if($isMultiple){ 115 if(!$row.find('.api_field:checked').length) 116 value = null; 117 } 118 119 if(!value){ 120 $row.removeClass('api_selected'); 121 } 122 } 123 }); 124 }); 125 126 $form.on('submit',function(e){ 127 128 let errors = false; 129 130 $form.find(inputs).each(function(){ 131 let valid = onValidateFields.call(this); 132 if(!!valid){ 133 errors = true; 134 } 135 }); 136 137 if(!!errors){ 138 if($form.find('.api_row_invalid:first').length){ 139 $('html, body').animate({ 140 scrollTop:$form.find('.api_row_invalid:first').offset().top - 60 141 },200); 142 } 143 144 e.preventDefault(); 145 return false; 146 } 147 }); 148 149 $form.on('click','.js_add_control_field',function(){ 150 let $controls = $(this).closest('.api_controls'); 151 let $ctrlFirst = $controls.find('.api_control:first').clone();
152 let ctrlLength = $controls.find('.api_control').length; 153 154 $ctrlFirst.find('.api_field_string').val(''); 155 156 let newName = $ctrlFirst.find('.api_field_string').attr('name'); 157 newName = newName.replace(/\[(\d+?)\]$/gi,'[new' + (ctrlLength + 1) + ']'); 158 $ctrlFirst.find('.api_field_string').attr('name',newName); 159 160 $ctrlFirst.find('.js_add_control_field').show(); 161 $(this).hide(); 162 163 let $ctrlLast = $controls.find('.api_control:last'); 164 $ctrlLast.after($ctrlFirst); 165 166 $controls.find('.api_control').each(function(index){ 167 let attrName = $(this).find('.api_field_string').attr('name'); 168 let newName2 = attrName.replace(/\[new(\d+?)\]$/gi,'[new' + (index + 1) + ']'); 169 $(this).find('.api_field_string').attr('name',newName2); 170 }); 171 }); 172 173 $form.on('click','.js_del_control_field',function(){ 174 let $row = $(this).closest('.api_row'); 175 let $controls = $(this).closest('.api_controls'); 176 let $control = $(this).closest('.api_control'); 177 let $ctrlFirst = $controls.find('.api_control:first'); 178 let ctrlLength = $controls.find('.api_control').length; 179 180 if(ctrlLength <= 2){ 181 $ctrlFirst.find('.js_add_control_field').show(); 182 } 183 184 if(ctrlLength > 1){ 185 $control.remove(); 186 $row.find('.api_control:last').find('.js_add_control_field').show(); 187 } 188 else{ 189 $control.find('.api_field_string').val(''); 190 } 191 }); 192 193 194 } 195 196 return this; 197 }, 198 }; 199 200 $.fn.apiForm = function(method){ 201 if(methods[method]){ 202 return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); 203 } 204 else if(typeof method === 'object' || !method){ 205 return methods.init.apply(this,arguments); 206 } 207 else{ 208 $.error('Error! Method "' + method + '" not found in plugin $.fn.apiForm'); 209 } 210 }; 211 212})(jQuery);
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.