Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

146 rindas
3.0 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/workbench/template',
  5. 'css!ui/templates/workbench/styles'
  6. ], function (
  7. events,
  8. client,
  9. template,
  10. styles
  11. ) {
  12. return {
  13. tpl: template,
  14. centered: true,
  15. modal: true,
  16. workbenchId: null,
  17. recipes: null,
  18. currentRecipe: null,
  19. postRender: function () {
  20. this.onEvent('onOpenWorkbench', this.onOpenWorkbench.bind(this));
  21. this.onEvent('onCloseWorkbench', this.hide.bind(this));
  22. this.on('.btnCraft', 'click', this.craft.bind(this));
  23. this.on('.btnCancel', 'click', this.hide.bind(this));
  24. },
  25. onOpenWorkbench: function (msg) {
  26. this.workbenchId = msg.workbenchId;
  27. this.find('.heading-text').html(msg.name);
  28. this.renderRecipes(msg.recipes);
  29. this.show();
  30. },
  31. renderRecipes: function (recipes) {
  32. this.recipes = recipes;
  33. let container = this.find('.list').empty();
  34. recipes.forEach(function (r) {
  35. let el = $('<div class="item">' + r + '</div>')
  36. .appendTo(container);
  37. el.on('click', this.onSelectRecipe.bind(this, el, r));
  38. }, this);
  39. },
  40. onSelectRecipe: function (el, recipeName) {
  41. el.parent().find('.selected').removeClass('selected');
  42. el.addClass('selected');
  43. client.request({
  44. cpn: 'player',
  45. method: 'performAction',
  46. data: {
  47. targetId: this.workbenchId,
  48. cpn: 'workbench',
  49. method: 'getRecipe',
  50. data: {
  51. name: recipeName
  52. }
  53. },
  54. callback: this.onGetRecipe.bind(this)
  55. });
  56. },
  57. onGetRecipe: function (recipe) {
  58. this.currentRecipe = recipe;
  59. this.find('.title').html(recipe.item.name);
  60. this.find('.description').html(recipe.item.description);
  61. this.find('.materialList .material').remove();
  62. let container = this.find('.materialList')
  63. .css({
  64. visibility: 'visible'
  65. });
  66. let canCraft = true;
  67. recipe.materials.forEach(function (m) {
  68. let el = $('<div class="material">' + m.quantity + 'x ' + (m.nameLike || m.name) + '</div>')
  69. .appendTo(container);
  70. if (m.need) {
  71. canCraft = false;
  72. el.addClass('need');
  73. }
  74. });
  75. this.find('.btnCraft')
  76. .removeClass('disabled');
  77. if (!canCraft) {
  78. this.find('.btnCraft')
  79. .addClass('disabled');
  80. }
  81. },
  82. craft: function () {
  83. let selectedRecipe = this.find('.list .item.selected').html();
  84. client.request({
  85. cpn: 'player',
  86. method: 'performAction',
  87. data: {
  88. targetId: this.workbenchId,
  89. cpn: 'workbench',
  90. method: 'craft',
  91. data: {
  92. name: selectedRecipe
  93. }
  94. },
  95. callback: this.onCraft.bind(this)
  96. });
  97. },
  98. onCraft: function (recipe) {
  99. this.onGetRecipe(recipe);
  100. },
  101. onAfterShow: function () {
  102. this.clear();
  103. },
  104. clear: function () {
  105. this.find('.left .list .selected').removeClass('selected');
  106. this.find('.title').html('');
  107. this.find('.description').html('');
  108. this.find('.materialList .material').remove();
  109. this.find('.materialList')
  110. .css({
  111. visibility: 'hidden'
  112. });
  113. this.find('.btnCraft').addClass('disabled');
  114. }
  115. };
  116. });