This patch adds support for a "namespace" attribute for the combobox and tokens input.
Rationale:. The #forminput parser function already has this feature, but it works only in page view mode. This feature ensure that users don't have to manually add a specific namespace to their input in the form edit mode.
E.g. namespace="Tag" will ensure that "Tag:" will be prefixed to (each) user input which doesn't already include a namespace already (somewhat smart behaviour).
Sadly, I couldn't find a place to put this more generically, so I had to fall back to including this into the forminputs itself.
It would also be possible to add this to text/text with autocomplete, if there's need for it.
From a72dc33723c0ab77942e85f71d01a32a2bf39957 Mon Sep 17 00:00:00 2001 From: Fannon <heimlersimon@gmail.com> Date: Thu, 12 May 2016 12:01:11 +0200 Subject: [PATCH] first working version, support for namespace attribute in tokens and combobox forminput --- includes/forminputs/SF_ComboBoxInput.php | 3 +++ includes/forminputs/SF_TokensInput.php | 3 +++ libs/ext.sf.select2.combobox.js | 10 +++++++++- libs/ext.sf.select2.tokens.js | 10 +++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/includes/forminputs/SF_ComboBoxInput.php b/includes/forminputs/SF_ComboBoxInput.php index 799766f..9a1e8d5 100644 --- a/includes/forminputs/SF_ComboBoxInput.php +++ b/includes/forminputs/SF_ComboBoxInput.php @@ -106,6 +106,9 @@ class SFComboBoxInput extends SFFormInput { if ( !is_null( $remoteDataType ) ) { $inputAttrs['autocompletedatatype'] = $remoteDataType; } + if ( array_key_exists( 'namespace', $other_args ) ) { + $inputAttrs['data-namespace'] = $other_args['namespace']; + } $inputText = Html::rawElement( 'input', $inputAttrs); diff --git a/includes/forminputs/SF_TokensInput.php b/includes/forminputs/SF_TokensInput.php index a5afbf8..2d2792a 100644 --- a/includes/forminputs/SF_TokensInput.php +++ b/includes/forminputs/SF_TokensInput.php @@ -150,6 +150,9 @@ class SFTokensInput extends SFFormInput { if ( array_key_exists( 'max values', $other_args ) ) { $inputAttrs['maxvalues'] = $other_args['max values']; } + if ( array_key_exists( 'namespace', $other_args ) ) { + $inputAttrs['data-namespace'] = $other_args['namespace']; + } $text = "\n\t" . Html::input( $input_name, $cur_value, 'text', $inputAttrs ) . "\n"; $spanClass = 'inputSpan'; diff --git a/libs/ext.sf.select2.combobox.js b/libs/ext.sf.select2.combobox.js index f300440..1320c0c 100644 --- a/libs/ext.sf.select2.combobox.js +++ b/libs/ext.sf.select2.combobox.js @@ -247,8 +247,16 @@ combobox_proto.onChange = function() { var self = this; var data = $(this).select2( "data" ); + var namespace = $(this).attr( "data-namespace" ); if (data !== null) { - $(this).val( data.text ); + var val = data.text; + + if (namespace) { + if (val.split(':').length === 1) { + val = namespace + ':' + val; + } + } + $(this).val( val ); } else { $(this).val( '' ); } diff --git a/libs/ext.sf.select2.tokens.js b/libs/ext.sf.select2.tokens.js index 03302da..e5fbe36 100644 --- a/libs/ext.sf.select2.tokens.js +++ b/libs/ext.sf.select2.tokens.js @@ -275,10 +275,18 @@ var tokens = new sf.select2.tokens(); var delim = tokens.getDelimiter( $(this) ); + var namespace = $(this).attr( "data-namespace" ); + if (data !== null) { var tokens_value = ""; data.forEach( function( token ) { - tokens_value += token.text.trim() + delim + " "; + var val = token.text.trim(); + if (namespace) { + if (val.split(':').length === 1) { + val = namespace + ':' + val; + } + } + tokens_value += val + delim + " "; }); $(this).val( tokens_value ); } else { -- 2.7.0.windows.1