Blame view

resources/js/profile.js 4.65 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  import './comment/frCommenter';
  import './vendor/jquery.timeago';
  import teamTypeahead from './common/directives/teamTypeahead';
  
  angular.module('footyroom').requires.push('fr.profile');
  
  angular.module('fr.profile', [teamTypeahead])
      .controller('ProfileSettingsController', ['$scope', '$http', function ($scope, $http) {
          $scope.submitForm = function (event) {
              if ($scope.userForm.$invalid) {
                  event.preventDefault();
              }
          };
  
          $scope.$watch('userForm', function () {
              $scope.userForm.verifyPassword.$validators.verify = function (modelValue, viewValue) {
                  return viewValue == $scope.userForm.newPassword.$viewValue;
              };
          });
  
          $scope.fbConnect = function () {
              $scope.fbLoginError = null;
  
              FB.login(fbStatusChangeCallback, { scope: 'public_profile, email' });
          };
  
          $scope.fbDisconnect = function () {
              $scope.fbLoginError = null;
  
              $http.post('/profile/settings/fbdisconnect')
  
                  .then(handleFbApiResponse);
          };
  
          /**
           *  Private methods.
           */
  
          function fbStatusChangeCallback(response) {
              var token = null;
  
              if (response.status === 'connected') {
                  token = response.authResponse.accessToken;
  
                  $http.post('/profile/settings/fbconnect', { token: token })
  
                      .then(handleFbApiResponse);
              }
          }
  
          function handleFbApiResponse(response) {
              if (response.data === 'success') {
                  window.location = '/profile/settings';
              } else {
                  $scope.fbLoginError = response.data;
              }
          }
      }])
  
      .controller('ProfileCtrl', function () {
      })
  
      .controller('FriendsPaginationCtrl', ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {
          $scope.currentPage = 1;
          $scope.itemsPerPage = 20;
  
          $scope.$on('loadFriends', function () {
              loadFriends();
          });
  
          $scope.init = function (type) {
              $scope.type = type;
              loadFriends();
          };
  
          $scope.pageChanged = function () {
              loadFriends();
          };
  
          $scope.removeFriend = function (userID) {
              $http.post('/profile/friends/unfriend', { userId: userID })
  
                  .then(handleFriendApiResponse);
          };
  
          $scope.ignoreFriend = function (userID) {
              $http.post('/profile/friends/delete-request', { fromUserId: userID })
  
                  .then(handleFriendApiResponse);
          };
  
          $scope.acceptFriend = function (userID) {
              $http.post('/profile/friends/accept-request', { fromUserId: userID })
  
                  .then(function (response) {
                      handleFriendApiResponse(response, true);
                  });
          };
  
          function handleFriendApiResponse(response, broadcast) {
              if (response.data === 'OK') {
                  if (broadcast) {
                      $rootScope.$broadcast('loadFriends');
                  } else {
                      loadFriends();
                  }
              }
          }
  
          function loadFriends(response) {
              $http({
                  method: 'GET',
                  url: '/profile/' + $scope.username + '/friends.json',
                  params: {
                      type: $scope.type,
                      page: $scope.currentPage,
                      perPage: $scope.itemsPerPage,
                  },
              })
  
                  .then(function (response) {
                      $scope.totalItems = parseInt(response.data.total);
                      $scope.friends = response.data.data;
                  });
          }
      }]);
  
  $(document).ready(function () {
      $('#changePhoto_label').click(function () {
          $('#inputChangePhoto').click();
      });
  
      $('#filter_all').click(function () {
          $('#all_friends').show();
          $('#pending_friends').hide();
      });
  
      $('#filter_pending').click(function () {
          $('#all_friends').hide();
          $('#pending_friends').show();
      });
  
      $('#inputChangePhoto').change(function () {
          $('#changePhotoForm').submit();
      });
  
      $.profile = {
          userID: $('#profile_userID').val(),
      };
  
      $('#add_as_friend').click(function () {
          const _this = this;
  
          $.post('/profile/friends/request', { userId: $.profile.userID }, function (result) {
              if (result.success == true) {
                  $('#profile_connect_text').removeClass('displayno');
                  $(_this).hide();
              } else {
                  alert('Error in communication');
              }
          }, 'json');
      });
  });