Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopwords code #11

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion Build/src/AjaxMetaGenerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Modal from '@typo3/backend/modal';
import Severity from '@typo3/backend/severity';
import { MessageUtility } from '@typo3/backend/utility/message-utility';
import GeneratorButton from './utils/GeneratorButton.js';
import { callAjaxSaveMetaDataAction } from './utils/RemoteCalls.js';
import { callAjaxSaveMetaDataAction, triggerBadwordButton } from './utils/RemoteCalls.js';

$(() => {
$('.textPromptSelect').on('change', function() {
Expand All @@ -12,6 +12,88 @@ $(() => {
});
});

$(() => {
const elements = $('.t3js-alternative-badword-trigger').not('.click-handled');
elements.addClass('click-handled');

elements.on('click', async function(e) {
e.preventDefault();
e.stopPropagation();

let badword = $(this).data('text-badword');
console.log(badword);

if ($(this).data('text-badword-field')) {
badword = $($(this).data('text-badword-field')).val().trim();
}

console.log("new: " + badword);

let imagelabelid = $($(this).data('text-imagelabelid-field')).val();
let badwordid = $(this).data('text-badwordid-field');
let action = $(this).data('text-action-field');
let funktion = $(this).data('text-funktion-field');

const results = await triggerBadwordButton(badword, imagelabelid, badwordid, action, funktion);
});
});

$(() => {
$('.textLabelSelect').on('change', function() {
const selectedValue = $(this).val();
$($(this).data('target')).val(selectedValue);
});
});

$(() => {
$('.badwordtabelbtn').on('click', function(e) {
e.preventDefault();
e.stopPropagation();

const uid = $(this).data('uid-field');

toggletabel(uid, false);
})
});

$(() => {
$('.textLabelSelect').on('change', function(e) {
e.preventDefault();
e.stopPropagation();

const uid = $(this).data('fileid');
toggletabel(uid, true);
})
});

var lastlabels = [];
function toggletabel(uid, element){
if(element &&(lastlabels[uid] == undefined || lastlabels[uid] == 0)){
return
}
let thislabel = document.getElementById('selectedimageLabel-' + uid).value;
let table = document.getElementById('table-' + thislabel + '-' + uid);
if(lastlabels[uid] != undefined && lastlabels[uid] > 0 && lastlabels[uid] != thislabel){
console.log(lastlabels[uid]);
var tmp = document.getElementById('table-' + lastlabels[uid] + '-' + uid);
tmp.style.display = 'none';
}
if(thislabel < 0){
return;
}
if ((table.style.display === 'none')) {
table.style.display = 'table';
lastlabels[uid] = thislabel;
} else {
table.style.display = 'none';
lastlabels[uid] = 0;
}
}

$(() => {
RemoteCalls.initBadwordButton();
});

$(() => {
$('.t3js-alternative-use-current-trigger').on('click', function(e) {
e.preventDefault();
Expand Down Expand Up @@ -140,6 +222,9 @@ $(() => {
$('.globalTextPrompt').on('change', function() {
$('.textPromptSelect').val($(this).val()).trigger('change');
});
$(".globalTextLabel").on("change", function() {
$(".textLabelSelect").val($(this).val()).trigger("change");
});
});

$(() => {
Expand Down
28 changes: 28 additions & 0 deletions Build/src/Listlabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import $ from 'jquery';
import { triggerBadwordButton } from './utils/RemoteCalls.js';

$(() => {
const elements = $('.t3js-alternative-badword-trigger').not('.click-handled');
elements.addClass('click-handled');

elements.on('click', async function(e) {
e.preventDefault();
e.stopPropagation();

let badword = $(this).data('text-badword');
console.log(badword);

if ($(this).data('text-badword-field')) {
badword = $($(this).data('text-badword-field')).val().trim();
}

console.log("new: " + badword);

let imagelabelid = $($(this).data('text-imagelabelid-field')).val();
let badwordid = $(this).data('text-badwordid-field');
let action = $(this).data('text-action-field');
let funktion = $(this).data('text-funktion-field');

const results = await triggerBadwordButton(badword, imagelabelid, badwordid, action, funktion);
});
});
11 changes: 9 additions & 2 deletions Build/src/utils/GeneratorButton.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $ from 'jquery';
import {callAjaxMetaGenerateAction} from './RemoteCalls.js';
import { callAjaxMetaGenerateAction } from './RemoteCalls.js';

class GeneratorButton {
static CSS_CLASSES = {
Expand Down Expand Up @@ -36,6 +36,11 @@ class GeneratorButton {
textPrompt = $(element.data('text-prompt-field')).val();
}

let textLabel = element.data('text-imagelabel');
if (element.data('text-imagelabel-field')) {
textLabel = $(element.data('text-imagelabel-field')).val();
}

element.prop('disabled', true);
element.addClass(GeneratorButton.CSS_CLASSES.GENERATING);

Expand All @@ -46,10 +51,12 @@ class GeneratorButton {
const results = await callAjaxMetaGenerateAction(
fileIdentifier,
targetLanguage,
textPrompt
textPrompt,
textLabel
);

console.log('Prompt generated', results);

target.val(results.alternative);
target.trigger('change');

Expand Down
108 changes: 105 additions & 3 deletions Build/src/utils/RemoteCalls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Modal from '@typo3/backend/modal';
import Severity from '@typo3/backend/severity';

const URLS = {
images: TYPO3.settings.ajaxUrls['aitools_ai_tools_images'],
credits: TYPO3.settings.ajaxUrls['aitools_ai_tools_credits'],
badwords: TYPO3.settings.ajaxUrls['aitools_ai_tools_badwords'],
};

export async function ajaxCall(parameters, url) {
Expand All @@ -21,17 +25,18 @@ export async function ajaxCall(parameters, url) {
} else if (this.status !== 200) {
reject('Error: status ' + this.status);
}
}
};
xhr.send(paramString);
});
}

export async function callAjaxMetaGenerateAction(fileIdentifier, targetLanguage, textPrompt) {
export async function callAjaxMetaGenerateAction(fileIdentifier, targetLanguage, textPrompt, textLabel) {
const params = {
action: 'generateMetaData',
target: fileIdentifier,
"target-language": targetLanguage,
textPrompt: textPrompt
textPrompt: textPrompt,
imageLabel: textLabel
};

top.TYPO3.Notification.info('Generating Metadata', 'Generating Metadata...', 5);
Expand Down Expand Up @@ -84,3 +89,100 @@ export async function callAjaxSaveMetaDataAction(fileIdentifier, targetLanguage,
throw error;
});
}

export async function triggerBadwordButton(badword, imagelabelid, badwordid, action, funktion) {
if ((funktion == "badword" || funktion == "metabadword") && (badword === "" || badword == ",")) {
await showModalConfirmation("You need to enter a badword to proceed.", "Parameters Missing");
return;
}

if (imagelabelid < 0) {
await showModalConfirmation("You need to select a label to proceed.", "Parameters Missing");
return;
}

let parameters = {};
let badwords = [];
if (funktion == "badword" || funktion == "metabadword") {
badwords = badword.split(',');
parameters = { funktion: funktion, badword: badwords[0], imagelabelid: imagelabelid, badwordid: badwordid, action: action };
} else if (funktion == "label") {
parameters = { funktion: funktion, imagelabelid: imagelabelid, action: action };
}

let dup = [];
if (action == "cut") {
await showModalConfirmation("Are you sure you want to delete this record?", "Confirmation");
}

console.log(parameters);
await ajaxCall(parameters, URLS.badwords)
.then(response => {
console.log('Erfolg:', response);
})
.catch(error => {
if (error === "Error: status 409") {
dup.push(badwords[0]);
}
});

if ((funktion == "badword" || funktion == "metabadword") && action === "add" && badwords.length > 1) {
let tmpbadwords = badwords.slice(1);
for (const word of tmpbadwords) {
parameters.badword = word;
await ajaxCall(parameters, URLS.badwords)
.then(response => {
console.log('Erfolg:', response);
})
.catch(error => {
if (error === "Error: status 409") {
dup.push(word);
}
});
}
}

if (dup.length >= 1) {
await showModalConfirmation("The word"
+ (dup.length === 1 ? " " : "s ")
+ dup.join(", ")
+ " could not be "
+ (action === "add" ? "added" : "changed")
+ " because " + (dup.length === 1 ? "it" : "they")
+ " already exists.", "Duplicate");
}

if (dup.length > 0 && dup.length === badwords.length) {
return;
}

setTimeout(() => {
window.location.reload();
}, 100);
};

async function showModalConfirmation(message, type) {
return new Promise((resolve) => {
if (typeof Modal !== 'undefined' && Modal.confirm) {
Modal.confirm(
type,
message,
Severity.warning,
[
{
text: 'Ok',
active: true,
btnClass: 'btn-warning',
name: 'ok',
trigger: function () {
resolve(true);
Modal.dismiss();
}
}
]
);
} else {
resolve(confirm(message));
}
});
}
Loading
Loading