mirror of https://github.com/zulip/zulip.git
input-pill: Add validation callback for pills.
This adds a validator that when calling `reject` will reject a pill from being added.
This commit is contained in:
parent
5d0aae283f
commit
5a0bba2afc
|
@ -14,6 +14,7 @@ var input_pill = function ($parent) {
|
||||||
pills: [],
|
pills: [],
|
||||||
$parent: $parent,
|
$parent: $parent,
|
||||||
getKeyFunction: function () {},
|
getKeyFunction: function () {},
|
||||||
|
validation: function () {},
|
||||||
lastUpdated: null,
|
lastUpdated: null,
|
||||||
lastCreated: {
|
lastCreated: {
|
||||||
keys: null,
|
keys: null,
|
||||||
|
@ -55,7 +56,17 @@ var input_pill = function ($parent) {
|
||||||
// default `undefined`.
|
// default `undefined`.
|
||||||
if (typeof optionalKey === "undefined") {
|
if (typeof optionalKey === "undefined") {
|
||||||
optionalKey = store.getKeyFunction(value, reject);
|
optionalKey = store.getKeyFunction(value, reject);
|
||||||
|
|
||||||
|
if (typeof optionalKey === "object" &&
|
||||||
|
optionalKey.key !== undefined && optionalKey.value !== undefined) {
|
||||||
|
value = optionalKey.value;
|
||||||
|
optionalKey = optionalKey.key;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// now run a separate round of validation, in case they are using
|
||||||
|
// `getKeyFunction` without `reject`, or not using it at all.
|
||||||
|
store.validation(value, optionalKey, reject);
|
||||||
|
|
||||||
// if the `rejected` global is now true, it means that the user's
|
// if the `rejected` global is now true, it means that the user's
|
||||||
// created pill was not accepted, and we should no longer proceed.
|
// created pill was not accepted, and we should no longer proceed.
|
||||||
|
@ -66,15 +77,6 @@ var input_pill = function ($parent) {
|
||||||
|
|
||||||
var id = Math.random().toString(16);
|
var id = Math.random().toString(16);
|
||||||
|
|
||||||
// the user may provide a function to get a key from a value
|
|
||||||
// that is entered, so return whatever value is gotten from
|
|
||||||
// this function.
|
|
||||||
// the default function is noop, so the return type is by
|
|
||||||
// default `undefined`.
|
|
||||||
if (typeof optionalKey === "undefined") {
|
|
||||||
optionalKey = store.getKeyFunction(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
var payload = {
|
var payload = {
|
||||||
id: id,
|
id: id,
|
||||||
value: value,
|
value: value,
|
||||||
|
@ -308,6 +310,10 @@ var input_pill = function ($parent) {
|
||||||
onPillCreate: function (callback) {
|
onPillCreate: function (callback) {
|
||||||
store.getKeyFunction = callback;
|
store.getKeyFunction = callback;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
validate: function (callback) {
|
||||||
|
store.validation = callback;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return prototype;
|
return prototype;
|
||||||
|
|
Loading…
Reference in New Issue