Advertisement
Guest User

Untitled

a guest
Jan 21st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. (function() {
  2. var base,
  3. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  4. extend = 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; },
  5. hasProp = {}.hasOwnProperty;
  6.  
  7. (base = Swap.Views).UserSessions || (base.UserSessions = {});
  8.  
  9. Swap.Views.UserSessions.LoginView = (function(superClass) {
  10. extend(LoginView, superClass);
  11.  
  12. function LoginView() {
  13. this.onClose = bind(this.onClose, this);
  14. this.onShow = bind(this.onShow, this);
  15. return LoginView.__super__.constructor.apply(this, arguments);
  16. }
  17.  
  18. LoginView.prototype.template = JST["backbone/templates/user_sessions/login"];
  19.  
  20. LoginView.prototype.className = 'modal fade hide';
  21.  
  22. LoginView.prototype.id = 'login';
  23.  
  24. LoginView.prototype.attributes = {
  25. tabindex: '-1',
  26. role: 'dialog'
  27. };
  28.  
  29. LoginView.prototype.events = {
  30. 'submit form': 'login',
  31. 'click .eyes': 'togglePassword',
  32. 'focusin input': 'clearError'
  33. };
  34.  
  35. LoginView.prototype.bindings = {
  36. "input#user_email": "email",
  37. "input#user_password": "password",
  38. "input[type='email'], input[type='password'], input[type='text']": {
  39. attributes: [
  40. {
  41. name: 'class',
  42. observe: 'error',
  43. onGet: function(val) {
  44. if (val && !_.isEmpty(val)) {
  45. return 'error';
  46. }
  47. }
  48. }
  49. ]
  50. },
  51. '.error_message': {
  52. observe: 'error',
  53. onGet: function(val) {
  54. if (val && !_.isEmpty(val)) {
  55. return val;
  56. }
  57. }
  58. }
  59. };
  60.  
  61. LoginView.prototype.render = function() {
  62. this.model = new Swap.Models.UserSession();
  63. $('.modal').modal('hide');
  64. this.$el.html(this.template());
  65. this.$el.on('shown.bs.modal', this.onShow);
  66. this.$el.on('hidden.bs.modal', this.onClose);
  67. this.stickit();
  68. this.$el.modal({
  69. keyboard: false,
  70. backdrop: true
  71. });
  72. return this;
  73. };
  74.  
  75. LoginView.prototype.onShow = function(e) {
  76. return this.$el.find('a, span, i, div').tooltip();
  77. };
  78.  
  79. LoginView.prototype.login = function(e) {
  80. if (e) {
  81. e.preventDefault();
  82. }
  83. return this.model.save(this.model.attributes, {
  84. success: (function(_this) {
  85. return function(userSession, response) {
  86. var csrf;
  87. csrf = _.pick(response, 'csrf_token', 'csrf_param');
  88. if (_.has(csrf, 'csrf_token')) {
  89. $('meta[name="csrf-token"]').attr('content', response.csrf_token);
  90. }
  91. Swap.currentUser = new Swap.Models.User(_.omit(response, 'csrf_token', 'csrf_param'));
  92. Swap.vent.trigger('authentication:logged_in');
  93. _this.$el.modal('hide');
  94. };
  95. })(this),
  96. error: (function(_this) {
  97. return function(userSession, response) {
  98. var result;
  99. result = $.parseJSON(response.responseText);
  100. _this.model.set(result);
  101. };
  102. })(this)
  103. });
  104. };
  105.  
  106. LoginView.prototype.onClose = function(e) {
  107. var view;
  108. view = this;
  109. return setTimeout((function() {
  110. view.$el.modal('hide');
  111. view.remove();
  112. }), 25);
  113. };
  114.  
  115. LoginView.prototype.togglePassword = function(e) {
  116. if ($('input#user_password').prop('type') === 'password') {
  117. return $('input#user_password').prop('type', 'text');
  118. } else {
  119. return $('input#user_password').prop('type', 'password');
  120. }
  121. };
  122.  
  123. LoginView.prototype.clearError = function(e) {
  124. var block, input, message;
  125. input = $(e.target);
  126. if (input.hasClass('error')) {
  127. block = input.closest('form');
  128. if (block.length > 0) {
  129. message = block.find('.error_message');
  130. }
  131. if (message) {
  132. return message.fadeOut('slow', (function(_this) {
  133. return function() {
  134. message.html();
  135. message.show();
  136. return _this.model.set({
  137. error: {}
  138. });
  139. };
  140. })(this));
  141. }
  142. }
  143. };
  144.  
  145. return LoginView;
  146.  
  147. })(Backbone.View);
  148.  
  149. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement