keeweb/app/scripts/views/fields/field-view-tags.js

105 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const FieldViewText = require('./field-view-text');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const FieldViewTags = FieldViewText.extend({
2015-10-17 23:49:24 +02:00
renderValue: function(value) {
return value ? _.escape(value.join(', ')) : '';
},
getEditValue: function(value) {
return value ? value.join(', ') : '';
},
2015-10-18 17:43:16 +02:00
valueToTags: function(val) {
2017-01-31 07:50:28 +01:00
const allTags = {};
2016-07-17 13:30:38 +02:00
this.model.tags.forEach(tag => {
2015-10-18 17:43:16 +02:00
allTags[tag.toLowerCase()] = tag;
});
2019-08-16 23:05:39 +02:00
return _.unique(
val
.split(/\s*[;,:]\s*/)
.filter(_.identity)
.map(tag => {
return allTags[tag.toLowerCase()] || tag;
})
);
2015-10-18 17:43:16 +02:00
},
2015-10-17 23:49:24 +02:00
endEdit: function(newVal, extra) {
if (newVal !== undefined) {
2015-10-18 17:43:16 +02:00
newVal = this.valueToTags(newVal);
}
if (this.tagsAutocomplete) {
this.tagsAutocomplete.remove();
this.tagsAutocomplete = null;
2015-10-17 23:49:24 +02:00
}
FieldViewText.prototype.endEdit.call(this, newVal, extra);
2015-10-18 17:43:16 +02:00
},
startEdit: function() {
FieldViewText.prototype.startEdit.call(this);
2017-01-31 07:50:28 +01:00
const fieldRect = this.input[0].getBoundingClientRect();
2016-02-28 12:16:05 +01:00
this.tagsAutocomplete = $('<div class="details__field-autocomplete"></div>').appendTo('body');
2015-10-18 17:43:16 +02:00
this.tagsAutocomplete.css({
top: fieldRect.bottom,
left: fieldRect.left,
width: fieldRect.width - 2
});
this.tagsAutocomplete.mousedown(this.tagsAutocompleteClick.bind(this));
this.setTags();
},
fieldValueInput: function(e) {
e.stopPropagation();
this.setTags();
FieldViewText.prototype.fieldValueInput.call(this, e);
},
getAvailableTags: function() {
2017-01-31 07:50:28 +01:00
const tags = this.valueToTags(this.input.val());
const last = tags[tags.length - 1];
const isLastPart = last && this.model.tags.indexOf(last) < 0;
2016-07-17 13:30:38 +02:00
return this.model.tags.filter(tag => {
2016-01-14 19:49:33 +01:00
return tags.indexOf(tag) < 0 && (!isLastPart || tag.toLowerCase().indexOf(last.toLowerCase()) >= 0);
2015-10-18 17:43:16 +02:00
});
},
setTags: function() {
2017-01-31 07:50:28 +01:00
const availableTags = this.getAvailableTags();
2019-08-16 23:05:39 +02:00
const tagsHtml = availableTags
.map(tag => {
return '<div class="details__field-autocomplete-item">' + _.escape(tag) + '</div>';
})
.join('');
2015-10-18 17:43:16 +02:00
this.tagsAutocomplete.html(tagsHtml);
2016-02-14 15:09:57 +01:00
this.tagsAutocomplete.toggle(!!tagsHtml);
2015-10-18 17:43:16 +02:00
},
tagsAutocompleteClick: function(e) {
e.stopPropagation();
2016-02-28 12:16:05 +01:00
if (e.target.classList.contains('details__field-autocomplete-item')) {
2017-01-31 07:50:28 +01:00
const selectedTag = $(e.target).text();
let newVal = this.input.val();
2016-01-16 13:35:34 +01:00
if (newVal) {
2017-01-31 07:50:28 +01:00
const tags = this.valueToTags(newVal);
const last = tags[tags.length - 1];
const isLastPart = last && this.model.tags.indexOf(last) < 0;
2016-01-16 13:35:34 +01:00
if (isLastPart) {
newVal = newVal.substr(0, newVal.lastIndexOf(last)) + selectedTag;
} else {
newVal += ', ' + selectedTag;
}
} else {
newVal = selectedTag;
}
this.input.val(newVal);
this.input.focus();
this.setTags();
2015-10-18 17:43:16 +02:00
}
2019-08-16 23:05:39 +02:00
this.afterPaint(function() {
this.input.focus();
});
2015-10-17 23:49:24 +02:00
}
});
module.exports = FieldViewTags;