Skip to content

Commit

Permalink
Merge pull request #52 from nebiolabs/develop
Browse files Browse the repository at this point in the history
Fix issues related to select2 converting ids to text
  • Loading branch information
gorj-tessella authored Apr 9, 2019
2 parents ac452d4 + 471619b commit d8e3099
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 117 deletions.
117 changes: 67 additions & 50 deletions dist/js/plate-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,6 @@ var plateLayOutWidget = plateLayOutWidget || {};

});

field.getValue = function() {
var v = field.input.val();
if (v.length) {
return v;
}
return null;
};

return field;
}
}
Expand Down Expand Up @@ -1465,20 +1457,21 @@ var plateLayOutWidget = plateLayOutWidget || {};
var opts = that._createOpts(field.data);
var optMap = {};
opts.data.forEach(function(opt) {
optMap[opt.id] = opt;
optMap[String(opt.id)] = opt;
});

input.select2(opts);

field.parseValue = function(value) {
var v = value;

if (v == "") {
if (v === "") {
v = null;
}
if (v == null) {
return null;
}
v = String(v);
if (v in optMap) {
return optMap[v].id;
} else {
Expand All @@ -1491,7 +1484,7 @@ var plateLayOutWidget = plateLayOutWidget || {};
};

field.getValue = function() {
return input.val();
return field.parseValue(input.val());
};

field.setValue = function(v) {
Expand All @@ -1503,18 +1496,19 @@ var plateLayOutWidget = plateLayOutWidget || {};
if (v == null) {
return "";
}
return optMap[v].text;
return optMap[String(v)].text;
};

field.parseText = function(value) {
var v = value;

if (v == "") {
if (v === "") {
v = null;
}
if (v == null) {
return null;
}
v = String(v);
if (v in optMap) {
return optMap[v].text;
} else {
Expand Down Expand Up @@ -1545,41 +1539,42 @@ var plateLayOutWidget = plateLayOutWidget || {};
field.root.find(".plate-setup-tab-field-container").append(input);
that.defaultWell[id] = null;

var separator = ",";
var opts = that._createOpts(field.data);
opts.multiple = true;
var optMap = {};
opts.data.forEach(function(opt) {
optMap[opt.id] = opt;
optMap[String(opt.id)] = opt;
});
input.select2(opts);

field.disabled = function(bool) {
field.input.prop("disabled", bool);
input.prop("disabled", bool);
};

field.parseValue = function(value) {
var v = value;
if (v && v.length) {
v = v.map(function(opt) {
if (opt in optMap) {
return optMap[opt].id;
} else {
throw "Invalid value " + opt + " for multiselect field " + id;
}
});
field._parseOne = function(val) {
val = String(val);
if (val in optMap) {
return optMap[val].id;
} else {
v = null;
throw "Invalid value " + val + " for multiselect field " + id;
}
return v;
};

field.getValue = function() {
var v = input.val();
if (v.length) {
return v;
field._parseMany = function(vals) {
if (vals && vals.length) {
vals = vals.map(field._parseOne);
} else {
vals = null;
}
return null;
return vals;
}

field.parseValue = function(value) {
return field._parseMany(value);
};

field.getValue = function() {
return field._parseMany(input.val());
};

field.setValue = function(v) {
Expand All @@ -1594,18 +1589,18 @@ var plateLayOutWidget = plateLayOutWidget || {};
}
if (v.length > 0) {
return v.map(function(v) {
return optMap[v].text
return optMap[String(v)].text
}).join("; ");
}
return "";
};

field.multiOnChange = function(added, removed) {
if (added) {
added = added.id.toString();
added = added.id;
}
if (removed) {
removed = removed.id.toString();
removed = removed.id;
}
var data = {};
data[field.id] = {
Expand All @@ -1621,6 +1616,7 @@ var plateLayOutWidget = plateLayOutWidget || {};
var v = value;
if (v && v.length) {
v = v.map(function(opt) {
opt = String(opt);
if (opt in optMap) {
return optMap[opt].text;
} else {
Expand All @@ -1634,11 +1630,15 @@ var plateLayOutWidget = plateLayOutWidget || {};
};

input.on("select2:select", function (e) {
field.multiOnChange(e.params.data, null);
var v = field._parseOne(e.params.data.id)
v = {id: v};
field.multiOnChange(v, null);
});

input.on("select2:unselect", function (e) {
field.multiOnChange(null, e.params.data);
var v = field._parseOne(e.params.data.id)
v = {id: v};
field.multiOnChange(null, v);
// Prevent select2 v4.0.6rc1 opening dropdown on unselect
input.one('select2:opening', function(e) { e.preventDefault(); });
});
Expand Down Expand Up @@ -1978,11 +1978,11 @@ var plateLayOutWidget = plateLayOutWidget || {};
return null;
}
var v = String(value).trim().toLowerCase();
if (v == "true") {
if (v === "true") {
v = true;
} else if (v == "false") {
} else if (v === "false") {
v = false;
} else if (v == "") {
} else if (v === "") {
v = null;
} else {
throw "Invalid value " + value + " for boolean field " + id;
Expand All @@ -2003,9 +2003,9 @@ var plateLayOutWidget = plateLayOutWidget || {};
};

field.setValue = function(v) {
if (v == true || v == "true") {
if (v == true || v === "true") {
v = "true";
} else if (v == false || v == "false") {
} else if (v == false || v === "false") {
v = "false";
} else {
v = null;
Expand Down Expand Up @@ -2053,8 +2053,17 @@ var plateLayOutWidget = plateLayOutWidget || {};

field.singleSelect.appendTo(fieldContainer1);

var multiselectSetValue = field.setValue;

field.singleSelectValue = function() {
return field.singleSelect.val();
var v = field.singleSelect.val();
if (v === "") {
return null;
}
if (v == null) {
return null;
}
return field._parseOne(v)
};

var setSingleSelectOptions = function(v, selected_v) {
Expand Down Expand Up @@ -2156,8 +2165,6 @@ var plateLayOutWidget = plateLayOutWidget || {};
that._addAllData(data);
};

var multiselectSetValue = field.setValue;

// overwrite multiplex set value
field.setValue = function(v) {
// used to keep track of initially loaded multiplex data
Expand Down Expand Up @@ -2291,15 +2298,25 @@ var plateLayOutWidget = plateLayOutWidget || {};
});

// make data for single select options
v.forEach(function(selectId) {
v.forEach(function(selectVal) {
field.data.options.forEach(function(opt) {
if (opt.id === selectId) {
if (opt.id === selectVal) {
selectList.push(opt);
}
});
});
// set the newest selected to be the current obj
curOpt = selectList[v.length - 1].id;

var selected = field.singleSelectValue();
for (var i = 0; i < v.length; i++) {
if (added && (added.id === v[i])) {
curOpt = v[i];
break;
} else if (i === 0) {
curOpt = v[i];
} else if (v[i] === selected) {
curOpt = v[i];
}
}
}

field.detailData = newMultiplexVal;
Expand Down
2 changes: 1 addition & 1 deletion dist/js/plate-map.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/plate-map.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plate-map",
"version": "1.0.3",
"version": "1.0.4",
"description": "JavaScript Plate Layout is an open source tool developed collaboratively by [Chai Biotechnologies](www.chaibio.com) and [New England Biolabs](www.neb.com) for visualizing and editing the layout of scientific assay plates.",
"scripts": {
"build.dist": "gulp build.dist",
Expand Down
8 changes: 0 additions & 8 deletions src/js/add-tab-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,6 @@ var plateLayOutWidget = plateLayOutWidget || {};

});

field.getValue = function() {
var v = field.input.val();
if (v.length) {
return v;
}
return null;
};

return field;
}
}
Expand Down
Loading

0 comments on commit d8e3099

Please sign in to comment.