Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Local music player app
window.addEvent('domready', function() {
//
// augment omni grid to allow finding the next song
//
omniGrid.implement({
findNext: function(randompos) {
if (randompos) {
ci = Math.floor(Math.random() * this.elements.length);
} else {
ci = this.getSelectedIndices()[0] + 1;
}
if (ci >= this.elements.length) ci = 0;
for (; true; ci++) {
if (!this.isHidden(ci)) {
return [ci];
}
}
},
selectNextVisible: function(i) {
var nextIndex = this.findNext(i);
this.setSelectedIndices(nextIndex);
this.elements[nextIndex].scrollIntoView(true);
}
});
//Create our Player
var Player = {
shouldShuffle: true,
shouldRepeat: false,
grid: {},
element: null,
listener: function(e) {
if (e.key === 'n') {
this.grid.selectNextVisible(this.shouldShuffle);
} else if (e.key === '/') {
$('mysearcher').focus();
}
},
init: function(data) {
function createGrid(omniObject, that) {
var grid = new omniGrid('mygrid', {
columnModel: [
{
header: 'Title',
dataIndex: 'title',
dataType: 'string',
width: 300
},
{
header: 'Artist',
dataIndex: 'artist',
dataType: 'string',
width: 300
},
{
header: 'url',
dataIndex: 'url',
dataType: '',
hidden: true
}
],
page: 1,
pagination: false,
serverSort: false,
alternaterows: true,
showHeader: true,
sortHeader: true,
resizeColumns: false,
multipleSelection: false,
width: 619,
height: 600
});
grid.setData(omniObject);
grid.addEvent('click', function(evt) {
that.element.setAttribute('src', evt.target.getDataByRow(evt.row).url);
that.element.play();
});
return grid;
}
function createDom(that) {
var searcher = (function() {
var timer;
function makeSearch() {
var a = $('mysearcher').get('value');
if (a === '') {
that.grid.clearFilter();
} else {
that.grid.filter(a);
}
}
return function() {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(makeSearch, 1000);
};
})();
var handleKeys = function(e) {
that.listener.call(that, e);
};
var search_field = new Element('div').adopt(new Element('input', {
id: 'mysearcher',
events: {
focus: function() {
window.removeEvent('keyup', handleKeys);
window.addEvent('keyup', searcher);
},
blur: function() {
window.removeEvent('keyup', searcher);
window.addEvent('keyup', handleKeys);
}
}
})).adopt(new Element('span', {
text: 'Clear',
styles: {
cursor: 'pointer'
},
events: {
'click': function() {
$('mysearcher').set('value', '');
that.grid.clearFilter();
}
}
}));
$$('body')[0].adopt(that.element).adopt(search_field).adopt(new Element('div', {
id: 'mygrid'
}));
window.addEvent('keyup', handleKeys);
}
this.element = (function() {
var a = document.createElement('audio');
a.setAttribute('controls', true);
a.addEventListener('ended', (function(coo) {
return function(ev) {
coo.grid.selectNextVisible(coo.shouldShuffle);
};
})(this), false);
//Alternatively bind the function, but binding seems like overkill
/*a.addEventListener('ended', (function(bind) {
return function() {
(function() {
this.grid.selectNextVisible(this.shouldShuffle);
}).call(bind);
};
})(this), false);*/
return a;
}).call(this);
createDom(this);
this.grid = createGrid(data, this);
}
};
// Helper functions
var getValueFromNode = function(node) {
if (node.childNodes[0]) {
return node.childNodes[0].nodeValue;
}
};
var isSong = function(node) {
return (node.getAttribute('type') === 'song');
};
var contentReceiver = function(text, xmlDoc) {
var omniObject = {
page: 1,
total: 1,
data: []
};
var collection = xmlDoc.getElementsByTagName('entry');
var tObj;
//
// compose object to feed to omnigrid
//
for (var i = 0; i < collection.length; i++) {
if (isSong(collection[i])) {
omniObject.data.push({
title: getValueFromNode(collection[i].getElementsByTagName('title')[0]),
artist: getValueFromNode(collection[i].getElementsByTagName('artist')[0]),
url: getValueFromNode(collection[i].getElementsByTagName('location')[0]).replace('file:///home/peterj/', '')
});
}
}
return omniObject;
};
new Request({
url: 'rhythmdb.xml',
method: 'get',
onSuccess: function(res, rex) {
Player.init(contentReceiver(res, rex));
}
}).send();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment