PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/monocle/controller/session.js

https://bitbucket.org/whallet/whallet-mobile
JavaScript | 343 lines | 307 code | 35 blank | 1 comment | 14 complexity | 77e211564e059470a4c6e9b785062f9e MD5 | raw file
  1. // Generated by CoffeeScript 1.7.1
  2. (function() {
  3. var SessionCtrl, contoller_login, controller_aside, controller_home, controller_login, controller_signup, controller_splash,
  4. __hasProp = {}.hasOwnProperty,
  5. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  6. SessionCtrl = (function(_super) {
  7. var afterNotification, balanceResponse, beforeSendUserInfo, budgetsResponse, currenciesResponse, errorRecoveryPassowrdResponse, errorResponse, helperTagsResponse, movementsResponse, registrationResponse, sendPasswordResponse, sessionResponse, userInfoResponse;
  8. __extends(SessionCtrl, _super);
  9. function SessionCtrl() {
  10. return SessionCtrl.__super__.constructor.apply(this, arguments);
  11. }
  12. SessionCtrl.prototype.events = {
  13. "load section#home": "onLoadHome",
  14. "load section#login": "onLoadLogin",
  15. "load section#signup": "onLoadSignup",
  16. "tap a[data-action=login]": "onLogin",
  17. "tap a[data-action=logout]": "onLogout",
  18. "tap a[data-action=create-account]": "onCreateAccount",
  19. "tap a[data-action=recovery]": "onRecoveryPassword",
  20. "tap a[data-action=recovery-password": "onSendPassword"
  21. };
  22. SessionCtrl.prototype.elements = {
  23. "#login_email": "login_email",
  24. "#login_password": "login_password",
  25. "#signup_email": "signup_email",
  26. "#signup_password": "signup_password",
  27. "#recovery_email": "recovery_email"
  28. };
  29. SessionCtrl.prototype.onSendPassword = function(event) {
  30. var post_data, result, url;
  31. console.log("onSendPassword");
  32. url = "http://www.whallet.com/api/v1/user/password.json";
  33. post_data = {
  34. email: this.recovery_email.val()
  35. };
  36. Lungo.Service.Settings.error = errorRecoveryPassowrdResponse;
  37. return result = Lungo.Service.post(url, post_data, sendPasswordResponse);
  38. };
  39. sendPasswordResponse = function(response) {
  40. console.log("sendPasswordResponse");
  41. return Lungo.Notification.success(Helpers.getTranslate('email_found'), Helpers.getTranslate("succes_restore_password"), "check", 3, afterNotification);
  42. };
  43. afterNotification = function() {
  44. return Lungo.Router.back();
  45. };
  46. SessionCtrl.prototype.onRecoveryPassword = function(event) {
  47. var template;
  48. console.log("onRecoveryPassword");
  49. template = "<form style=\"width: 100%\">";
  50. template += "<fieldset style=\"border-bottom: 2px solid #0093D5\">";
  51. template += "<span id=\"screen-input\" style=\"font-size: 1.4em; height:40px; margin-top:15px\" class=\"text thin left\">";
  52. template += "<abbr><input type=\"text\" id=\"emailRecovery\" placeholder=\"" + (Helpers.getTranslate("txtemail")) + "\"></abbr>";
  53. template += "</span></fieldset></form>";
  54. return Lungo.Notification.confirm({
  55. icon: '',
  56. title: Helpers.getTranslate("description_recovery"),
  57. description: template,
  58. accept: {
  59. icon: 'checkmark',
  60. label: Helpers.getTranslate("accept"),
  61. callback: function() {
  62. var inputScreen, post_data, result, url;
  63. inputScreen = Lungo.dom("#emailRecovery")[0].value;
  64. url = "http://www.whallet.com/api/v1/user/password.json";
  65. post_data = {
  66. email: inputScreen
  67. };
  68. Lungo.Service.Settings.error = errorRecoveryPassowrdResponse;
  69. return result = Lungo.Service.post(url, post_data, sendPasswordResponse);
  70. }
  71. },
  72. cancel: {
  73. icon: 'close',
  74. label: Helpers.getTranslate("cancel"),
  75. callback: null
  76. }
  77. });
  78. };
  79. SessionCtrl.prototype.onLoadSignup = function(event) {
  80. return console.log("onLoadSignup");
  81. };
  82. SessionCtrl.prototype.onLoadLogin = function(event) {
  83. var post_data, result, tokenStored, url;
  84. console.log("onLoadLogin");
  85. tokenStored = Lungo.Data.Storage.persistent("tokenAuth");
  86. if (tokenStored !== null) {
  87. url = "http://www.whallet.com/api/v1/helper/tags.json";
  88. post_data = {
  89. token: tokenStored
  90. };
  91. result = Lungo.Service.post(url, post_data, helperTagsResponse);
  92. return Lungo.Router.section("monitor");
  93. }
  94. };
  95. SessionCtrl.prototype.onLoadHome = function(event) {
  96. return console.log("onLoadHome");
  97. };
  98. SessionCtrl.prototype.onCreateAccount = function(event) {
  99. var post_data, result, url;
  100. console.log("onCreateAccount");
  101. url = "http://www.whallet.com/api/v1/registrations.json";
  102. post_data = {
  103. email: this.signup_email.val(),
  104. password: this.signup_password.val(),
  105. password_confirmation: this.signup_password.val(),
  106. locale: String.locale
  107. };
  108. Lungo.Service.Settings.error = errorResponse;
  109. return result = Lungo.Service.post(url, post_data, registrationResponse);
  110. };
  111. SessionCtrl.prototype.onLogout = function(event) {
  112. Lungo.Data.Storage.persistent("tokenAuth", null);
  113. Lungo.Data.Storage.persistent("user", null);
  114. Lungo.Data.Storage.persistent("userInfo", null);
  115. Lungo.Data.Storage.persistent("balances", null);
  116. Lungo.Data.Storage.persistent("movements", null);
  117. Lungo.Data.Storage.persistent("expenses", null);
  118. Lungo.Data.Storage.persistent("movementID", null);
  119. Lungo.Data.Storage.persistent("movementUID", null);
  120. Lungo.Data.Storage.persistent("budgetID", null);
  121. Lungo.Data.Storage.persistent("budgetUID", null);
  122. __Model.Movement.destroyAll();
  123. __Model.Spending.destroyAll();
  124. __Model.Balance.destroyAll();
  125. __Model.Tag.destroyAll();
  126. __Model.Budget.destroyAll();
  127. Lungo.dom("section#login #login_email").val("");
  128. Lungo.dom("section#login #login_password").val("");
  129. return Lungo.Router.section("home");
  130. };
  131. SessionCtrl.prototype.onLogin = function(event) {
  132. var post_data, result, tokenStored, url;
  133. console.log("onLogin");
  134. tokenStored = Lungo.Data.Storage.persistent("tokenAuth");
  135. Lungo.Notification.show();
  136. if (tokenStored === null) {
  137. url = "http://www.whallet.com/api/v1/tokens.json";
  138. post_data = {
  139. email: this.login_email.val(),
  140. password: this.login_password.val()
  141. };
  142. Lungo.Service.Settings.error = errorResponse;
  143. return result = Lungo.Service.post(url, post_data, sessionResponse);
  144. } else {
  145. Lungo.Data.Storage.persistent("tokenAuth", null);
  146. Lungo.Data.Storage.persistent("user", null);
  147. Lungo.Data.Storage.persistent("userInfo", null);
  148. Lungo.Data.Storage.persistent("balances", null);
  149. Lungo.Data.Storage.persistent("movements", null);
  150. Lungo.Data.Storage.persistent("expenses", null);
  151. Lungo.Data.Storage.persistent("movementID", null);
  152. Lungo.Data.Storage.persistent("movementUID", null);
  153. Lungo.Data.Storage.persistent("budgetID", null);
  154. Lungo.Data.Storage.persistent("budgetUID", null);
  155. Lungo.Data.Storage.persistent("budgets", null);
  156. __Model.Budget.destroyAll();
  157. __Model.Movement.destroyAll();
  158. __Model.Spending.destroyAll();
  159. __Model.Balance.destroyAll();
  160. __Model.Tag.destroyAll();
  161. return Lungo.Router.section("monitor");
  162. }
  163. };
  164. errorResponse = function(type, xhr) {
  165. console.log("errorResponse");
  166. console.log("xhr: %o", xhr);
  167. return Lungo.Notification.error(Helpers.getTranslate("error"), Helpers.getTranslate("login_error"), "cancel", 3, null);
  168. };
  169. errorRecoveryPassowrdResponse = function(type, xhr) {
  170. console.log("errorResponse");
  171. console.log("xhr: %o", xhr);
  172. return Lungo.Notification.error(Helpers.getTranslate("email_not_found"), Helpers.getTranslate("something_wrong"), "cancel", 3, null);
  173. };
  174. registrationResponse = function(response) {
  175. var post_data, result, url;
  176. console.log("registrationResponse");
  177. console.log(response);
  178. url = "http://www.whallet.com/api/v1/tokens.json";
  179. post_data = {
  180. email: Lungo.dom("section#signup #signup_email").val(),
  181. password: Lungo.dom("section#signup #signup_password").val()
  182. };
  183. return result = Lungo.Service.post(url, post_data, sessionResponse);
  184. };
  185. sessionResponse = function(response) {
  186. var date, email, post_data, result, url;
  187. console.log("sessionResponse");
  188. if (response.token != null) {
  189. date = new Date();
  190. Lungo.Data.Storage.persistent("dateSelected", null);
  191. Lungo.Data.Storage.persistent("dateSelected", date);
  192. Lungo.Data.Storage.persistent("tokenAuth", response.token);
  193. email = Lungo.dom("section#login #login_email").val();
  194. Lungo.Data.Storage.persistent("user", email);
  195. url = "http://www.whallet.com/api/v1/helper/tags.json";
  196. post_data = {
  197. token: response.token
  198. };
  199. result = Lungo.Service.post(url, post_data, helperTagsResponse);
  200. url = "http://www.whallet.com/api/v1/user/info.json";
  201. post_data = {
  202. token: response.token
  203. };
  204. result = Lungo.Service.post(url, post_data, userInfoResponse);
  205. url = "http://www.whallet.com/api/v1/dashboard/balance.json";
  206. post_data = {
  207. token: response.token
  208. };
  209. result = Lungo.Service.post(url, post_data, balanceResponse);
  210. Lungo.Data.Storage.persistent("movements", null);
  211. url = "http://www.whallet.com/api/v1/movements.json";
  212. post_data = {
  213. token: response.token,
  214. year: date.getFullYear(),
  215. month: date.getMonth() + 1
  216. };
  217. result = Lungo.Service.post(url, post_data, movementsResponse);
  218. Lungo.Data.Storage.persistent("budgets", null);
  219. url = "http://www.whallet.com/api/v1/budget/list.json";
  220. post_data = {
  221. token: response.token
  222. };
  223. return result = Lungo.Service.get(url, post_data, budgetsResponse);
  224. } else {
  225. return Lungo.Notification.error(Helpers.getTranslate("error"), Helpers.getTranslate("login_error"), "cancel", 3, null);
  226. }
  227. };
  228. movementsResponse = function(response) {
  229. console.log("Splash.movementsResponse");
  230. return WhalletMovements.loadFromResponse(response);
  231. };
  232. budgetsResponse = function(response) {
  233. console.log("Splash.budgetsResponse");
  234. return WhalletBudgets.loadFromResponse(response);
  235. };
  236. balanceResponse = function(response) {
  237. var balanceModel, dataResponse, _i, _len;
  238. console.log("Splash.balanceResponse");
  239. __Model.Balance.destroyAll();
  240. for (_i = 0, _len = response.length; _i < _len; _i++) {
  241. dataResponse = response[_i];
  242. balanceModel = new __Model.Balance;
  243. balanceModel.date = dataResponse.date;
  244. balanceModel.expense = dataResponse.data.expense;
  245. balanceModel.income = dataResponse.data.income;
  246. balanceModel.balance = dataResponse.data.balance;
  247. balanceModel.save();
  248. }
  249. return Lungo.Router.section("monitor");
  250. };
  251. helperTagsResponse = function(response) {
  252. var dataResponse, tag, tagModel, _i, _len, _results;
  253. console.log("helperTagsResponse");
  254. _results = [];
  255. for (_i = 0, _len = response.length; _i < _len; _i++) {
  256. dataResponse = response[_i];
  257. _results.push((function() {
  258. var _j, _len1, _ref, _results1;
  259. _ref = dataResponse.tags;
  260. _results1 = [];
  261. for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
  262. tag = _ref[_j];
  263. _results1.push(tagModel = __Model.Tag.create({
  264. description: tag.name,
  265. category: dataResponse.category
  266. }));
  267. }
  268. return _results1;
  269. })());
  270. }
  271. return _results;
  272. };
  273. beforeSendUserInfo = function(request) {
  274. return console.log("beforeSendUserInfo");
  275. };
  276. userInfoResponse = function(response) {
  277. var post_data, result, url;
  278. console.log("userInfoResponse");
  279. if (response != null) {
  280. String.locale = response.locale;
  281. Helpers.updateAllTranslateElement();
  282. Lungo.Data.Storage.persistent("userInfo", null);
  283. Lungo.Data.Storage.persistent("userInfo", response);
  284. if (User.isPro()) {
  285. Lungo.dom('li#donate').addClass("hidden");
  286. }
  287. Android.hideAd();
  288. Lungo.Data.Storage.persistent("currencies", null);
  289. url = "http://www.whallet.com/api/v1/helper/currencies.json";
  290. post_data = {
  291. locale: response.locale
  292. };
  293. return result = Lungo.Service.get(url, post_data, currenciesResponse);
  294. }
  295. };
  296. currenciesResponse = function(response) {
  297. console.log("currenciesResponse");
  298. return Lungo.Data.Storage.persistent("currencies", response);
  299. };
  300. return SessionCtrl;
  301. })(Monocle.Controller);
  302. contoller_login = new SessionCtrl("section#recovery");
  303. controller_login = new SessionCtrl("section#login");
  304. controller_signup = new SessionCtrl("section#signup");
  305. controller_home = new SessionCtrl("section#home");
  306. controller_splash = new SessionCtrl("section#splash");
  307. controller_aside = new SessionCtrl("aside#options");
  308. }).call(this);