1/** 2 * RightPress Javascript Helper Functions 3 */ 4 5(function () { 6 7 /** 8 * Register functions 9 */ 10 jQuery.extend({ 11 12 rightpress: { 13 14 /** 15 * Attempt to sanitize JSON response 16 * For use as jQuery Ajax dataFilter 17 * Response must start with {"result and end with ]} 18 */ 19 sanitize_json_response: function(response) { 20 21 try { 22 23 // Attempt to parse JSON 24 jQuery.parseJSON(response); 25 26 // Parsing succeeded 27 return response; 28 } 29 catch (e) { 30 31 // Attempt to fix malformed JSON string 32 var valid_response = response.match(/{"result.*]}/); 33 34 // Check if we were able to fix it 35 if (valid_response !== null) { 36 return valid_response[0]; 37 } 38 } 39 }, 40 41 /** 42 * Safely parse JSON Ajax response 43 */ 44 parse_json_response: function (response, return_raw_data) { 45 46 // Check if we need to return parsed object or potentially fixed raw data 47 var return_raw_data = (typeof return_raw_data !== 'undefined') ? return_raw_data : false; 48 49 try { 50 51 // Attempt to parse data 52 var parsed = jQuery.parseJSON(response); 53 54 // Return appropriate value 55 return return_raw_data ? response : parsed; 56 } 57 catch (e) { 58 59 // Attempt to fix malformed JSON string 60 var regex = return_raw_data ? /{"result.*"}]}/ : /{"result.*"}/; 61 var valid_response = response.match(regex); 62 63 // Check if we were able to fix it 64 if (valid_response !== null) { 65 response = valid_response[0]; 66 } 67 } 68 69 // Second attempt to parse response data 70 return return_raw_data ? response : jQuery.parseJSON(response); 71 }, 72 73 /** 74 * Add nested object value 75 */ 76 add_nested_object_value: function (object, path, value) { 77 78 var last_key_index = path.length - 1; 79 80 for (var i = 0; i < last_key_index; ++ i) { 81 82 var key = jQuery.isNumeric(path[i]) ? parseInt(path[i]) : path[i]; 83 84 if (jQuery.isNumeric(path[i + 1])) { 85 if (typeof object[key] === 'undefined') { 86 object[key] = []; 87 } 88 } 89 else if (!(key in object)) { 90 object[key] = {}; 91 } 92 93 object = object[key]; 94 } 95 96 object[path[last_key_index]] = value; 97 }, 98 99 /** 100 * Nested object key existence check 101 */ 102 object_key_check: function (object /*, key_1, key_2... */) { 103 104 var keys = Array.prototype.slice.call(arguments, 1); 105 var current = object; 106 107 // Iterate over keys 108 for (var i = 0; i < keys.length; i++) { 109 110 // Check if current key exists 111 if (typeof current[keys[i]] === 'undefined') { 112 return false; 113 } 114 115 // Check if all but last keys are for object 116 if (i < (keys.length - 1) && typeof current[keys[i]] !== 'object') { 117 return false; 118 } 119 120 // Go one step down 121 current = current[keys[i]]; 122 } 123 124 // If we reached this point all keys from path 125 return true; 126 }, 127 128 /** 129 * Clear field value 130 */ 131 clear_field_value: function (field) { 132 133 if (field.is('select')) { 134 field.prop('selectedIndex', 0); 135 if (field.hasClass('rightpress_select2')) { 136 field.val('').change(); 137 } 138 } 139 else if (field.is(':radio, :checkbox')) { 140 field.removeAttr('checked'); 141 } 142 else { 143 field.val(''); 144 } 145 }, 146 147 /** 148 * Check if field is multiselect 149 */ 150 field_is_multiselect: function (field) { 151 return (field.is('select') && typeof field.attr('multiple') !== 'undefined' && field.attr('multiple') !== false); 152 }, 153 154 /** 155 * Check if field is empty, i.e. does not contain text, is not checked, value is not selected etc. 156 * 157 * Accepts jQuery object with multiple elements for checkbox/radio sets and single element for all other input types 158 * 159 * TODO: REVIEW IF THIS IS OK, NEVER USED/TESTED THIS 160 */ 161 field_is_empty: function (field) { 162 163 // Multiple inputs - set of checkboxes or radio buttons 164 if (field.length > 1) { 165
166 // No inputs can be checked 167 return !field.filter(':checked').length; 168 } 169 // No inputs 170 else if (field.length < 1) { 171 172 // We should never end up here but lets treat this as empty just in case 173 return true; 174 } 175 // Single input 176 else { 177 178 // Get field value 179 var value = field.val(); 180 181 // Multiselect 182 if (jQuery.rightpress.field_is_multiselect(field)) { 183 184 // Default to empty array 185 value = value || []; 186 187 // No options can be selected 188 return !value.length; 189 } 190 // Radio or checkbox inputs 191 else if (field.is(':radio, :checkbox')) { 192 193 // Field must not be checked 194 return !field.is(':checked'); 195 } 196 // Regular select fields and other inputs 197 else { 198 199 // Check if value is empty string or null 200 return (value === '' || value === null || value === undefined); 201 } 202 } 203 }, 204 205 /** 206 * Get current page url with appended extra query var 207 */ 208 get_current_url_with_query_var: function(key, value) { 209 210 // Get current url 211 var url = window.location.href; 212 213 // Check if current url has query string 214 var has_query = url.indexOf('?') > -1; 215 216 // Get operator symbol 217 var operator = has_query ? '&' : '?'; 218 219 // Append extra var if it does not exist yet 220 // Note: This function currently does not check for differences in values 221 if (!has_query || (url.indexOf(('?' + key + '=')) === -1 && url.indexOf(('&' + key + '=')) === -1)) { 222 url += operator + key + '=' + value; 223 } 224 225 // Return url 226 return url; 227 }, 228 229 /** 230 * Serialize form including disabled inputs defined by selector 231 */ 232 serialize_including_disabled: function(form, selector) { 233 234 // Get disabled inputs and enable them 235 var disabled = form.find(':input:disabled').filter(selector).removeAttr('disabled'); 236 237 // Serialize form 238 var serialized = form.serialize(); 239 240 // Enable previously disabled inputs 241 disabled.attr('disabled', 'disabled'); 242 243 // Return serialized data 244 return serialized; 245 } 246 } 247 248 }); 249 250}());
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.