Skip to content

Commit 87a2b21

Browse files
authored
feat: add tooltips for html question (#2480)
1 parent ed57d58 commit 87a2b21

File tree

4 files changed

+74
-42
lines changed

4 files changed

+74
-42
lines changed

libs/shared/src/lib/components/dashboard-filter/filter-builder-modal/filter-builder-modal.component.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AfterViewInit,
33
Component,
44
Inject,
5+
OnChanges,
56
OnDestroy,
67
OnInit,
78
} from '@angular/core';
@@ -166,7 +167,7 @@ const CORE_QUESTION_ALLOWED_PROPERTIES = [
166167
],
167168
})
168169
export class FilterBuilderModalComponent
169-
implements OnInit, AfterViewInit, OnDestroy
170+
implements OnInit, AfterViewInit, OnChanges, OnDestroy
170171
{
171172
/** Survey creator instance */
172173
surveyCreator!: SurveyCreatorModel;
@@ -201,6 +202,14 @@ export class FilterBuilderModalComponent
201202
this.setFormBuilder();
202203
}
203204

205+
ngOnChanges(): void {
206+
if (this.surveyCreator) {
207+
this.surveyCreator.survey.onAfterRenderQuestion.add(
208+
this.formHelpersService.addQuestionTooltips
209+
);
210+
}
211+
}
212+
204213
/**
205214
* Creates the form builder and sets up all the options.
206215
*/
@@ -214,6 +223,13 @@ export class FilterBuilderModalComponent
214223
};
215224
this.surveyCreator = new SurveyCreatorModel(creatorOptions);
216225

226+
this.surveyCreator.onPreviewSurveyCreated.add((_: any, options: any) => {
227+
const survey: SurveyModel = options.survey;
228+
survey.onAfterRenderQuestion.add(
229+
this.formHelpersService.addQuestionTooltips
230+
);
231+
});
232+
217233
new SurveyCustomJSONEditorPlugin(this.surveyCreator);
218234

219235
// this.surveyCreator.text = '';
@@ -247,13 +263,15 @@ export class FilterBuilderModalComponent
247263

