-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (116 loc) · 4.76 KB
/
index.js
File metadata and controls
128 lines (116 loc) · 4.76 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
// Array in which to store contacts
var contacts = new Array();
// Output activity log to the JavaScript console with a time/date stamp for debugging
function log(texttolog) {
var d = new Date();
var time = padLeft(d.getHours(), 2) + ":" + padLeft(d.getMinutes(), 2) + ":" + padLeft(d.getSeconds(), 2) + ":" + padLeft(d.getMilliseconds(), 3);
console.log(time + ": " + texttolog);
$('#logging_box').html("<b>Status: </b>" + time + ": " + texttolog + "<br>");
}
function padLeft(nr, n, str) {
return Array(n - String(nr).length + 1).join(str || '0') + nr;
}
// Creates a comma delimited string for each contact found
function addToContacts(name, emailAddress, title, company, workPhone) {
// Not every contact will have a title or company,
// so lets check for undefined values and display them more politely
if (typeof (title) == "undefined") { title = 'n/a'; }
if (typeof (company) == "undefined") { company = 'n/a'; }
if (typeof (workPhone) == "undefined") { workPhone = 'n/a'; }
// Comma delimited
contacts.push('"' + name + '","' + emailAddress + '","' + title + '","' + company + '","' + workPhone + '"<br>');
// Lets sort the contact alphabetically.
contacts.sort();
$('#export_box').html('name,email,title,company,telephone<br>' + contacts.join(""));
log("");
$('#logs').hide();
}
$(function () {
'use strict';
// new instance of clipboard.js
var clipboard = new Clipboard('.btn');
clipboard.on('success', function (e) {
// clear selection after copying to clipboard
e.clearSelection();
});
// Let's hide the Copy to Clipboard button until the export is finished.
$('#btn').hide();
log("App Loaded");
var Application
var client;
Skype.initialize({
apiKey: 'SWX-BUILD-SDK',
}, function (api) {
Application = api.application;
client = new Application();
}, function (err) {
log('some error occurred: ' + err);
});
// Authenticates against a Lync or Skype for Business service
function sign_in() {
$('#signin').hide();
log('Signing in...');
// and invoke its asynchronous "signIn" method
client.signInManager.signIn({
username: $('#address').text(),
password: $('#password').text()
}).then(function () {
log('Logged In Succesfully');
$('#loginbox').hide();
}).then(null, function (error) {
// if either of the operations above fails, tell the user about the problem
log(error || 'Oops, Something went wrong.');
$('#signin').show()
});
}
// when the user clicks the "Sign In" button
$('#signin').click(function () {
sign_in();
});
// Retrieves all contacts ('persons') asynchronously.
// Note they do not return in any particular order, so they should be sorted
function retrieve_all() {
log('Retrieving all contacts...');
client.personsAndGroupsManager.all.persons.get().then(function (persons) {
// `persons` is an array, so we can use Array::forEach here
persons.forEach(function (person) {
person.displayName.get().then(function (name) {
var personEmail = "";
person.emails.get().then(function (emails) {
// a JSON string is returned containing one or more email addresses
var json_text = JSON.stringify(emails, null, 2).toString();
json_text = json_text.replace("[", "");
json_text = json_text.replace("]", "");
var obj = $.parseJSON(json_text);
var personEmail = obj['emailAddress'];
// Pass values to the addToContacts function that creates the CSV export
addToContacts(name, personEmail, person.title(), person.company(), person.office());
});
});
});
// Once finished, we can show the copy button
$('#btn').show();
});
}
$('#retrieve_all').click(function () {
retrieve_all();
});
// when the user clicks on the "Sign Out" button
$('#signout').click(function () {
// start signing out
log("Signing Out");
client.signInManager.signOut().then(
//onSuccess callback
function () {
// and report the success
log('Signed out');
$('#loginbox').show();
$('#signin').show();
},
//onFailure callback
function (error) {
// or a failure
log(error || 'Cannot Sign Out');
});
});
});