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