1+ function init ( Survey ) {
2+ var widget = {
3+ settings : {
4+ radiogroup : {
5+ rootClass : "pretty p-default p-round" ,
6+ inputType : "radio" ,
7+ titleClass : "state p-success"
8+ } ,
9+ checkbox : {
10+ rootClass : "pretty p-default" ,
11+ inputType : "checkbox" ,
12+ titleClass : "state p-success"
13+ }
14+ } ,
15+ name : "pretty-checkbox" ,
16+ widgetIsLoaded : function ( ) {
17+ for ( var i = 0 ; i < document . styleSheets . length ; i ++ ) {
18+ var href = document . styleSheets [ i ] . ownerNode [ "href" ] ;
19+ if ( ! ! href && href . indexOf ( "pretty-checkbox" ) != - 1 ) {
20+ return true ;
21+ }
22+ }
23+ return false ;
24+ } ,
25+ htmlTemplate : "<fieldset></fieldset>" ,
26+ isFit : function ( question ) {
27+ var type = question . getType ( ) ;
28+ return type === "radiogroup" || type === "checkbox" ; // || type === "matrix";
29+ } ,
30+ isDefaultRender : false ,
31+ afterRender : function ( question , el ) {
32+ var itemInputs = { } ;
33+ var options = this . settings [ question . getType ( ) ] ;
34+ var inChangeHandler = false ;
35+ var changeHandler = function ( event ) {
36+ inChangeHandler = true ;
37+ try {
38+ var value = arguments [ 0 ] . target . value ;
39+ if ( question . getType ( ) === "checkbox" ) {
40+ var qValue = question . value || [ ] ;
41+ if ( arguments [ 0 ] . target . checked ) {
42+ if ( qValue . indexOf ( value ) === - 1 ) {
43+ qValue . push ( value ) ;
44+ }
45+ } else {
46+ if ( qValue . indexOf ( value ) !== - 1 ) {
47+ qValue . splice ( qValue . indexOf ( value ) , 1 ) ;
48+ }
49+ }
50+ question . value = qValue ;
51+ } else {
52+ question . value = value ;
53+ }
54+ } finally {
55+ inChangeHandler = false ;
56+ }
57+ } ;
58+ question . choices . forEach ( function ( choiceItem , index ) {
59+ var itemRoot = document . createElement ( "div" ) ;
60+ itemRoot . className = options . rootClass ;
61+ var input = document . createElement ( "input" ) ;
62+ input . type = options . inputType ;
63+ input . name =
64+ question . name + ( question . getType ( ) === "checkbox" ? "" + index : "" ) ;
65+ input . onchange = changeHandler ;
66+ input . value = choiceItem . value ;
67+ var titleRoot = document . createElement ( "div" ) ;
68+ titleRoot . className = options . titleClass ;
69+ var label = document . createElement ( "label" ) ;
70+ label . textContent = choiceItem . text ;
71+ titleRoot . appendChild ( label ) ;
72+ itemRoot . appendChild ( input ) ;
73+ itemRoot . appendChild ( titleRoot ) ;
74+ el . appendChild ( itemRoot ) ;
75+
76+ itemInputs [ choiceItem . value ] = input ;
77+ } ) ;
78+ var updateValueHandler = function ( newValue ) {
79+ if ( ! inChangeHandler ) {
80+ var checkedItems = newValue || [ ] ;
81+ if ( question . getType ( ) === "radiogroup" ) {
82+ checkedItems = [ newValue ] ;
83+ }
84+ Object . values ( itemInputs ) . forEach ( function ( inputItem ) {
85+ if ( checkedItems . indexOf ( inputItem . value ) !== - 1 ) {
86+ inputItem . setAttribute ( "checked" , undefined ) ;
87+ } else {
88+ inputItem . removeAttribute ( "checked" ) ;
89+ }
90+ } ) ;
91+ }
92+ } ;
93+ question . valueChangedCallback = updateValueHandler ;
94+ updateValueHandler ( question . value ) ;
95+ } ,
96+ willUnmount : function ( question , el ) {
97+ question . valueChangedCallback = undefined ;
98+ }
99+ } ;
100+
101+ Survey . CustomWidgetCollection . Instance . addCustomWidget ( widget , "type" ) ;
102+ }
103+
104+ if ( typeof Survey !== "undefined" ) {
105+ init ( Survey ) ;
106+ }
107+
108+ export default init ;
0 commit comments