Skip to content
Browse files

added host sorting

  • Loading branch information...
1 parent 5e55e14 commit 9c8dfd4e1f86919a14dc3b23832e2d2c2f687a23 @crahles crahles committed with i0rek Nov 13, 2011
Showing with 78 additions and 1 deletion.
  1. +1 −0 public/index.html
  2. +65 −0 public/js/jquery.sortElements.js
  3. +12 −1 public/js/main.js
View
1 public/index.html
@@ -8,6 +8,7 @@
<script src="js/jquery.getUrlParam.js"></script>
<script src="js/jquery.flot.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
+ <script src="js/jquery.sortElements.js"></script>
<script src="js/main.js"></script>
<script src="js/pimp.js"></script>
</head>
View
65 public/js/jquery.sortElements.js
@@ -0,0 +1,65 @@
+/**
+ * jQuery.fn.sortElements
+ * --------------
+ * @param Function comparator:
+ * Exactly the same behaviour as [1,2,3].sort(comparator)
+ *
+ * @param Function getSortable
+ * A function that should return the element that is
+ * to be sorted. The comparator will run on the
+ * current collection, but you may want the actual
+ * resulting sort to occur on a parent or another
+ * associated element.
+ *
+ * E.g. $('td').sortElements(comparator, function(){
+ * return this.parentNode;
+ * })
+ *
+ * The <td>'s parent (<tr>) will be sorted instead
+ * of the <td> itself.
+ */
+jQuery.fn.sortElements = (function(){
+
+ var sort = [].sort;
+
+ return function(comparator, getSortable) {
+
+ getSortable = getSortable || function(){return this;};
+
+ var placements = this.map(function(){
+
+ var sortElement = getSortable.call(this),
+ parentNode = sortElement.parentNode,
+
+ // Since the element itself will change position, we have
+ // to have some way of storing its original position in
+ // the DOM. The easiest way is to have a 'flag' node:
+ nextSibling = parentNode.insertBefore(
+ document.createTextNode(''),
+ sortElement.nextSibling
+ );
+
+ return function() {
+
+ if (parentNode === this) {
+ throw new Error(
+ "You can't sort elements if any one is a descendant of another."
+ );
+ }
+
+ // Insert before flag:
+ parentNode.insertBefore(this, nextSibling);
+ // Remove flag:
+ parentNode.removeChild(nextSibling);
+
+ };
+
+ });
+
+ return sort.call(this, comparator).each(function(i){
+ placements[i].call(getSortable.call(this));
+ });
+
+ };
+
+})();
View
13 public/js/main.js
@@ -12,7 +12,7 @@ var Healthety = function(){
var socket;
var colors = ["#0063ed", "#d44105", "#81e439", "#efb100", "#8f46f1",
- "#ce4176", "#52cffa", "#ffd528"];
+ "#ce4176", "#52cffa", "#ffd528"];
minime.draw = function(){
$('#wrapper').append('<div id="legend"></div>');
@@ -22,6 +22,7 @@ var Healthety = function(){
json = jQuery.parseJSON(data);
initChart(json);
});
+
setInterval(function(){
// clean up values
for(var name in lines){
@@ -56,6 +57,16 @@ var Healthety = function(){
);
}
+ $('#legend > span').sortElements(function(a, b){
+ return $(a).text() > $(b).text() ? 1 : -1;
+ });
+
+ $.each($('.header'), function(idx, elm){
+ $('.values > span', elm).sortElements(function(a, b){
+ return $(a).attr('class') > $(b).attr('class') ? 1 : -1;
+ })
+ });
+
}, 500);
};

0 comments on commit 9c8dfd4

Please sign in to comment.
Something went wrong with that request. Please try again.