UNPKG

foam-framework

Version:
77 lines (69 loc) 2.46 kB
/** * @license * Copyright 2014 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ CLASS({ package: 'foam.ui', name: 'StringArrayView', extends: 'foam.ui.TextFieldView', methods: { findCurrentValues: function() { var start = this.$.selectionStart; var value = this.$.value; var values = value.split(','); var i = 0; var sum = 0; while ( sum + values[i].length < start ) { sum += values[i].length + 1; i++; } return { values: values, i: i }; }, setValues: function(values, index) { this.domValue.set(this.valueToText(values) + ','); this.data = this.textToValue(this.domValue.get()); var isLast = values.length - 1 === index; var selection = 0; for ( var i = 0; i <= index; i++ ) { selection += values[i].length + 1; } this.$.setSelectionRange(selection, selection); isLast && this.X.setTimeout((function() { this.autocompleteView.autocomplete(''); }).bind(this), 0); }, onAutocomplete: function(data) { var current = this.findCurrentValues(); current.values[current.i] = data; this.setValues(current.values, current.i); }, bindAutocompleteEvents: function(view) { // TODO: Refactor this. var self = this; function onInput() { var values = self.findCurrentValues(); view.autocomplete(values.values[values.i]); } this.$.addEventListener('input', onInput); this.$.addEventListener('focus', onInput); this.$.addEventListener('blur', function() { // Notify the autocomplete view of a blur, it can decide what to do from there. view.publish('blur'); }); }, textToValue: function(text) { return text === "" ? [] : text.replace(/\s/g,'').split(','); }, valueToText: function(value) { return value ? value.toString() : ""; } } });