Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Berry <bryan@olenepal.org>2009-12-15 04:19:15 (GMT)
committer Bryan Berry <bryan@olenepal.org>2009-12-15 04:19:15 (GMT)
commit0980b66980f00543ae31f135555b84843686d0b7 (patch)
tree69a0563d9ecbfc22026ff6d0785d41b397e80444
parent94be0c7e16a9bf2282529d890dde7eaa4ed07383 (diff)
cleaned up methods for creating collections
-rwxr-xr-xjs/karma.js233
-rwxr-xr-xtests/js/tests.js100
2 files changed, 201 insertions, 132 deletions
diff --git a/js/karma.js b/js/karma.js
index 94cfc6a..c8c0623 100755
--- a/js/karma.js
+++ b/js/karma.js
@@ -329,23 +329,23 @@ Karma.karma = {
break;
case "image":
options[option]._type = 'image';
- Karma._makeImageCollection(options[option]);
+ Karma._makeCollection(options[option], 'image');
break;
case "audio":
options[option]._type = 'audio';
- Karma._makeAudioCollection(options[option]);
+ Karma._makeCollection(options[option], 'audio');
break;
case "video":
options[option]._type = 'video';
- Karma._makeVideoCollection(options[option]);
+ Karma._makeCollection(options[option], 'video');
break;
case "svg":
options[option]._type = 'svg';
- Karma._makeSvgs(options[option]);
+ Karma._makeCollection(options[option], 'svg');
break;
case "canvas":
options[option]._type = 'canvas';
- Karma._makeCanvases(options[option]);
+ Karma._makeCollection(options[option], 'canvas');
break;
}
}
@@ -505,6 +505,60 @@ Karma.karma = {
};
+//Helper functions for creating assets
+
+Karma._isLocalized = function (boolLocalized) {
+ if (typeof boolLocalized === "boolean" ) {
+ if(boolLocalized === true &&
+ Karma.karma.locale === undefined){
+ throw new Error("You cannot localize a media asset" +
+ " if the global locale for Karma isn't set");
+ } else {
+ return boolLocalized;
+ }
+ } else if (typeof boolLocalized === undefined){
+ return false;
+ } else{
+ throw new Error("This is not a valid value for the localized option");
+ }
+};
+
+Karma._computeLocalePath = function(locale) {
+ return Karma.karma._assetPath + locale + "/";
+};
+
+
+Karma._makeCollection = function (configs, type){
+ var makeAsset = function (config){
+ var asset = undefined;
+ var target = undefined;
+ switch(type){
+ case "image":
+ target = Karma.kImage;
+ break;
+ case "audio":
+ target = Karma.kAudio;
+ break;
+ case "video":
+ target = Karma.kVideo;
+ break;
+ case "svg":
+ target = Karma.kSvg;
+ break;
+ case "canvas":
+ target = Karma.kCanvas;
+ break;
+ }
+
+ asset = Karma.create(target)._init(config);
+ Karma.karma[type][config.name] = asset;
+ };
+
+ configs.forEach(function(config){ makeAsset(config);});
+};
+
+//Prototype objects for assets
+
/** Prototypal object for images
* @class This object is the prototype for images
@@ -617,7 +671,7 @@ Karma.kAudio = {
*/
file : "",
/** media object
- * @type Image
+ * @type Audio
* @default undefined
*/
media : undefined,
@@ -697,82 +751,113 @@ Karma.kAudio = {
}, false);
+ },
+ play : function () {
+ this.media.play();
}
};
+/** NYI:Prototypal object for Video files
+ * @class This object is the prototype for video files
+ * @ throws {Error} if the individual audio asset is set to be localized but
+ * the globale locale is not set on the Karma.karma object
+ * @ throws {Error} if the name and file properties are not supplied
+ * @example
+ * kAudio is the prototype object for audio
+ * The audio assets are loaded in a distinctly different way
+ * from the canvas or svg assets. They also have distinctly different
+ * helper methods
+ *
+ * You initialize the kVideo assets by passing an array of objects
+ */
+Karma.kVideo = {
+ /** file location of asset
+ * @type String
+ * @default ""
+ */
+ file : "",
+ /** media object
+ * @type Video
+ * @default undefined
+ */
+ media : undefined,
+ //actual path to the file
+ _path : "",
+ //if using localized version of this asset
+ _localized : false,
+ _type : "video",
+ //initializes kVideo instance with values provided by user
+ _init : function (video) {
+ video._localized = video._localized || false;
+ Karma.karma._counters.total++;
-
-
-//determine if it is a valid type of asset
-Karma._isValidType = function (type){
- return type === "image" ||
- type === "svg" ||
- type === "audio" ||
- type === "video" ||
- type === "canvas";
-};
-
-Karma._isLocalized = function (boolLocalized) {
- if (typeof boolLocalized === "boolean" ) {
- if(boolLocalized === true &&
- Karma.karma.locale === undefined){
- throw new Error("You cannot localize a media asset" +
- " if the global locale for Karma isn't set");
+ if (video.name === undefined || video.file === undefined){
+ throw new Error("properties name and file have to be defined");
} else {
- return boolLocalized;
+ this.name = video.name;
+ this.file = video.file;
}
- } else if (typeof boolLocalized === undefined){
- return false;
- } else{
- throw new Error("This is not a valid value for the localized option");
- }
-};
-Karma._computeLocalePath = function(locale) {
- return Karma.karma._assetPath + locale + "/";
-};
+ this.media = new Video();
+
+ if(Karma._isLocalized(video._localized)){
+ this._localized = video._localized;
+ this._path = Karma.karma._localePath + "video/";
+ } else {
+ this._path = Karma.karma._assetPath + "video/";
+ }
-Karma._makeImageCollection = function (imgConfigs){
- var makeImage = function (imgConfig){
- var image = undefined;
- image = Karma.create(Karma.kImage)._init(imgConfig);
- Karma.karma.image[imgConfig.name] = image;
- };
-
- imgConfigs.forEach(function(imgConfig){ makeImage(imgConfig);});
-
-};
-Karma._makeAudioCollection = function (audioConfigs){
- var makeAudio = function (audioConfig){
- var audio = undefined;
- audio = Karma.create(Karma.kAudio)._init(audioConfig);
- audio.play = function () {
- //hack to fix the audio "stuttering" problem
- //more info: https://bugs.launchpad.net/karma/+bug/426108
- this.media.currentTime = 0.1;
- this.media.play();
- };
- Karma.karma.audio[audioConfig.name] = audio;
- };
-
- audioConfigs.forEach(function(audioConfig){ makeAudio(audioConfig);});
+ //IMPORTANT: This one magic line loads the file
+ this.media.src = this.src = this._path + this.file;
+
+ //add event handlers
+ this._addEventHandlers();
-};
+ return this;
+ },
+ //Adds event handlers to update the counters when
+ //the asset is successfully or unsuccessfully loaded
+ _addEventHandlers : function () {
+ var that = this;
+ //'canplaythrough' event is a Browser Hack recommended by chromium devs
+ //http://code.google.com/p/chromium/issues/detail?id=20251&q=loading%20audio&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS#c4
+ that.media.addEventListener(
+ "canplaythrough",
+ function (e) {
+ Karma.karma._counters.loaded++;
+ Karma.karma._updateStatus();
+ that.status = "loaded";}, false);
+
+ that.media.addEventListener(
+ "error",
+ function (e) {
+ Karma.karma._counters.errors++;
+ that.status = "error";
+ var errorMsg = "Error: " + that._type.toUpperCase() +
+ " " + that.name + " cannot be loaded.";
+ Karma.karma._updateStatus(errorMsg);
+ },
+ false);
+ that.media.addEventListener(
+ "abort",
+ function (e) {
+ Karma.karma._counters.total++;
+ that.status = "aborted";
+ var errorMsg = "ABORT: " + that._type.toUpperCase() +
+ " " + that.name + " loading was aborted.";
+ Karma.karma._updateStatus(errorMsg);
-Karma._makeCanvases = function (canvasConfigs){
- var makeCanvas = function (canvasConfig){
- var canvas = undefined;
- canvas = Karma.create(Karma.kCanvas)._init(canvasConfig);
- Karma.karma.canvas[canvasConfig.name] = canvas;
- };
-
- canvasConfigs.forEach(function(canvasConfig){ makeCanvas(canvasConfig);});
+ }, false);
+ }
+
};
+
+
/** Prototypal object for each canvas element submitted to Karma in the
* Karma() method
* @throws {Error} if the name and domId for the canvas element are not specified
@@ -912,18 +997,6 @@ Karma.kCanvas = {
};
-
-Karma._makeSvgs = function (svgConfigs){
- var makeSvg = function (svgConfig){
- var svg = undefined;
- svg = Karma.create(Karma.kSvg)._init(svgConfig);
- Karma.karma.svg[svgConfig.name] = svg;
- };
-
- svgConfigs.forEach(function(svgConfig){ makeSvg(svgConfig);});
-
-};
-
/** Prototypal object for each svg element submitted to Karma in the
* Karma() method
* @throws {Error} if the name and domId for the svg element are not specified
@@ -1067,7 +1140,3 @@ Karma.kSvg = {
}
};
-
-Karma._makeVideoCollection = function (video){
- throw new Error('NYI: Not Yet Implemented');
-};
diff --git a/tests/js/tests.js b/tests/js/tests.js
index dc9695a..c46e25e 100755
--- a/tests/js/tests.js
+++ b/tests/js/tests.js
@@ -467,7 +467,7 @@
});
- /* Karma._makeImageCollection tests
+ /* Karma._makeCollection for image tests
* good image added to k.image and total loaded incremented,
* and total assets incremented
*
@@ -479,19 +479,19 @@
* for n good images, w/ at least 1 localized, all n images added
*
*/
- asyncTest("Karma._makeImageCollection(image) w/ good images", 3,
+ asyncTest("Karma._makeCollection(collection, 'image') w/ good images", 3,
function(){
k.reset()._init();
- var imgConfigs = [
+ var configs = [
{name : "chimp", file:"happyMonkey.jpg"},
{name:"chili", file:"chili.png"},
{name:"plussign", file:"plussign.png"}
];
- Karma._makeImageCollection(imgConfigs);
+ Karma._makeCollection(configs, 'image');
setTimeout(
function(){
- ok(k.image.chili.name === imgConfigs[1].name,
+ ok(k.image.chili.name === configs[1].name,
"can access image by name");
ok(k._counters.loaded === 3,
"Counter of loaded assets was properly incremented");
@@ -504,16 +504,16 @@
}, 2000);
});
- asyncTest("Karma._makeImageCollection(image) w/ 2 good images and " +
+ asyncTest("Karma._makeCollection(collection, 'image') w/ 2 good images and " +
"1 bad one.", 4,
function(){
k.reset()._init();
- var imgConfigs = [
+ var configs = [
{name : "chimp", file:"happyMonkey.jpg"},
{name:"notthere", file:"notthere.png"},
{name:"chili", file:"chili.png"}
];
- Karma._makeImageCollection(imgConfigs);
+ Karma._makeCollection(configs, 'image');
setTimeout(
function(){
ok(k._counters.loaded === 2,
@@ -529,18 +529,18 @@
}, 2000);
});
- asyncTest("Karma._makeImageCollection(image) w/ 3 good imgs, 1 localized",
+ asyncTest("Karma._makeCollection(collection, 'image') w/ 3 good imgs, 1 localized",
function(){
expect(4);
k.reset()._init({locale: "es"});
- var imgConfigs = [
+ var configs = [
{name : "chimp", file:"happyMonkey.jpg",
_localized : true},
{name:"chili", file:"chili.png"},
{name:"plussign", file:"plussign.png"}
];
- Karma._makeImageCollection(imgConfigs);
+ Karma._makeCollection(configs, 'image');
setTimeout(
function(){
ok(k.image.chimp._path ===
@@ -561,7 +561,7 @@
- /* Karma._makeAudioCollection tests
+ /* Karma._makeCollection audio tests
* good sound added to k.audio and total loaded incremented,
* and total assets incremented
*
@@ -574,20 +574,20 @@
* for n good sounds, w/ at least 1 localized, all n sounds added
*
*/
- asyncTest("Karma._makeAudioCollection(audio) w/ good audio",
+ asyncTest("Karma._makeCollection(configs, 'audio') w/ good audio",
function(){
expect(5);
k.reset()._init();
- var soundConfigs = [
+ var configs = [
{name : "correct", file:"correct.ogg"},
{name:"incorrect", file:"incorrect.ogg"},
{name:"trigger", file:"trigger.ogg"}
];
- Karma._makeAudioCollection(soundConfigs);
+ Karma._makeCollection(configs, 'audio');
setTimeout(
function(){
- ok(k.audio.correct.name === soundConfigs[0].name,
+ ok(k.audio.correct.name === configs[0].name,
"can access sound by name");
ok(k.audio.correct.play,
"play() method is attached");
@@ -602,18 +602,18 @@
}, 2000);
});
- asyncTest("Karma._makeAudioCollection(audio) w/ 2 good sounds and " +
+ asyncTest("Karma._makeCollection(configs, 'audio') w/ 2 good sounds and " +
"1 bad one.",
function(){
expect(4);
k.reset()._init();
- var soundConfigs = [
+ var configs = [
{name : "correct", file: "correct.ogg"},
{name:"notthere", file: "notthere.ogg"},
{name:"trigger", file: "trigger.ogg"}
];
- Karma._makeAudioCollection(soundConfigs);
+ Karma._makeCollection(configs, 'audio');
setTimeout(
function(){
ok(k._counters.loaded === 2,
@@ -629,18 +629,18 @@
}, 2000);
});
- asyncTest("Karma._makeAudioCollection(audio) w/ 3 good sounds, 1 localized",
+ asyncTest("Karma._makeCollection(audio, 'audio') w/ 3 good sounds, 1 localized",
function(){
expect(4);
k.reset()._init({locale: "es"});
- var soundConfigs = [
+ var configs = [
{name : "correct", file:"correct.ogg",
_localized: true},
{name:"incorrect", file:"incorrect.ogg"},
{name:"trigger", file:"trigger.ogg"}
];
- Karma._makeAudioCollection(soundConfigs);
+ Karma._makeCollection(configs, 'audio');
setTimeout(
function(){
ok(k.audio.correct._path ===
@@ -658,7 +658,7 @@
- /* Karma._makeCanvases tests
+ /* Karma._makeCollection for Canvas tests
*
* throw error is domId not specified
*
@@ -679,27 +679,27 @@
*
*/
- test("Karma._makeCanvases",
+ test("Karma._makeCollection for Canvases",
function(){
expect(7);
- var canvas = [{name: "myCanvas"}];
+ var configs = [{name: "myCanvas"}];
ok(shouldError(
function(){
- Karma._makeCanvases(canvas);
+ Karma._makeCollection(configs, 'canvas');
}
), "throws error if domId not specified");
- canvas = [{name: "myCanvas", domId:"notThere"}];
+ configs = [{name: "myCanvas", domId:"notThere"}];
ok(shouldError(
function(){
- Karma._makeCanvases(canvas);
+ Karma._makeCollection(configs, "canvas");
}
), "throws error if domId not present in html");
- canvas = [{name: "myCanvas", domId:"testCanvas"}];
+ configs = [{name: "myCanvas", domId:"testCanvas"}];
ok(shouldNotError(
function(){
- Karma._makeCanvases(canvas);
+ Karma._makeCollection(configs, 'canvas');
}
), "accepts valid canvas options");
@@ -710,11 +710,11 @@
ok(k.canvas.myCanvas.height === 200,
"height set the dom value");
- canvas = [{name: "badCanvas", domId:"badCanvas",
+ configs = [{name: "badCanvas", domId:"badCanvas",
width: 100}];
ok(shouldError(
function(){
- Karma._makeCanvases(canvas);
+ Karma._makeCollection(configs, 'canvas');
}
),
"Throws error if only width or height but not both" +
@@ -722,7 +722,7 @@
});
- /* Karma._makeSvgs tests
+ /* Karma._makeCollection for SVG tests
*
* throw error is domId not specified
*
@@ -748,37 +748,37 @@
* properly sets doc element for svg
*
*/
- test("Karma._makeSvgs",
+ test("Karma._makeCollection for SVGs",
function(){
expect(5);
- var svg = [{name: "mySvg"}];
+ var configs = [{name: "mySvg"}];
ok(shouldError(
function(){
- Karma._makeSvgs(svg);
+ Karma._makeCollection(configs, 'svg');
}
), "throws error if domId not specified");
- svg = [{name: "mySvg", domId:"notThere"}];
+ configs = [{name: "mySvg", domId:"notThere"}];
ok(shouldError(
function(){
- Karma._makeSvgs(svg);
+ Karma._makeCollection(configs, 'svg');
}
), "throws error if domId not present in html");
- svg = [{name: "mySvg", domId:"testSvg"}];
+ configs = [{name: "mySvg", domId:"testSvg"}];
ok(shouldNotError(
function(){
- Karma._makeSvgs(svg);
+ Karma._makeCollection(configs, 'svg');
}
), "accepts valid svg options");
ok(k.svg.mySvg, "Valid svg accessible by name");
- svg = [{name: "badSvg", domId:"badSvg",
+ configs = [{name: "badSvg", domId:"badSvg",
width: 100}];
ok(shouldError(
function(){
- Karma._makeSvgs(svg);
+ Karma._makeCollection(configs, 'svg');
}
),
"Throws error if only width or height but not both" +
@@ -787,12 +787,12 @@
});
- asyncTest("Karma._makeSvgs good svg loads",
+ asyncTest("Karma._makeCollection good svg loads",
function(){
expect(4);
k.reset()._init();
- var svg = [{name: "testSvg", domId:"testSvg"}];
- Karma._makeSvgs(svg);
+ var configs = [{name: "testSvg", domId:"testSvg"}];
+ Karma._makeCollection(configs, 'svg');
setTimeout(
function(){
ok(k.svg.testSvg, "svg exists");
@@ -804,13 +804,13 @@
}, 100);
});
- asyncTest("Karma._makeSvgs good localized svg loads",
+ asyncTest("Karma._makeCollection good localized svg loads",
function(){
expect(3);
k.reset()._init();
- var svg = [{name: "testSvg", domId:"testSvg",
+ var configs = [{name: "testSvg", domId:"testSvg",
_localized : true}];
- Karma._makeSvgs(svg);
+ Karma._makeCollection(configs, 'svg');
setTimeout(
function(){
ok(k._counters.loaded === 1, "loaded counter incremented " +
@@ -875,8 +875,8 @@
"sets strokeStyle correctly",
function(){
expect(3);
- var canvas = [{name: "myCanvas", domId:"testCanvas"}];
- Karma._makeCanvases(canvas);
+ var configs = [{name: "myCanvas", domId:"testCanvas"}];
+ Karma._makeCollection(configs, 'canvas');
k.canvas.myCanvas.strokeStyle('#ffffff');
ok( k.canvas.myCanvas.ctx.strokeStyle ===
'#ffffff', 'Stroke style properly set');