From 5c11ab857e905166932dd806462f0ca4c362240a Mon Sep 17 00:00:00 2001 From: Tarun Kumar Date: Fri, 6 Apr 2018 03:00:11 +0530 Subject: [PATCH] pills: Add exportable function for creating non-editable pills. --- static/js/input_pill.js | 55 +++++++++++++++++++++++++++++++++++++++++ static/js/user_pill.js | 4 ++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/static/js/input_pill.js b/static/js/input_pill.js index ecafd2fea9..fdf069612f 100644 --- a/static/js/input_pill.js +++ b/static/js/input_pill.js @@ -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; }()); diff --git a/static/js/user_pill.js b/static/js/user_pill.js index 7be9b8f8f1..55ae19c9d0 100644 --- a/static/js/user_pill.js +++ b/static/js/user_pill.js @@ -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) {