Skip to content

Commit

Permalink
Hotfix #15240 tag sorting in hubs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Lewis committed Feb 14, 2018
1 parent 40731e7 commit da8a744
Show file tree
Hide file tree
Showing 6 changed files with 582 additions and 75 deletions.
74 changes: 1 addition & 73 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//= require modernizr.custom.15012
//= require jquery.sortChildren
//= require bootstrap-sprockets
//= require jquery-migrate-1.1.1
//= require_tree .
//
( function($){
Expand Down Expand Up @@ -170,79 +171,6 @@
}
});
},
observeTagCloudControls: function(){
var sort_tags_on_change = function(e){
var sort_by = $('#sort-tags-by').val();
var sort = $("#sort-tags-direction").val();
var mapping_function = function(elem) {
var tag_frequency = $(elem).find('.tag').data('tag-frequency'),
tag_text = $(elem).find('.tag').data('tag-name'),
attributes = {};

attributes.frequency = tag_frequency;
attributes.name = tag_text.toString();

return attributes;
};
var compare_function = function(a,b) {
if(sort_by == "frequency" && sort == "desc") {
return (b.value.frequency - a.value.frequency) || (b.value.frequency === a.value.frequency && a.value.name.localeCompare(b.value.name));
}
else if(sort_by == "frequency" && sort == "asc") {
return (a.value.frequency - b.value.frequency) || (b.value.frequency === a.value.frequency && a.value.name.localeCompare(b.value.name));
}
else if(sort_by == "alpha" && sort == "asc") {
return a.value.name.localeCompare(b.value.name);
}
else if(sort_by == "alpha" && sort == "desc") {
return b.value.name.localeCompare(a.value.name);
}
else {
return null;
}
};
$("#tag-cloud").detach(function() {
$(this).sortChildren(mapping_function, compare_function);
});
};
$('#sort-tags-by').change(sort_tags_on_change);
$('#sort-tags-direction').change(sort_tags_on_change);

$('#reset-filter').click(function(e){
$('#tag-cloud li').show();
$('#filter-by').val('');
});

var filterStuff = function(e){
if(e != ''){
e.preventDefault();
}
$('#tag-cloud li').show();
var filterVal = $('#filter-by').val();
var filterregex = new RegExp(filterVal,'i');
$('#tag-cloud li').each(function(){
if(! $(this).find('.tag').html().match(filterregex)){
$(this).hide();
}
});
};

$('#filter-button').click(filterStuff);
$('#filter-by').observe_field(1,filterStuff);

$('#tag_slider').slider({
value: 0,
min: 0,
max: 9,
step: 1,
slide: function(event, ui){
$('.tag').show();
for(var i = 1; i <= ui.value; i=i+1){
$('.s' + i).hide();
}
}
});
},
initEditor: function(textArea){
CKEDITOR.replace(textArea);
},
Expand Down
27 changes: 27 additions & 0 deletions app/assets/javascripts/hubs/tags-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* globals $ */
this.observeTagFilterControls = function () {
$('#reset-filter').click(function (e) {
$('#tag-cloud li').show()
$('#filter-by').val('')
})

var filterStuff = function (e) {
if (e !== '') {
e.preventDefault()
}

$('#tag-cloud li').show()

var filterVal = $('#filter-by').val()
var filterregex = new RegExp(filterVal, 'i')

$('#tag-cloud li').each(function () {
if (!$(this).find('.tag').html().match(filterregex)) {
$(this).hide()
}
})
}

$('#filter-button').click(filterStuff)
$('#filter-by').observe_field(1, filterStuff)
}
37 changes: 37 additions & 0 deletions app/assets/javascripts/hubs/tags-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* globals $ */
this.observeTagSortControls = function () {
var sortTagsOnChange = function (e) {
var sortBy = $('#sort-tags-by').val()
var sort = $('#sort-tags-direction').val()

var mappingFunction = function (elem) {
var attributes = {}

attributes.frequency = $(elem).find('.tag').data('tag-frequency')
attributes.name = $(elem).find('.tag').data('tag-name').toString()

return attributes
}

var compareFunction = function (a, b) {
if (sortBy === 'frequency' && sort === 'desc') {
return (b.value.frequency - a.value.frequency) || (b.value.frequency === a.value.frequency && a.value.name.localeCompare(b.value.name))
} else if (sortBy === 'frequency' && sort === 'asc') {
return (a.value.frequency - b.value.frequency) || (b.value.frequency === a.value.frequency && a.value.name.localeCompare(b.value.name))
} else if (sortBy === 'alpha' && sort === 'asc') {
return a.value.name.localeCompare(b.value.name)
} else if (sortBy === 'alpha' && sort === 'desc') {
return b.value.name.localeCompare(a.value.name)
} else {
return null
}
}

$('#tag-cloud').detach(function () {
$(this).sortChildren(mappingFunction, compareFunction)
})
}

$('#sort-tags-by').change(sortTagsOnChange)
$('#sort-tags-direction').change(sortTagsOnChange)
}
1 change: 0 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
= stylesheet_link_tag 'application', media: 'all'
= javascript_include_tag 'application'
= csrf_meta_tag
%script{ src: 'http://code.jquery.com/jquery-migrate-1.1.1.js' }
%link{ href: "#{root_url}humans.txt", rel: 'author' }
%link{ href: asset_path('favicon.png'), rel: 'icon', type: 'image/png' }
- unless @show_auto_discovery_params.blank?
Expand Down
7 changes: 6 additions & 1 deletion app/views/tags/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
= render partial: 'tags/graph_item', as: :tag, collection: @tags,
locals: { hub: @hub, hub_feed: @hub_feed, max_count: @tags.empty? ? 0 : @tags.first.count }
:javascript
$(document).ready(function(){$.observeTagCloudControls()});
$(document).ready(
function () {
observeTagFilterControls()
observeTagSortControls()
}
);
Loading

0 comments on commit da8a744

Please sign in to comment.