Skip to content

Commit

Permalink
Merge pull request GeoNode#55 from simod/hierarchical-tags
Browse files Browse the repository at this point in the history
make autocomplete to work with Hierarchical keywords
  • Loading branch information
jj0hns0n committed Dec 17, 2015
2 parents 28cb224 + 4ed2d90 commit da7bc6d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion geonode/base/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ class HierarchicalKeywordAdmin(TreeAdmin):
admin.site.register(HierarchicalKeyword, HierarchicalKeywordAdmin)

class ResourceBaseAdminForm(autocomplete_light.ModelForm):
keywords = TaggitField(widget=TaggitWidget('TagAutocomplete'), required=False)
keywords = TaggitField(widget=TaggitWidget('HierarchicalKeywordAutocomplete'), required=False)
4 changes: 2 additions & 2 deletions geonode/base/autocomplete_light_registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import autocomplete_light

from taggit.models import Tag
from models import ResourceBase, Region
from models import ResourceBase, Region, HierarchicalKeyword


autocomplete_light.register(Region,
Expand All @@ -12,7 +12,7 @@
search_fields=['title'],
autocomplete_js_attributes={'placeholder': 'Resource name..', },)

autocomplete_light.register(Tag,
autocomplete_light.register(HierarchicalKeyword,
search_fields=['name', 'slug'],
autocomplete_js_attributes={'placeholder':
'A space or comma-separated list of keywords', },)
2 changes: 1 addition & 1 deletion geonode/static/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"leaflet-fullscreen": "https://github.com/Leaflet/Leaflet.fullscreen.git",
"eonasdan-bootstrap-datetimepicker": "~3.1.3",
"bootstrap-treeview": "1.2.0",
"bower-tokenfield": "0.12.0"
"bootstrap-tokenfield": "0.12.0"
}
}
75 changes: 67 additions & 8 deletions geonode/static/geonode/js/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@
module.load_h_keywords = function($http, $rootScope, $location){
var params = typeof FILTER_TYPE == 'undefined' ? {} : {'type': FILTER_TYPE};
$http.get(H_KEYWORDS_ENDPOINT, {params: params}).success(function(data){
$('#treeview').treeview({
data: data,
levels: 1,
onNodeSelected: function($event, $data) {
console.log($data.text);
}
});
$('#treeview').treeview({
data: data,
levels: 1,
multiSelect: true,
onNodeSelected: function($event, $data) {
$rootScope.$broadcast('select_h_keyword', $data);
},
onNodeUnselected: function($event, $data){
$rootScope.$broadcast('unselect_h_keyword', $data);
}
});
});
}
};

module.load_regions = function ($http, $rootScope, $location){
var params = typeof FILTER_TYPE == 'undefined' ? {} : {'type': FILTER_TYPE};
Expand Down Expand Up @@ -300,6 +304,61 @@
}, true);
}

// Hyerarchical keywords listeners
$scope.$on('select_h_keyword', function($event, element){
var data_filter = 'keywords__slug__in';
var query_entry = [];
var value = element.text;
// If the query object has the record then grab it
if ($scope.query.hasOwnProperty(data_filter)){

// When in the location are passed two filters of the same
// type then they are put in an array otherwise is a single string
if ($scope.query[data_filter] instanceof Array){
query_entry = $scope.query[data_filter];
}else{
query_entry.push($scope.query[data_filter]);
}
}

// Add the entry in the correct query
if (query_entry.indexOf(value) == -1){
query_entry.push(value);
}

//save back the new query entry to the scope query
$scope.query[data_filter] = query_entry;

query_api($scope.query);
});

$scope.$on('unselect_h_keyword', function($event, element){
var data_filter = 'keywords__slug__in';
var query_entry = [];
var value = element.text;
// If the query object has the record then grab it
if ($scope.query.hasOwnProperty(data_filter)){

// When in the location are passed two filters of the same
// type then they are put in an array otherwise is a single string
if ($scope.query[data_filter] instanceof Array){
query_entry = $scope.query[data_filter];
}else{
query_entry.push($scope.query[data_filter]);
}
}

query_entry.splice(query_entry.indexOf(value), 1);

//save back the new query entry to the scope query
$scope.query[data_filter] = query_entry;

//if the entry is empty then delete the property from the query
if(query_entry.length == 0){
delete($scope.query[data_filter]);
}
query_api($scope.query);
});

/*
* Add the selection behavior to the element, it adds/removes the 'active' class
Expand Down
3 changes: 1 addition & 2 deletions geonode/templates/search/search_scripts.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% if DEBUG_STATIC %}

<script src="{{ STATIC_URL }}lib/js/bootstrap-datepicker.js?v={{ VERSION }}" type="text/javascript"></script>
<script src="{{ STATIC_URL }}lib/js/angular.js?v={{ VERSION }}"></script>
<script src="{{ STATIC_URL }}lib/js/angular-leaflet-directive.min.js?v={{ VERSION }}"></script>
<script src="{{ STATIC_URL }}lib/js/bootstrap-treeview.js?v={{ VERSION }}"></script>
<script src="{{ STATIC_URL }}lib/js/bootstrap-treeview.min.js?v={{ VERSION }}"></script>
{% endif %}

{% if include_spatial == 'true' %}
Expand Down

0 comments on commit da7bc6d

Please sign in to comment.