Files
WooDoo/assets/js/woodoo-admin.js

131 lines
5.2 KiB
JavaScript
Raw Normal View History

/* WooDoo Admin JS */
jQuery( function ( $ ) {
const cfg = window.WooDooAdmin || {};
// ── Tab navigation ──────────────────────────────────────────────────
$( '.woodoo-settings .nav-tab' ).on( 'click', function ( e ) {
e.preventDefault();
const target = $( this ).attr( 'href' );
$( '.woodoo-settings .nav-tab' ).removeClass( 'nav-tab-active' );
$( this ).addClass( 'nav-tab-active' );
$( '.woodoo-tab' ).hide();
$( target ).show();
} );
// ── Test Connection ──────────────────────────────────────────────────
$( '#woodoo-test-connection' ).on( 'click', function () {
const $btn = $( this );
const $result = $( '#woodoo-test-result' );
$btn.prop( 'disabled', true ).text( cfg.i18n.testing );
$result.html( '' );
$.post( cfg.ajax_url, {
action : 'woodoo_test_connection',
nonce : cfg.nonce,
} )
.done( function ( res ) {
if ( res.success ) {
const d = res.data;
const cls = d.success ? 'success' : 'error';
$result.html(
'<span class="woodoo-badge woodoo-badge--' + ( d.success ? 'green' : 'red' ) + '">' +
escHtml( d.message ) + '</span>'
);
} else {
$result.html( '<span style="color:red;">' + escHtml( res.data || cfg.i18n.error ) + '</span>' );
}
} )
.fail( function () {
$result.html( '<span style="color:red;">' + escHtml( cfg.i18n.error ) + '</span>' );
} )
.always( function () {
$btn.prop( 'disabled', false ).text( 'Test Connection' );
} );
} );
// ── Partner search on user profile ──────────────────────────────────
$( '#woodoo-partner-search-btn' ).on( 'click', function () {
const $input = $( '#woodoo-partner-search-input' );
const $results = $( '#woodoo-partner-search-results' );
const query = $input.val().trim();
const userId = $input.data( 'user-id' );
if ( ! query ) return;
$results.html( '<em>' + escHtml( cfg.i18n.searching ) + '</em>' );
$.post( cfg.ajax_url, {
action : 'woodoo_search_partners',
nonce : cfg.nonce,
query : query,
} )
.done( function ( res ) {
if ( ! res.success || ! res.data.length ) {
$results.html( '<em>No partners found.</em>' );
return;
}
let html = '<ul style="margin:0;padding:0;list-style:none;">';
res.data.forEach( function ( p ) {
html +=
'<li style="padding:4px 0;border-bottom:1px solid #eee;">' +
'<strong>' + escHtml( p.name ) + '</strong>' +
( p.email ? ' &lt;' + escHtml( p.email ) + '&gt;' : '' ) +
' <button type="button" class="button button-small woodoo-pick-partner"' +
' data-id="' + parseInt( p.id, 10 ) + '"' +
' data-name="' + escAttr( p.name ) + '"' +
' data-user-id="' + parseInt( userId, 10 ) + '">' +
'Select</button>' +
'</li>';
} );
html += '</ul>';
$results.html( html );
} )
.fail( function () {
$results.html( '<em>' + escHtml( cfg.i18n.error ) + '</em>' );
} );
} );
// ── Pick partner from search results ────────────────────────────────
$( document ).on( 'click', '.woodoo-pick-partner', function () {
const $btn = $( this );
const partnerId = $btn.data( 'id' );
const name = $btn.data( 'name' );
const userId = $btn.data( 'user-id' );
$( '#woodoo_odoo_partner_id' ).val( partnerId );
$( '#woodoo-partner-name-display' ).text( name ).show();
$( '#woodoo-partner-search-results' ).html(
'<span style="color:green;">' + escHtml( cfg.i18n.link_done ) + ' → ' + escHtml( name ) + ' (#' + partnerId + ')</span>'
);
// Persist immediately if we have the AJAX handler
if ( userId ) {
$.post( cfg.ajax_url, {
action : 'woodoo_link_customer',
nonce : cfg.nonce,
user_id : userId,
partner_id : partnerId,
} );
}
} );
// ── Utility ──────────────────────────────────────────────────────────
function escHtml( str ) {
return String( str )
.replace( /&/g, '&amp;' )
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
.replace( /"/g, '&quot;' );
}
function escAttr( str ) {
return escHtml( str ).replace( /'/g, '&#039;' );
}
} );