From 1f5c9422375074e6eb3bf9bb58b34b16d813529b Mon Sep 17 00:00:00 2001 From: Dan Camina Date: Wed, 19 Jun 2019 11:03:30 +0100 Subject: [PATCH] feat: allow combolist to be limited --- lib/components/List.js | 22 ++++++++++++++++++++-- lib/forms/Form.js | 2 +- lib/forms/item_types.js | 3 ++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/components/List.js b/lib/components/List.js index 9c57821..0f44bdd 100644 --- a/lib/components/List.js +++ b/lib/components/List.js @@ -8,14 +8,19 @@ var MLList = module.exports = milo.createComponentClass({ }, data: undefined, events: undefined, - model: undefined, + model: { + messages: { + '*': { subscriber: onListChange, context: 'owner' } + } + }, list: undefined }, methods: { init: MLList$init, destroy: MLList$destroy, removeItem: MLList$removeItem, - moveItem: MLList$moveItem + moveItem: MLList$moveItem, + setListLimit: MLList$setListLimit, } }); @@ -43,8 +48,21 @@ function MLList$moveItem(from, to) { return this.model.splice(to, 0, splicedData[0]); } +function MLList$setListLimit(limit) { + this._listLimit = Number.isInteger(limit) && limit !== 0 && limit; +} + function onChildrenBound() { this.model.set([]); this._connector = milo.minder(this.model, '<<<-', this.data).deferChangeMode('<<<->>>'); } + +function onListChange(_msg, { type }) { + const { _listLimit, list } = this; + const currentItemCount = list.count(); + if(type === 'added' && _listLimit && currentItemCount >= _listLimit){ + const diff = Math.abs(currentItemCount - _listLimit) + 1; + list.removeItems(0, diff); + } +} diff --git a/lib/forms/Form.js b/lib/forms/Form.js index b5e2392..ec55f78 100644 --- a/lib/forms/Form.js +++ b/lib/forms/Form.js @@ -147,7 +147,7 @@ var SCHEMA_KEYWORDS = _.object([ 'modelPath', 'modelPattern', 'notInModel', 'messages', 'translate', 'validate', 'items', 'selectOptions', 'radioOptions', 'comboOptions', - 'comboOptionsURL', 'addItemPrompt', 'placeHolder', + 'comboOptionsURL', 'limitComboList', 'addItemPrompt', 'placeHolder', 'value', 'dataValidation', 'asyncHandler', 'autoresize', 'maxLength' ], true); diff --git a/lib/forms/item_types.js b/lib/forms/item_types.js index edaf8d6..6ced2db 100644 --- a/lib/forms/item_types.js +++ b/lib/forms/item_types.js @@ -96,13 +96,14 @@ function processSuperComboSchema(comp, schema) { function processComboListSchema(comp, schema) { - const { comboOptions, comboOptionsURL, addItemPrompt, placeHolder } = schema; + const { comboOptions, comboOptionsURL, addItemPrompt, placeHolder, limitComboList } = schema; _.deferTicks(function() { if (addItemPrompt) comp.setAddItemPrompt(addItemPrompt); if (placeHolder) comp.setPlaceholder(placeHolder); if (!comboOptionsURL) comp.setDataValidation(schema.dataValidation); setComponentOptions(comp, comboOptions, setComboOptions); if (comboOptionsURL) comp.container.scope.combo.initOptionsURL(comboOptionsURL); + if (limitComboList) comp.container.scope.list.setListLimit(limitComboList); }, 2); }