1function AutoshipSchedule() { 2 this.send_request_interval = null; 3} 4 5AutoshipSchedule.prototype.onBindScheduleActions = []; 6AutoshipSchedule.prototype.onReloadSchedule = []; 7 8AutoshipSchedule.prototype.callEventHandlers = function (eventHandlers, arg1, arg2, arg3) { 9 for (var i = 0; i < eventHandlers.length; i++) { 10 eventHandlers[i].call(this, arg1, arg2, arg3); 11 } 12} 13 14AutoshipSchedule.prototype.sendRequest = function (schedule_id, url, data, delay, success, fail, always) { 15 var context = this; 16 clearInterval(this.send_request_interval); 17 var $schedule = jQuery('#wc-autoship-schedule-' + schedule_id); 18 this.send_request_interval = setTimeout(function () { 19 context.setScheduleLoadingState(schedule_id); 20 var request = jQuery.post(url, data) 21 .success(function (response) { 22 if (success != null) { 23 success(response); 24 } 25 }).fail(function () { 26 alert( "error" ); 27 if (fail != null) { 28 fail(); 29 } 30 }).always(function () { 31 context.unsetScheduleLoadingState(schedule_id); 32 if (always != null) { 33 always(); 34 } 35 }); 36 }, delay); 37} 38 39AutoshipSchedule.prototype.setScheduleLoadingState = function (schedule_id) { 40 var $schedule = jQuery('#wc-autoship-schedule-' + schedule_id); 41 $schedule.addClass('wc-autoship-loading'); 42 $schedule.find('input').attr('disabled', 'disabled'); 43 $schedule.find('button').attr('disabled', 'disabled'); 44} 45 46AutoshipSchedule.prototype.unsetScheduleLoadingState = function (schedule_id) { 47 var $schedule = jQuery('#wc-autoship-schedule-' + schedule_id); 48 $schedule.removeClass('wc-autoship-loading'); 49 $schedule.find('input').removeAttr('disabled'); 50 $schedule.find('button').removeAttr('disabled'); 51} 52 53AutoshipSchedule.prototype.reloadSchedule = function (schedule_id, active) { 54 var context = this; 55 var data = { 56 schedule_id: schedule_id, 57 active: active ? 1 : 0 58 } 59 this.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_get_schedule', data, 0, function (schedule_data) { 60 var $new_schedule = jQuery(schedule_data.html); 61 jQuery('#wc-autoship-schedule-' + schedule_id).replaceWith($new_schedule); 62 context.bindScheduleActions($new_schedule); 63 context.callEventHandlers(context.onReloadSchedule, schedule_id, active, schedule_data); 64 }); 65} 66 67AutoshipSchedule.prototype.bindScheduleActions = function ($schedule) { 68 var context = this; 69 70 // Autoship UI 71 init_wc_autoship_ui(); 72 73 // Autoship status 74 var toggleAutoshipStatus = function () { 75 var $input = jQuery(this); 76 var schedule_id = $input.data('schedule-id'); 77 var current_autoship_status = $input.data('autoship-status'); 78 var data = { 79 schedule_id: schedule_id, 80 autoship_status: (current_autoship_status == 1) ? 0 : 1 81 }; 82 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_set_autoship_status', data, 0, function (schedule) { 83 context.reloadSchedule(schedule.id, (schedule.autoship_status == 1) ? true : false); 84 }); 85 }; 86 $schedule.find('.wc-autoship-schedule-autoship-status-btn').click(toggleAutoshipStatus); 87 // Next order date 88 var saveNextOrderDate = function () { 89 var $input = jQuery(this); 90 var schedule_id = $input.data('schedule-id'); 91 var data = { 92 schedule_id: schedule_id, 93 next_order_date: $input.val() 94 }; 95 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_set_next_order_date', data, 0, function (schedule) { 96 context.reloadSchedule(schedule.id, true); 97 }); 98 }; 99 $schedule.find('.wc-autoship-schedule-next-order-date-input').change(saveNextOrderDate) 100 .each(function () { 101 $picker = jQuery(this); 102 $picker.datepicker({ 103 dateFormat: 'yy-mm-dd', 104 minDate: $picker.data('min') 105 }); 106 } ); 107 // Delete schedule 108 var deleteSchedule = function () { 109 var confirm_delete = confirm('Are you sure you want to delete this schedule?'); 110 if (!confirm_delete) { 111 return; 112 } 113 var $input = jQuery(this); 114 var schedule_id = $input.data('schedule-id'); 115 var data = { 116 schedule_id: schedule_id, 117 }; 118 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_delete', data, 0, function () { 119 jQuery('#wc-autoship-schedule-' + schedule_id).fadeOut(); 120 }); 121 }; 122 $schedule.find('.wc-autoship-schedule-delete-btn').click(deleteSchedule); 123 124 // Schedule item quantity 125 var saveItemQuantity = function() { 126 var $input = jQuery(this); 127 var schedule_id = $input.data('schedule-id'); 128 var schedule_item_id = $input.data('schedule-item-id'); 129 var data = { 130 schedule_item_id: schedule_item_id, 131 quantity: $input.val() 132 }; 133 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_item_set_quantity', data, 700, function (schedule_item) { 134 context.reloadSchedule(schedule_item.schedule_id, true); 135 }); 136 }; 137 $schedule.find('.wc-autoship-schedule-item-quantity-input').keyup(saveItemQuantity).change(saveItemQuantity); 138 139 // Schedule item delete 140 var deleteItem = function() { 141 var $input = jQuery(this); 142 var schedule_id = $input.data('schedule-id'); 143 var schedule_item_id = $input.data('schedule-item-id'); 144 var data = { 145 schedule_item_id: schedule_item_id 146 }; 147 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_item_delete', data, 0, function () { 148 context.reloadSchedule(schedule_id, true); 149 }); 150 }; 151 $schedule.find('.wc-autoship-schedule-item-delete-btn').click(deleteItem); 152 153 // Add schedule item 154 var addItem = function() { 155 var $input = jQuery(this); 156 // Parse product data 157 var value = $input.val(); 158 if (value == '') { 159 return; 160 } 161 var schedule_id = $input.data('schedule-id'); 162 var product_data = JSON.parse(value); 163 // Create ajax data 164 var data = { 165 schedule_id: schedule_id, 166 product_id: product_data.product_id, 167 variation_id: product_data.variation_id, 168 quantity: 1 169 }; 170 // Send request 171 context.sendRequest(schedule_id, AUTOSHIP_SCHEDULES.ajax_url + '?action=schedules_action_item_add', data, 0, function (schedule_item) { 172 context.reloadSchedule(schedule_item.schedule_id, true); 173 }); 174 }; 175 $schedule.find('.wc-autoship-schedule-item-add-input').bind('add-item', addItem).change(addItem); 176 $schedule.find('.wc-autoship-schedule-item-add-btn').click(function () { 177 jQuery(jQuery(this).data('target')).trigger('add-item'); 178 }); 179 180 this.callEventHandlers(this.onBindScheduleActions, $schedule); 181} 182 183 184jQuery(function ($) { 185 var autoshipSchedule = new AutoshipSchedule(); 186 autoshipSchedule.bindScheduleActions($('.wc-autoship-schedule')); 187}); 188
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.