Skip to content

Commit

Permalink
Fixed issue #18811: Button that triggers HTML editor in pop-up window…
Browse files Browse the repository at this point in the history
… doesn't work

Dev: Review questionEditor script
Dev: placeholder without url encoded character
Dev: use $tr, not $row
Fix: Add single subquestion or answer
Dev: Fix button decoration
  • Loading branch information
Shnoulle committed May 15, 2023
1 parent 43c9bfa commit 6c9cc79
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 77 deletions.
8 changes: 4 additions & 4 deletions application/controllers/QuestionAdministrationController.php
Expand Up @@ -683,7 +683,7 @@ public function actionGetSubquestionRowForAllLanguages($surveyid, $gid, $codes,
}
$html = [];
$first = true;
$qid = 'new' . rand(0, 99999);
$qid = App()->getRequest()->getParam('subqid') ?? 'new' . rand(0, 99999);
foreach ($oSurvey->allLanguages as $language) {
$html[$language] = $this->getSubquestionRow(
$oSurvey->sid,
Expand All @@ -710,7 +710,7 @@ public function actionGetSubquestionRowForAllLanguages($surveyid, $gid, $codes,
*/
public function actionGetSubquestionRowQuickAdd($surveyid, $gid)
{
$qid = '{{quid_placeholder}}';
$qid = '-QUIDPLACEHOLDER-';
$request = Yii::app()->request;
$codes = $request->getPost('codes');
$language = $request->getPost('language');
Expand All @@ -729,7 +729,7 @@ public function actionGetSubquestionRowQuickAdd($surveyid, $gid)
*/
public function actionGetAnswerOptionRowQuickAdd($surveyid, $gid)
{
$qid = '{{quid_placeholder}}';
$qid = '-QUIDPLACEHOLDER-';
$request = Yii::app()->request;
$codes = $request->getPost('codes');
$language = $request->getPost('language');
Expand All @@ -755,7 +755,7 @@ public function actionGetAnswerOptionRowForAllLanguages($surveyid, $gid, $codes,
}
$html = [];
$first = true;
$qid = 'new' . rand(0, 99999);
$qid = App()->getRequest()->getParam('subqid') ?? 'new' . rand(0, 99999);
foreach ($oSurvey->allLanguages as $language) {
$html[$language] = $this->getAnswerOptionRow(
$oSurvey->sid,
Expand Down
17 changes: 10 additions & 7 deletions application/helpers/admin/htmleditor_helper.php
Expand Up @@ -206,7 +206,7 @@ function getPopupEditor($fieldtype, $fieldname, $fieldtext, $surveyID = null, $g
) {
$class = "editorLink";
} else {
$class = "editorLink input-group-text";
$class = "editorLink";
}
/* @var string[] parameters of the editor url */
$editorUrlParams = array(
Expand Down Expand Up @@ -235,14 +235,14 @@ function getPopupEditor($fieldtype, $fieldname, $fieldtext, $surveyID = null, $g
);
/* @var string content of the action link */
$content = CHtml::tag('i', array(
'class' => "fa fa-pencil btneditanswerena",
'class' => "ri-pencil-fill btneditanswerena",
'id' => $fieldname . "_popupctrlena",
'data-bs-toggle' => "tooltip",
'data-placement' => "tooltip",
'title' => gT("Start HTML editor in a popup window")
), '')
. CHtml::tag('span', array(
'class' => "sr-only",
), gT("Start HTML editor in a popup window"))
. CHtml::tag('i', array(
'class' => "fa fa-pencil btneditanswerdis",
'class' => "ri-pencil-fill btneditanswerdis",
'id' => $fieldname . "_popupctrldis",
'style' => "display:none",
), '');
Expand All @@ -252,7 +252,10 @@ function getPopupEditor($fieldtype, $fieldname, $fieldtext, $surveyID = null, $g
"javascript:start_popup_editor('{$fieldname}','" . $editorUrl . "');",
array(
'id' => $fieldname . "_ctrl",
'class' => "{$class} btn btn-default btn-xs",
'class' => "{$class} btn btn-outline-secondary",
'title' => gT("Start HTML editor in a popup window"),
'data-bs-toggle' => "tooltip",
'data-bs-placement' => "bottom",
)
);
}
Expand Down
Expand Up @@ -107,9 +107,7 @@
value="{{ question }}"
onkeypress=" if(event.keyCode==13) { if (event && event.preventDefault) event.preventDefault(); document.getElementById('save-button').click(); return false;}"
/>
<span class="input-group-addon">
{{ getEditor("editanswer","answer_"~language~"_"~qid~"_"~scale_id, "["~gT("Subquestion:", "js")~"]("~language~")",surveyid,gid,qid,'editanswer') }}
</span>
{{ getEditor("editanswer","answer_"~language~"_"~qid~"_"~scale_id, "["~gT("Subquestion:", "js")~"]("~language~")",surveyid,gid,qid,'editanswer') }}
</div>
</td>

Expand Down
4 changes: 1 addition & 3 deletions application/views/admin/survey/prepareEditorScript_view.php
Expand Up @@ -53,13 +53,12 @@ function find_popup_editor(fieldname)
return null;
}
function start_popup_editor(fieldnamee, editorurl)
function start_popup_editor(fieldname, editorurl)
{
controlidena = fieldname + '_popupctrlena';
controliddis = fieldname + '_popupctrldis';
numwindows = editorwindowsHash.length;
activepopup = find_popup_editor(fieldname);
if (activepopup == null)
{
var targetField = document.getElementById(fieldname);
Expand All @@ -72,7 +71,6 @@ function start_popup_editor(fieldnamee, editorurl)
var inputLangDirection = targetField.getAttribute('data-contents-dir');
editorurl = editorurl + '/contdir/' + (inputLangDirection ? inputLangDirection : '');
}
popup = window.open(editorurl,'', 'location=no, status=yes, scrollbars=auto, menubar=no, resizable=yes, width=690, height=500');
editorwindowsHash[fieldname] = popup;
Expand Down
2 changes: 1 addition & 1 deletion application/views/admin/token/invite.php
Expand Up @@ -129,7 +129,7 @@

<label class='form-label ' for='message_<?php echo $language; ?>'><?php eT("Message:"); ?></label>
<div class=''>
<div class="htmleditor">
<div class="input-group htmleditor">
<?php echo CHtml::textArea("message_{$language}",$textarea,array('cols'=>80,'rows'=>20, 'class' => 'form-control')); ?>
<?php echo getEditor("email-invitation", "message_$language", "[" . gT("Invitation email:", "js") . "](" . $language . ")", $surveyid, '', '', "tokens"); ?>
</div>
Expand Down
2 changes: 1 addition & 1 deletion application/views/admin/token/remind.php
Expand Up @@ -162,7 +162,7 @@
<div class='mb-3'>
<label class='form-label '
for='message_<?php echo $language; ?>'><?php eT("Message:"); ?></label>
<div class="htmleditor ">
<div class="input-group htmleditor ">
<?php echo CHtml::textArea("message_{$language}", $textarea, array('cols' => 80, 'rows' => 20, 'class' => 'form-control')); ?>
<?php echo getEditor("email-reminder", "message_$language", "[" . gT("Reminder Email:", "js") . "](" . $language . ")", $surveyid, '', '', "tokens"); ?>
</div>
Expand Down
20 changes: 9 additions & 11 deletions application/views/questionAdministration/answerOptionRow.twig
Expand Up @@ -115,17 +115,15 @@
data-contents-dir="{{ getLanguageRTL(language) ? 'rtl' : 'ltr' }}"
value="{{ answerOptionl10n.answer|escape('html_attr') }}"
/>
<span class="input-group-addon">
{{ getEditor(
"editanswer",
"answeroptions["~answerOption.aid~"]["~scale_id~"][answeroptionl10n]["~language~"]",
"["~gT("Answer option:", "js")~"]("~language~")",
question.sid,
question.gid,
question.qid,
'editanswer'
) }}
</span>
{{ getEditor(
"editanswer",
"answeroptions["~answerOption.aid~"]["~scale_id~"][answeroptionl10n]["~language~"]",
"["~gT("Answer option:", "js")~"]("~language~")",
question.sid,
question.gid,
question.qid,
'editanswer'
) }}
</div>
</td>

Expand Down
20 changes: 9 additions & 11 deletions application/views/questionAdministration/subquestionRow.twig
Expand Up @@ -115,17 +115,15 @@
data-contents-dir="{{ getLanguageRTL(language) ? 'rtl' : 'ltr' }}"
onkeypress=" if(event.keyCode==13) { if (event && event.preventDefault) event.preventDefault(); document.getElementById('save-button').click(); return false;}"
/>
<span class="input-group-addon">
{{ getEditor(
"editanswer",
"subquestions["~subquestion.qid~"]["~scale_id~"][subquestionl10n]["~language~"]",
"["~gT("Subquestion:", "js")~"]("~language~")",
subquestion.sid,
gid,
(subquestion.qid matches '/^\\d+$/') ? subquestion.qid : "",
'editanswer'
) }}
</span>
{{ getEditor(
"editanswer",
"subquestions["~subquestion.qid~"]["~scale_id~"][subquestionl10n]["~language~"]",
"["~gT("Subquestion:", "js")~"]("~language~")",
subquestion.sid,
gid,
(subquestion.qid matches '/^\\d+$/') ? subquestion.qid : "",
'editanswer'
) }}
</div>
</td>

Expand Down
55 changes: 19 additions & 36 deletions assets/scripts/admin/questionEditor.js
Expand Up @@ -141,23 +141,6 @@ $(document).on('ready pjax:scriptcomplete', function () {
updateIfEmpty($(this).find('.answer'), 'name', `answer_${language}_${uniqueRowId}_${scaleId}`);
updateIfEmpty($(this).find('.assessment'), 'id', `assessment_${uniqueRowId}_${scaleId}`);
updateIfEmpty($(this).find('.assessment'), 'name', `assessment_${uniqueRowId}_${scaleId}`);
// Newly inserted row editor button
$(this).find('.editorLink').each(function() {
var inputName = $(this).closest('.input-group').find('input[type=text]').first().attr('name');
if (inputName) {
$(this).attr(
'href',
`javascript:start_popup_editor(
'${inputName}','[Answer:](${language})','${sID}','${gID}','${qID}','editanswer','editanswer'
)`
);
$(this).attr('id', `${inputName}_ctrl`);
$(this).find('.btneditanswerena').attr('id', `${inputName}_popupctrlena`);
$(this).find('.btneditanswerena').attr('name', `${inputName}_popupctrlena`);
$(this).find('.btneditanswerdis').attr('id', `${inputName}_popupctrldis`);
$(this).find('.btneditanswerdis').attr('name', `${inputName}_popupctrldis`);
}
});
});
});
}
Expand Down Expand Up @@ -226,6 +209,7 @@ $(document).on('ready pjax:scriptcomplete', function () {
position: position,
type: 'subquestion',
languages: JSON.stringify($dataInput.data('languages').join(';')),
subqid: '-QUIDPLACEHOLDER-'
};
// We get the HTML of the new row to insert
return $.ajax({
Expand Down Expand Up @@ -893,7 +877,6 @@ $(document).on('ready pjax:scriptcomplete', function () {
$tr = $row.eq(4);
} else if (source === 'answeroptions') {
$tr = $row.eq(2);

// Make sure codes are limited to 5 characters
label.code = label.code.substr(0, 5);
} else {
Expand All @@ -902,7 +885,6 @@ $(document).on('ready pjax:scriptcomplete', function () {
if ($tr.length === 0) {
throw 'Found no $tr in transferLabels';
}

// Only define random ids the FIRST language we loop for.
// Different translations still use the same question code in the input name.
if (langIds[i] === undefined) {
Expand All @@ -918,31 +900,34 @@ $(document).on('ready pjax:scriptcomplete', function () {
generatedIds.push(randId);
langIds[i] = randId;
}

$tr.attr('data-common-id', $tr.attr('data-common-id').replace(/new[0-9]{3,6}/, langIds[i]));
$tr.attr('id', $tr.attr('id').replace(/new[0-9]{3,6}/, langIds[i]));

$row.find('input').each((j /*: number */, inputField) => {
$(inputField).attr('name', $(inputField).attr('name').replace(/new[0-9]{3,6}/, langIds[i]));
$(inputField).attr('id', $(inputField).attr('id').replace(/new[0-9]{3,6}/, langIds[i]));
$tr.attr('data-common-id', $tr.attr('data-common-id').replace(/-QUIDPLACEHOLDER-/, langIds[i]));
$tr.attr('id', $tr.attr('id').replace(/-QUIDPLACEHOLDER-/, langIds[i]));
$tr.find('[name]').each((j /*: number */, nameElement) => {
$(nameElement).attr('name', $(nameElement).attr('name').replace(/-QUIDPLACEHOLDER-/, langIds[i]));
});
$tr.find('[id]').each((j /*: number */, idElement) => {
$(idElement).attr('id', $(idElement).attr('id').replace(/-QUIDPLACEHOLDER-/, langIds[i]));
});
$tr.find('[href]').each((j /*: number */, hrefElement) => {
$(hrefElement).attr('href', $(hrefElement).attr('href').replace(/-QUIDPLACEHOLDER-/g, langIds[i]));
});

if ($row.find('td.code-title').find('input[type=text]').length > 0) {
$row.find('td.code-title').find('input[type=text]').val(label.code);
if ($tr.find('td.code-title').find('input[type=text]').length > 0) {
$tr.find('td.code-title').find('input[type=text]').val(label.code);
} else if ($row.find('td.code-title').length > 0) {
$row.find('td.code-title').text(label.code);
$tr.find('td.code-title').text(label.code);
} else {
throw 'Found nowhere to put label.code';
}

if ($row.find('td.relevance-equation').find('input[type=text]').length > 0) {
$row.find('td.relevance-equation').find('input[type=text]').val(1);
if ($tr.find('td.relevance-equation').find('input[type=text]').length > 0) {
$tr.find('td.relevance-equation').find('input[type=text]').val(1);
} else {
// ??
}

$row.find('td.subquestion-text, td.answeroption-text').find('input[type=text]').val(label.title);
$table.find('tbody').append($row);
$tr.find('td.subquestion-text, td.answeroption-text').find('input[type=text]').val(label.title);
$table.find('tbody').append($tr);

if (source === 'subquestions') {
$table.find('.btnaddsubquestion').off('click.subquestions').on('click.subquestions', addSubquestionInput);
Expand Down Expand Up @@ -1128,8 +1113,7 @@ $(document).on('ready pjax:scriptcomplete', function () {
answers[item.lang].forEach((row /*: {quid: string, text: string, code: string} */) => {
try {
const { html } = item;
const htmlQuid = html.replace(/{{quid_placeholder}}/g, row.quid);

const htmlQuid = html.replace(/-QUIDPLACEHOLDER-/g, row.quid);
// Create HTMLElement from HTML string.
const wrapper = document.createElement('tbody');
wrapper.innerHTML = htmlQuid;
Expand Down Expand Up @@ -1159,7 +1143,6 @@ $(document).on('ready pjax:scriptcomplete', function () {
} else {
// Do nothing.
}

$(item.langtable).find('tbody').append(tableRow);
} catch (e) {
alert('Internal error in quickAddLabels:' + e);
Expand Down

0 comments on commit 6c9cc79

Please sign in to comment.