1/** 2 * RightPress Live Product Update 3 */ 4 5(function () { 6 7 'use strict'; 8 9 /** 10 * Delay helper 11 */ 12 var delay = (function(){ 13 14 var timers = {}; 15 16 return function(callback, ms, unique){ 17 clearTimeout(timers[unique]); 18 timers[unique] = setTimeout(callback, ms); 19 }; 20 })(); 21 22 /** 23 * Register plugin 24 */ 25 jQuery.fn.rightpress_live_product_update = function(params) { 26 27 var self = this; 28 var form = this.closest('.product').find('form.cart'); 29 30 // Unique id for each instance 31 var unique = Math.random().toString(36).slice(2); 32 33 // On input change 34 form.find(':input').on('change keyup', function() { 35 queue(); 36 }); 37 38 // Allow inputs to be attached on the fly 39 form.on('rightpress_live_product_update_attach_input', function(event, element) { 40 jQuery(element).find(':input').on('change keyup', function() { 41 queue(); 42 }); 43 }); 44 45 // On variation select and our custom event 46 form.on('found_variation, rightpress_live_product_update_trigger', function() { 47 queue(); 48 }); 49 50 // Trigger now 51 queue(); 52 53 /** 54 * Make Ajax call 55 */ 56 function call() 57 { 58 // Serialize form data 59 var form_data = form.serialize(); 60 61 // Get product id 62 var product_id = params.product_id !== undefined ? params.product_id : form.find('button[type="submit"][name="add-to-cart"]').val(); 63 64 // Add product id 65 if (product_id) { 66 form_data += (form_data !== '' ? '&' : '') + 'rightpress_reference_product_id=' + product_id; 67 } 68 69 // Compile a list of field names so that even empty fields (checkboxes, file uploads etc) are submitted 70 form.find('input, textarea, select').each(function() { 71 if (jQuery(this).is(':visible') && typeof jQuery(this).prop('name') !== 'undefined') { 72 form_data += (form_data !== '' ? '&' : '') + 'rightpress_complete_input_list[]=' + jQuery(this).prop('name'); 73 } 74 }); 75 76 // Send request 77 jQuery.ajax({ 78 type: 'POST', 79 url: params.ajax_url, 80 context: self, 81 data: { 82 action: params.action, 83 data: form_data 84 }, 85 dataType: 'json', 86 dataFilter: jQuery.rightpress.sanitize_json_response, 87 beforeSend: params.before_send, 88 success: params.response_handler 89 }); 90 } 91 92 /** 93 * Queue call 94 * Waits for 500 ms before actually executing, cancels any pending processes 95 */ 96 function queue() 97 { 98 delay(function() { 99 call(); 100 }, 500, unique); 101 } 102 103 104 }; 105 106}());
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.