Skip to content

Commit

Permalink
Store files as instanace objects
Browse files Browse the repository at this point in the history
With files encapsulated as objects, we can store them, sort them, and pass them
around. This is laying a foundation for handling playlists. (#10)

Also, we move the modification of the loading state outside of the skin
manager. This creates a better separation of concerns.
  • Loading branch information
captbaritone committed Jan 25, 2015
1 parent 4ac0ac0 commit c864300
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 55 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
</p>
<script src="js/jszip.2.4.0.min.js?inline"></script>
<script src="js/browser.js?inline"></script>
<script src="js/file-manager.js?inline"></script>
<script src="js/file.js?inline"></script>
<script src="js/visualizer.js?inline"></script>
<script src="js/media.js?inline"></script>
<script src="js/font.js?inline"></script>
Expand Down
11 changes: 7 additions & 4 deletions js/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Context = {
option: document.getElementById('option'),

init: function(winamp) {
this.winamp = winamp;
var self = this;

document.onclick = function() {
Expand All @@ -17,20 +18,22 @@ Context = {
var skinSelectNodes = document.getElementsByClassName('skin-select');
for(var i = 0; i < skinSelectNodes.length; i++) {
skinSelectNodes[i].onclick = function() {
winamp.setSkinByUrl(this.dataset.skinUrl);
var skinFile = new MyFile();
skinFile.setUrl(this.dataset.skinUrl);
self.winamp.setSkin(skinFile);
}
}

document.getElementById('context-play-file').onclick = function(event) {
self.option.classList.remove('selected');
winamp.openFileDialog();
self.winamp.openFileDialog();
};
document.getElementById('context-load-skin').onclick = function(event) {
self.option.classList.remove('selected');
winamp.openFileDialog();
self.winamp.openFileDialog();
};
document.getElementById('context-exit').onclick = function() {
winamp.close();
self.winamp.close();
};
}
}
29 changes: 0 additions & 29 deletions js/file-manager.js

This file was deleted.

43 changes: 43 additions & 0 deletions js/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Custom object representing a file
// `File` is already a builtin, so we use `MyFile`
MyFile = function(){
this.reader = new FileReader();
this.url = null;
this.fileReference = null;
this.setUrl = function(url){
this.url = url;
}
this.setFileReference = function(fileReference){
this.fileReference = fileReference;
}
this.processBuffer = function(bufferHandler) {
if(this.url) {
var oReq = new XMLHttpRequest();
oReq.open("GET", this.url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
bufferHandler(arrayBuffer);
};

oReq.send(null);
return

} else if(this.fileReference) {
this.reader.onload = function (e) {
var arrayBuffer = e.target.result;
bufferHandler(arrayBuffer);
};
this.reader.onerror = function (e) {
console.error(e);
};

this.reader.readAsArrayBuffer(this.fileReference);
return
}

console.error('Tried to process an unpopulated file object');
return false;
}
};
17 changes: 4 additions & 13 deletions js/skin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Dynamically set the css background images for all the sprites
SkinManager = {
fileManager: FileManager,
font: Font,
init: function(visualizerNode, analyser) {
this._createNewStyleNode();
Expand Down Expand Up @@ -46,14 +45,9 @@ SkinManager = {
},

// Given a file of an original Winamp WSZ file, set the current skin
setSkinByFileReference: function(fileReference) {
this.fileManager.bufferFromFileReference(fileReference, this._setSkinByBuffer.bind(this));
},

// Given the url of an original Winamp WSZ file, set the current skin
setSkinByUrl: function(url) {
Winamp.setLoadingState();
this.fileManager.bufferFromUrl(url, this._setSkinByBuffer.bind(this));
setSkinByFile: function(file, completedCallback) {
this.completedCallback = completedCallback;
file.processBuffer(this._setSkinByBuffer.bind(this));
},

// Given a bufferArray containing a Winamp WSZ file, set the current skin
Expand All @@ -80,12 +74,9 @@ SkinManager = {
}
}

// Clear the loading state
Winamp.unsetLoadingState();
this.styleNode.appendChild(document.createTextNode(cssRules));

this._parseVisColors(zip);

this.completedCallback();
},

_parseVisColors: function(zip) {
Expand Down
21 changes: 13 additions & 8 deletions js/winamp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// UI and App logic
Winamp = {
init: function(options) {
this.fileManager = FileManager;

this.fileInput = document.createElement('input');
this.fileInput.type = 'file';
this.fileInput.style.display = 'none';
Expand All @@ -27,7 +25,9 @@ Winamp = {
this.setVolume(options.volume);
this.setBalance(options.balance);
this.loadFromUrl(options.mediaFile.url, options.mediaFile.name);
this.setSkinByUrl(options.skinUrl);
var skinFile = new MyFile();
skinFile.setUrl(options.skinUrl);
this.setSkin(skinFile);

this._registerListeners();
return this;
Expand Down Expand Up @@ -184,23 +184,28 @@ Winamp = {
},

loadFromFileReference: function(fileReference) {
var file = new MyFile();
file.setFileReference(fileReference);
if(new RegExp("(wsz|zip)$", 'i').test(fileReference.name)) {
this.skin.setSkinByFileReference(fileReference);
this.skin.setSkinByFile(file);
} else {
this.media.autoPlay = true;
this.fileName = fileReference.name;
this.fileManager.bufferFromFileReference(fileReference, this._loadBuffer.bind(this));
file.processBuffer(this._loadBuffer.bind(this));
}
},

// Used only for the initial load, since it must have a CORS header
loadFromUrl: function(url, fileName) {
this.fileName = fileName;
this.fileManager.bufferFromUrl(url, this._loadBuffer.bind(this));
var file = new MyFile();
file.setUrl(url);
file.processBuffer(this._loadBuffer.bind(this));
},

setSkinByUrl: function(url) {
this.skin.setSkinByUrl(url);
setSkin: function(file) {
this.setLoadingState();
this.skin.setSkinByFile(file, this.unsetLoadingState.bind(this));
},

setLoadingState: function() {
Expand Down

0 comments on commit c864300

Please sign in to comment.