| 1 |
import lpModalOverlay from '../../../../utils/lp-modal-overlay'; |
| 2 |
import handleAjax from '../../../../utils/handle-ajax-api'; |
| 3 |
|
| 4 |
const createIndexes = () => { |
| 5 |
const elCreateIndexTables = document.querySelector( '#lp-tool-create-indexes-tables' ); |
| 6 |
|
| 7 |
if ( ! elCreateIndexTables ) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
const elBtnCreateIndexes = elCreateIndexTables.querySelector( '.lp-btn-create-indexes' ); |
| 12 |
|
| 13 |
elBtnCreateIndexes.addEventListener( 'click', ( e ) => { |
| 14 |
e.preventDefault(); |
| 15 |
const elLoading = elCreateIndexTables.querySelector( '.wrapper-lp-loading' ); |
| 16 |
|
| 17 |
if ( ! lpModalOverlay.init() ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
lpModalOverlay.elLPOverlay.show(); |
| 22 |
lpModalOverlay.setContentModal( elLoading.innerHTML ); |
| 23 |
lpModalOverlay.setTitleModal( elCreateIndexTables.querySelector( 'h2' ).textContent ); |
| 24 |
lpModalOverlay.elBtnYes[ 0 ].style.display = 'inline-block'; |
| 25 |
lpModalOverlay.elBtnYes[ 0 ].textContent = 'Run'; |
| 26 |
lpModalOverlay.elBtnNo[ 0 ].textContent = 'Close'; |
| 27 |
|
| 28 |
const url = '/lp/v1/admin/tools/list-tables-indexs'; |
| 29 |
const params = {}; |
| 30 |
const functions = { |
| 31 |
success: ( res ) => { |
| 32 |
const { status, message, data: { tables, table } } = res; |
| 33 |
const elSteps = document.querySelector( '.example-lp-group-step' ); |
| 34 |
|
| 35 |
lpModalOverlay.setContentModal( elSteps.innerHTML ); |
| 36 |
|
| 37 |
const elGroupStep = lpModalOverlay.elLPOverlay[ 0 ].querySelector( '.lp-group-step ' ); |
| 38 |
|
| 39 |
// Show progress when upgrading. |
| 40 |
const showProgress = ( stepCurrent, percent ) => { |
| 41 |
const elItemStepCurrent = elGroupStep.querySelector( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); |
| 42 |
elItemStepCurrent.classList.add( 'running' ); |
| 43 |
|
| 44 |
if ( 100 === percent ) { |
| 45 |
elItemStepCurrent.classList.remove( 'running' ); |
| 46 |
elItemStepCurrent.classList.add( 'completed' ); |
| 47 |
} |
| 48 |
|
| 49 |
const progressBar = elItemStepCurrent.querySelector( '.progress-bar' ); |
| 50 |
progressBar.style.width = percent; |
| 51 |
}; |
| 52 |
|
| 53 |
// Scroll to step current. |
| 54 |
const scrollToStepCurrent = ( stepCurrent ) => { |
| 55 |
const elItemStepCurrent = elGroupStep.querySelector( 'input[value=' + stepCurrent + ']' ).closest( '.lp-item-step' ); |
| 56 |
|
| 57 |
const offset = elItemStepCurrent.offsetTop - lpModalOverlay.elMainContent[ 0 ].offsetTop + |
| 58 |
lpModalOverlay.elMainContent[ 0 ].scrollTop; |
| 59 |
|
| 60 |
lpModalOverlay.elMainContent.stop().animate( { |
| 61 |
scrollTop: offset, |
| 62 |
}, 600 ); |
| 63 |
}; |
| 64 |
|
| 65 |
for ( const table in tables ) { |
| 66 |
const elItemStep = lpModalOverlay.elLPOverlay[ 0 ].querySelector( '.lp-item-step' ).cloneNode( true ); |
| 67 |
const input = elItemStep.querySelector( 'input' ); |
| 68 |
const label = elItemStep.querySelector( 'label' ); |
| 69 |
|
| 70 |
label.textContent = `Table: ${ table }`; |
| 71 |
input.value = table; |
| 72 |
|
| 73 |
elGroupStep.append( elItemStep ); |
| 74 |
} |
| 75 |
|
| 76 |
lpModalOverlay.callBackYes = () => { |
| 77 |
const url = '/lp/v1/admin/tools/create-indexs'; |
| 78 |
const params = { tables, table }; |
| 79 |
|
| 80 |
lpModalOverlay.elBtnNo[ 0 ].style.display = 'none'; |
| 81 |
lpModalOverlay.elBtnYes[ 0 ].style.display = 'none'; |
| 82 |
|
| 83 |
showProgress( table, 0.1 ); |
| 84 |
|
| 85 |
const functions = { |
| 86 |
success: ( res ) => { |
| 87 |
const { status, message, data: { table, percent } } = res; |
| 88 |
|
| 89 |
showProgress( params.table, percent ); |
| 90 |
|
| 91 |
if ( undefined !== table ) { |
| 92 |
if ( params.table !== table ) { |
| 93 |
showProgress( table, 0.1 ); |
| 94 |
scrollToStepCurrent( table ); |
| 95 |
} |
| 96 |
|
| 97 |
params.table = table; |
| 98 |
} |
| 99 |
|
| 100 |
if ( 'success' === status ) { |
| 101 |
setTimeout( () => { |
| 102 |
handleAjax( url, params, functions ); |
| 103 |
}, 2000 ); |
| 104 |
} else if ( 'finished' === status ) { |
| 105 |
console.log( 'finished' ); |
| 106 |
lpModalOverlay.elBtnNo[ 0 ].style.display = 'inline-block'; |
| 107 |
lpModalOverlay.elBtnNo[ 0 ].textContent = 'Finish'; |
| 108 |
} else { |
| 109 |
console.log( message ); |
| 110 |
} |
| 111 |
}, |
| 112 |
error: ( err ) => { |
| 113 |
console.log( err ); |
| 114 |
}, |
| 115 |
completed: () => { |
| 116 |
|
| 117 |
}, |
| 118 |
}; |
| 119 |
handleAjax( url, params, functions ); |
| 120 |
}; |
| 121 |
}, |
| 122 |
error: ( err ) => { |
| 123 |
|
| 124 |
}, |
| 125 |
completed: () => { |
| 126 |
|
| 127 |
}, |
| 128 |
}; |
| 129 |
|
| 130 |
handleAjax( url, params, functions ); |
| 131 |
} ); |
| 132 |
}; |
| 133 |
|
| 134 |
export default createIndexes; |
| 135 |
|