pills: Add exportable function for creating non-editable pills.

This commit is contained in:
Tarun Kumar 2018-04-06 03:00:11 +05:30 committed by Tim Abbott
parent 9a6a82516d
commit 5c11ab857e
2 changed files with 58 additions and 1 deletions

View File

@ -367,6 +367,61 @@ exports.create = function (opts) {
return prototype;
};
// Following function is used for creating non-editable pills.
exports.create_non_editable_pills = function (opts) {
if (!opts.container) {
blueslip.error('Pill needs container.');
return;
}
var store = {
pills: [],
$parent: opts.container,
};
var funcs = {
// This is generally called by typeahead logic, where we have all
// the data we need (as opposed to, say, just a user-typed email).
appendValidatedData: function (item) {
var id = exports.random_id();
if (!item.display_value) {
blueslip.error('no display_value returned');
return;
}
var payload = {
id: id,
item: item,
};
store.pills.push(payload);
var opts = {
id: payload.id,
display_value: item.display_value,
cannot_edit: true,
};
var pill_html = templates.render('input_pill', opts);
payload.$element = $(pill_html);
store.$parent.append(payload.$element);
},
items: function () {
return _.pluck(store.pills, 'item');
},
};
var prototype = {
appendValidatedData: funcs.appendValidatedData.bind(funcs),
items: funcs.items,
};
return prototype;
};
return exports;
}());

View File

@ -60,7 +60,9 @@ exports.append_person = function (opts) {
user_id: person.user_id,
email: person.email,
});
pill_widget.clear_text();
if (pill_widget.clear_text !== undefined) {
pill_widget.clear_text();
}
};
exports.get_user_ids = function (pill_widget) {