Skip to content

Commit

Permalink
revert global-checklists.js
Browse files Browse the repository at this point in the history
  • Loading branch information
richaferry committed Jul 30, 2024
1 parent ce9cfaf commit 9c72573
Showing 1 changed file with 79 additions and 72 deletions.
151 changes: 79 additions & 72 deletions modules/checklists/assets/js/global-checklists.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
$(function () {
show_post_type_requirements(objectL10n_checklists_global_checklist.first_post_type);

$('#pp-checklists-requirements tbody').sortable({ items: ' > tr' });
$('#pp-checklists-requirements tbody').sortable({
items : ' > tr'
});

// Set the event for the post type filter
$('#pp-checklists-post-type-filter a').on('click', function (event) {
Expand All @@ -52,12 +54,9 @@
$('.pp-checklists-number').on('keypress', function (event) {
var key = event.keyCode || event.which;
var allowed_keys = [
// arrows
35, 36, 37, 38, 39, 40,
// backspace, tab, delete, esc, enter
8, 9, 46, 27, 13,
// 0-9
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
35, 36, 37, 38, 39, 40, // arrows
8, 9, 46, 27, 13, // backspace, tab, delete, esc, enter
48, 49, 50, 51, 52, 53, 54, 55, 56, 57 // 0-9
];

// Ignore any key different than number
Expand All @@ -73,14 +72,10 @@
$('.pp-checklists-float').on('keypress', function (event) {
var key = event.keyCode || event.which;
var allowed_keys = [
// arrows
35, 36, 37, 38, 39, 40,
// decimal separators
44, 46,
// backspace, tab, delete, esc, enter
8, 9, 46, 27, 13,
// 0-9
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
35, 36, 37, 38, 39, 40, // arrows
44, 46, // decimal separators
8, 9, 46, 27, 13, // backspace, tab, delete, esc, enter
48, 49, 50, 51, 52, 53, 54, 55, 56, 57 // 0-9
];

// Ignore any key different than number
Expand All @@ -99,7 +94,7 @@
*
* @param {string} post_type
*/
function show_post_type_requirements(post_type) {
function show_post_type_requirements (post_type) {
// Hide the requirements which are not for the current post type
$('#pp-checklists-requirements tr.pp-checklists-requirement-row').hide();
// Display the correct requirements
Expand All @@ -110,7 +105,7 @@

//add active class to title tab
$('.pp-checklists-tabs a[data-tab="title"]').addClass('active');

// Mark the filter as selected
$('#pp-checklists-post-type-filter li.nav-tab-active').removeClass('nav-tab-active');
$('#pp-checklists-post-type-filter li.post-type-' + post_type).addClass('nav-tab-active');
Expand All @@ -121,7 +116,7 @@
*
* @return string
*/
function get_current_post_type() {
function get_current_post_type () {
var post_type = $('#pp-checklists-post-type-filter li.nav-tab-active a').attr('href').substring(1);

if (post_type === '' || post_type === false || post_type === null || typeof post_type === undefined) {
Expand All @@ -138,7 +133,7 @@
* @param {string} id
* @param {string} type
*/
function remove_row(id, type) {
function remove_row (id, type) {
// Add a special hidden input to flag the delete action
var $input = $('<input type="hidden" />')
.attr('name', 'publishpress_checklists_checklists_options[' + type + '_items_remove][]')
Expand All @@ -154,7 +149,7 @@
*
* @param {Event} event
*/
function callback_remove_row(event) {
function callback_remove_row (event) {
var $target = $(event.target);

remove_row($target.data('id'), $target.data('type'));
Expand All @@ -168,7 +163,7 @@
*
* @return {Element}
*/
function create_row(id, title, action, post_type, type) {
function create_row (id, title, action, post_type, type) {
var $table = $('#pp-checklists-requirements'),
$tr = $('<tr>'),
$td = null,
Expand All @@ -195,13 +190,19 @@

// ID field
$idField
.attr('name', 'publishpress_checklists_checklists_options[' + type + '_items][]')
.attr(
'name',
'publishpress_checklists_checklists_options[' + type + '_items][]'
)
.val(id)
.appendTo($td);

// Title cell
$titleField
.attr('name', 'publishpress_checklists_checklists_options[' + id + '_title][' + post_type + ']')
.attr(
'name',
'publishpress_checklists_checklists_options[' + id + '_title][' + post_type + ']'
)
.val(title)
.addClass('pp-checklists-custom-item-title')
.focus()
Expand All @@ -211,22 +212,10 @@

// Suggestion
if (typeof $suggestionsObject !== 'undefined') {
$suggestionItem.append(
'<span class="suggestion-title">' +
objectL10n_checklists_global_checklist.suggestion_title +
':</span> ',
);
$suggestionItem.append('<span class="suggestion-title">' + objectL10n_checklists_global_checklist.suggestion_title + ':</span> ');
for (var key in $suggestionsObject) {
if ($suggestionsObject.hasOwnProperty(key)) {
$suggestionItem.append(
'<span>&#x2022; <a href="javascript:void(0);" class="' +
key +
'" data-prompt="' +
$suggestionsObject[key].prompt +
'">' +
$suggestionsObject[key].label +
'</a></span> ',
);
$suggestionItem.append('<span>&#x2022; <a href="javascript:void(0);" class="' + key + '" data-prompt="' + $suggestionsObject[key].prompt + '">' + $suggestionsObject[key].label + '</a></span> ');
}
}
$suggestionItem.appendTo($td);
Expand All @@ -235,48 +224,68 @@
// Action cell
$td = $('<td>').appendTo($tr);
$actionField
.attr('name', 'publishpress_checklists_checklists_options[' + id + '_rule][' + post_type + ']')
.attr(
'name',
'publishpress_checklists_checklists_options[' + id + '_rule][' + post_type + ']'
)
.attr('data-id', id)
.appendTo($td);

$.each(objectL10n_checklists_global_checklist.rules, function (value, label) {
$option = $('<option>').attr('value', value).text(label).appendTo($actionField);
$option = $('<option>')
.attr('value', value)
.text(label)
.appendTo($actionField);
});

// can_ignore cell
$td = $('<td>').appendTo($tr);
$canIgnoreField
.attr('class', 'pp-checklists-can-ignore')
.attr('name', 'publishpress_checklists_checklists_options[' + id + '_can_ignore][' + post_type + '][]')
.attr(
'name',
'publishpress_checklists_checklists_options[' + id + '_can_ignore][' + post_type + '][]'
)
.attr('multiple', 'multiple')
.appendTo($td);

$option = $('<option value=""></option>').appendTo($canIgnoreField);
$.each(objectL10n_checklists_global_checklist.roles, function (value, label) {
$option = $('<option>').attr('value', value).text(label).appendTo($canIgnoreField);
$option = $('<option>')
.attr('value', value)
.text(label)
.appendTo($canIgnoreField);
});

// Options cell
$td = $('<td>').addClass('pp-checklists-task-params').appendTo($tr);
$td = $('<td>')
.addClass('pp-checklists-task-params')
.appendTo($tr);

if (type !== 'openai') {
$optionsField
.attr('id', '' + post_type + '-checklists-' + id + '_editable_by')
.attr(
'id',
'' + post_type + '-checklists-' + id + '_editable_by'
)
.attr(
'name',
'publishpress_checklists_checklists_options[' + id + '_editable_by][' + post_type + '][]',
'publishpress_checklists_checklists_options[' + id + '_editable_by][' + post_type + '][]'
)
.attr('multiple', 'multiple')
.appendTo($td);

$option = $('<option value=""></option>').appendTo($optionsField);
$.each(objectL10n_checklists_global_checklist.roles, function (value, label) {
$option = $('<option>').attr('value', value).text(label).appendTo($optionsField);
$option = $('<option>')
.attr('value', value)
.text(label)
.appendTo($optionsField);
});

var $label = $('<p>')
.addClass('pp-checklists-editable-by-description')
.text(objectL10n_checklists_global_checklist.editable_by);
.addClass('pp-checklists-editable-by-description')
.text(objectL10n_checklists_global_checklist.editable_by);
$optionsField.after($label);
}

Expand All @@ -293,6 +302,7 @@
.attr('data-type', type)
.appendTo($a);


// Re-initialize select 2
$('#pp-checklists-global select').select2();

Expand All @@ -304,19 +314,21 @@
//hide all tabs contents, and show Custom tab
$('.pp-checklists-requirement-row').hide();
$('.ppch-custom-group').show();

var newId = uidGen(15);

create_row(newId, '', '', get_current_post_type(), 'custom');
});

// Hide all requirements except the first one (title)
$('.pp-checklists-requirement-row:not(.ppch-title-group)').hide();


/**
* Requirements tab switch
*/
$(document).on('click', '.pp-checklists-tabs a', function (event) {

event.preventDefault();

var clicked_tab = $(this).attr('data-tab');
Expand All @@ -332,8 +344,10 @@

// Show the current tab contents that also have the matching data-post-type attribute
$('.ppch-' + clicked_tab + '-group[data-post-type="' + get_current_post_type() + '"]').show();

});


/*---------- OpenAI items ----------*/
$('#pp-checklists-openai-promt-button').on('click', function (event) {
//hide all tabs contents, and show Custom tab
Expand All @@ -352,55 +366,45 @@
$('.pp-checklists-remove-custom-item').on('click', callback_remove_row);

/*---------- Form validation ----------*/
$('#pp-checklists-global').submit(function () {
$("#pp-checklists-global").submit(function () {
var submit_form = true,
submit_error = '',
required_rules = objectL10n_checklists_global_checklist.required_rules,
required_rules_notice = objectL10n_checklists_global_checklist.submit_error,
custom_task_error_displayed = false;

//remove previous notice
$('.checklists-save-notice').remove();
$(".checklists-save-notice").remove();

//select all row
$('.pp-checklists-requirement-row').each(function () {
$(".pp-checklists-requirement-row").each(function () {
var requirement_id = $(this).attr('data-id');
var row_requirement_title = $(this).find('td:first-child').text();
var requirement_rule = $(this)
.find('#post-checklists-' + requirement_id + '_rule option:selected')
.val();
var row_requirement_title = $(this).find("td:first-child").text();
var requirement_rule = $(this).find('#post-checklists-' + requirement_id + '_rule option:selected').val();
var min_field = $(this).find('#post-checklists-' + requirement_id + '_min');
var max_field = $(this).find('#post-checklists-' + requirement_id + '_max');

//check if selected rule require validation and option is Base_counter
if (
$.inArray(requirement_rule, required_rules) !== -1 &&
(min_field.length > 0 || max_field.length > 0)
) {
if ($.inArray(requirement_rule, required_rules) !== -1 && (min_field.length > 0 || max_field.length > 0)) {

//void submit and add to error if none of min and max field is set
if (Number(min_field.val()) === 0 && Number(max_field.val()) === 0) {
submit_form = false;
submit_error +=
'<div class="alert alert-danger alert-dismissible"><a href="javascript:void(0);" class="close">×</a>' +
required_rules_notice +
' "<strong>' +
row_requirement_title +
'</strong>"' +
'.</div>';
submit_form = false
submit_error += '<div class="alert alert-danger alert-dismissible"><a href="javascript:void(0);" class="close">×</a>' + required_rules_notice + ' "<strong>' + row_requirement_title + '</strong>"' + '.</div>';
}
}
});

$('.pp-checklists-custom-item-title').each(function () {
if ($(this).val().trim() === '' && !custom_task_error_displayed) {
submit_form = false;
submit_error += `<div class="alert alert-danger alert-dismissible"><a href="javascript:void(0);" class="close">×</a> ${objectL10n_checklists_global_checklist.custom_item_error}</div>`;
submit_error += '<div class="alert alert-danger alert-dismissible"><a href="javascript:void(0);" class="close">×</a> ' + objectL10n_checklists_global_checklist.custom_item_error + '</div>';
custom_task_error_displayed = true;
}
});

if (!submit_form) {
$('#pp-checklists-global #submit').before(`<div class="checklists-save-notice">${submit_error}</div>`);
$("#pp-checklists-global #submit").before('<div class="checklists-save-notice">' + submit_error + '</div>');
}

return submit_form;
Expand All @@ -416,10 +420,12 @@
// Remove notice on any number input changed
$(document).on('change input paste', '.pp-checklists-number', function () {
//remove previous notice
$('.checklists-save-notice').remove();
$(".checklists-save-notice").remove();
});

});
function uidGen(len) {

function uidGen (len) {
var text = ' ',
charset = 'abcdefghijklmnopqrstuvwxyz';

Expand All @@ -429,4 +435,5 @@

return text.trim();
}
})(jQuery, objectL10n_checklists_global_checklist);

})(jQuery, objectL10n_checklists_global_checklist);

0 comments on commit 9c72573

Please sign in to comment.