123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- if(MIDAS == undefined) {
- var MIDAS = {};
- }
- MIDAS.CharacterSheet = function() {
- var history = {};
- var keymap = {
- "n" : "characterName"
- ,"p" : "playerName"
- ,"cl" : "classAndLevel"
- ,"r" : "race"
- ,"a" : "alignment"
- ,"de" : "deity"
- ,"sz" : "size"
- ,"ag" : "age"
- ,"gd" : "gender"
- ,"ht" : "height"
- ,"wt" : "weight"
- ,"ey" : "eyes"
- ,"ha" : "hair"
- ,"sk" : "skin"
- ,"s" : "strengthScore"
- ,"sm" : "strengthModifier"
- ,"tss" : "tempStrengthScore"
- ,"tsm" : "tempStrengthModifier"
- ,"d" : "dexterityScore"
- ,"dm" : "dexterityModifier"
- ,"tds" : "tempDexterityScore"
- ,"tdm" : "tempDexterityModifier"
- ,"c" : "constitutionScore"
- ,"cm" : "constitutionModifier"
- ,"tcs" : "tempConstitutionScore"
- ,"tcm" : "tempConstitutionModifier"
- ,"i" : "intelligenceScore"
- ,"im" : "intelligenceModifier"
- ,"tis" : "tempIntelligenceScore"
- ,"tim" : "tempIntelligenceModifier"
- ,"w" : "wisdomScore"
- ,"wm" : "wisdomModifier"
- ,"tws" : "tempWisdomScore"
- ,"twm" : "tempWisdomModifier"
- ,"ca" : "charismaScore"
- ,"cam" : "charismaModifier"
- ,"tcas" : "tempCharismaScore"
- ,"tcam" : "tempCharismaModifier"
- };
- this.load = function() {
- if(window.location.hash) {
- var dataToLoad = parseJQueryParams(window.location.hash.substring(1));
- $.each(keymap, function(key, value) {
- if(dataToLoad[key]) {
- var decodedValue = decodeURIComponent(dataToLoad[key]);
- $('#' + value).val(decodedValue);
- history[key] = decodedValue;
- console.log(value + ": \"" + decodedValue + "\"");
- }
- });
- }
- };
- this.save = function() {
- var dataToSave = {};
- $.each(keymap, function(key, value) {
- var valueToSave = $('#' + value).val();
- dataToSave[key] = encodeURIComponent(valueToSave);
- });
- var queryString = $.param(dataToSave);
- window.location.hash = queryString;
- };
-
- this.updateText = function(object) {
- var objectId = object.id;
- var key = getKey(objectId);
- var newValue = object.value;
- if(newValue != history[key]) {
- console.log(objectId + " changed from \"" + history[key] + "\" to \"" + newValue + "\".");
- history[key] = newValue;
- this.save();
- }
- };
-
- function getKey(objectId) {
- var keyResponse = null;
- $.each(keymap, function(key, value) {
- if(value == objectId) {
- keyResponse = key;
- return false;
- }
- });
- return keyResponse;
- }
-
- function parseJQueryParams(p) {
- var params = {};
- var pairs = p.split('&');
- for (var i=0; i<pairs.length; i++) {
- var pair = pairs[i].split('=');
- var accessors = [];
- var name = decodeURIComponent(pair[0]), value = decodeURIComponent(pair[1]);
- var name = name.replace(/\[([^\]]*)\]/g, function(k, acc) { accessors.push(acc); return ""; });
- accessors.unshift(name);
- var o = params;
- for (var j=0; j<accessors.length-1; j++) {
- var acc = accessors[j];
- var nextAcc = accessors[j+1];
- if (!o[acc]) {
- if ((nextAcc == "") || (/^[0-9]+$/.test(nextAcc)))
- o[acc] = [];
- else
- o[acc] = {};
- }
- o = o[acc];
- }
- acc = accessors[accessors.length-1];
- if (acc == "")
- o.push(value);
- else
- o[acc] = value;
- }
- return params;
- }
- };
|