From 763f8ed32d885745ff3f4a4a78f1ce37514a99e6 Mon Sep 17 00:00:00 2001 From: Joel Hughes Date: Tue, 29 Sep 2015 11:48:10 -0400 Subject: [PATCH] Add support for 'optional' ChoiceViews An optional ChoiceView does not require a selection, and additionally can be used to 'unset' a property. When enabled an entry such as '--' is added to the top of the choices list. --- js/foam/ui/AbstractChoiceView.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/js/foam/ui/AbstractChoiceView.js b/js/foam/ui/AbstractChoiceView.js index 0ab10fe61..983bbda5f 100644 --- a/js/foam/ui/AbstractChoiceView.js +++ b/js/foam/ui/AbstractChoiceView.js @@ -83,7 +83,7 @@ CLASS({ if ( a.hasOwnProperty(key) ) out.push([key, a[key]]); } - return out; + return this.addOptional(out); } a = a.clone(); @@ -91,7 +91,7 @@ CLASS({ for ( var i = 0 ; i < a.length ; i++ ) if ( ! Array.isArray(a[i]) ) a[i] = [a[i], a[i]]; - return a; + return this.addOptional(a); }, postSet: function(oldValue, newValue) { var value = this.data; @@ -181,6 +181,20 @@ CLASS({ if ( nu && this.choices.length ) console.warn('ChoiceView data set to invalid choice: ', nu); } + }, + { + documentation: function() {/*When 'true', choice selection is optional. A optional or no-selection entry with text 'optionalText' and value 'optionalValue', will be placed at the top of the 'choices' list.*/}, + model_: 'BooleanProperty', + name: 'optional', + defaultValue: false, + }, + { + name: 'optionalText', + defaultValue: '--', + }, + { + name: 'optionalValue', + defaultValue: undefined, } ], @@ -215,6 +229,14 @@ CLASS({ commit: function() { if ( this.useSelection && this.choices[this.index] ) this.choice = this.choices[this.index]; - } + }, + + addOptional: function(a) { + if (this.optional && + a[0] && a[0][0] != this.optionalValue) { + a.unshift([this.optionalValue, this.optionalText]); + } + return a; + }, } });