// Register eslint ignored glabals - to be revisited. // https://github.com/woocommerce/automatewoo/issues/1212 /* global $metabox, _, ajaxurl, AutomateWoo, automatewooWorkflowLocalizeScript, AW, Backbone, Cookies, quicktags, tinyMCE */ // Especially those seem pretty leaky, and dengerous. // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars /* global canvas, name, settings, html, theButtons, use, edButtons, inst, qt */ /** * AutomateWoo Workflows Admin * * @param {Function} $ jQuery. * @param {Object} workflowData automatewooWorkflowLocalizeScript */ ( function ( $, workflowData ) { AW.Workflow = Backbone.Model.extend( { getAction( actionName ) { const actions = AW.workflow.get( 'actions' ); if ( actions[ actionName ] ) { return actions[ actionName ]; } }, } ); AW.WorkflowView = Backbone.View.extend( { el: $( 'form#post' ), $triggerSelect: $( '.js-trigger-select' ), $triggerDescription: $( '.js-trigger-description' ), $manualTriggerSelect: $( '.js-manual-trigger-select' ), $typeSelect: $( '.automatewoo-workflow-type-field' ), initialize() { this.listenTo( this.model, 'change:trigger', this.changeTrigger ); this.model.set( 'prevTrigger', this.$triggerSelect.val() ); this.model.set( 'prevManualTrigger', this.$manualTriggerSelect.val() ); this.model.set( 'prevType', this.$typeSelect.val() ); this.setTriggerOptions(); this.insertMetaboxHelpTips(); }, changeTrigger() { AW.rules.resetAvailableRules(); $( document.body ).trigger( 'wc-enhanced-select-init' ); AW.initTooltips(); AW.Validate.validateAllFields(); if ( AutomateWoo.Workflows.trigger_compatibility_warning() ) { AW.workflowView.completeTriggerChange(); } }, completeTriggerChange() { AW.rules.clearIncompatibleRules(); AutomateWoo.Workflows.clearIncompatibleActions(); AutomateWoo.Modal.close(); AutomateWoo.Workflows.refine_variables(); AutomateWoo.Workflows.refine_action_selects(); AutomateWoo.Workflows.maybe_disable_queueing(); this.updateTriggerDescription(); // update the prev trigger, manual trigger, and type values this.model.set( 'prevTrigger', this.$triggerSelect.val() ); this.model.set( 'prevManualTrigger', this.$manualTriggerSelect.val() ); this.model.set( 'prevType', this.$typeSelect.val() ); $( document.body ).trigger( 'automatewoo_trigger_changed' ); }, cancelTriggerChange() { if ( this.$typeSelect.val() !== this.model.get( 'prevType' ) ) { this.$typeSelect .val( this.model.get( 'prevType' ) ) .trigger( 'change' ); } this.$triggerSelect .val( this.model.get( 'prevTrigger' ) ) .trigger( 'change', true ); this.$manualTriggerSelect .val( this.model.get( 'prevManualTrigger' ) ) .trigger( 'change' ); }, setTriggerOptions() { const options = []; $( '[name^="aw_workflow_data[trigger_options]"]' ).each( function () { if ( $( this ).attr( 'type' ) === 'checkbox' ) { options.push( { name: $( this ).attr( 'name' ), value: $( this ).is( ':checked' ), } ); } else { options.push( { name: $( this ).attr( 'name' ), text: $( this ).text(), value: $( this ).val(), } ); } } ); this.model.set( 'prevTriggerOptions', options ); }, /** * Update the trigger description */ updateTriggerDescription() { const trigger = this.model.get( 'trigger' ); if ( trigger && trigger.description ) { this.$triggerDescription.html( '

' + trigger.description + '

' ); } else { this.$triggerDescription.html( '' ); } }, initAction( actionName, $action ) { const data = this.model.getAction( actionName ); if ( data ) { $action.attr( 'data-automatewoo-action-name', actionName ); $action.attr( 'data-automatewoo-action-group', data.group ); $action.attr( 'data-automatewoo-action-can-be-previewed', data.can_be_previewed ); } else { $action.removeAttr( 'data-automatewoo-action-name' ); $action.removeAttr( 'data-automatewoo-action-group' ); $action.removeAttr( 'data-automatewoo-action-can-be-previewed' ); } // update dynamic fields $action .find( '[data-automatewoo-dynamic-select]' ) .each( function ( i, el ) { AW.workflowView.initDynamicActionSelect( $( el ), $action ); } ); AW.initTooltips(); AutomateWoo.Workflows.action_dependent_fields( $action ); AutomateWoo.init_date_pickers(); // Expand hidden action fields if required fields are empty when the form is submitted. $action.find( '.automatewoo-field' ).on( 'invalid', function () { const actionsBox = $( '#aw_actions_box' ); const actionRow = $( this ).parents( '.automatewoo-action' ); if ( actionsBox.hasClass( 'closed' ) ) { actionsBox.find( '.handlediv' ).trigger( 'click' ); } if ( ! actionRow.hasClass( 'js-open' ) ) { AutomateWoo.Workflows.action_edit_open( actionRow ); this.scrollIntoView(); } } ); }, initDynamicActionSelect( $field, $action ) { const referenceFieldName = $field.attr( 'data-automatewoo-dynamic-select-reference' ); const $referenceField = $action.find( '.automatewoo-field[data-name="' + referenceFieldName + '"]' ); $referenceField.on( 'change', function () { AW.workflowView.updateDynamicActionSelect( $field, $referenceField, $action ); } ); }, updateDynamicActionSelect( $field, $referenceField, $action ) { if ( $field.is( '.automatewoo-field--loading' ) ) { return; } // remove existing options $field.find( "option[value!='']" ).remove(); if ( ! $referenceField.val() ) { return; } const $fieldRow = $field.parents( '.automatewoo-table__row' ); $fieldRow.addClass( 'automatewoo-field-row--loading' ); const data = { action: 'aw_update_dynamic_action_select', action_name: $action.data( 'automatewoo-action-name' ), target_field_name: $field.attr( 'data-name' ), reference_field_value: $referenceField.val(), nonce: AW.workflow.get( 'nonces' ) .aw_update_dynamic_action_select, }; $.post( ajaxurl, data, function ( response ) { $fieldRow.removeClass( 'automatewoo-field-row--loading' ); if ( response.success ) { $.each( response.data, function ( value, text ) { $field.append( $( '