Skip to content

Commit

Permalink
#1411 Refactor auto complete tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Jul 7, 2020
1 parent a4daae0 commit e6f4bc6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
52 changes: 25 additions & 27 deletions frontend/app/scripts/services/api/TagSrv.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
(function() {
'use strict';
angular.module('theHiveServices')
.service('TagSrv', function(StatSrv, $q) {

var getPromiseFor = function(objectType) {
return StatSrv.getPromise({
objectType: objectType,
field: 'tags',
limit: 1000
});
};

var mapTags = function(collection, term) {
return _.map(_.filter(_.keys(collection), function(tag) {
var regex = new RegExp(term, 'gi');
return regex.test(tag);
}), function(tag) {
return {text: tag};
});
};
.service('TagSrv', function(QuerySrv, $q) {

var getTags = function(objectType, term) {
var defer = $q.defer();

getPromiseFor(objectType).then(function(response) {
defer.resolve(mapTags(response.data, term) || []);
});
var operations = [
{ _name: 'listTag' },
{ _name: objectType },
{
_name: 'filter',
_like: {
_field: 'text',
_value: '*' + term + '*'
}
},
{
_name: 'text'
}
];

// Get the list
QuerySrv.call('v0', operations)
.then(function(data) {
defer.resolve(_.map(data, function(tag) {
return {text: tag};
}));
});

return defer.promise;
};

this.fromCases = function(term) {
return getTags('case', term);
};

this.fromAlerts = function(term) {
return getTags('alert', term);
return getTags('fromCase', term);
};

this.fromObservables = function(term) {
return getTags('case/artifact', term);
return getTags('fromObservable', term);
};

});
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/directives/updatable-tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<span ng-show="updatable.updating">
<div class="form-group">
<tags-input class="ti-input-sm" min-length="2" ng-model="value" placeholder="Add labels" replace-spaces-with-dashes="false">
<auto-complete ng-if="source" min-length="1" debounceDelay="400" source="source($query)"></auto-complete>
<auto-complete ng-if="source" min-length="3" debounce-delay="400" source="source($query)"></auto-complete>
</tags-input>
</div>
<div class="form-group">
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/views/partials/case/case.creation.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h4 class="vpad10 text-primary">Case details</h4>

<input class="form-control input-sm" name="title" ng-if="!fromTemplate" ng-model="newCase.title" placeholder="Title" required type="text"/>

<div class="input-group" ng-if="fromTemplate">
<div class="input-group" ng-if="fromTemplate">
<span class="input-group-addon">{{template.titlePrefix}}</span>
<input class="form-control input-sm" name="title" ng-model="newCase.title" placeholder="Title" required type="text"/>
</div>
Expand Down Expand Up @@ -71,7 +71,7 @@ <h4 class="vpad10 text-primary">Case details</h4>
min-length="2"
ng-model="tags"
replace-spaces-with-dashes="false">
<auto-complete min-length="1" debounceDelay="400" source="getTags($query)"></auto-complete>
<auto-complete min-length="3" debounce-delay="400" source="getTags($query)"></auto-complete>
</tags-input>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/partials/case/case.details.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h4 class="vpad10 text-primary">Summary</h4>
</dd>
</dl>

<dl class="dl-horizontal clear">
<dl class="dl-horizontal">
<dt class="pull-left">Tags</dt>
<dd ng-if="canEdit">
<updatable-tags on-update="updateField('tags', getTags(newValue))" value="caze.tags" source="getCaseTags"></updatable-tags>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/partials/observables/creation/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<div class="col-md-9">
<input type="hidden" name="tags" ng-model="params.tagNames" ng-required="!params.message.length"/>
<tags-input name="tagsInput" ng-model="params.tags" class="ti-input-sm" placeholder="Add tags" replace-spaces-with-dashes="false" min-length="2">
<auto-complete min-length="1" debounceDelay="400" source="getTags($query)"></auto-complete>
<auto-complete min-length="3" debounce-delay="400" source="getTags($query)"></auto-complete>
</tags-input>
<p class="help-block" ng-show="observableForm.tags.$invalid && !observableForm.tagsInput.$pristine">The observable(s) description or tags are required.</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h3 class="modal-title">Update observable(s)</h3>
<tags-input name="addTagsInput" ng-model="$dialog.params.addTags" class="ti-input-sm"
placeholder="Add tags" replace-spaces-with-dashes="false" min-length="2"
on-tag-added="$dialog.toggleAddTags()">
<auto-complete min-length="1" debounceDelay="400" source="$dialog.getTags($query)"></auto-complete>
<auto-complete min-length="3" debounce-delay="400" source="$dialog.getTags($query)"></auto-complete>
</tags-input>
</div>
</div>
Expand All @@ -60,7 +60,7 @@ <h3 class="modal-title">Update observable(s)</h3>
<tags-input name="removeTagsInput" ng-model="$dialog.params.removeTags" class="ti-input-sm"
placeholder="Remove tags" replace-spaces-with-dashes="false" min-length="2"
on-tag-added="$dialog.toggleRemoveTags()">
<auto-complete min-length="1" debounceDelay="400" source="$dialog.getTags($query)"></auto-complete>
<auto-complete min-length="3" debounce-delay="400" source="$dialog.getTags($query)"></auto-complete>
</tags-input>
</div>
</div>
Expand Down

0 comments on commit e6f4bc6

Please sign in to comment.