Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '!=':
return (v1 != v2) ? options.fn(this) : options.inverse(this);
case '!==':
return (v1 !== v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
jQuery(document).ready(function($){
function get_the_header(data) {
var link_format = $('.sheet-url').val();
if (link_format === '') {
return false;
}
// Determine the format by checking the last few characters
var the_format = link_format.slice(-3);
var lines = data.split("\n");
var result = [];
var delimiter = ',';
// Set the correct delimiter based on the format
if (the_format === 'csv') {
delimiter = ',';
} else if (the_format === 'tsv') {
delimiter = "\t";
}
// Read headers
var headers = lines[0].split(delimiter).map(header => header.trim()); // Trim any whitespace
// Process each line and create objects
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentLine = lines[i].split(delimiter);
// Only process if the line has data
if (currentLine.length > 1 || currentLine[0] !== '') {
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = (currentLine[j] !== undefined) ? currentLine[j].trim() : null; // Handle missing values
}
result.push(obj);
}
}
setfields(result);
// Append the result as a JSON string in a textarea
$('.checker-preview').append(`
`);
append_fields_to_preview();
}
function setfields(data){
$.each(data, function(i, j){
if(i == 0){
var options = '';
$.each(j, function(k,l){
var id = 'checker-item-'+k.replace(' ', '_').replace('.', '_').toLowerCase();
options += '';
});
var exist = $('.repeater-card');
if(!$('#post_id').val()){
$('.repeater-form-field').append($('#repeater-template-empty').html());
$('.select-kolom, .field-placeholder').trigger('change');
append_fields_to_preview();
}else{
setTimeout(() => {
$.ajax({
type: 'post',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'load_repeater_field_card',
pid: $('#post_id').val(),
json: $('#link_data').val()
},
success: function (response) {
console.log(response);
// renderRepeaterFields(response);
// Ambil template dari script di atas
var source = $("#repeater-template").html();
var template = Handlebars.compile(source);
// Render template dengan data respons dari server
var html = template({ fields: response });
// Masukkan hasil render ke dalam DOM
$('.repeater-form-field').html(html);
append_fields_to_preview(); // Panggil fungsi tambahan setelah render
}
});
}, 2500);
}
$('.checker-preview > *').removeClass('d-none');
setTimeout(() => {
$.ajax({
type: 'post',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'load_output_setting',
pid: $('#post_id').val(),
json: $('#link_data').val()
},
success: function (response) {
if (response.success) {
// Compile the Handlebars template
var source = $("#output-template").html();
var template = Handlebars.compile(source);
// Pass data to the template
var html = template(response.data);
// Append the rendered HTML
$('.result-value-output').html(html);
// You can call other functions after the template is rendered
append_fields_to_preview();
} else {
console.log('Error: ', response.data);
}
}
});
}, 2500);
}
});
}
$('.sheet-url').on('change', function(){
if($(this).is(':valid') && $(this).val() !== ''){
$('tr.has-link').slideDown();
$('#checker_preview.postbox').slideDown();
$('#dummy').hide();
$.ajax({
type: "GET",
url: $(this).val(),
dataType: "text",
beforeSend: function(){
$('.checker-preview').append(`
`);
},
success: function(data) {
console.log(data);
$('.checker-preview textarea').remove();
get_the_header(data);
}
});
}else{
$('tr.has-link').slideUp();
$('#dummy').show();
$('#checker_preview.postbox').slideUp();
}
});
$('.sheet-url').trigger('change');
function append_fields_to_preview() {
var form_card = $('.repeater-card');
$('.dw-checker-form-fields').html('');
if(form_card.length > 0){
$.each(form_card, function(o,p){
if($(p).find('.select-field-type').val() == 'text'){
$('.dw-checker-form-fields').append(`
`);
}else if($(p).find('.select-field-type').val() == 'select') {
var jsonData = JSON.parse($('#link_data').val());
var uniqueValues = [];
$.each(jsonData, function(index, item) {
var skema = item[$(p).find('.select-kolom').val()];
if ($.inArray(skema, uniqueValues) === -1) {
uniqueValues.push(skema);
}
});
// console.log(uniqueValues);
var options = '';
$.each(uniqueValues, function(q, r){
options += '';
});
var exist = $('.dw-checker-field');
$('.dw-checker-form-fields').append(`