-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
171 lines (151 loc) · 4.82 KB
/
main.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
/**
* Created by zavr on 19/08/2015.
*/
var form = document.getElementById("login-form");
var userpic, user = readCookie('username');
//check if user is authenticated
if(user){
form.style.display = 'none';
document.getElementById("user-info-div").style.display = "block";
userpic = readCookie('userPic');
document.getElementById("userPic").src = userpic;
document.getElementById("userName").innerHTML = user;
}
/**
* Simple sign in without password
* @returns {boolean} true on successfull sign in and false otherwise.
*/
function signIn(){
var signedIn = true;
switch(form.login.value){
case 'Lila':
createCookie('username','Lila',1);
createCookie('userPic','users/lila.gif',1);
break;
case 'Fry':
createCookie('username','Fry',1);
createCookie('userPic','users/Fry.jpg',1);
break;
case 'Bender':
createCookie('username','Bender',1);
createCookie('userPic','users/bender.jpg',1);
break;
default:
signedIn = false;
}
if(signedIn) window.location.reload();
return false;
}
/**
* Sign out from the system by deleting cookies.
*/
function signOut(){
eraseCookie('username');
eraseCookie('userPic');
window.location.reload();
}
/**
* Reads link from body's data and appends share token to it.
* @param ast amigo share token
* @returns {string} a link
*/
function getLink(ast) {
return document.getElementsByTagName("body")[0].dataset.touchpoint+"?ast="+ast;
}
var supportedChannels = ['facebook', 'email', 'vk','g-plus', 'touch'];
/**
* This function is called from HTML when user decides to share.
* A share token is then created, populated with user metadata and locked.
* @param link pointer to a_href DOM element which was clicked.
* @returns {boolean} false.
*/
function share(link){
var channel = link.dataset.channel;
//check if channel is supported
var supported = false, i;
for(i=0; i<supportedChannels.length; i++){
if(supportedChannels[i] == channel){
supported = true;
break;
}
}
if(!supported){
console.warn('Channel '+channel+' not supported');
return false;
}
var data = {
userId: '1',
username: 'zavr',
email: '[email protected]',
name: 'Anton',
metadata: { name: user, userPic: userpic }
}
//console.log(data.metadata);
if(amigo) {
amigo.createToken('ad_sharepoint', data, function (err, meta) {
if (err) {
console.warn(err);
return false;
}
//could use autolock instead
amigo.lockToken(meta[0].token, function(err,token) {
if (err) {
console.warn(err);
return false;
}
console.info('Successfully locked', token);
_share(channel, token); //goto step 2
});
});
}else {
console.warn('Amigo was not initialized');
return false;
}
return false;
}
var productDetail = document.getElementById("product-detail-plain").innerHTML;
/**
* Second step of share: consume token and perform channel-specific action
* @param channel
* @param ast amigo share token
* @returns {boolean} true of success and false otherwise
* @private
*/
function _share(channel, ast){
amigo.consumeToken(ast, {shareChannel: channel}, function(err,token){
if (err) {
console.warn(err);
return false;
}
var link = getLink(token);
switch (channel) {
case "facebook":
window.open("https://www.facebook.com/sharer/sharer.php?u=" + link, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
break;
case "email":
window.location.href = getEmail("Check it out: Extreme Walrus Juice", productDetail+link);
break;
case "vk":
window.open("http://vk.com/share.php?url="+link, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
break;
case "g-plus":
window.open("https://plus.google.com/share?url="+link,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
break;
case "touch":
window.location = link;
break;
default:
return false;
}
});
return true;
}
/**
* Returns a mailto: link with specified subject and body
* @param subject subject of the email
* @param body body of the email
* @returns {string} link which will open native mail client
*/
function getEmail(subject, body){
return "mailto:?subject="+subject+"&body="+body;
}