1- import "./surveyjs_importer.js" ;
2-
3- var widget = {
4- activatedBy : "property" ,
5- name : "select2" ,
6- htmlTemplate : "<select style='width: 100%;'></select>" ,
7- isFit : function ( question ) {
8- if ( widget . activatedBy == "property" ) return question [ "renderAs" ] === 'select2' && question . getType ( ) === 'dropdown' ;
9- if ( widget . activatedBy == "type" ) return question . getType ( ) === 'dropdown' ;
10- if ( widget . activatedBy == "customtype" ) return question . getType ( ) === 'select2' ;
11- return false ;
12- } ,
13- activatedByChanged : function ( activatedBy ) {
14- widget . activatedBy = activatedBy ;
15- Survey . JsonObject . metaData . removeProperty ( "dropdown" , "renderAs" ) ;
16- if ( activatedBy == "property" ) {
17- Survey . JsonObject . metaData . addProperty ( "dropdown" , { name : "renderAs" , default : "standard" , choices : [ "standard" , "select2" ] } ) ;
18- }
19- if ( activatedBy == "customtype" ) {
20- Survey . JsonObject . metaData . addClass ( "select2" , [ ] , null , "dropdown" ) ;
21- }
22- } ,
23- afterRender : function ( question , el ) {
24- var $el = $ ( el ) . is ( "select" ) ? $ ( el ) : $ ( el ) . find ( "select" ) ;
25- var othersEl = document . createElement ( "input" ) ;
26- othersEl . type = "text" ;
27- othersEl . style . marginTop = "3px" ;
28- othersEl . style . display = "none" ;
29- othersEl . style . width = "100%" ;
30- $el . parent ( ) . get ( 0 ) . appendChild ( othersEl ) ;
31- var widget = $el . select2 ( {
32- theme : "classic"
33- } ) ;
34- var updateValueHandler = function ( ) {
35- $el . val ( question . value ) . trigger ( "change" ) ;
36- othersEl . style . display = ! question . isOtherSelected ? "none" : "" ;
37- } ;
38- var updateCommentHandler = function ( ) {
39- othersEl . value = question . comment ? question . comment : "" ;
40- }
41- var othersElChanged = function ( ) {
42- question . comment = othersEl . value ;
43- }
44- var updateChoices = function ( ) {
45- $el . select2 ( { data : question . visibleChoices . map ( function ( choice ) { return { id : choice . value , text : choice . text } ; } ) } ) ;
1+ function init ( Survey ) {
2+ var widget = {
3+ activatedBy : "property" ,
4+ name : "select2" ,
5+ htmlTemplate : "<select style='width: 100%;'></select>" ,
6+ isFit : function ( question ) {
7+ if ( widget . activatedBy == "property" ) return question [ "renderAs" ] === 'select2' && question . getType ( ) === 'dropdown' ;
8+ if ( widget . activatedBy == "type" ) return question . getType ( ) === 'dropdown' ;
9+ if ( widget . activatedBy == "customtype" ) return question . getType ( ) === 'select2' ;
10+ return false ;
11+ } ,
12+ activatedByChanged : function ( activatedBy ) {
13+ widget . activatedBy = activatedBy ;
14+ Survey . JsonObject . metaData . removeProperty ( "dropdown" , "renderAs" ) ;
15+ if ( activatedBy == "property" ) {
16+ Survey . JsonObject . metaData . addProperty ( "dropdown" , { name : "renderAs" , default : "standard" , choices : [ "standard" , "select2" ] } ) ;
17+ }
18+ if ( activatedBy == "customtype" ) {
19+ Survey . JsonObject . metaData . addClass ( "select2" , [ ] , null , "dropdown" ) ;
20+ }
21+ } ,
22+ afterRender : function ( question , el ) {
23+ var $el = $ ( el ) . is ( "select" ) ? $ ( el ) : $ ( el ) . find ( "select" ) ;
24+ var othersEl = document . createElement ( "input" ) ;
25+ othersEl . type = "text" ;
26+ othersEl . style . marginTop = "3px" ;
27+ othersEl . style . display = "none" ;
28+ othersEl . style . width = "100%" ;
29+ $el . parent ( ) . get ( 0 ) . appendChild ( othersEl ) ;
30+ var widget = $el . select2 ( {
31+ theme : "classic"
32+ } ) ;
33+ var updateValueHandler = function ( ) {
34+ $el . val ( question . value ) . trigger ( "change" ) ;
35+ othersEl . style . display = ! question . isOtherSelected ? "none" : "" ;
36+ } ;
37+ var updateCommentHandler = function ( ) {
38+ othersEl . value = question . comment ? question . comment : "" ;
39+ }
40+ var othersElChanged = function ( ) {
41+ question . comment = othersEl . value ;
42+ }
43+ var updateChoices = function ( ) {
44+ $el . select2 ( { data : question . visibleChoices . map ( function ( choice ) { return { id : choice . value , text : choice . text } ; } ) } ) ;
45+ updateValueHandler ( ) ;
46+ updateCommentHandler ( ) ;
47+ }
48+ question . choicesChangedCallback = updateChoices ;
49+ updateChoices ( ) ;
50+ $el . on ( 'select2:select' , function ( e ) {
51+ question . value = e . target . value ;
52+ } ) ;
53+ othersEl . onchange = othersElChanged ;
54+ question . valueChangedCallback = updateValueHandler ;
55+ question . commentChangedCallback = updateCommentHandler ;
4656 updateValueHandler ( ) ;
4757 updateCommentHandler ( ) ;
58+ } ,
59+ willUnmount : function ( question , el ) {
60+ $ ( el ) . find ( "select" ) . off ( 'select2:select' ) . select2 ( "destroy" ) ;
4861 }
49- question . choicesChangedCallback = updateChoices ;
50- updateChoices ( ) ;
51- $el . on ( 'select2:select' , function ( e ) {
52- question . value = e . target . value ;
53- } ) ;
54- othersEl . onchange = othersElChanged ;
55- question . valueChangedCallback = updateValueHandler ;
56- question . commentChangedCallback = updateCommentHandler ;
57- updateValueHandler ( ) ;
58- updateCommentHandler ( ) ;
59- } ,
60- willUnmount : function ( question , el ) {
61- $ ( el ) . find ( "select" ) . off ( 'select2:select' ) . select2 ( "destroy" ) ;
6262 }
63+
64+ Survey . CustomWidgetCollection . Instance . addCustomWidget ( widget ) ;
65+ }
66+
67+ if ( typeof Survey !== "undefined" ) {
68+ init ( Survey ) ;
6369}
6470
65- Survey . CustomWidgetCollection . Instance . addCustomWidget ( widget ) ;
71+ export default init ;
0 commit comments