This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathngLinkedIn.js
175 lines (156 loc) · 5.67 KB
/
ngLinkedIn.js
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
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Angular LinkedIn Service
*
* For more info see official API Documentation:
* https://developer.linkedin.com/documents/javascript-api-reference-0
*
* @author Roman Alexeev <roman@boket.to>
* @date April 21, 2014
* @version 0.1.2
* @license MIT
*/
'use strict';
angular.module('ngLinkedIn', [])
.provider('$linkedIn', function() {
var config = {
appKey: null,
authorize: false,
lang: 'en_US',
scope: 'r_basicprofile',
credentials_cookie: false
};
this.set = function(property, value) {
if (!config.hasOwnProperty(property)) {
throw 'Config does not support property: ' + property;
}
config[property] = value;
return this;
};
this.get = function(property) {
if (!config.hasOwnProperty(property)) {
throw 'Config does not support property: ' + property;
}
return config[property];
};
this.$get = ['$rootScope', '$q', '$window', function($rootScope, $q, $window) {
var $linkedIn = $q.defer();
$rootScope.$on("in.load", function(e, IN) {
$linkedIn.resolve(IN);
var events = ['auth', 'logout'];
angular.forEach(events, function(event) {
IN.Event.on(IN, event, function(response) {
$rootScope.$broadcast("in." + event, response);
if (!$rootScope.$$phase) {
$rootScope.$apply();
}
});
});
});
$linkedIn.config = function(property) {
return config[property];
};
// init
$linkedIn.init = function() {
if (!$linkedIn.config('appKey')) {
throw '$linkedInProvider: appKey is not set';
}
$window.inAsyncLoad = function() {
$rootScope.$broadcast("in.load", $window.IN);
};
$window.IN.ENV.js.suppressWarnings = true;
$window.IN.init(angular.extend({
api_key: $linkedIn.config('appKey'),
onLoad: 'inAsyncLoad'
}, config));
};
// check auth
$linkedIn.isAuthorized = function() {
return $linkedIn.promise.then(function(IN) {
return IN.User.isAuthorized();
});
};
// authorize
$linkedIn.authorize = function() {
var defer = $q.defer();
return $linkedIn.promise.then(function(IN) {
IN.User.authorize(function() {
defer.resolve();
});
return defer.promise;
});
};
// refresh token
$linkedIn.refresh = function() {
IN.User.refresh();
};
// logout
$linkedIn.logout = function() {
var defer = $q.defer();
return $linkedIn.promise.then(function(IN) {
IN.User.logout(function() {
defer.resolve();
});
return defer.promise;
});
};
// share
$linkedIn.share = function(url) {
if (!url) {
throw 'Url is not specified';
}
IN.UI.Share()
.params({ url: url })
.place();
};
// general api request
$linkedIn.api = function(api, ids, fields, params) {
var defer = $q.defer();
return $linkedIn.promise.then(function(IN) {
IN.API[api](ids.toString() || 'me')
.fields(fields || null)
.params(params || {})
.result(function(response) {
defer.resolve(response);
});
return defer.promise;
});
};
// api shortcut methods
// profile
$linkedIn.profile = function(ids, fields, params) {
return $linkedIn.api('Profile', ids, fields, params);
};
// connections
// requires 'r_network' and 'rw_nus' permissions
$linkedIn.connections = function(ids, fields, params) {
return $linkedIn.api('Connections', ids, fields, params);
};
// member updates
// requires 'rw_nus' permission
$linkedIn.memberUpdates = function(ids, fields, params) {
return $linkedIn.api('MemberUpdates', ids, fields, params);
};
return $linkedIn;
}];
})
.run(['$rootScope', '$linkedIn', function($rootScope, $linkedIn) {
if (!window.IN) {
// If jQuery doesn't exist - script is loaded with regular JS.
try {
$.getScript("//platform.linkedin.com/in.js?async=true", function () {
$linkedIn.init();
if (!$rootScope.$$phase) {
$rootScope.$apply();
}
});
}
catch (e) {
(function(d, t) {
var g = d.createElement(t), // create a script tag
s = d.getElementsByTagName(t)[0]; // find the first script tag in the document
g.src = '//platform.linkedin.com/in.js?async=true'; // set the source of the script to your script
s.parentNode.insertBefore(g, s); // append the script to the DOM
}(document, 'script'));
}
}
}]);