import * as ko from "knockout" ; import * as Survey from "survey-knockout" ; import { SurveyPropertyModalEditor } from "./propertyModalEditor" ; import { SurveyPropertyEditorBase , ISurveyObjectEditorOptions , } from "./propertyEditorBase" ; import { editorLocalization } from "../editorLocalization" ; import { SurveyPropertyEditorFactory } from "./propertyEditorFactory" ; import { EditableObject } from "./editableObject" ; import { SurveyHelper } from "../surveyHelper" ; export class SurveyPropertyDefaultValueEditor extends SurveyPropertyModalEditor { private static skipPropertyNames = [ "width" , "minWidth" , "maxWidth" ] ; public static defaultQuestionName = "question" ; public static createJsonFromQuestion ( obj , readOnly ) { var qjson = new Survey . JsonObject ( ) . toJsonObject ( obj ) ; qjson . name = SurveyPropertyDefaultValueEditor . defaultQuestionName ; qjson . title = editorLocalization . getString ( "pe.defaultValue" ) ; qjson . type = obj . getType ( ) ; if ( qjson . type == "expression" ) { qjson . type = "text" ; } if ( ! ! qjson . cellType && qjson . type == "matrixdropdowncolumn" ) { qjson . type = qjson . cellType ; delete qjson [ "cellType" ] ; } SurveyPropertyDefaultValueEditor . filterJsonProperties ( qjson ) ; qjson . titleLocation = "hidden" ; qjson . readOnly = readOnly ; return qjson ; } private static filterJsonProperties ( qjson ) { var properties = SurveyPropertyDefaultValueEditor . skipPropertyNames ; for ( let i = 0 ; i < properties . length ; i ++ ) { const name = properties [ i ] ; if ( ! ! qjson [ name ] ) { delete qjson [ name ] ; } } } public static createSurveyFromJsonQuestion ( questionJson , options , surveyName ) { var json = { questions : [ ] , showNavigationButtons : false , showQuestionNumbers : "off" , textUpdateMode : "onTyping" , } ; SurveyHelper . updateQuestionJson ( questionJson ) ; json . questions . push ( questionJson ) ; var survey = ! ! options ? options . createSurvey ( json , surveyName ) : new Survey . Survey ( json ) ; SurveyPropertyDefaultValueEditor . updateSurveyStyle ( survey ) ; return survey ; } public static updateSurveyStyle ( survey ) { survey . css . body += " svd-property-editor-survey" ; survey . css . page . root += " svd-property-editor-survey-page" ; if ( ! ! survey . css . question ) { survey . css . question . mainRoot += " svd-survey-nopadding" ; } } public survey : Survey . Survey ; koSurvey : any ; protected currentObject : any ; constructor ( property ) { super ( property ) ; this . koSurvey = ko . observable ( ! ! this . options && this . options . createSurvey ( { } , "defaultValueEditor" ) ) ; } public resetText ( ) { return editorLocalization . getString ( "pe.reset" ) ; } public refreshText ( ) { return editorLocalization . getString ( "pe.refresh" ) ; } public resetValue ( model ) { model . koSurvey ( ) . clearValue ( SurveyPropertyDefaultValueEditor . defaultQuestionName ) ; } public refreshSurvey ( model ) { model . createSurvey ( ) ; } public getValueText ( value ) { if ( ! value ) return editorLocalization . getString ( "pe.empty" ) ; return JSON . stringify ( value ) ; } public beforeShowCore ( ) { super . beforeShowCore ( ) ; this . createSurvey ( ) ; } public get editorType ( ) : string { return "default-value" ; } protected createSurvey ( ) { var json = this . buildQuestionJson ( ) ; if ( ! ! json ) { this . survey = SurveyPropertyDefaultValueEditor . createSurveyFromJsonQuestion ( json , this . options , "defaultValueEditor" ) ; this . survey . setValue ( SurveyPropertyDefaultValueEditor . defaultQuestionName , this . getSurveyInitialValue ( ) ) ; var self = this ; this . survey . onValueChanged . add ( function ( sender , options ) { self . koValue ( self . getSurveyResult ( ) ) ; } ) ; } else { this . survey = null ; } this . koSurvey ( this . survey ) ; } protected buildQuestionJson ( ) { this . currentObject = this . getObject ( ) ; if ( ! this . currentObject ) return null ; return SurveyPropertyDefaultValueEditor . createJsonFromQuestion ( this . currentObject , this . readOnly ( ) ) ; } protected getObject ( ) { return this . object ; } protected getSurveyInitialValue ( ) { return this . koValue ( ) ; } protected getSurveyResult ( ) { return this . survey . getValue ( SurveyPropertyDefaultValueEditor . defaultQuestionName ) ; } } export class SurveyPropertyTriggerValueEditor extends SurveyPropertyDefaultValueEditor { constructor ( property ) { super ( property ) ; } public get editorTypeTemplate ( ) : string { return "default-value" ; } public updateDynamicProperties ( ) { super . updateDynamicProperties ( ) ; if ( this . currentObject != this . getObject ( ) ) { this . createSurvey ( ) ; } } protected getObject ( ) { var survey = EditableObject . getSurvey ( this . object ) ; if ( ! survey || ! this . property ) return null ; var propName = this . getDependOnPropName ( ) ; if ( ! propName ) return null ; var questionName = this . object [ propName ] ; return ! ! questionName ? survey . getQuestionByName ( questionName ) : null ; } private getDependOnPropName ( ) { var properties = Survey . Serializer . getPropertiesByObj ( this . object ) ; for ( var i = 0 ; i < properties . length ; i ++ ) { var dps = properties [ i ] . getDependedProperties ( ) ; if ( ! ! dps && dps . indexOf ( this . property . name ) > - 1 ) return properties [ i ] . name ; } return "" ; } } export class SurveyPropertyDefaultRowValueEditorBase extends SurveyPropertyDefaultValueEditor { constructor ( property ) { super ( property ) ; } public get editorTypeTemplate ( ) : string { return "default-value" ; } public get editorType ( ) : string { return "triggervalue" ; } protected getSurveyInitialValue ( ) { var res = this . koValue ( ) ; if ( ! res ) return res ; if ( ! Array . isArray ( res ) ) { res = [ res ] ; } return res ; } protected getSurveyResult ( ) { var res = super . getSurveyResult ( ) ; if ( res && Array . isArray ( res ) ) { if ( res . length == 0 ) return null ; return res [ 0 ] ; } return res ; } } export class SurveyPropertyDefaultRowValueEditor extends SurveyPropertyDefaultRowValueEditorBase { constructor ( property ) { super ( property ) ; } protected buildQuestionJson ( ) { var qjson = super . buildQuestionJson ( ) ; qjson . rowCount = 1 ; qjson . minRowCount = 1 ; qjson . maxRowCount = 1 ; qjson . columnsLocation = "vertical" ; return qjson ; } public get editorType ( ) : string { return "rowvalue" ; } } export class SurveyPropertyDefaultPanelValueEditor extends SurveyPropertyDefaultRowValueEditorBase { constructor ( property ) { super ( property ) ; } protected buildQuestionJson ( ) { var qjson = super . buildQuestionJson ( ) ; qjson . panelCount = 1 ; qjson . minPanelCount = 1 ; qjson . maxPanelCount = 1 ; return qjson ; } public get editorType ( ) : string { return "panelvalue" ; } } export class SurveyPropertySetEditor extends SurveyPropertyDefaultValueEditor { constructor ( property ) { super ( property ) ; } public get editorType ( ) : string { return "set" ; } public get editorTypeTemplate ( ) : string { return "default-value" ; } protected getSurveyInitialValue ( ) { var res = this . koValue ( ) ; if ( ! res ) return res ; if ( ! Array . isArray ( res ) ) { res = [ res ] ; } return res ; } protected buildQuestionJson ( ) { var question = new Survey . QuestionCheckbox ( "q1" ) ; var hasTagbox = ! ! Survey . Serializer . findClass ( "tagbox" ) ; question . hasSelectAll = ! hasTagbox ; if ( ! ! this . property ) { question . choices = this . getPropertyChoices ( ) ; } var json = SurveyPropertyDefaultValueEditor . createJsonFromQuestion ( question , this . readOnly ( ) ) ; if ( hasTagbox ) { json . type = "tagbox" ; } return json ; } private setChoices ( choices ) { var survey = this . koSurvey ( ) ; if ( ! survey || survey . getAllQuestions ( ) . length > 1 ) return ; survey . getAllQuestions ( ) [ 0 ] . choices = choices ; } private getPropertyChoices ( ) { if ( ! this . property ) return [ ] ; var self = this ; return this . property . getChoices ( this . object , function ( choices ) { self . setChoices ( choices ) ; } ) ; } } SurveyPropertyEditorFactory . registerEditor ( "value" , function ( property ) : SurveyPropertyEditorBase { return new SurveyPropertyDefaultValueEditor ( property ) ; } ) ; SurveyPropertyEditorFactory . registerEditor ( "triggervalue" , function ( property ) : SurveyPropertyEditorBase { return new SurveyPropertyTriggerValueEditor ( property ) ; } ) ; SurveyPropertyEditorFactory . registerEditor ( "rowvalue" , function ( property ) : SurveyPropertyEditorBase { return new SurveyPropertyDefaultRowValueEditor ( property ) ; } ) ; SurveyPropertyEditorFactory . registerEditor ( "panelvalue" , function ( property ) : SurveyPropertyEditorBase { return new SurveyPropertyDefaultPanelValueEditor ( property ) ; } ) ; SurveyPropertyEditorFactory . registerEditor ( "set" , function ( property ) : SurveyPropertyEditorBase { return new SurveyPropertySetEditor ( property ) ; } ) ;