Skip to content
Draft
14 changes: 14 additions & 0 deletions forum/qa-plugin/outdated-question-info/frontend/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.qa-outdated-question-container{
width: 100%;

text-align: center;
padding: 1%;

border: 6px solid #f74040;
margin-bottom: 2.5%;
font-size: 1.5rem;
}

.hidden{
display: none;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const placeOfOutdatedQuestionInfo = document.querySelector('.qa-a-form');
if(placeOfOutdatedQuestionInfo){
const publishDateSpan = document.querySelector('.published > .value-title');
const now = new Date();
const publishDate = new Date(publishDateSpan.title);
const publishYearOlderThanNow = publishDate.getFullYear() < now.getFullYear();
const publishMonthNewerThanNow = publishDate.getMonth() - 1 >= now.getMonth();

if (publishYearOlderThanNow && publishMonthNewerThanNow) {
let shouldBeDisplayed = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Można to przypisanie zrobić przy pomocy ternarki:

const shouldBeDisplayed = placeOfOutdatedQuestionInfo.classList.contains('hidden') ? 'hidden' : '';

Poza tym, nazwa zmiennej sugeruje, że to boolean, a nie string z nazwą klasy, więc to bym poprawił.

if(placeOfOutdatedQuestionInfo.style.display === 'none'){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeśli nie ma potrzeby obliczania stylu dynamicznie, to lepiej jest manipulować klasą niż ustawiać styl inline.

shouldBeDisplayed = "hidden";
}
placeOfOutdatedQuestionInfo.insertAdjacentHTML('beforebegin',
`<p class = "qa-outdated-question-container ${shouldBeDisplayed}">
To pytanie zostało zadane już dawno temu i może być nieaktualne.<br/>
Upewnij się, że Twoja odpowiedź nadal będzie pomocna.
</p>`);
}
const QuestionElemExist = document.querySelector('.qa-outdated-question-container');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dużą literą powinno nazywać się klasy lub funkcje-konstruktory, a DOM element to jest zwykły obiekt, więc lepiej jest użyć camelCase.


if(QuestionElemExist){
const cancelAnswer = document.querySelectorAll('input[name=docancel]')[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Z tego co widzę w DOM inspektorze na forum, to inputy [name=docancel] służą za czerwony przycisk anulujący pisanie komentarza (lub odpowiedzi, jeśli jest już jakaś udzielona). Jeśli więc jest potrzeba złapania przycisku anulującego aktualnie pisany post, to raczej na pewno nie można tego tak zahardkodować.

Można wyciągnąć wszystkie te inputy (przyciski) i w pętli podpiąć im event listener, albo podpiąć się pod aktualnie aktywną instancję CKEditora i relatywnie od niej znaleźć przycisk anulujący; jakbyś miał z tym problem, to daj znać, pomogę (w trakcie developmentu dla ułatwienia można sobie wrzucić console.log(CKEDITOR.currentInstance) w paro sekundowy setTimeout, żeby móc zfocusować edytor na czas sprawdzania instancji - inaczej to property zwróci null). Ewentualnie zmatchować najbliższego przodka edytora i przycisku anulującego na podstawie klucza z obiektu CKEDITOR.instances.

const outdatedInfoContainerClassList = QuestionElemExist.classList;

cancelAnswer.addEventListener('click', ()=>{
outdatedInfoContainerClassList.toggle('hidden');
}, false)

document.querySelector('#q_doanswer').addEventListener('click', ()=>{
outdatedInfoContainerClassList.toggle('hidden');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nie .toggle('hidden') tylko .remove('hidden') - eksplicytnie usuwamy klasę (pokazując tym samym komunikat) na kliknięcie w przycisk dodający odpowiedź. Tak jak pokazałem w komentarzu.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nie jestem pewny czy to mogłoby zadziałać. Formularz odpowiedzi możemy zamknąć również przyciskiem otwierania odpowiedzi, a nie tylko czerwonym anuluj. Toggle jest tu aby nie było jasno narzucone, którym przyciskiem mamy zamykać formularz

Copy link
Member

@ScriptyChris ScriptyChris Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faktycznie, przycisk "Odpowiedz" toggluje formularz z edytorem - nie zauważyłem. Więc .toggle('hidden') może zostać.

})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class qa_html_theme_layer extends qa_html_theme_base
{
public function head_script(){
parent::head_script();
$this->output('
<script src = "'. QA_HTML_THEME_LAYER_URLTOROOT .'frontend/show-outdated-question-info.js?v=" defer></script>
<link rel = "stylesheet" href = "'. QA_HTML_THEME_LAYER_URLTOROOT .'frontend/css/styles.css?v=" />
');
}
}
9 changes: 9 additions & 0 deletions forum/qa-plugin/outdated-question-info/qa-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

//Don't let this page to be available directly from browser
if (!defined('QA_VERSION')) {
header('Location: ../../');
exit;
}

qa_register_plugin_layer('qa-outdated-question-info-layer.php', 'Outdated Question Info');