Permalink
Browse files

loading works yay

  • Loading branch information...
1 parent 1b137de commit d3d7566ac54d0984d38aa1f576140f13b4449248 @substack committed May 8, 2011
Showing with 68 additions and 37 deletions.
  1. +4 −2 server.js
  2. +64 −35 static/js/main.js
View
6 server.js
@@ -34,8 +34,10 @@ dnode({
else {
fs.readFile(
__dirname + '/songs/' + filename + '.json',
- function (data) {
- cb(null, JSON.parse(data));
+ 'utf8',
+ function (err, data) {
+ if (err) cb(err)
+ else cb(null, JSON.parse(data))
}
);
}
View
99 static/js/main.js
@@ -24,22 +24,25 @@ $(window).ready(function () {
has : function (i) {
return notes.notes[i].active;
},
- add : function (i, j, elems) {
+ add : function (i, j, target) {
var note = notes.notes[i];
+
+ $('<img>')
+ .attr('src', '/images/quarter_up.png')
+ .addClass('note')
+ .appendTo(target)
+ ;
+ $('<div>')
+ .addClass('note-label')
+ .text(i)
+ .click(function () {
+ note.trigger('toggle');
+ })
+ .appendTo(target)
+ ;
+
note.active = true;
- note.note = {
- 10 : 'C3',
- 9 : 'D3',
- 8 : 'E3',
- 7 : 'F3',
- 6 : 'G3',
- 5 : 'A4',
- 4 : 'B4',
- 3 : 'C4',
- 2 : 'D4',
- 1 : 'E4',
- 0 : 'F4',
- }[j];
+ note.note = toNote(j) || j;
note.beats = 0.1;
var tr = note.tr;
@@ -104,18 +107,30 @@ $(window).ready(function () {
}
});
},
+ load : function (data) {
+ Object.keys(notes.notes).forEach(function (i) {
+ notes.remove(i);
+ });
+
+ Object.keys(data).forEach(function (key, i) {
+ var cell = cells[i][fromNote(data[key].note)];
+ notes.add(i, data[key].note, cell);
+ });
+ },
};
+ var cells = {};
range(columns).forEach(function (i) {
var column = $('<div>')
.addClass('column')
.appendTo($('#treble'))
;
var tr = $('<tr>').appendTo($('#notes'));
notes.notes[i] = { tr : tr };
+ cells[i] = {};
range(12).forEach(function (j) {
- $('<div>')
+ cells[i][j] = $('<div>')
.addClass('cell')
.appendTo(column)
.mouseover(function () {
@@ -130,25 +145,7 @@ $(window).ready(function () {
.toggle(
function () {
if (notes.has(i)) return;
-
- var note = $('<img>')
- .attr('src', '/images/quarter_up.png')
- .addClass('note')
- .appendTo($(this))
- ;
- var label = $('<div>')
- .addClass('note-label')
- .text(i)
- .click(function () {
- note.trigger('toggle');
- })
- .appendTo($(this))
- ;
-
- notes.add(i, j, {
- note : note,
- label : label,
- });
+ notes.add(i, j, $(this));
},
function () {
notes.remove(i);
@@ -158,7 +155,6 @@ $(window).ready(function () {
.droppable({
accept : '.note',
drop : function (ev, ui) {
- console.log(i + ',' + j);
$(this).css('background-color', 'red');
},
})
@@ -198,5 +194,38 @@ $(window).ready(function () {
}
);
});
+
+ $('#load').submit(function (ev) {
+ ev.preventDefault();
+ remote.load($('#files').val(), function (err, data) {
+ if (err) alert(err)
+ else notes.load(data)
+ });
+ });
});
});
+
+var noteMap = {
+ 10 : 'C3',
+ 9 : 'D3',
+ 8 : 'E3',
+ 7 : 'F3',
+ 6 : 'G3',
+ 5 : 'A4',
+ 4 : 'B4',
+ 3 : 'C4',
+ 2 : 'D4',
+ 1 : 'E4',
+ 0 : 'F4',
+};
+
+function toNote (j) {
+ return noteMap[j];
+}
+
+function fromNote (n) {
+ var keys = Object.keys(noteMap);
+ for (var i = 0; i < keys.length; i++) {
+ if (noteMap[keys[i]] === n) return keys[i];
+ }
+}

0 comments on commit d3d7566

Please sign in to comment.