You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

144 lines
2.9 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-title').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, name) {
  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: name
  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. this.clear();
  85. client.request({
  86. cpn: 'player',
  87. method: 'performAction',
  88. data: {
  89. targetId: this.workbenchId,
  90. cpn: 'workbench',
  91. method: 'craft',
  92. data: {
  93. name: selectedRecipe
  94. }
  95. }
  96. });
  97. },
  98. onAfterShow: function () {
  99. this.clear();
  100. },
  101. clear: function () {
  102. this.find('.left .list .selected').removeClass('selected');
  103. this.find('.title').html('');
  104. this.find('.description').html('');
  105. this.find('.materialList .material').remove();
  106. let container = this.find('.materialList')
  107. .css({
  108. visibility: 'hidden'
  109. });
  110. this.find('.btnCraft').addClass('disabled');
  111. }
  112. };
  113. });