Skip to content

Commit 0d7f9b9

Browse files
committed
Implemented colCount and addOn features for the pretty-checkbox custom widget
1 parent e01508f commit 0d7f9b9

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/pretty-checkbox.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ function init(Survey) {
44
radiogroup: {
55
rootClass: "pretty p-default p-round",
66
inputType: "radio",
7+
addOn: "",
78
titleClass: "state p-success"
89
},
910
checkbox: {
1011
rootClass: "pretty p-default",
1112
inputType: "checkbox",
13+
addOn: "",
1214
titleClass: "state p-success"
1315
}
1416
},
1517
name: "pretty-checkbox",
16-
widgetIsLoaded: function () {
18+
widgetIsLoaded: function() {
1719
for (var i = 0; i < document.styleSheets.length; i++) {
1820
var href = document.styleSheets[i].ownerNode["href"];
1921
if (!!href && href.indexOf("pretty-checkbox") != -1) {
@@ -23,16 +25,16 @@ function init(Survey) {
2325
return false;
2426
},
2527
htmlTemplate: "<fieldset></fieldset>",
26-
isFit: function (question) {
28+
isFit: function(question) {
2729
var type = question.getType();
2830
return type === "radiogroup" || type === "checkbox"; // || type === "matrix";
2931
},
3032
isDefaultRender: false,
31-
afterRender: function (question, el) {
33+
afterRender: function(question, el) {
3234
var itemInputs = {};
3335
var options = this.settings[question.getType()];
3436
var inChangeHandler = false;
35-
var changeHandler = function (event) {
37+
var changeHandler = function(event) {
3638
inChangeHandler = true;
3739
try {
3840
var value = arguments[0].target.value;
@@ -55,9 +57,15 @@ function init(Survey) {
5557
inChangeHandler = false;
5658
}
5759
};
58-
question.choices.forEach(function (choiceItem, index) {
60+
var itemWidth =
61+
question.colCount > 0 ? 100 / question.colCount + "%" : "";
62+
question.choices.forEach(function(choiceItem, index) {
5963
var itemRoot = document.createElement("div");
60-
itemRoot.className = options.rootClass;
64+
itemRoot.className = "sv_cw_pretty_checkbox_" + question.getType();
65+
itemRoot.style.display = "inline-block";
66+
itemRoot.style.width = itemWidth;
67+
var controlRoot = document.createElement("div");
68+
controlRoot.className = options.rootClass;
6169
var input = document.createElement("input");
6270
input.type = options.inputType;
6371
input.name =
@@ -69,19 +77,23 @@ function init(Survey) {
6977
var label = document.createElement("label");
7078
label.textContent = choiceItem.text;
7179
titleRoot.appendChild(label);
72-
itemRoot.appendChild(input);
73-
itemRoot.appendChild(titleRoot);
80+
controlRoot.appendChild(input);
81+
controlRoot.appendChild(titleRoot);
82+
if (!!options.addOn) {
83+
titleRoot.insertAdjacentHTML("afterbegin", options.addOn);
84+
}
85+
itemRoot.appendChild(controlRoot);
7486
el.appendChild(itemRoot);
7587

7688
itemInputs[choiceItem.value] = input;
7789
});
78-
var updateValueHandler = function (newValue) {
90+
var updateValueHandler = function(newValue) {
7991
if (!inChangeHandler) {
8092
var checkedItems = newValue || [];
8193
if (question.getType() === "radiogroup") {
8294
checkedItems = [newValue];
8395
}
84-
Object.values(itemInputs).forEach(function (inputItem) {
96+
Object.values(itemInputs).forEach(function(inputItem) {
8597
if (checkedItems.indexOf(inputItem.value) !== -1) {
8698
inputItem.setAttribute("checked", undefined);
8799
} else {
@@ -93,7 +105,7 @@ function init(Survey) {
93105
question.valueChangedCallback = updateValueHandler;
94106
updateValueHandler(question.value);
95107
},
96-
willUnmount: function (question, el) {
108+
willUnmount: function(question, el) {
97109
question.valueChangedCallback = undefined;
98110
}
99111
};
@@ -105,4 +117,4 @@ if (typeof Survey !== "undefined") {
105117
init(Survey);
106118
}
107119

108-
export default init;
120+
export default init;

0 commit comments

Comments
 (0)