248264
// add the rendering of custom properties
249265
this.surveyCreator.survey.onAfterRenderQuestion.add(
250-
renderGlobalProperties(this.referenceDataService, this.http) as any
266+
renderGlobalProperties(this.referenceDataService, this.http)
251267
);
252-
(this.surveyCreator.onTestSurveyCreated as any).add(
253-
(sender: any, opt: any) =>
254-
opt.survey.onAfterRenderQuestion.add(
255-
renderGlobalProperties(this.referenceDataService, this.http)
256-
)
268+
this.surveyCreator.survey.onAfterRenderQuestion.add(
269+
this.formHelpersService.addQuestionTooltips
270+
);
271+
this.surveyCreator.onPreviewSurveyCreated.add((sender: any, opt: any) =>
272+
opt.survey.onAfterRenderQuestion.add(
273+
renderGlobalProperties(this.referenceDataService, this.http)
274+
)
257275
);
258276

259277
this.surveyCreator.onPropertyGridShowModal.add(updateModalChoicesAndValue);

libs/shared/src/lib/components/form-builder/form-builder.component.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class FormBuilderComponent
195195

196196
// add the rendering of custom properties
197197
this.surveyCreator.survey.onAfterRenderQuestion.add(
198-
renderGlobalProperties(this.referenceDataService, this.http) as any
198+
renderGlobalProperties(this.referenceDataService, this.http)
199199
);
200200
this.surveyCreator.survey.onAfterRenderQuestion.add(
201201
this.formHelpersService.addQuestionTooltips
@@ -228,18 +228,16 @@ export class FormBuilderComponent
228228

229229
this.surveyCreator = new SurveyCreatorModel(creatorOptions);
230230

231-
(this.surveyCreator.onTestSurveyCreated as any).add(
232-
(_: any, options: any) => {
233-
const survey: SurveyModel = options.survey;
234-
survey.applyTheme({
235-
isPanelless: true,
236-
});
237-
survey.onAfterRenderQuestion.add(
238-
this.formHelpersService.addQuestionTooltips
239-
);
240-
this.formHelpersService.addUserVariables(survey);
241-
}
242-
);
231+
this.surveyCreator.onPreviewSurveyCreated.add((_: any, options: any) => {
232+
const survey: SurveyModel = options.survey;
233+
survey.applyTheme({
234+
isPanelless: true,
235+
});
236+
survey.onAfterRenderQuestion.add(
237+
this.formHelpersService.addQuestionTooltips
238+
);
239+
this.formHelpersService.addUserVariables(survey);
240+
});
243241
this.surveyCreator.haveCommercialLicense = true;
244242
this.surveyCreator.text = structure;
245243
this.surveyCreator.saveSurveyFunc = this.saveMySurvey;
@@ -327,17 +325,16 @@ export class FormBuilderComponent
327325

328326
// add the rendering of custom properties
329327
this.surveyCreator.survey.onAfterRenderQuestion.add(
330-
renderGlobalProperties(this.referenceDataService, this.http) as any
328+
renderGlobalProperties(this.referenceDataService, this.http)
331329
);
332330
this.surveyCreator.survey.onAfterRenderQuestion.add(
333-
this.formHelpersService.addQuestionTooltips as any
331+
this.formHelpersService.addQuestionTooltips
334332
);
335333

336-
(this.surveyCreator.onTestSurveyCreated as any).add(
337-
(sender: any, options: any) =>
338-
options.survey.onAfterRenderQuestion.add(
339-
renderGlobalProperties(this.referenceDataService, this.http)
340-
)
334+
this.surveyCreator.onPreviewSurveyCreated.add((sender: any, options: any) =>
335+
options.survey.onAfterRenderQuestion.add(
336+
renderGlobalProperties(this.referenceDataService, this.http)
337+
)
341338
);
342339

343340
this.surveyCreator.onPropertyGridShowModal.add(updateModalChoicesAndValue);
@@ -417,14 +414,13 @@ export class FormBuilderComponent
417414
* @param coreFields list of core fields
418415
*/
419416
private addCustomClassToCoreFields(coreFields: string[]): void {
420-
this.surveyCreator.survey.onAfterRenderQuestion.add(((
421-
survey: SurveyModel,
422-
options: any
423-
) => {
424-
if (coreFields.includes(options.question.valueName)) {
425-
options.htmlElement.children[0].className += ` ${CORE_FIELD_CLASS}`;
417+
this.surveyCreator.survey.onAfterRenderQuestion.add(
418+
(survey: SurveyModel, options: any) => {
419+
if (coreFields.includes(options.question.valueName)) {
420+
options.htmlElement.children[0].className += ` ${CORE_FIELD_CLASS}`;
421+
}
426422
}
427-
}) as any);
423+
);
428424
}
429425

430426
/**

libs/shared/src/lib/services/form-helper/form-helper.service.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,42 @@ export class FormHelpersService {
368368
*
369369
* @param survey current survey
370370
* @param options current survey question options
371+
* @param options.question current question
372+
* @param options.htmlElement html element associated to question
371373
*/
372-
public addQuestionTooltips(survey: any, options: any): void {
374+
public addQuestionTooltips(
375+
survey: SurveyModel,
376+
options: { question: Question; htmlElement: HTMLElement }
377+
): void {
373378
//Return if there is no description to show in popup
374379
if (!options.question.tooltip) {
375380
return;
376381
}
377-
const titleElement = (options.htmlElement as HTMLElement).querySelector(
382+
const titleElement = options.htmlElement.querySelector(
378383
'.sd-question__title'
379384
);
385+
const createTooltip = (htmlElement: Element) => {
386+
const tooltip = document.createElement('span');
387+
tooltip.title = options.question.tooltip || '';
388+
tooltip.innerHTML = '?';
389+
tooltip.classList.add('survey-title__tooltip');
390+
htmlElement.appendChild(tooltip);
391+
};
380392
if (titleElement) {
381393
const selector = survey.isDesignMode
382394
? '.svc-string-editor'
383395
: '.sv-string-viewer';
384-
titleElement.querySelectorAll(selector).forEach((el: any) => {
385-
const tooltip = document.createElement('span');
386-
tooltip.title = options.question.tooltip;
387-
tooltip.innerHTML = '?';
388-
tooltip.classList.add('survey-title__tooltip');
389-
el.appendChild(tooltip);
396+
titleElement.querySelectorAll(selector).forEach((el: Element) => {
397+
createTooltip(el);
390398
});
399+
} else if (options.question.getType() === 'html') {
400+
const wrapper = document.createElement('div');
401+
wrapper.classList.add('flex', 'items-center');
402+
const htmlQuestion = options.htmlElement.querySelector('.sd-html');
403+
if (!htmlQuestion) return;
404+
htmlQuestion.parentNode?.insertBefore(wrapper, htmlQuestion);
405+
wrapper.appendChild(htmlQuestion);
406+
createTooltip(wrapper);
391407
}
392408
}
393409

libs/shared/src/lib/style/survey.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
margin-left: 0.25rem;
7171
font-size: 0.75rem !important;
7272
font-weight: bold;
73+
height: 17px;
74+
line-height: 17px;
7375
}
7476

7577
::ng-deep .sd-matrixdropdown.sd-table,

0 commit comments

Comments
 (0)