_agents.js
1 year ago
_calendar.js
3 months ago
_chart.js
9 months ago
_customers.js
1 year ago
_customers_import.js
9 months ago
_delete-confirm.js
1 week ago
_orders.js
9 months ago
_processes.js
1 year ago
_razorpay_connect.js
1 month ago
_steps.js
9 months ago
_stripe_connect.js
1 year ago
main.js
2 weeks ago
updates.js
3 months ago
_processes.js
427 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('latepoint:initProcessActionForm latepoint:initProcessActionTypeSettings', '.process-action-form', async function () { |
| 42 | if (jQuery(this).find('.latepoint-whatsapp-templates-loader').length) { |
| 43 | let $loader = jQuery(this).find('.latepoint-whatsapp-templates-loader'); |
| 44 | let $settings = jQuery(this).find('.process-action-settings'); |
| 45 | let $holder = jQuery(this).find('.latepoint-whatsapp-templates-holder'); |
| 46 | $settings.addClass('os-loading'); |
| 47 | |
| 48 | let data = { |
| 49 | action: latepoint_helper.route_action, |
| 50 | route_name: $loader.data('route'), |
| 51 | params: { |
| 52 | template_id: $loader.data('selected-template-id'), |
| 53 | action_id: $loader.data('process-action-id'), |
| 54 | process_id: $loader.data('process-id') |
| 55 | }, |
| 56 | layout: 'none', |
| 57 | return_format: 'json' |
| 58 | } |
| 59 | try { |
| 60 | let response = await jQuery.ajax({ |
| 61 | type: "post", |
| 62 | dataType: "json", |
| 63 | url: latepoint_timestamped_ajaxurl(), |
| 64 | data: data |
| 65 | }); |
| 66 | $settings.removeClass('os-loading'); |
| 67 | if (response.status === 'success') { |
| 68 | $loader.remove(); |
| 69 | $holder.html(response.message); |
| 70 | } else { |
| 71 | throw new Error(response.message); |
| 72 | } |
| 73 | } catch (e) { |
| 74 | $settings.removeClass('os-loading'); |
| 75 | console.log(e); |
| 76 | alert(e); |
| 77 | } |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | jQuery('.os-processes-w').on('click', '.os-run-process', function () { |
| 82 | let $btn = jQuery(this); |
| 83 | $btn.addClass('os-loading'); |
| 84 | let $process_form = $btn.closest('.os-process-form'); |
| 85 | // remove previously assigned class on other forms |
| 86 | jQuery('.os-process-form.prepared-to-run').removeClass('prepared-to-run'); |
| 87 | // add class so we know which form is about to be processed |
| 88 | $process_form.addClass('prepared-to-run'); |
| 89 | |
| 90 | |
| 91 | let form_data = new FormData($process_form[0]); |
| 92 | form_data.set('process_event_type', $process_form.closest('.os-process-form').find('.process-event-type-selector').val()); |
| 93 | |
| 94 | |
| 95 | let data = new FormData(); |
| 96 | data.append('params', latepoint_formdata_to_url_encoded_string(form_data)); |
| 97 | data.append('action', latepoint_helper.route_action); |
| 98 | data.append('route_name', $btn.data('route')); |
| 99 | data.append('return_format', 'json'); |
| 100 | |
| 101 | jQuery.ajax({ |
| 102 | type: "post", |
| 103 | dataType: "json", |
| 104 | processData: false, |
| 105 | contentType: false, |
| 106 | url: latepoint_timestamped_ajaxurl(), |
| 107 | data: data, |
| 108 | success: function (data) { |
| 109 | latepoint_show_data_in_side_panel(data.message, 'width-600'); |
| 110 | latepoint_init_process_test_form(); |
| 111 | $btn.removeClass('os-loading'); |
| 112 | } |
| 113 | }); |
| 114 | return false; |
| 115 | }); |
| 116 | |
| 117 | jQuery('.os-processes-w').find('.process-action-form').each(function (index) { |
| 118 | latepoint_init_process_action_form(jQuery(this)); |
| 119 | }); |
| 120 | |
| 121 | jQuery('.os-processes-w').on('click', '.pe-remove-condition', (event) => { |
| 122 | if (jQuery(event.currentTarget).closest('.pe-conditions').find('.pe-condition').length > 1) { |
| 123 | jQuery(event.currentTarget).closest('.pe-condition').remove(); |
| 124 | } else { |
| 125 | alert('You need to have at least one condition if your custom field is set to be conditional.') |
| 126 | } |
| 127 | return false; |
| 128 | }); |
| 129 | |
| 130 | |
| 131 | jQuery('.os-processes-w').on('change', 'select.process-condition-operator-selector', (event) => { |
| 132 | let $select = jQuery(event.currentTarget); |
| 133 | if ($select.val() == 'changed' || $select.val() == 'not_changed') { |
| 134 | $select.closest('.pe-condition').find('.process-condition-values-w').hide(); |
| 135 | } else { |
| 136 | $select.closest('.pe-condition').find('.process-condition-values-w').show(); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | jQuery('.os-processes-w').on('change', 'select.process-event-type-selector', (event) => { |
| 141 | let $select = jQuery(event.currentTarget); |
| 142 | latepoint_replace_process_condition_element($select, {event_type: $select.val()}, $select.closest('.os-form-block').find('.process-event-condition-wrapper')); |
| 143 | }); |
| 144 | |
| 145 | jQuery('.os-processes-w').on('change', 'select.process-condition-object-selector', (event) => { |
| 146 | let $select = jQuery(event.currentTarget); |
| 147 | let $property_selector = $select.closest('.pe-condition').find('.process-condition-properties-w select'); |
| 148 | latepoint_replace_process_condition_element($select, {object_code: $select.val()}, $property_selector, () => { |
| 149 | $property_selector.trigger('change'); |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | jQuery('.os-processes-w').on('change', 'select.process-condition-property-selector', (event) => { |
| 154 | let $select = jQuery(event.currentTarget); |
| 155 | let $operator_selector = $select.closest('.pe-condition').find('.process-condition-operators-w select'); |
| 156 | latepoint_replace_process_condition_element($select, {property: $select.val()}, $operator_selector, () => { |
| 157 | $operator_selector.trigger('change'); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | jQuery('.os-processes-w').on('change', 'select.process-condition-operator-selector', (event) => { |
| 162 | let $select = jQuery(event.currentTarget); |
| 163 | latepoint_replace_process_condition_element($select, { |
| 164 | property: $select.closest('.pe-condition').find('select.process-condition-property-selector').val(), |
| 165 | trigger_condition_id: $select.closest('.pe-condition').data('condition-id'), |
| 166 | operator: $select.val() |
| 167 | }, $select.closest('.pe-condition').find('.process-condition-values-w')); |
| 168 | }); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | function latepoint_init_process_conditions_form() { |
| 173 | jQuery('.os-late-select').lateSelect(); |
| 174 | } |
| 175 | |
| 176 | function latepoint_add_process_condition($btn, response) { |
| 177 | $btn.closest('.pe-condition').after(response.message); |
| 178 | latepoint_init_process_conditions_form(); |
| 179 | } |
| 180 | |
| 181 | function latepoint_init_added_process_action_form($trigger) { |
| 182 | let $action_form = $trigger.prev('.process-action-form'); |
| 183 | $action_form.addClass('is-editing').trigger('latepoint:initProcessActionForm'); |
| 184 | latepoint_init_process_action_form($action_form); |
| 185 | } |
| 186 | |
| 187 | function latepoint_init_process_test_form() { |
| 188 | |
| 189 | jQuery('.latepoint-run-process-btn').on('click', function () { |
| 190 | let $btn = jQuery(this); |
| 191 | if ($btn.hasClass('os-loading')) return false; |
| 192 | $btn.addClass('os-loading'); |
| 193 | let $test_action_form = jQuery('.latepoint-side-panel-w .action-settings-wrapper'); |
| 194 | |
| 195 | |
| 196 | let form_data = new FormData(jQuery('.os-process-form.prepared-to-run')[0]); |
| 197 | |
| 198 | // set data sources |
| 199 | jQuery('.process-test-data-source-selector').each(function () { |
| 200 | form_data.set(jQuery(this).prop('name'), jQuery(this).val()); |
| 201 | }); |
| 202 | |
| 203 | // set selected actions |
| 204 | jQuery('.process-test-data-source-selector').each(function () { |
| 205 | form_data.set(jQuery(this).prop('name'), jQuery(this).val()); |
| 206 | }); |
| 207 | |
| 208 | let action_ids_to_run = []; |
| 209 | jQuery('.action-to-run input[type="hidden"]').each(function () { |
| 210 | if (jQuery(this).val() == 'on') action_ids_to_run.push(jQuery(this).closest('.action-to-run').data('id')); |
| 211 | }); |
| 212 | form_data.set('action_ids', action_ids_to_run.join(',')); |
| 213 | |
| 214 | |
| 215 | let data = new FormData(); |
| 216 | data.append('params', latepoint_formdata_to_url_encoded_string(form_data)); |
| 217 | data.append('action', latepoint_helper.route_action); |
| 218 | data.append('route_name', $btn.data('route')); |
| 219 | data.append('return_format', 'json'); |
| 220 | |
| 221 | jQuery.ajax({ |
| 222 | type: "post", |
| 223 | dataType: "json", |
| 224 | processData: false, |
| 225 | contentType: false, |
| 226 | url: latepoint_timestamped_ajaxurl(), |
| 227 | data: data, |
| 228 | success: function (data) { |
| 229 | $btn.removeClass('os-loading'); |
| 230 | if (data.status == 'success') { |
| 231 | latepoint_add_notification(data.message); |
| 232 | } else { |
| 233 | latepoint_add_notification(data.message, 'error'); |
| 234 | } |
| 235 | } |
| 236 | }); |
| 237 | }); |
| 238 | |
| 239 | jQuery('.process-action-test-data-source-selector').on('change', function () { |
| 240 | // TODO add call to server to check if selected data sources matches conditions of this process |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | function latepoint_init_process_action_test_form() { |
| 246 | |
| 247 | latepoint_init_json_view(jQuery('.action-preview-wrapper.type-trigger_webhook pre')); |
| 248 | |
| 249 | jQuery('.latepoint-run-action-btn').on('click', function () { |
| 250 | let $btn = jQuery(this); |
| 251 | if ($btn.hasClass('os-loading')) return false; |
| 252 | $btn.addClass('os-loading'); |
| 253 | let $test_action_form = jQuery('.latepoint-side-panel-w .action-settings-wrapper'); |
| 254 | |
| 255 | let action_data = new FormData(); |
| 256 | |
| 257 | |
| 258 | action_data.append('params', $test_action_form.find('select, textarea, input').serialize()); |
| 259 | action_data.append('action', latepoint_helper.route_action); |
| 260 | action_data.append('route_name', $btn.data('route')); |
| 261 | action_data.append('return_format', 'json'); |
| 262 | |
| 263 | jQuery.ajax({ |
| 264 | type: "post", |
| 265 | dataType: "json", |
| 266 | processData: false, |
| 267 | contentType: false, |
| 268 | url: latepoint_timestamped_ajaxurl(), |
| 269 | data: action_data, |
| 270 | success: function (data) { |
| 271 | $btn.removeClass('os-loading'); |
| 272 | if (data.status == 'success') { |
| 273 | latepoint_add_notification(data.message); |
| 274 | } else { |
| 275 | latepoint_add_notification(data.message, 'error'); |
| 276 | } |
| 277 | } |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | jQuery('.process-action-test-data-source-selector').on('change', function () { |
| 282 | let $select = jQuery(this); |
| 283 | jQuery('.action-preview-wrapper').addClass('os-loading'); |
| 284 | let $test_action_form = $select.closest('.action-settings-wrapper'); |
| 285 | |
| 286 | let action_data = new FormData(); |
| 287 | |
| 288 | |
| 289 | action_data.append('params', $test_action_form.find('select, textarea, input').serialize()); |
| 290 | action_data.append('action', latepoint_helper.route_action); |
| 291 | action_data.append('route_name', $select.data('route')); |
| 292 | action_data.append('return_format', 'json'); |
| 293 | |
| 294 | jQuery.ajax({ |
| 295 | type: "post", |
| 296 | dataType: "json", |
| 297 | processData: false, |
| 298 | contentType: false, |
| 299 | url: latepoint_timestamped_ajaxurl(), |
| 300 | data: action_data, |
| 301 | success: function (data) { |
| 302 | jQuery('.action-preview-wrapper').html(data.message).removeClass('os-loading'); |
| 303 | latepoint_init_json_view(jQuery('.action-preview-wrapper.type-trigger_webhook pre')); |
| 304 | } |
| 305 | }); |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | function latepoint_init_process_action_form($action_form) { |
| 310 | $action_form.on('click', '.os-run-process-action', function () { |
| 311 | let $btn = jQuery(this); |
| 312 | $btn.addClass('os-loading'); |
| 313 | let $action_form = $btn.closest('.process-action-form'); |
| 314 | |
| 315 | if (window.tinyMCE !== undefined) window.tinyMCE.triggerSave(); |
| 316 | |
| 317 | let action_data = new FormData(); |
| 318 | let params = latepoint_create_form_data_from_non_form_element($action_form); |
| 319 | |
| 320 | params.set('process_event_type', $action_form.closest('.os-process-form').find('.process-event-type-selector').val()); |
| 321 | |
| 322 | action_data.append('params', latepoint_formdata_to_url_encoded_string(params)); |
| 323 | action_data.append('action', latepoint_helper.route_action); |
| 324 | action_data.append('route_name', $btn.data('route')); |
| 325 | action_data.append('return_format', 'json'); |
| 326 | |
| 327 | jQuery.ajax({ |
| 328 | type: "post", |
| 329 | dataType: "json", |
| 330 | processData: false, |
| 331 | contentType: false, |
| 332 | url: latepoint_timestamped_ajaxurl(), |
| 333 | data: action_data, |
| 334 | success: function (data) { |
| 335 | latepoint_show_data_in_side_panel(data.message, 'width-800'); |
| 336 | latepoint_init_process_action_test_form(); |
| 337 | $btn.removeClass('os-loading'); |
| 338 | } |
| 339 | }); |
| 340 | return false; |
| 341 | }); |
| 342 | $action_form.on('click', '.process-action-heading', function () { |
| 343 | let $action_form = jQuery(this).closest('.process-action-form'); |
| 344 | if (!$action_form.hasClass('is-editing')) $action_form.trigger('latepoint:initProcessActionForm') |
| 345 | $action_form.toggleClass('is-editing'); |
| 346 | return false; |
| 347 | }); |
| 348 | |
| 349 | $action_form.find('textarea.os-wp-editor-textarea').each(function (index) { |
| 350 | latepoint_init_tiny_mce(jQuery(this).attr('id')); |
| 351 | }); |
| 352 | $action_form.on('click', '.os-remove-process-action', function () { |
| 353 | if (confirm(jQuery(this).data('os-prompt'))) { |
| 354 | jQuery(this).closest('.process-action-form').remove(); |
| 355 | } |
| 356 | return false; |
| 357 | }); |
| 358 | |
| 359 | $action_form.on('change', '.process-action-type-whatsapp-template-selector', async function () { |
| 360 | let $select = jQuery(this); |
| 361 | let $preview_holder = $select.closest('.process-action-settings').find('.latepoint-whatsapp-template-preview-holder'); |
| 362 | $preview_holder.addClass('os-loading'); |
| 363 | |
| 364 | let data = { |
| 365 | action: latepoint_helper.route_action, |
| 366 | route_name: $select.data('route'), |
| 367 | params: { |
| 368 | template_id: $select.val(), |
| 369 | action_id: $select.data('action-id') |
| 370 | }, |
| 371 | layout: 'none', |
| 372 | return_format: 'json' |
| 373 | } |
| 374 | try { |
| 375 | let response = await jQuery.ajax({ |
| 376 | type: "post", |
| 377 | dataType: "json", |
| 378 | url: latepoint_timestamped_ajaxurl(), |
| 379 | data: data |
| 380 | }); |
| 381 | $preview_holder.removeClass('os-loading'); |
| 382 | if (response.status === 'success') { |
| 383 | $preview_holder.html(response.message); |
| 384 | } else { |
| 385 | throw new Error(response.message); |
| 386 | } |
| 387 | } catch (e) { |
| 388 | $preview_holder.removeClass('os-loading'); |
| 389 | console.log(e); |
| 390 | alert(e); |
| 391 | } |
| 392 | return false; |
| 393 | }); |
| 394 | |
| 395 | $action_form.on('change', '.process-action-type', function () { |
| 396 | let $select = jQuery(this); |
| 397 | jQuery(this).closest('.process-action-form').find('.process-action-name').text($select.find('option:selected').text()); |
| 398 | let action_type = $select.val(); |
| 399 | let action_id = $select.data('action-id'); |
| 400 | let route_name = $select.data('route'); |
| 401 | let $action_settings = $select.closest('.process-action-content').find('.process-action-settings'); |
| 402 | $action_settings.addClass('os-loading'); |
| 403 | let data = { |
| 404 | action: latepoint_helper.route_action, |
| 405 | route_name: route_name, |
| 406 | params: { |
| 407 | action_type: action_type, |
| 408 | action_id: action_id |
| 409 | }, |
| 410 | layout: 'none', |
| 411 | return_format: 'json' |
| 412 | } |
| 413 | jQuery.ajax({ |
| 414 | type: "post", |
| 415 | dataType: "json", |
| 416 | url: latepoint_timestamped_ajaxurl(), |
| 417 | data: data, |
| 418 | success: function (data) { |
| 419 | $action_settings.html(data.message).removeClass('os-loading'); |
| 420 | let $action_form = $select.closest('.process-action-form'); |
| 421 | latepoint_init_input_masks($action_form); |
| 422 | $action_form.trigger('latepoint:initProcessActionTypeSettings'); |
| 423 | } |
| 424 | }); |
| 425 | return false; |
| 426 | }); |
| 427 | } |