Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. String.prototype.replaceAll = function (search, replacement) {
  2. var target = this;
  3. return target.replace(new RegExp(search, 'g'), replacement);
  4. };
  5.  
  6. function httpGet(theUrl) {
  7. var xmlHttp = new XMLHttpRequest();
  8. xmlHttp.open("GET", theUrl, false); // false for synchronous request
  9. xmlHttp.send(null);
  10. return xmlHttp.responseText;
  11. }
  12.  
  13. function getZero(obj) {
  14. var o = JSON.parse(JSON.stringify(obj));
  15. o.count = 0;
  16. return o;
  17. }
  18.  
  19. var getTeams = "getTeams";
  20. var getSavedMatches = "getSavedMatches";
  21. var getPlayer = "getPlayer";
  22. var setPlayer = "setPlayer";
  23. var addPlayer = "addPlayer";
  24. var addTeam = "addTeam";
  25. var getPlayerReport = "getPlayerReport";
  26.  
  27. var squadraPlayer = "";
  28. var numeroPlayer = "";
  29.  
  30. var apiHost = "https://cors.io/?http://bettervolleyscouting.altervista.org";
  31.  
  32. function getFromApi(res, prams) {
  33. if (!res.startsWith("/")) res = "/" + res;
  34. if (!res.endsWith(".php")) res = res + ".php";
  35.  
  36. if (prams !== undefined) {
  37. res += "?";
  38. Object.keys(prams).forEach(function (e) {
  39. res += e + "=" + prams[e] + "&";
  40. });
  41. res += "foo=foo";
  42. }
  43. //console.log(res);
  44. return JSON.parse(httpGet((apiHost + res).replace(" ", "%20")));
  45. }
  46.  
  47. var user = "";
  48.  
  49. sap.ui.define([
  50. "sap/ui/core/mvc/Controller",
  51. 'sap/m/Button',
  52. 'sap/m/Dialog',
  53. 'sap/ui/model/json/JSONModel',
  54. 'sap/m/Text',
  55. 'sap/m/MessageToast'
  56. ], function (Controller, Button, Dialog, JSONModel, Text, MessageToast) {
  57. "use strict";
  58.  
  59. return Controller.extend("BVS.controller.Player", {
  60.  
  61. /**
  62. * Called when a controller is instantiated and its View controls (if available) are already created.
  63. * Can be used to modify the View before it is displayed, to bind event handlers and do other one-time initialization.
  64. * @memberOf BVS.view.Player
  65. */
  66. onInit: function () {
  67. sap.ui.core.UIComponent.getRouterFor(this).getRoute("Player").attachMatched(this._onRouteMatched, this);
  68.  
  69. //this.getView().byId('profilePic').setSrc(getFromApi(getPlayer, {NUMERO: numeroPlayer, SQUADRA: squadraPlayer}));
  70. //console.log(getFromApi(getPlayer, {NUMERO: numeroPlayer, SQUADRA: squadraPlayer}));
  71. },
  72. _onRouteMatched: function (oEvent) {
  73. var oArgs, oQuery;
  74. oArgs = oEvent.getParameter("arguments");
  75. oQuery = oArgs["?query"];
  76. if (oQuery) {
  77. user = oQuery.username;
  78. squadraPlayer = oQuery.squadra;
  79. numeroPlayer = oQuery.numero;
  80. var lol = getFromApi(getPlayerReport, {NUMERO: numeroPlayer, SQUADRA: squadraPlayer});
  81. //console.log(lol);
  82. var oModel_w;
  83. var chrono = [];
  84. var k = 0;
  85. // lol.single.forEach(function(e){
  86. // e.forEach(function (i){
  87. // chrono.push({
  88. // azione: i.azione,
  89. // incremental: k++,
  90. // opt: parseInt(i.qualita, 10)
  91. // });
  92. // })
  93. // // str += e[e.length - 1].puntinostri + " " + e[e.length - 1].puntiloro;
  94. // })
  95. oModel_w = new JSONModel({
  96. global: lol.byplayer[Object.keys(lol.byplayer)[0]],
  97. chrono: chrono
  98. }, true);
  99.  
  100. oModel_w.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);
  101. this.getView().setModel(oModel_w);
  102. //this.getView().byId('profilePic').setSrc('https://i.imgur.com/oUsxIhd.png');
  103. if (numeroPlayer === "null") {
  104. this.getView().byId('profilePic').setSrc("https://i.imgur.com/oUsxIhd.png");
  105. this.getView().byId('nomecognomeField').setValue("");
  106. this.getView().byId('numeroField').setValue("");
  107. this.getView().byId('datanascitaField').setValue("");
  108. if (squadraPlayer === "null") { //AGGIUNGI SQUADRA
  109. this.getView().byId('squadraField').setEnabled(true);
  110. this.getView().byId("multiBtn").setText("Aggiungi squadra");
  111. this.getView().byId('squadraField').setValue("");
  112. this.getView().byId("multiBtn").setIcon("sap-icon://add-activity");
  113. } else { //NEW PLAYER
  114. this.getView().byId('squadraField').setEnabled(false);
  115. this.getView().byId('squadraField').setValue(squadraPlayer);
  116. this.getView().byId("multiBtn").setText("Aggiungi giocatore");
  117. this.getView().byId("multiBtn").setIcon("sap-icon://add");
  118. }
  119. } else { //EDIT PLAYER
  120. sap.ui.core.BusyIndicator.show(0);
  121. this.getView().byId('squadraField').setEnabled(false);
  122. this.getView().byId("multiBtn").setText("Modifica giocatore");
  123. this.getView().byId("multiBtn").setIcon("sap-icon://edit");
  124. var mthis = this;
  125. setTimeout(function () {
  126. mthis.getView().byId('squadraField').setValue(squadraPlayer);
  127. mthis.getView().byId('profilePic').setSrc(getFromApi(getPlayer, {
  128. NUMERO: numeroPlayer,
  129. SQUADRA: squadraPlayer
  130. })[0].foto);
  131. mthis.getView().byId('nomecognomeField').setValue(getFromApi(getPlayer, {
  132. NUMERO: numeroPlayer,
  133. SQUADRA: squadraPlayer
  134. })[0].nome);
  135. mthis.getView().byId('numeroField').setValue(getFromApi(getPlayer, {
  136. NUMERO: numeroPlayer,
  137. SQUADRA: squadraPlayer
  138. })[0].numero);
  139. mthis.getView().byId('datanascitaField').setValue(getFromApi(getPlayer, {
  140. NUMERO: numeroPlayer,
  141. SQUADRA: squadraPlayer
  142. })[0].data_di_nascita);
  143. sap.ui.core.BusyIndicator.hide();
  144. }, 0);
  145. }
  146.  
  147. }
  148. },
  149.  
  150. /**
  151. * Similar to onAfterRendering, but this hook is invoked before the controller's View is re-rendered
  152. * (NOT before the first rendering! onInit() is used for that one!).
  153. * @memberOf BVS.view.Player
  154. */
  155. // onBeforeRendering: function() {
  156. //
  157. // },
  158.  
  159. /**
  160. * Called when the View has been rendered (so its HTML is part of the document). Post-rendering manipulations of the HTML could be done here.
  161. * This hook is the same one that SAPUI5 controls get after being rendered.
  162. * @memberOf BVS.view.Player
  163. */
  164.  
  165. onAfterRendering: function () {
  166. var loadFile = this.getView().createId("inFile");
  167. $('#' + loadFile).append('<input id="myfile" type="file" accept="image/gif, image/jpeg, image/png" onchange="readURL(this);" />');
  168.  
  169. },
  170. onNavBack: function () {
  171. sap.ui.core.UIComponent.getRouterFor(this).navTo("Home");
  172. },
  173.  
  174. multiBtn: function () {
  175. if (this.getView().byId('numeroField').getValue() >= 0) {
  176. if (this.getView().byId('numeroField').getValue().length > 2) {
  177. MessageToast.show("Il numero del giocatore supera le due cifre.");
  178. } else {
  179. if ($('#__component0---player--profilePic')[0].src.includes('loading.gif')) {
  180. MessageToast.show("Sto ancora caricando la foto. Attendere.");
  181. } else {
  182. if (numeroPlayer === "null") {
  183. if (squadraPlayer === "null") { //AGGIUNGI SQUADRA
  184. var addableTeam = getFromApi(addTeam, {
  185. NOME: this.getView().byId('nomecognomeField').getValue(),
  186. NUMERO: this.getView().byId('numeroField').getValue(),
  187. SQUADRA: this.getView().byId('squadraField').getValue(),
  188. FOTO: $('#__component0---player--profilePic')[0].src,
  189. DATANASCITA: this.getView().byId('datanascitaField').getValue()
  190. });
  191. if (!addableTeam) {
  192. var dialog = new Dialog({
  193. title: 'Errore',
  194. type: 'Message',
  195. state: 'Error',
  196. content: new Text({
  197. text: 'Esiste già una squadra con lo stesso nome.'
  198. }),
  199. beginButton: new Button({
  200. text: 'OK',
  201. press: function () {
  202. dialog.close();
  203. }
  204. }),
  205. afterClose: function () {
  206. dialog.destroy();
  207. }
  208. });
  209. dialog.open();
  210. } else {
  211. sap.ui.core.UIComponent.getRouterFor(this).navTo("Home", {
  212. query: {
  213. newTeam: true,
  214. newTeamName: this.getView().byId('squadraField').getValue(),
  215. username: user
  216. }
  217. });
  218. }
  219. } else {//NEW PLAYER
  220. var addablePlayer = getFromApi(addPlayer, {
  221. NOME: this.getView().byId('nomecognomeField').getValue(),
  222. NUMERO: this.getView().byId('numeroField').getValue(),
  223. SQUADRA: squadraPlayer,
  224. FOTO: $('#__component0---player--profilePic')[0].src,
  225. DATANASCITA: this.getView().byId('datanascitaField').getValue()
  226. });
  227. if (!addablePlayer) {
  228. var dialog = new Dialog({
  229. title: 'Errore',
  230. type: 'Message',
  231. state: 'Error',
  232. content: new Text({
  233. text: 'Il numero del giocatore è già presente in questa rosa.'
  234. }),
  235. beginButton: new Button({
  236. text: 'OK',
  237. press: function () {
  238. dialog.close();
  239. }
  240. }),
  241. afterClose: function () {
  242. dialog.destroy();
  243. }
  244. });
  245. dialog.open();
  246. } else {
  247. sap.ui.core.UIComponent.getRouterFor(this).navTo("Home", {
  248. query: {
  249. refreshTeam: true,
  250. username: user
  251. }
  252. });
  253. }
  254. }
  255. } else {//EDIT
  256. getFromApi(setPlayer, {
  257. NOME: this.getView().byId('nomecognomeField').getValue(),
  258. NUMERO: this.getView().byId('numeroField').getValue(),
  259. NUMEROVECCHIO: numeroPlayer,
  260. SQUADRA: squadraPlayer,
  261. FOTO: $('#__component0---player--profilePic')[0].src,
  262. DATANASCITA: this.getView().byId('datanascitaField').getValue()
  263. });
  264. sap.ui.core.UIComponent.getRouterFor(this).navTo("Home", {
  265. query: {
  266. refreshTeam: true,
  267. username: user
  268. }
  269. });
  270. }
  271. }
  272. }
  273. } else {
  274. MessageToast.show("Invalid player number.");
  275. }
  276.  
  277. }
  278.  
  279.  
  280.  
  281. /**
  282. * Called when the Controller is destroyed. Use this one to free resources and finalize activities.
  283. * @memberOf BVS.view.Player
  284. */
  285. // onExit: function() {
  286. //
  287. // }
  288.  
  289. });
  290.  
  291. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement