Permalink
Browse files

finally works reliably

  • Loading branch information...
substack committed Aug 5, 2012
1 parent 71b4ff2 commit aae86cdf6e1a335cf0e8f41a05d94cffc29a3e7e
Showing with 66 additions and 43 deletions.
  1. +35 −0 example/vote/main.js
  2. +6 −0 example/vote/static/index.html
  3. +25 −43 index.js
View
@@ -0,0 +1,35 @@
+var sorta = require('../../');
+var $ = require('jquery-browserify');
+
+var s = sorta(function (row) {
+ var name = $('<span>').attr('class', 'name').text(row.key);
+ var rank = $('<span>').attr('class', 'rank');
+ var score = $('<span>').attr('class', 'score');
+
+ var up = $('<input>')
+ .attr('type', 'button')
+ .val('+')
+ .click(function () { row.update(row.value + 1) })
+ ;
+
+ var down = $('<input>')
+ .attr('type', 'button')
+ .val('-')
+ .click(function () { row.update(Math.max(0, row.value - 1)) })
+ ;
+
+ row.on('update', function () {
+ rank.text(row.index + 1);
+ score.text(row.value);
+ });
+
+ return $('<div>').append(
+ '#', rank, ' ', name, ', ', score, ' points', up, down
+ )[0];
+});
+s.appendTo(document.body);
+
+s.write({ key : 'robots', value : 0 });
+s.write({ key : 'dinosaurs', value : 0 });
+s.write({ key : 'insects', value : 0 });
+s.write({ key : 'electromagnets', value : 0 });
@@ -0,0 +1,6 @@
+<html>
+<body>
+<h1>vote for the best thing</h1>
+<script src="bundle.js"></script>
+</body>
+</html>
View
@@ -10,25 +10,14 @@ var sorta = module.exports = function (opts, cb) {
}
};
-sorta.ascend = sorta;
-
-sorta.descend = function (cb) {
- return sorta({
- compare : function (a, b) { return a - b }
- }, cb);
-};
-
function Sorta (opts, createElement) {
Stream.call(this);
if (!opts) opts = {};
- this.compare = opts.compare || function (a, b) { return b - a };
this.writable = true;
this.element = document.createElement('div');
this.rows = {};
- this.sorted = [];
-
this._createElement = createElement;
}
@@ -40,55 +29,48 @@ Sorta.prototype.appendTo = function (target) {
Sorta.prototype.write = function (row) {
var self = this;
+ var rows = self.rows;
if (typeof row !== 'object') {
- this.emit('error', new Error('non-object parameter to write: ' + row));
- }
-
- for (var i = 0; i < this.sorted.length; i++) {
- var c = this.compare(this.sorted[i].value, row.value);
- if (c > 0) break;
- }
-
- var r = this.rows[row.key];
- if (r && r.index === i) {
- if (r.value !== row.value) {
- r.value = row.value;
- r.emit('update', r);
- }
- return;
+ self.emit('error', new Error('non-object parameter to write: ' + row));
}
+ var r = rows[row.key];
if (!r) {
- r = this.rows[row.key] = new EventEmitter;
+ r = rows[row.key] = new EventEmitter;
r.key = row.key;
r.value = row.value;
- r.index = i;
- r.element = this._createElement(r);
+ r.element = self._createElement(r);
+ r.element.dataset.key = row.key;
r.update = function (v) {
self.write({ key : r.key, value : v });
};
- this.element.appendChild(r.element);
}
else {
- this.element.removeChild(r.element);
- this.sorted.splice(r.index, 1);
+ self.element.removeChild(r.element);
}
- this.sorted.splice(i, 0, r);
- r.index = i;
- r.value = row.value;
- if (i === this.sorted.length - 1) {
- this.element.appendChild(r.element);
+ var nodes = self.element.childNodes;
+ for (var i = 0; i < nodes.length; i++) {
+ var key = nodes[i].dataset.key;
+ if (row.value > rows[key].value) {
+ self.element.insertBefore(r.element, nodes[i]);
+ break;
+ }
}
- else {
- this.element.insertBefore(r.element, this.sorted[i+1].element);
+ if (i === nodes.length) {
+ self.element.appendChild(r.element);
}
+ r.value = row.value;
- for (; i < this.sorted.length; i++) {
- this.sorted[i].index = i;
- this.sorted[i].emit('update', this.sorted[i]);
- this.emit('update', this.sorted[i]);
+ nodes = self.element.childNodes;
+ for (var j = 0; j < nodes.length; j++) {
+ var key = nodes[j].dataset.key;
+ if (rows[key].index !== j || key === row.key) {
+ rows[key].index = j;
+ rows[key].emit('update');
+ self.emit('update', rows[key]);
+ }
}
};

0 comments on commit aae86cd

Please sign in to comment.