PageRenderTime 1673ms CodeModel.GetById 40ms RepoModel.GetById 3ms app.codeStats 0ms

/public/javascripts/show_customer_agenda.js

https://github.com/Sophrinix/padelotron
JavaScript | 177 lines | 168 code | 9 blank | 0 comment | 7 complexity | d275587e55a54d36b3df788a776e0da1 MD5 | raw file
  1. $(function(){
  2. GameEvent = Backbone.Model.extend({
  3. initalize: function(){
  4. _.bindAll(this,[]);
  5. }
  6. });
  7. GameEventsController = Backbone.Collection.extend({
  8. model: GameEvent,
  9. place_id: $("input[name='init_place_id']")[0].value,
  10. url: function(){
  11. return '/customers/agenda/games.json';
  12. },
  13. comparator: function(todo) {
  14. return todo.get('play_date');
  15. }
  16. });
  17. window.GameEvents = new GameEventsController();
  18. GameEventView = Backbone.View.extend({
  19. initialize: function() {
  20. this.model.bind('change', this.render, this);
  21. },
  22. render: function() {
  23. return this;
  24. }
  25. });
  26. ShowCustomerAgendaView= Backbone.View.extend({
  27. el: $("div#main_panel"),
  28. events: {
  29. },
  30. initialize: function() {
  31. _.bindAll(this,['initScheduler']);
  32. this.confirmGame= _.bind(this.confirmGame, this);
  33. this.rejectGame= _.bind(this.rejectGame, this);
  34. GameEvents.bind('add', this.addGameEvent, this);
  35. GameEvents.bind('reset', this.resetGameEvents, this);
  36. $("button#confirm_game_action").bind("click", this.confirmGame);
  37. $("button#reject_game_action").bind("click", this.rejectGame);
  38. $("div.dhx_cal_event div").live("click", this.setSelectedGame);
  39. $.when(
  40. $.ajax("/places/"+GameEvents.place_id+"/playgrounds.json"))
  41. .then(
  42. this.initScheduler);
  43. GameEvents.fetch({
  44. data: {
  45. place_id: GameEvents.place_id
  46. }
  47. });
  48. },
  49. setSelectedGame: function(scheduleGameElem){
  50. console.log("Selected new game from agenda:" + scheduleGameElem.target);
  51. $("div.dhx_cal_event.selected").removeClass("selected");
  52. $(scheduleGameElem.target).parent().addClass("selected");
  53. },
  54. initScheduler: function(playgrounds){
  55. if (!playgrounds){
  56. console.debug("Customer:Agenda error loading place playgrounds");
  57. return;
  58. }
  59. var sections=_(playgrounds).map(function(item){
  60. console.debug("PlacePlaygrounds::Loaded " + item.name);
  61. return {
  62. key: item.id,
  63. label: item.name
  64. };
  65. });
  66. scheduler.createUnitsView({
  67. name: "unit",
  68. property: "playground_id",
  69. list: sections
  70. });
  71. scheduler.locale.labels.unit_tab = "Playground"
  72. scheduler.init('scheduler',null,"day");
  73. scheduler.config.api_date=""
  74. scheduler.attachEvent("onClick", this.showGameDetails);
  75. },
  76. showGameDetails:function(gameId,event){
  77. console.log("Customer:Agenda selected from schedule Game" + gameId);
  78. var game = GameEvents.get(gameId);
  79. App.selectedGame = game;
  80. console.log("Customer:Agenda loaded Game" + game);
  81. $("#game_title").html('Game: ' + game.get("team1").name + " VS " + game.get("team2").name);
  82. $("#game_description").html(game.get("description"));
  83. $("#game_date_and_place").html(game.get("play_date") +" @ "+ game.get("playground").name);
  84. var gameTeamsPanel = $("#game_team1_panel");
  85. gameTeamsPanel.find("h4[name='team_name']").html(game.get("team1").name);
  86. gameTeamsPanel.find("li[name='team_player1']").html(game.get("team1").players[0].name);
  87. gameTeamsPanel.find("li[name='team_player2']").html(game.get("team1").players[1].name);
  88. gameTeamsPanel = $("#game_team2_panel");
  89. gameTeamsPanel.find("h4[name='team_name']").html(game.get("team2").name);
  90. gameTeamsPanel.find("li[name='team_player1']").html(game.get("team2").players[0].name);
  91. gameTeamsPanel.find("li[name='team_player2']").html(game.get("team2").players[1].name);
  92. },
  93. render: function() {
  94. },
  95. addGameEvent: function(event){
  96. var conversor= scheduler.date.str_to_date("%Y-%m-%dT%H:%i:%sZ");
  97. var start_date = conversor(event.get('play_date').replace("Z", " ").replace("T", " "));
  98. var end_date = scheduler.date.add(start_date, 1, "hour");
  99. scheduler.addEvent({
  100. start_date: start_date,
  101. end_date: end_date,
  102. id: event.get("id"),
  103. text:event.get('team1').name + " VS " + event.get('team2').name +": "+ event.get('description')
  104. });
  105. },
  106. resetGameEvents: function(){
  107. },
  108. confirmGame: function(event){
  109. var gameId= this.selectedGame.get("id");
  110. console.log("Agenda: confirming game" + gameId);
  111. $.ajax({
  112. type: "PUT",
  113. url: "/customers/playground_requests/" + gameId +".json",
  114. data: {customer_playground_request:{status: 'confirmed'}},
  115. success: function(data, status){
  116. console.debug("Agenda: confirmed game" + data.model.game_id);
  117. $("div.dhx_cal_event.selected").addClass("confirmed");
  118. }
  119. });
  120. },
  121. rejectGame: function(event){
  122. var gameId= this.selectedGame.get("id");
  123. console.log("Agenda: confirming game" + gameId);
  124. $.ajax({
  125. type: "PUT",
  126. url: "/customers/playground_requests/" + gameId +".json",
  127. data: {customer_playground_request:{status: 'rejected'}},
  128. success: function(data, status){
  129. console.debug("Agenda: confirmed game" + data.model.game_id);
  130. $("div.dhx_cal_event.selected").addClass("rejected");
  131. }
  132. });
  133. }
  134. });
  135. Backbone.sync= function(method, model, options){
  136. if (method == "read"){
  137. $.ajax({
  138. url: "/customers/agenda/games.json",
  139. data: options.data,
  140. type: "GET",
  141. success: function(data,response){
  142. console.log("Customers:Agenda got games:" + data.length);
  143. var returningList= [];
  144. for (var i=0; i< data.length; i++){
  145. var event= data[i];
  146. console.log("Customers:Agenda processing Event" + event.id);
  147. window.GameEvents.add(new GameEvent(event));
  148. }
  149. },
  150. error: function(event,response){
  151. console.log("Customers:Agenda read error:" + response);
  152. var data = JSON.parse(response.responseText);
  153. if (response.status == 200){
  154. try {
  155. if (data.model){
  156. return;
  157. }
  158. }
  159. catch(ex){
  160. console.debug("Maybe actually something DID go wrong..." + ex);
  161. }
  162. }
  163. }
  164. })
  165. }
  166. }
  167. window.App = new ShowCustomerAgendaView();
  168. })