From c1129894d66e4328a7aa8d0158c240c30f54126f Mon Sep 17 00:00:00 2001 From: Bryan Berry Date: Mon, 02 Nov 2009 21:13:41 +0000 Subject: refactored code so k.library is no longer used. k.images, k.sounds, k.surfaces are used instead of k.library.images, k.library.sounds, etc. Also, changed code to use the that=this metaphor more consistently --- diff --git a/examples/adding_up_to_10_canvas/js/lesson.js b/examples/adding_up_to_10_canvas/js/lesson.js index 852bd3f..ed5d365 100755 --- a/examples/adding_up_to_10_canvas/js/lesson.js +++ b/examples/adding_up_to_10_canvas/js/lesson.js @@ -62,7 +62,7 @@ k.main(function() { if ( startTimerY >= endTimerY ){ //you didn't answer in time - k.library.sounds["trigger"].play(); + k.sounds["trigger"].play(); answer(false, true); game(); } @@ -137,7 +137,7 @@ k.main(function() { }while ( flag === true ); pos.push( { "x":x, "y": y } ); - k.library.images[ imgId ].draw(surface, x, y ) + k.images[ imgId ].draw(surface, x, y ) } @@ -173,9 +173,9 @@ k.main(function() { playerCorrect = playerCorrect - 1; writeScore(); if (tooSlow === true) { - k.library.sounds[ "trigger" ].play(); + k.sounds[ "trigger" ].play(); } else { - k.library.sounds[ "incorrect" ].play(); + k.sounds[ "incorrect" ].play(); } //animate sad monkey animateChimp(false); @@ -184,7 +184,7 @@ k.main(function() { score = score + 1; playerCorrect = playerCorrect + 1; writeScore(); - k.library.sounds[ "correct" ].play(); + k.sounds[ "correct" ].play(); animateChimp(true); if (playerCorrect === 5){ level = (level+1)% imgNames.length; @@ -201,14 +201,14 @@ k.main(function() { var timerChimp; k.surfaces["chimp"].clear(); if( answer === true){ - k.library.images["happyChimp"].draw(k.surfaces["chimp"], 0, 0); + k.images["happyChimp"].draw(k.surfaces["chimp"], 0, 0); } else { - k.library.images["sadChimp"].draw(k.surfaces["chimp"], 0, 0); + k.images["sadChimp"].draw(k.surfaces["chimp"], 0, 0); } var restoreChimp = function () { k.surfaces["chimp"].clear(); - k.library.images["normalChimp"].draw(k.surfaces["chimp"], 0, 0); + k.images["normalChimp"].draw(k.surfaces["chimp"], 0, 0); }; timerChimp = setTimeout(restoreChimp, 800); @@ -288,7 +288,7 @@ k.main(function() { document.getElementById('reset'). addEventListener('click', reset, false); - k.library.images["normalChimp"].draw(k.surfaces["chimp"], 0, 0); + k.images["normalChimp"].draw(k.surfaces["chimp"], 0, 0); //end of Karma.main diff --git a/examples/adding_up_to_10_svg/js/lesson.js b/examples/adding_up_to_10_svg/js/lesson.js index ef878d3..eda09b4 100755 --- a/examples/adding_up_to_10_svg/js/lesson.js +++ b/examples/adding_up_to_10_svg/js/lesson.js @@ -139,7 +139,7 @@ k.main(function() { }while ( isOverlapping === true ); positions.push( { "x":x, "y": y } ); imageVarNames[varPrefix][i] = card.paper. - image(k.library.images[imageId].src, x , y, 35, 35); + image(k.images[imageId].src, x , y, 35, 35); card.set.push(imageVarNames[varPrefix][i]); } @@ -209,9 +209,9 @@ k.main(function() { numCorrectAnswers = numCorrectAnswers - 1; writeScore(score); if (tooSlow === true) { - k.library.sounds.trigger.play(); + k.sounds.trigger.play(); } else { - k.library.sounds.incorrect.play(); + k.sounds.incorrect.play(); } //animate sad monkey animateChimp(false); @@ -220,7 +220,7 @@ k.main(function() { score = score + 1; numCorrectAnswers = numCorrectAnswers + 1; writeScore(score); - k.library.sounds.correct.play(); + k.sounds.correct.play(); animateChimp(true); if (numCorrectAnswers == 5){ level = level + 1; @@ -316,7 +316,7 @@ k.main(function() { "width": 800, "height": 600, "z-index": 10}); $('#overlayPaper').css({"position": "absolute", "z-index": "100", "opacity": 1}); congratsChimp = overlayCard.paper.image( - k.library.images.happyChimp.src, 200, 100, 300, 400); + k.images.happyChimp.src, 200, 100, 300, 400); congratsChimp.attr({"fill-opacity": "1", "opacity": "1"}); congratsText = overlayCard.paper.text(400, 550, "Great Job!"); congratsText.attr({"font-size": 80}); @@ -347,11 +347,11 @@ k.main(function() { //Set up the monkeys chimpPaper = Raphael('chimpPaper', 120, 125); - normalChimpImage = chimpPaper.image(k.library.images.normalChimp.src, + normalChimpImage = chimpPaper.image(k.images.normalChimp.src, 0, 20, 100, 100); - sadChimpImage = chimpPaper.image(k.library.images.sadChimp.src, + sadChimpImage = chimpPaper.image(k.images.sadChimp.src, 0, 20, 100, 100); - happyChimpImage = chimpPaper.image(k.library.images.happyChimp.src, + happyChimpImage = chimpPaper.image(k.images.happyChimp.src, 0, 20, 100, 100); happyChimpImage.hide(); sadChimpImage.hide(); diff --git a/js/jquery.karma.js b/js/jquery.karma.js index 90e7811..d144592 100755 --- a/js/jquery.karma.js +++ b/js/jquery.karma.js @@ -31,8 +31,9 @@ /** * @fileOverview Contains karma library -* @version 0.1 -* @author Felipe Lopez Toledo +* @version 0.2 +* @authors Felipe Lopez Toledo , Bryan Berry +* */ @@ -100,12 +101,12 @@ Karma */ var Karma = function( options ) { var that = this; - this.version = "0.01"; + that.version = "0.01"; // //relative path to the po, images, sounds, etc. from the html //defined here: http://wiki.sugarlabs.org/go/Karma/Bundle_layout //localized is recalculated inside localizeContent ( $ = language.lang ) - this.paths = { + that.paths = { po: "po/", images: { localized: "assets/$/images/", @@ -120,7 +121,7 @@ var Karma = function( options ) { generic: "assets/generic/videos/" } }; - this.supportedLangFileTypes = [ + that.supportedLangFileTypes = [ { ext: "po", type: 'application/x-po' }, { ext: "json", type: 'application/json'} ]; @@ -181,7 +182,7 @@ var Karma = function( options ) { return gt.gettext(str1); } else { // nothing passed in; return blank string. - // XXX: we could error here, but that may cause more harm than good. + // XXX: we could error here, but this may cause more harm than good. return ''; } }); @@ -243,7 +244,7 @@ var Karma = function( options ) { domain : lang, file : { type: that.supportedLangFileTypes[i].type, - uri: this.url, data: data + uri: that.url, data: data } } );*/ @@ -276,7 +277,7 @@ var Karma = function( options ) { } }; // - this.library = { "images": [], "sounds": [], "videos":[], "shapes":[] }; + that.images = [], that.sounds = [], that.videos = [], that.shapes =[]; //initializes the defaultOptions argument //1 argument: string. assume it's the container @@ -291,34 +292,34 @@ var Karma = function( options ) { } $.extend( true, defaultOptions, options ); // - //copy defaultOptions to this, we use this.xyz instead this.defaultOptions.xyz + //copy defaultOptions to that, we use that.xyz instead that.defaultOptions.xyz for (var i in defaultOptions ) { - this[ i ] = defaultOptions[i]; + that[ i ] = defaultOptions[i]; } //initializes i18n //add the localized language to the language.alternatives - if ( typeof this.language.countryCode !== "undefined" ) { - this.language.alternatives.unshift( - this.language.langCode, - this.language.countryCode + if ( typeof that.language.countryCode !== "undefined" ) { + that.language.alternatives.unshift( + that.language.langCode, + that.language.countryCode ); } - if ( typeof this.language.lang !== "undefined" ) { - this.language.alternatives.unshift( this.language.lang ); + if ( typeof that.language.lang !== "undefined" ) { + that.language.alternatives.unshift( that.language.lang ); } //try to load the localized lang file (po or json or ...) - this.language.fileLoaded = loadAlternatives( ); + that.language.fileLoaded = loadAlternatives( ); //initializes the container - if ( typeof this.container === "string" ) { - this.container = $( this.container )[ 0 ]; - if ( !valid(this.container) ) delete this.container; + if ( typeof that.container === "string" ) { + that.container = $( that.container )[ 0 ]; + if ( !valid(that.container) ) delete that.container; } gk = { - "paths": this.paths + "paths": that.paths } - this.surfaces = {}; + that.surfaces = {}; }; /** @@ -389,7 +390,7 @@ Karma.prototype.math = { $.extend( Karma.prototype, Karma.prototype.graphics); // /** -@param {Object} [toLoad] The Object that has the arrays for preloading. +@param {Object} [toLoad] The Object this has the arrays for preloading. @param {Array} [toLoad.images] The images @param {Array} [toLoad.sounds] The sounds @param {Array} [toLoad.videos] The videos @@ -409,7 +410,9 @@ The callback function will be executed when the preloading finishes. @see Karma#init **/ Karma.prototype.main = function ( cb ) { - if ( valid( this.pendingToLoad ) ) { + var that = this; + + if ( valid( that.pendingToLoad ) ) { //loader var loaderDiv = $("body").append('
Karma is \ loading ...
'); @@ -419,21 +422,20 @@ Karma.prototype.main = function ( cb ) { statusDiv.html(current + "/" + total + (error > 0 ? " [ "+error+" ]":'')); }; - var that = this; var categories = ["images", "sounds", "videos" ]; var counters = { "loaded":0, "error": 0 }; var totalItems = 0; //creates the surfaces - if ( valid( this.pendingToLoad[ "surfaces" ] ) ) { - $.each (this.pendingToLoad[ "surfaces" ], function( key, config ){ + if ( valid( that.pendingToLoad[ "surfaces" ] ) ) { + $.each (that.pendingToLoad[ "surfaces" ], function( key, config ){ Karma.prototype.surface.call( that, config ); }); } statusUpdate( 0, 0, totalItems); //get the total items for ( var i=0; i < categories.length; i++ ) { - if ( valid ( this.pendingToLoad[ categories[ i ] ] ) ) { - totalItems += this.pendingToLoad[ categories[ i ] ].length; + if ( valid ( that.pendingToLoad[ categories[ i ] ] ) ) { + totalItems += that.pendingToLoad[ categories[ i ] ].length; } } @@ -454,27 +456,27 @@ Karma.prototype.main = function ( cb ) { throw ( "Media files not found: " + errors ); } $("#karma-loader:hiden:first").fadeOut("slow",function(){ - $(this).remove();}); + $(that).remove();}); if ( cb ) cb(); } }; for ( var i=0; i < categories.length; i++ ) { var category = categories[ i ]; - if ( valid ( this.pendingToLoad[ category ] ) ) { + if ( valid ( that.pendingToLoad[ category ] ) ) { //load all the category elements var type = category.substr( 0, category.length-1 ) - $.each (this.pendingToLoad[ category ], function( key, config ){ + $.each (that.pendingToLoad[ category ], function( key, config ){ var name = config.name; delete config.name; //register the elements into the library - that.library[ category ][ name ] = Karma.prototype[ type ]( + that[ category ][ name ] = Karma.prototype[ type ]( config ); - that.library[ category ][ name ].media.addEventListener( + that[ category ][ name ].media.addEventListener( "load",checkAllLoaded,false ); - that.library[ category ][ name ].media.addEventListener( + that[ category ][ name ].media.addEventListener( "error",checkAllLoaded,false ); }); @@ -743,11 +745,11 @@ var KSurface = Class( //events - this.canvas.addEventListener("contextmenu", function(ev){ + that.canvas.addEventListener("contextmenu", function(ev){ // },false ); - this.canvas.addEventListener("click", + that.canvas.addEventListener("click", handleEvents, false ); -- cgit v0.9.1