_agents.js
1 year ago
_calendar.js
1 year ago
_chart.js
1 year ago
_customers.js
1 year ago
_orders.js
1 year ago
_processes.js
1 year ago
_steps.js
1 year ago
_stripe_connect.js
1 year ago
main.js
1 year ago
updates.js
1 year ago
_processes.js
339 lines
| 1 | /* |
| 2 | * Copyright (c) 2022 LatePoint LLC. All rights reserved. |
| 3 | */ |
| 4 | |
| 5 | function latepoint_process_updated(){ |
| 6 | location.reload(); |
| 7 | } |
| 8 | |
| 9 | function latepoint_process_action_removed($elem){ |
| 10 | $elem.closest('.os-form-block').remove(); |
| 11 | } |
| 12 | |
| 13 | function latepoint_replace_process_condition_element($trigger, params, $target, callback = null){ |
| 14 | $trigger.closest('.sub-section-content').addClass('os-loading'); |
| 15 | let route_name = $trigger.data('route'); |
| 16 | let data = { action: latepoint_helper.route_action, route_name: route_name, params: params, return_format: 'json' } |
| 17 | jQuery.ajax({ |
| 18 | type: 'post', |
| 19 | dataType : "json", |
| 20 | url : latepoint_timestamped_ajaxurl(), |
| 21 | data : data, |
| 22 | success: (response) => { |
| 23 | if(response.status === latepoint_helper.response_status.success){ |
| 24 | $target.html(response.message); |
| 25 | latepoint_init_process_conditions_form(); |
| 26 | $trigger.closest('.sub-section-content').removeClass('os-loading'); |
| 27 | if (typeof callback === 'function') { |
| 28 | callback(); |
| 29 | } |
| 30 | }else{ |
| 31 | alert("Error!"); |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | function latepoint_init_process_forms(){ |
| 39 | latepoint_init_process_conditions_form(); |
| 40 | |
| 41 | jQuery('.os-processes-w').on('click', '.os-run-process', function(){ |
| 42 | let $btn = jQuery(this); |
| 43 | $btn.addClass('os-loading'); |
| 44 | let $process_form = $btn.closest('.os-process-form'); |
| 45 | // remove previously assigned class on other forms |
| 46 | jQuery('.os-process-form.prepared-to-run').removeClass('prepared-to-run'); |
| 47 | // add class so we know which form is about to be processed |
| 48 | $process_form.addClass('prepared-to-run'); |
| 49 | |
| 50 | |
| 51 | |
| 52 | let form_data = new FormData($process_form[0]); |
| 53 | form_data.set('process_event_type', $process_form.closest('.os-process-form').find('.process-event-type-selector').val()); |
| 54 | |
| 55 | |
| 56 | let data = new FormData(); |
| 57 | data.append('params', latepoint_formdata_to_url_encoded_string(form_data)); |
| 58 | data.append('action', latepoint_helper.route_action); |
| 59 | data.append('route_name', $btn.data('route')); |
| 60 | data.append('return_format', 'json'); |
| 61 | |
| 62 | jQuery.ajax({ |
| 63 | type : "post", |
| 64 | dataType : "json", |
| 65 | processData: false, |
| 66 | contentType: false, |
| 67 | url : latepoint_timestamped_ajaxurl(), |
| 68 | data : data, |
| 69 | success: function(data){ |
| 70 | latepoint_show_data_in_side_panel(data.message, 'width-600'); |
| 71 | latepoint_init_process_test_form(); |
| 72 | $btn.removeClass('os-loading'); |
| 73 | } |
| 74 | }); |
| 75 | return false; |
| 76 | }); |
| 77 | |
| 78 | jQuery('.os-processes-w').find('.process-action-form').each(function(index){ |
| 79 | latepoint_init_process_action_form(jQuery(this)); |
| 80 | }); |
| 81 | |
| 82 | jQuery('.os-processes-w').on('click', '.pe-remove-condition', (event) => { |
| 83 | if(jQuery(event.currentTarget).closest('.pe-conditions').find('.pe-condition').length > 1){ |
| 84 | jQuery(event.currentTarget).closest('.pe-condition').remove(); |
| 85 | }else{ |
| 86 | alert('You need to have at least one condition if your custom field is set to be conditional.') |
| 87 | } |
| 88 | return false; |
| 89 | }); |
| 90 | |
| 91 | |
| 92 | jQuery('.os-processes-w').on('change', 'select.process-condition-operator-selector', (event) => { |
| 93 | let $select = jQuery(event.currentTarget); |
| 94 | if($select.val() == 'changed' || $select.val() == 'not_changed'){ |
| 95 | $select.closest('.pe-condition').find('.process-condition-values-w').hide(); |
| 96 | }else{ |
| 97 | $select.closest('.pe-condition').find('.process-condition-values-w').show(); |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | jQuery('.os-processes-w').on('change', 'select.process-event-type-selector', (event) => { |
| 102 | let $select = jQuery(event.currentTarget); |
| 103 | latepoint_replace_process_condition_element($select, { event_type: $select.val() }, $select.closest('.os-form-block').find('.process-event-condition-wrapper')); |
| 104 | }); |
| 105 | |
| 106 | jQuery('.os-processes-w').on('change', 'select.process-condition-object-selector', (event) => { |
| 107 | let $select = jQuery(event.currentTarget); |
| 108 | let $property_selector = $select.closest('.pe-condition').find('.process-condition-properties-w select'); |
| 109 | latepoint_replace_process_condition_element($select, { object_code: $select.val() }, $property_selector, () => { $property_selector.trigger('change'); }); |
| 110 | }); |
| 111 | |
| 112 | jQuery('.os-processes-w').on('change', 'select.process-condition-property-selector', (event) => { |
| 113 | let $select = jQuery(event.currentTarget); |
| 114 | let $operator_selector = $select.closest('.pe-condition').find('.process-condition-operators-w select'); |
| 115 | latepoint_replace_process_condition_element($select, { property: $select.val() }, $operator_selector, () => { $operator_selector.trigger('change'); }); |
| 116 | }); |
| 117 | |
| 118 | jQuery('.os-processes-w').on('change', 'select.process-condition-operator-selector', (event) => { |
| 119 | let $select = jQuery(event.currentTarget); |
| 120 | latepoint_replace_process_condition_element($select, { property: $select.closest('.pe-condition').find('select.process-condition-property-selector').val(), trigger_condition_id: $select.closest('.pe-condition').data('condition-id'), operator: $select.val() }, $select.closest('.pe-condition').find('.process-condition-values-w')); |
| 121 | }); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | function latepoint_init_process_conditions_form(){ |
| 126 | jQuery('.os-late-select').lateSelect(); |
| 127 | } |
| 128 | |
| 129 | function latepoint_add_process_condition($btn, response){ |
| 130 | $btn.closest('.pe-condition').after(response.message); |
| 131 | latepoint_init_process_conditions_form(); |
| 132 | } |
| 133 | |
| 134 | function latepoint_init_added_process_action_form($trigger){ |
| 135 | let $action_form = $trigger.prev('.process-action-form'); |
| 136 | $action_form.addClass('is-editing'); |
| 137 | latepoint_init_process_action_form($action_form); |
| 138 | } |
| 139 | |
| 140 | function latepoint_init_process_test_form(){ |
| 141 | |
| 142 | jQuery('.latepoint-run-process-btn').on('click', function(){ |
| 143 | let $btn = jQuery(this); |
| 144 | if($btn.hasClass('os-loading')) return false; |
| 145 | $btn.addClass('os-loading'); |
| 146 | let $test_action_form = jQuery('.latepoint-side-panel-w .action-settings-wrapper'); |
| 147 | |
| 148 | |
| 149 | let form_data = new FormData(jQuery('.os-process-form.prepared-to-run')[0]); |
| 150 | |
| 151 | // set data sources |
| 152 | jQuery('.process-test-data-source-selector').each(function(){ |
| 153 | form_data.set(jQuery(this).prop('name'), jQuery(this).val()); |
| 154 | }); |
| 155 | |
| 156 | // set selected actions |
| 157 | jQuery('.process-test-data-source-selector').each(function(){ |
| 158 | form_data.set(jQuery(this).prop('name'), jQuery(this).val()); |
| 159 | }); |
| 160 | |
| 161 | let action_ids_to_run = []; |
| 162 | jQuery('.action-to-run input[type="hidden"]').each(function(){ |
| 163 | if(jQuery(this).val() == 'on') action_ids_to_run.push(jQuery(this).closest('.action-to-run').data('id')); |
| 164 | }); |
| 165 | form_data.set('action_ids', action_ids_to_run.join(',')); |
| 166 | |
| 167 | |
| 168 | let data = new FormData(); |
| 169 | data.append('params', latepoint_formdata_to_url_encoded_string(form_data)); |
| 170 | data.append('action', latepoint_helper.route_action); |
| 171 | data.append('route_name', $btn.data('route')); |
| 172 | data.append('return_format', 'json'); |
| 173 | |
| 174 | jQuery.ajax({ |
| 175 | type : "post", |
| 176 | dataType : "json", |
| 177 | processData: false, |
| 178 | contentType: false, |
| 179 | url : latepoint_timestamped_ajaxurl(), |
| 180 | data : data, |
| 181 | success: function(data){ |
| 182 | $btn.removeClass('os-loading'); |
| 183 | if(data.status == 'success'){ |
| 184 | latepoint_add_notification(data.message); |
| 185 | }else{ |
| 186 | latepoint_add_notification(data.message, 'error'); |
| 187 | } |
| 188 | } |
| 189 | }); |
| 190 | }); |
| 191 | |
| 192 | jQuery('.process-action-test-data-source-selector').on('change', function(){ |
| 193 | // TODO add call to server to check if selected data sources matches conditions of this process |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | function latepoint_init_process_action_test_form(){ |
| 199 | |
| 200 | latepoint_init_json_view(jQuery('.action-preview-wrapper.type-trigger_webhook pre')); |
| 201 | |
| 202 | jQuery('.latepoint-run-action-btn').on('click', function(){ |
| 203 | let $btn = jQuery(this); |
| 204 | if($btn.hasClass('os-loading')) return false; |
| 205 | $btn.addClass('os-loading'); |
| 206 | let $test_action_form = jQuery('.latepoint-side-panel-w .action-settings-wrapper'); |
| 207 | |
| 208 | let action_data = new FormData(); |
| 209 | |
| 210 | |
| 211 | action_data.append('params', $test_action_form.find('select, textarea, input').serialize()); |
| 212 | action_data.append('action', latepoint_helper.route_action); |
| 213 | action_data.append('route_name', $btn.data('route')); |
| 214 | action_data.append('return_format', 'json'); |
| 215 | |
| 216 | jQuery.ajax({ |
| 217 | type : "post", |
| 218 | dataType : "json", |
| 219 | processData: false, |
| 220 | contentType: false, |
| 221 | url : latepoint_timestamped_ajaxurl(), |
| 222 | data : action_data, |
| 223 | success: function(data){ |
| 224 | $btn.removeClass('os-loading'); |
| 225 | if(data.status == 'success'){ |
| 226 | latepoint_add_notification(data.message); |
| 227 | }else{ |
| 228 | latepoint_add_notification(data.message, 'error'); |
| 229 | } |
| 230 | } |
| 231 | }); |
| 232 | }); |
| 233 | |
| 234 | jQuery('.process-action-test-data-source-selector').on('change', function(){ |
| 235 | let $select = jQuery(this); |
| 236 | jQuery('.action-preview-wrapper').addClass('os-loading'); |
| 237 | let $test_action_form = $select.closest('.action-settings-wrapper'); |
| 238 | |
| 239 | let action_data = new FormData(); |
| 240 | |
| 241 | |
| 242 | action_data.append('params', $test_action_form.find('select, textarea, input').serialize()); |
| 243 | action_data.append('action', latepoint_helper.route_action); |
| 244 | action_data.append('route_name', $select.data('route')); |
| 245 | action_data.append('return_format', 'json'); |
| 246 | |
| 247 | jQuery.ajax({ |
| 248 | type : "post", |
| 249 | dataType : "json", |
| 250 | processData: false, |
| 251 | contentType: false, |
| 252 | url : latepoint_timestamped_ajaxurl(), |
| 253 | data : action_data, |
| 254 | success: function(data){ |
| 255 | jQuery('.action-preview-wrapper').html(data.message).removeClass('os-loading'); |
| 256 | latepoint_init_json_view(jQuery('.action-preview-wrapper.type-trigger_webhook pre')); |
| 257 | } |
| 258 | }); |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | function latepoint_init_process_action_form($action_form){ |
| 263 | $action_form.on('click', '.os-run-process-action', function(){ |
| 264 | let $btn = jQuery(this); |
| 265 | $btn.addClass('os-loading'); |
| 266 | let $action_form = $btn.closest('.process-action-form'); |
| 267 | |
| 268 | if(window.tinyMCE !== undefined) window.tinyMCE.triggerSave(); |
| 269 | |
| 270 | let action_data = new FormData(); |
| 271 | let params = latepoint_create_form_data_from_non_form_element($action_form); |
| 272 | |
| 273 | params.set('process_event_type', $action_form.closest('.os-process-form').find('.process-event-type-selector').val()); |
| 274 | |
| 275 | action_data.append('params', latepoint_formdata_to_url_encoded_string(params)); |
| 276 | action_data.append('action', latepoint_helper.route_action); |
| 277 | action_data.append('route_name', $btn.data('route')); |
| 278 | action_data.append('return_format', 'json'); |
| 279 | |
| 280 | jQuery.ajax({ |
| 281 | type : "post", |
| 282 | dataType : "json", |
| 283 | processData: false, |
| 284 | contentType: false, |
| 285 | url : latepoint_timestamped_ajaxurl(), |
| 286 | data : action_data, |
| 287 | success: function(data){ |
| 288 | latepoint_show_data_in_side_panel(data.message, 'width-800'); |
| 289 | latepoint_init_process_action_test_form(); |
| 290 | $btn.removeClass('os-loading'); |
| 291 | } |
| 292 | }); |
| 293 | return false; |
| 294 | }); |
| 295 | $action_form.on('click', '.process-action-heading', function(){ |
| 296 | jQuery(this).closest('.process-action-form').toggleClass('is-editing'); |
| 297 | return false; |
| 298 | }); |
| 299 | $action_form.on('change', '.process-action-type', function(){ |
| 300 | jQuery(this).closest('.process-action-form').find('.process-action-name').text(jQuery(this).find('option:selected').text()); |
| 301 | }); |
| 302 | |
| 303 | $action_form.find('textarea.os-wp-editor-textarea').each(function(index){ |
| 304 | latepoint_init_tiny_mce(jQuery(this).attr('id')); |
| 305 | }); |
| 306 | $action_form.on('click', '.os-remove-process-action', function(){ |
| 307 | if(confirm(jQuery(this).data('os-prompt'))){ |
| 308 | jQuery(this).closest('.process-action-form').remove(); |
| 309 | } |
| 310 | return false; |
| 311 | }); |
| 312 | $action_form.on('change', '.process-action-type', function(){ |
| 313 | let $select = jQuery(this); |
| 314 | let action_type = $select.val(); |
| 315 | let action_id = $select.data('action-id'); |
| 316 | let route_name = $select.data('route'); |
| 317 | let data = { |
| 318 | action: latepoint_helper.route_action, |
| 319 | route_name: route_name, |
| 320 | params: { |
| 321 | action_type: action_type, |
| 322 | action_id: action_id |
| 323 | }, |
| 324 | layout: 'none', |
| 325 | return_format: 'json' |
| 326 | } |
| 327 | jQuery.ajax({ |
| 328 | type : "post", |
| 329 | dataType : "json", |
| 330 | url : latepoint_timestamped_ajaxurl(), |
| 331 | data : data, |
| 332 | success: function(data){ |
| 333 | $select.closest('.process-action-content').find('.process-action-settings').html(data.message); |
| 334 | latepoint_init_input_masks($select.closest('.process-action-form')); |
| 335 | } |
| 336 | }); |
| 337 | return false; |
| 338 | }); |
| 339 | } |