Skip to content

Instantly share code, notes, and snippets.

@ghinch
Created January 7, 2011 19:52
Show Gist options
  • Save ghinch/770000 to your computer and use it in GitHub Desktop.
Save ghinch/770000 to your computer and use it in GitHub Desktop.
var AutoCompleteMultiQueryDelim = Y.Base.create('ac-multi-query-delim', Y.Plugin.Base, [], {
_beforeParseValue : function (value) {
return new Y.Do.Prevent();
},
_newParseValue : function (value) {
var delim = this.get(HOST).get(QUERY_DELIMITER),
delimIndex = 0,
useDelim;
value = value || '';
if (Y.Lang.isArray(delim)) {
Y.Array.each(delim, function (d) {
var index = value.lastIndexOf(d);
if (index > delimIndex) {
delimIndex = index;
useDelim = d;
}
});
if (useDelim) {
delim = useDelim;
}
}
if (Y.Lang.isString(delim)) {
value = value.split(delim);
value = value[value.length - 1];
}
return new Y.Do.AlterReturn('', Y.Lang.trimLeft(value));
},
_newUpdateValue : function (newVal) {
var host = this.get(HOST),
delim = host.get(QUERY_DELIMITER),
insertDelim,
len,
prevVal;
newVal = Y.Lang.trimLeft(newVal);
if (delim) {
if (Y.Lang.isString(delim)) {
insertDelim = Y.Lang.trim(delim); // so we don't double up on spaces
prevVal = Y.Array.map(Y.Lang.trim(host.get(VALUE)).split(delim), Y.Lang.trim);
len = prevVal.length;
if (len > 1) {
prevVal[len - 1] = newVal;
newVal = prevVal.join(insertDelim + ' ');
}
newVal = newVal + insertDelim + ' ';
} else if (Y.Lang.isArray(delim)) {
// For now if you have multiple delimiters, there is no need to
// try and append one after completing
}
}
host.set(VALUE, newVal);
return new Y.Do.Prevent();
},
_newOnInputBlur : function (e) {
var host = this.get(HOST),
delim = host.get(QUERY_DELIMITER),
delimPos,
newVal,
value;
function trimDelim(str, d) {
while ((str = Y.Lang.trimRight(str)) &&
(delimPos = str.length - d.length) &&
str.lastIndexOf(d) === delimPos) {
str = str.substring(0, delimPos);
}
return str
}
// If a query delimiter is set and the input's value contains one or
// more trailing delimiters, strip them.
if (delim && !host.get('allowTrailingDelimiter')) {
value = newVal = host._inputNode.get(VALUE);
if (delim) {
if (Y.Lang.isString(delim)) {
delim = Y.Lang.trimRight(delim);
newVal = trimDelim(newVal, delim);
} else if (Y.Lang.isArray(delim)) {
var testArr = Y.Array.map(delim, function () {
return false;
});
while (Y.Array.indexOf(testArr, false) > -1) {
Y.Array.each(delim, function (d, i) {
var trimmed = trimDelim(newVal, Y.Lang.trimRight(d));
if (trimmed == newVal) {
testArr[i] = true;
} else {
testArr[i] = false;
}
newVal = trimmed;
});
}
}
} else {
// Delimiter is one or more space characters, so just trim the
// value.
newVal = Y.Lang.trimRight(newVal);
}
if (newVal !== value) {
host.set(VALUE, newVal);
}
}
return new Y.Do.Prevent();
},
initializer : function () {
this.doBefore('_parseValue', this._beforeParseValue);
this.doAfter('_parseValue', this._newParseValue);
this.doBefore('_updateValue', this._newUpdateValue);
this.doBefore('_onInputBlur', this._newOnInputBlur);
}
}, {
NS : 'multiQueryDelim'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment