1(function(w,$){ 2 3 "use strinct"; 4 5 /** 6 * Decode HTML to string 7 * 8 * @param {string} value 9 * 10 * @returns {string} 11 */ 12 w.ps_html_decode = function(value) 13 { 14 return $("<textarea/>").html(value).text(); 15 } 16 17 /** 18 * Encode string to HTML 19 * 20 * @param {string} value 21 * 22 * @returns {string} 23 */ 24 w.ps_html_encode = function(value) 25 { 26 return $('<textarea/>').text(value).html(); 27 } 28 29 /** 30 * Validate survey form 31 * 32 * @param {$} $surveyContainer 33 * 34 * @returns {Array|ps_validate_survey.errors} 35 */ 36 w.ps_validate_survey = function($surveyContainer) 37 { 38 var $questions = $surveyContainer.find(".survey_question_box:visible"); 39 var errors = []; 40 41 $questions.each(function(i){ 42 43 var required = $(this).data('validationRequired'); 44 45 if(required) 46 { 47 var inputs = $(this).find("*").serializeArray(); 48 49 var not_checked_inputs = $(this).find('input[type=checkbox]:not(:checked),input[type=radio]:not(:checked)').map(function() { 50 return {"name": this.name, "value": ''} 51 }).get(); 52 53 if(not_checked_inputs.length > 0) 54 { 55 for(var l = 0;l<not_checked_inputs.length;l++) 56 { 57 var found = false; 58 for(var k = 0;k<inputs.length; k++) 59 { 60 if(inputs[k].name == not_checked_inputs[l].name) 61 { 62 found = true; 63 } 64 } 65 66 if(!found) 67 { 68 inputs.push(not_checked_inputs[l]); 69 } 70 } 71 } 72 73 if(inputs.length == 0) 74 { 75 errors.push(ps_html_decode($(this).data('validationMessage'))); 76 } 77 else 78 { 79 for(var i = 0;i<inputs.length;i++) 80 { 81 if($.trim(inputs[i].value).length == 0) 82 { 83 errors.push(ps_html_decode($(this).data('validationMessage'))); 84 } 85 } 86 } 87 } 88 }); 89 90 return $.unique(errors); 91 } 92 93 /** 94 * Transform form data array as form data object 95 * 96 * @param {array} formArrayData 97 * 98 * @returns {object} 99 */ 100 w.ps_form_array_to_object = function(formArrayData) 101 { 102 var formData = {}; 103 104 for (var i = 0; i < formArrayData.length; i++) 105 { 106 var value = formArrayData[i]['value']; 107 108 if(typeof formData[formArrayData[i]['name']] != 'undefined') 109 { 110 var curr_value = formData[formArrayData[i]['name']]; 111 if(typeof curr_value != 'object'){ 112 curr_value = [curr_value]; 113 } 114 curr_value.push(value); 115 value = curr_value; 116 } 117 118 formData[formArrayData[i]['name']] = value; 119 } 120 return formData; 121 } 122 123 /** 124 * Reload survey box 125 * 126 * @param {string} value 127 * 128 * @returns {string} 129 */ 130 w.ps_survey_reload = function($surveyContainer) 131 { 132 ps_ajax_get('get_survey', { ID: $surveyContainer.data('id') } ,function(response){ 133 $surveyContainer.replaceWith(response.html); 134 }); 135 } 136 137 /** 138 * Send ajax request GET to PsSurveyPostTypeAjax 139 * 140 * @param string action 141 * @param object data 142 * @param function onSuccess 143 * @param function onError 144 * 145 * @returns {jqXHR} 146 */ 147 w.ps_ajax_get = function(action, data, onSuccess, onError) 148 { 149 var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)); 150 args.unshift('GET'); 151 return ps_ajax.apply(this,args); 152 } 153 154 /** 155 * Send ajax request POST to PsSurveyPostTypeAjax 156 * 157 * @param string action 158 * @param object data 159 * @param function onSuccess 160 * @param function onError 161 * 162 * @returns {jqXHR} 163 */ 164 w.ps_ajax_post = function(action, data, onSuccess, onError) 165 { 166 var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)); 167 args.unshift('POST'); 168 return ps_ajax.apply(this, args); 169 } 170 171 172 /** 173 * Send ajax request to PsSurveyPostTypeAjax 174 * 175 * @param string method 176 * @param string action 177 * @param object data 178 * @param function onSuccess 179 * @param function onError 180 * 181 * @returns {jqXHR} 182 */ 183 w.ps_ajax = function(method, action, data, onSuccess, onError) 184 { 185 var method = typeof method == 'undefined' ? 'GET' : method; 186 var onSuccess = typeof onSuccess == 'function' ? onSuccess : function(data){}; 187 var onError = typeof onError == 'function' ? onError : function(data){}; 188 var data = typeof data == 'undefined' ? {} : data; 189 190 data['action'] = action; 191 192 //console.log('ps ajax ['+method+'] url: '+ajaxurl+' data: ',data); 193 194 return $.ajax({ 195 url: ajaxurl, 196 method: method, 197 data: data, 198 success: function(response){ 199 //console.log('ps ajax response success: ',response); 200 onSuccess(response); 201 }, 202 error: function(response){ 203 //console.log('ps ajax response error: ',response); 204 onError(response); 205 }, 206 dataType: 'json' 207 }); 208 } 209 210})(window,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.