Skip to content

Commit ede230d

Browse files
modified structure because of modules
1 parent 8a8b284 commit ede230d

File tree

2 files changed

+72
-116
lines changed

2 files changed

+72
-116
lines changed

src/select2.js

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,71 @@
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;

webpack.config.js

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var webpack = require("webpack");
44
var path = require("path");
55
var FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
6-
const VirtualModulePlugin = require("virtual-module-webpack-plugin");
76
var CopyWebpackPlugin = require('copy-webpack-plugin');
87
var GenerateJsonPlugin = require('generate-json-webpack-plugin');
98

@@ -15,76 +14,31 @@ var copyright = [
1514
].join("\n");
1615

1716
var outputFolder = "packages";
18-
var commonDependencies = {
19-
'select2': '>=^4.0.4'
17+
var commonDependencies = { //TODO add jquery and all widgets
18+
'select2': '^4.0.4'
2019
};
2120
var widgets = ["select2", "imagepicker", "icheck", "datepicker", "tagbox"];
2221
var entry = {};
2322

2423
var platformOptions = {
2524
'react': {
26-
externals: {
27-
'survey-react': {
28-
root: 'Survey',
29-
commonjs2: 'survey-react',
30-
commonjs: 'survey-react',
31-
amd: 'survey-react'
32-
}
33-
},
34-
import: 'import * as Survey from "survey-react";',
35-
dependencies: { 'survey-react': '>=^0.12.32' },
25+
dependencies: { 'survey-react': '^0.12.32' },
3626
keywords: ['react', 'react-component']
3727
},
3828
'knockout': {
39-
externals: {
40-
'survey-knockout': {
41-
root: 'Survey',
42-
commonjs2: 'survey-knockout',
43-
commonjs: 'survey-knockout',
44-
amd: 'survey-knockout'
45-
}
46-
},
47-
import: 'import * as Survey from "survey-knockout";',
48-
dependencies: { 'survey-knockout': '>=^0.12.32' },
29+
dependencies: { 'survey-knockout': '^0.12.32' },
4930
keywords: ['knockout']
5031
},
5132
'jquery': {
52-
externals: {
53-
'survey-jquery': {
54-
root: 'Survey',
55-
commonjs2: 'survey-jquery',
56-
commonjs: 'survey-jquery',
57-
amd: 'survey-jquery'
58-
}
59-
},
60-
import: 'import * as Survey from "survey-jquery";',
61-
dependencies: { 'survey-jquery': '>=^0.12.32' },
33+
dependencies: { 'survey-jquery': '^0.12.32' },
6234
keywords: ['jquery', 'jquery-plugin']
6335
},
6436
'angular': {
65-
externals: {
66-
'survey-angular': {
67-
root: 'Survey',
68-
commonjs2: 'survey-angular',
69-
commonjs: 'survey-angular',
70-
amd: 'survey-angular'
71-
}
72-
},
73-
import: 'import * as Survey from "survey-angular";',
74-
dependencies: { 'survey-angular': '>=^0.12.32' },
37+
dependencies: { 'survey-angular': '^0.12.32' },
7538
keywords: ['angular', 'angular-component']
7639
},
7740
'vue': {
78-
externals: {
79-
'survey-vue': {
80-
root: 'Survey',
81-
commonjs2: 'survey-vue',
82-
commonjs: 'survey-vue',
83-
amd: 'survey-vue'
84-
}
85-
},
86-
import: 'import * as Survey from "survey-vue";',
87-
dependencies: { 'survey-vue': '>=^0.12.32' },
41+
dependencies: { 'survey-vue': '^0.12.32' },
8842
keywords: ['vue']
8943
}
9044
};
@@ -142,10 +96,6 @@ module.exports = function(options) {
14296
new FriendlyErrorsWebpackPlugin(),
14397
new webpack.DefinePlugin({
14498
'PLATFORM': JSON.stringify(options.platform)
145-
}),
146-
new VirtualModulePlugin({
147-
moduleName: 'src/surveyjs_importer.js',
148-
contents: platformOptions[options.platform].import
14999
})
150100
],
151101
devtool: options.buildType === "prod" ? "source-map" : "inline-source-map",

0 commit comments

Comments
 (0)