characterSheet.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. if(MIDAS == undefined) {
  2. var MIDAS = {};
  3. }
  4. MIDAS.CharacterSheet = function() {
  5. var history = {};
  6. var keymap = {
  7. "n" : "characterName"
  8. ,"p" : "playerName"
  9. ,"cl" : "classAndLevel"
  10. ,"r" : "race"
  11. ,"a" : "alignment"
  12. ,"de" : "deity"
  13. ,"sz" : "size"
  14. ,"ag" : "age"
  15. ,"gd" : "gender"
  16. ,"ht" : "height"
  17. ,"wt" : "weight"
  18. ,"ey" : "eyes"
  19. ,"ha" : "hair"
  20. ,"sk" : "skin"
  21. ,"s" : "strengthScore"
  22. ,"sm" : "strengthModifier"
  23. ,"tss" : "tempStrengthScore"
  24. ,"tsm" : "tempStrengthModifier"
  25. ,"d" : "dexterityScore"
  26. ,"dm" : "dexterityModifier"
  27. ,"tds" : "tempDexterityScore"
  28. ,"tdm" : "tempDexterityModifier"
  29. ,"c" : "constitutionScore"
  30. ,"cm" : "constitutionModifier"
  31. ,"tcs" : "tempConstitutionScore"
  32. ,"tcm" : "tempConstitutionModifier"
  33. ,"i" : "intelligenceScore"
  34. ,"im" : "intelligenceModifier"
  35. ,"tis" : "tempIntelligenceScore"
  36. ,"tim" : "tempIntelligenceModifier"
  37. ,"w" : "wisdomScore"
  38. ,"wm" : "wisdomModifier"
  39. ,"tws" : "tempWisdomScore"
  40. ,"twm" : "tempWisdomModifier"
  41. ,"ca" : "charismaScore"
  42. ,"cam" : "charismaModifier"
  43. ,"tcas" : "tempCharismaScore"
  44. ,"tcam" : "tempCharismaModifier"
  45. };
  46. this.load = function() {
  47. if(window.location.hash) {
  48. var dataToLoad = parseJQueryParams(window.location.hash.substring(1));
  49. $.each(keymap, function(key, value) {
  50. if(dataToLoad[key]) {
  51. var decodedValue = decodeURIComponent(dataToLoad[key]);
  52. $('#' + value).val(decodedValue);
  53. history[key] = decodedValue;
  54. console.log(value + ": \"" + decodedValue + "\"");
  55. }
  56. });
  57. }
  58. };
  59. this.save = function() {
  60. var dataToSave = {};
  61. $.each(keymap, function(key, value) {
  62. var valueToSave = $('#' + value).val();
  63. dataToSave[key] = encodeURIComponent(valueToSave);
  64. });
  65. var queryString = $.param(dataToSave);
  66. window.location.hash = queryString;
  67. };
  68. this.updateText = function(object) {
  69. var objectId = object.id;
  70. var key = getKey(objectId);
  71. var newValue = object.value;
  72. if(newValue != history[key]) {
  73. console.log(objectId + " changed from \"" + history[key] + "\" to \"" + newValue + "\".");
  74. history[key] = newValue;
  75. this.save();
  76. }
  77. };
  78. function getKey(objectId) {
  79. var keyResponse = null;
  80. $.each(keymap, function(key, value) {
  81. if(value == objectId) {
  82. keyResponse = key;
  83. return false;
  84. }
  85. });
  86. return keyResponse;
  87. }
  88. function parseJQueryParams(p) {
  89. var params = {};
  90. var pairs = p.split('&');
  91. for (var i=0; i<pairs.length; i++) {
  92. var pair = pairs[i].split('=');
  93. var accessors = [];
  94. var name = decodeURIComponent(pair[0]), value = decodeURIComponent(pair[1]);
  95. var name = name.replace(/\[([^\]]*)\]/g, function(k, acc) { accessors.push(acc); return ""; });
  96. accessors.unshift(name);
  97. var o = params;
  98. for (var j=0; j<accessors.length-1; j++) {
  99. var acc = accessors[j];
  100. var nextAcc = accessors[j+1];
  101. if (!o[acc]) {
  102. if ((nextAcc == "") || (/^[0-9]+$/.test(nextAcc)))
  103. o[acc] = [];
  104. else
  105. o[acc] = {};
  106. }
  107. o = o[acc];
  108. }
  109. acc = accessors[accessors.length-1];
  110. if (acc == "")
  111. o.push(value);
  112. else
  113. o[acc] = value;
  114. }
  115. return params;
  116. }
  117. };