-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.js
More file actions
128 lines (114 loc) · 3.38 KB
/
Copy pathtoggle.js
File metadata and controls
128 lines (114 loc) · 3.38 KB
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
flag.directive('flagToggle', function($http, flagConfig, $rootScope) {
return {
restrict: 'E',
template: '<i ng-class="class_element"></i><a href="#" ng-click="$event.preventDefault(); toggle()">{{text}}</a>',
scope: {
endpoint: '@',
textFlagged: '@',
textUnflagged: '@',
classFlagged: '@',
classUnflagged: '@',
entity: '@',
entityId: '@'
},
link: function($scope) {
/**
* Holds the access token for other controllers to change.
*
* @type {{accessToken: string}}
*/
$scope.token = {
'accessToken': ''
};
// Set up the flag value.
$rootScope.$broadcast('flagAccessToken', $scope.token);
$scope.accessToken = $scope.token.accessToken;
$scope.$watch('entityId', function(value) {
if (+value == 0) {
return;
}
var request = {
method: 'get',
url: flagConfig.server + $scope.endpoint + '?check_flagged&entity=' + $scope.entity + '&id=' + value,
headers: {
'access_token': $scope.accessToken,
'access-token': $scope.accessToken
}
};
$http(request).success(function(data) {
$scope.alterText(data.data.length != 0);
});
});
/**
* Altering the text of the flag by the state of the flag.
*
* @param flagged
* Determine if the user flagged this entity or not.
*/
$scope.alterText = function(flagged) {
$scope.text = flagged ? $scope.textFlagged : $scope.textUnflagged;
$scope.class_element = flagged ? $scope.classFlagged : $scope.classUnflagged;
};
/**
* Toggle between the states of the flag.
*/
$scope.toggle = function() {
// Check if the current user already flagged the entity.
var request = {
method: 'get',
url: flagConfig.server + $scope.endpoint + '?check_flagged&entity=' + $scope.entity + '&id=' + $scope.entityId,
headers: {
'access_token': $scope.accessToken,
'access-token': $scope.accessToken
}
};
$http(request).success(function(data) {
$scope.updateDirective($scope.getActions(data));
});
};
/**
* Build the operation type.
*
* @returns {{type: *, address: *}}
*/
$scope.getActions = function(data) {
var type, address;
address = flagConfig.server + $scope.endpoint;
if (data.count == 0) {
type = 'post';
}
else {
type = 'delete';
address += '/' + $scope.entityId;
}
return {
type: type,
address: address
};
};
/**
* Increase or decrease the likes number.
*
* @param results
* The results from getActions function.
*/
$scope.updateDirective = function(results) {
var request = {
method: results.type,
url: results.address,
data: {
entity_type: $scope.entity,
entity_id: $scope.entityId
},
headers: {
'access_token': $scope.accessToken,
'access-token': $scope.accessToken
}
};
$http(request).success(function(data) {
$scope.alterText(data instanceof Object);
});
};
}
};
});