@@ -3,28 +3,23 @@ import {CSSResultGroup, html, nothing} from 'lit';
33import { customElement , property } from 'lit/decorators.js' ;
44import '../stages/survey_editor_menu' ;
55import '@material/web/checkbox/checkbox.js' ;
6- import './condition_editor' ;
76import '../../pair-components/textarea_template' ;
8- import {
9- ConditionTarget ,
10- surveyQuestionsToConditionTargets ,
11- } from './condition_editor' ;
127
138import { core } from '../../core/core' ;
149import { AuthService } from '../../services/auth.service' ;
1510import { ExperimentEditor } from '../../services/experiment.editor' ;
16-
11+ import { renderConditionEditor } from '../../shared/condition_editor.utils' ;
1712import {
1813 CheckSurveyQuestion ,
1914 Condition ,
15+ getConditionTargetsFromStages ,
2016 MultipleChoiceItem ,
2117 MultipleChoiceSurveyQuestion ,
2218 ScaleSurveyQuestion ,
2319 SurveyPerParticipantStageConfig ,
2420 SurveyStageConfig ,
2521 SurveyQuestion ,
2622 SurveyQuestionKind ,
27- StageKind ,
2823 TextSurveyQuestion ,
2924 createMultipleChoiceItem ,
3025} from '@deliberation-lab/utils' ;
@@ -193,9 +188,11 @@ export class SurveyEditor extends MobxLitElement {
193188 ` ;
194189 }
195190
196- private renderConditionEditor ( question : SurveyQuestion , index : number ) {
191+ private renderQuestionConditionEditor (
192+ question : SurveyQuestion ,
193+ index : number ,
194+ ) {
197195 if ( ! this . stage ) return nothing ;
198- if ( ! this . authService . showAlphaFeatures ) return nothing ;
199196
200197 const onConditionChange = ( condition : Condition | undefined ) => {
201198 this . updateQuestion (
@@ -207,53 +204,20 @@ export class SurveyEditor extends MobxLitElement {
207204 ) ;
208205 } ;
209206
210- // Get all questions before this one in current stage (can only reference previous questions)
211- const currentStageQuestions = this . stage . questions . slice ( 0 , index ) ;
212- const currentTargets = surveyQuestionsToConditionTargets (
213- currentStageQuestions ,
207+ // Get condition targets from all preceding stages and questions before this one
208+ const targets = getConditionTargetsFromStages (
209+ this . experimentEditor . stages ,
214210 this . stage . id ,
215- this . stage . name ,
211+ { includeCurrentStage : true , currentStageQuestionIndex : index } ,
216212 ) ;
217213
218- // Get questions from all previous stages
219- const allTargets : ConditionTarget [ ] = [ ...currentTargets ] ;
220- const currentStageIndex = this . experimentEditor . stages . findIndex (
221- ( s ) => s . id === this . stage ?. id ,
222- ) ;
223-
224- if ( currentStageIndex > 0 ) {
225- // Add questions from previous stages
226- for ( let i = 0 ; i < currentStageIndex ; i ++ ) {
227- const prevStage = this . experimentEditor . stages [ i ] ;
228- if (
229- prevStage . kind === StageKind . SURVEY ||
230- prevStage . kind === StageKind . SURVEY_PER_PARTICIPANT
231- ) {
232- const prevSurveyStage = prevStage as
233- | SurveyStageConfig
234- | SurveyPerParticipantStageConfig ;
235- const prevTargets = surveyQuestionsToConditionTargets (
236- prevSurveyStage . questions ,
237- prevStage . id ,
238- prevStage . name ,
239- ) ;
240- allTargets . push ( ...prevTargets ) ;
241- }
242- }
243- }
244-
245- if ( allTargets . length === 0 ) {
246- return nothing ; // No questions to reference
247- }
248-
249- return html `
250- < condition-editor
251- .condition =${ question . condition }
252- .targets =${ allTargets }
253- .disabled=${ ! this . experimentEditor . canEditStages }
254- .onConditionChange=${ onConditionChange }
255- > </ condition-editor >
256- ` ;
214+ return renderConditionEditor ( {
215+ condition : question . condition ,
216+ targets,
217+ showAlphaFeatures : this . authService . showAlphaFeatures ,
218+ canEdit : this . experimentEditor . canEditStages ,
219+ onConditionChange,
220+ } ) ;
257221 }
258222
259223 private renderQuestionNav ( question : SurveyQuestion , index : number ) {
@@ -329,7 +293,7 @@ export class SurveyEditor extends MobxLitElement {
329293 > Make this question required for participants</ span
330294 >
331295 </ label >
332- ${ this . renderConditionEditor ( question , index ) }
296+ ${ this . renderQuestionConditionEditor ( question , index ) }
333297 ` ;
334298 }
335299
@@ -406,7 +370,7 @@ export class SurveyEditor extends MobxLitElement {
406370 >
407371 Add multiple choice item
408372 </ pr-button >
409- ${ this . renderConditionEditor ( question , index ) }
373+ ${ this . renderQuestionConditionEditor ( question , index ) }
410374 ` ;
411375 }
412376
@@ -588,7 +552,7 @@ export class SurveyEditor extends MobxLitElement {
588552 Optional: Display the scale as a slider instead of radio buttons
589553 </ span >
590554 </ label >
591- ${ this . renderConditionEditor ( question , index ) }
555+ ${ this . renderQuestionConditionEditor ( question , index ) }
592556 ` ;
593557 }
594558
@@ -640,7 +604,7 @@ export class SurveyEditor extends MobxLitElement {
640604 />
641605 </ div >
642606 </ div >
643- ${ this . renderConditionEditor ( question , index ) }
607+ ${ this . renderQuestionConditionEditor ( question , index ) }
644608 ` ;
645609 }
646610}
0 commit comments