PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/views/pocket.js

https://bitbucket.org/Selvatico/warlock
JavaScript | 151 lines | 110 code | 10 blank | 31 comment | 15 complexity | f712f29bb3dca10f6b0170d4d27bd19b MD5 | raw file
  1. /**
  2. * View for user pocket
  3. */
  4. define([
  5. 'jquery',
  6. 'underscore',
  7. 'backbone',
  8. 'service/helper',
  9. 'service/mediator'
  10. ], function ($, _, Backbone, Helper, Mediator) {
  11. return Backbone.View.extend({
  12. tagName: 'div',
  13. // Cache the template function for a single item.
  14. template : Helper.getTemplate('#pocket-tpl'),
  15. infoTpl : Helper.getTemplate('#info-tpl', true),
  16. collection : null,
  17. // The DOM events specific to an item.
  18. events: {
  19. 'click .cardsWrap .deckItem' : 'toggleSelected',
  20. "mouseover .cardsWrap .deckList li": "switchSpellBtnOn",
  21. 'click .cardsWrap a.spell' : 'callCard'
  22. },
  23. setCollection: function (collection) {
  24. this.collection = collection;
  25. return this;
  26. },
  27. // The TodoView listens for changes to its model, re-rendering.
  28. initialize: function () {
  29. Mediator.on('switch-element', this.switchElements, this);
  30. Mediator.on('change-elements', this.setAvalaibleCard, this);
  31. },
  32. /**
  33. * Action on call button click in the pocket and switch between cards call click
  34. * @param event
  35. * @returns {boolean}
  36. */
  37. //TODO: check spel click
  38. callCard: function (event) {
  39. var _self = this,
  40. $target = $(event.target),
  41. cardData = $target.data("options"),
  42. $parentLi = $target.parent('li'),
  43. $otherCards = _self.$(".cards li").not(".active"),
  44. //second click on this card decline selection and remove green fields from the deck
  45. clickSpell = function clickSpell(event) {
  46. if (event) {
  47. event.stopPropagation();
  48. }
  49. $parentLi.off("click", ".spell", clickSpell).removeClass("active");
  50. $parentLi.find(".spell").html("Call");
  51. //say to deck holder to hide avaible slots
  52. Mediator.trigger('click-cast', false);
  53. },
  54. //if member click on spell button of another card. Reset this card and set as acive another card
  55. setToAll = function setToAll(){
  56. $otherCards.off("click", ".spell", setToAll);
  57. clickSpell();
  58. };
  59. if (cardData && cardData.n && cardData.e && this.collection.get(cardData.n)) {
  60. //if card type spell just trigger event to cast it
  61. if (cardData.type && 'spell' == cardData.type) {
  62. Mediator.trigger('cast-spell', cardData);
  63. return true;
  64. }
  65. $parentLi.on("click", ".spell", clickSpell).addClass("active");
  66. $parentLi.find(".spell").html("Cancel");
  67. $otherCards.on("click", ".spell", setToAll);
  68. //say to deck holder to show avaible places for call
  69. Mediator.trigger('click-cast', true, cardData);
  70. }
  71. return true;
  72. },
  73. /**
  74. * Switches displyed cards in the pocket only for one element
  75. * @param {String} element
  76. */
  77. switchElements: function (element) {
  78. this.$(".cardsWrap").find(".h1d").hide();
  79. this.$(".cardsWrap").find("#" + element +"Slider").show();
  80. },
  81. /**
  82. * switch selected cards in the pocket and show info tpl
  83. * @param {Object} event
  84. */
  85. toggleSelected: function(event) {
  86. this.$('.deckItem').removeClass('selected');
  87. this.$(event.currentTarget).toggleClass('selected');
  88. this.showInfoPanel(event);
  89. },
  90. /**
  91. * Show info panel at the right side of the pocket
  92. * @param event
  93. */
  94. showInfoPanel : function(event) {
  95. var name = $(event.currentTarget).data('real');
  96. this.$("#infoHolder").html(this.infoTpl(this.collection.get(name).toJSON()));
  97. },
  98. switchSpellBtnOn: function (event) {
  99. this.$('.cardsWrap a.spell').hide();
  100. this.$(event.currentTarget).find("a.spell").show();
  101. },
  102. addCardsToPocket: function(cardsList, CardView) {
  103. var cardView;
  104. if (cardsList) {
  105. for (var card in cardsList) {
  106. if (cardsList.hasOwnProperty(card)) {
  107. cardView = new CardView({card: cardsList[card]});
  108. this.$("#" + cardsList[card].element + "Slider .deckList").append(cardView.render().view.el);
  109. }
  110. }
  111. }
  112. //show cards
  113. this.$(".cardsWrap").find(".h1d").hide();
  114. this.$(".cardsWrap").find("#waterSlider").show();
  115. //activate carousel
  116. this.$('.cards').jCarouselLite({
  117. btnNext: ".next",
  118. btnPrev: ".prev",
  119. mouseWheel: true,
  120. circular: false,
  121. visible: 3
  122. });
  123. },
  124. setAvalaibleCard: function(resources) {
  125. var _self = this;
  126. $.each(this.$(".cards li"), function(index, element){
  127. var $card = _self.$(element),
  128. costs = $card.data("costs"),
  129. elem = $card.data("elem"),
  130. disabler = $card.find(".disabledCard");
  131. if (costs && elem) {
  132. if (costs <= resources[elem]) {
  133. disabler.hide();
  134. } else {
  135. disabler.show();
  136. }
  137. }
  138. });
  139. }
  140. });
  141. });