From 4604a412fd67101fef77d205a80d82ac07a53ee5 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 20 May 2010 21:16:35 -0400 Subject: [PATCH] show biology assets on item preview from pet state --- app/controllers/swf_assets_controller.rb | 2 +- public/javascripts/items/show.js | 72 +++++++++++++++++++----- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index 5bd8ccba..9c68677c 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -3,7 +3,7 @@ class SwfAssetsController < ApplicationController if params[:item_id] @swf_assets = Item.find(params[:item_id]).swf_assets.for_json.all elsif params[:pet_type_id] - @swf_assets = PetType.find(params[:pet_type_id]).swf_assets.for_json.all + @swf_assets = PetType.find(params[:pet_type_id]).pet_states.first.swf_assets.for_json.all end render :json => @swf_assets end diff --git a/public/javascripts/items/show.js b/public/javascripts/items/show.js index 54a3b3ae..2b978cd0 100644 --- a/public/javascripts/items/show.js +++ b/public/javascripts/items/show.js @@ -1,7 +1,14 @@ var PREVIEW_SWF_ID = 'item-preview-swf', PREVIEW_SWF = document.getElementById(PREVIEW_SWF_ID), IMPRESS_HOST = PREVIEW_SWF.getAttribute('data-impress-host'), - speciesList = $('#item-preview a'); + speciesList = $('#item-preview a'), + MainWardrobe; + +if(console === undefined || console.log === undefined) { + function log() {} +} else { + log = $.proxy(console, 'log'); +} function impressUrl(path) { return 'http://' + IMPRESS_HOST + path; @@ -10,9 +17,15 @@ function impressUrl(path) { function PetType() {} PetType.prototype.load = function () { - var url = '/species/' + this.species_id + '/color/' + this.color_id + '/pet_type.json'; + var url = '/species/' + this.species_id + '/color/' + this.color_id + '/pet_type.json', + pet_type = this; $.getJSON(url, function (data) { - console.log(data); + pet_type.id = data.id; + pet_type.body_id = data.body_id; + $.getJSON('/pet_types/' + data.id + '/swf_assets.json', function (assets) { + pet_type.assets = assets; + Preview.update(); + }); }); } @@ -31,16 +44,47 @@ PetType.create_from_link = function (link) { return pet_type; } -swfobject.embedSWF( - impressUrl('/assets/swf/preview.swf'), // URL - PREVIEW_SWF_ID, // ID - 400, // width - 400, // height - '9', // required version - impressUrl('/assets/js/swfobject/expressInstall.swf'), // express install URL - {'swf_assets_path': impressUrl('/assets')}, // flashvars - {'wmode': 'transparent', 'allowscriptaccess': 'always'} // params -) +Preview = new function Preview() { + var assets = [], swf_id, swf, updateWhenFlashReady = false; + + this.setFlashIsReady = function () { + log('flash ready'); + swf = document.getElementById(swf_id); + if(updateWhenFlashReady) this.update(); + } + + this.update = function (assets) { + var assets; + log('want to update'); + if(swf) { + log('got to update'); + log(PetType.current.assets); + assets = $.each(PetType.current.assets, function () { + this.local_path = this.local_url; + }); + log(assets); + swf.setAssets(assets); + } else { + updateWhenFlashReady = true; + } + } + + this.embed = function (id) { + swf_id = id; + swfobject.embedSWF( + impressUrl('/assets/swf/preview.swf'), // URL + id, // ID + 400, // width + 400, // height + '9', // required version + impressUrl('/assets/js/swfobject/expressInstall.swf'), // express install URL + {'swf_assets_path': impressUrl('/assets')}, // flashvars + {'wmode': 'transparent', 'allowscriptaccess': 'always'} // params + ); + } +} + +Preview.embed(PREVIEW_SWF_ID); PetType.create_from_link(speciesList.eq(Math.floor(Math.random()*speciesList.length))).setAsCurrent(); @@ -48,3 +92,5 @@ speciesList.click(function (e) { e.preventDefault(); PetType.create_from_link($(this)).setAsCurrent(); }); + +MainWardrobe = { View: { Outfit: Preview } };