This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
spark-browser.js
106 lines (88 loc) · 2.78 KB
/
spark-browser.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
var $ = require('jquery');
var jModal = require('../vendor/jquery.modal.min.js');
var cssify = require('cssify');
require('./spark-browser-style.css');
window.sparkLogin = function(callback) {
addLoginButton();
addLoginForm();
addBehaviour(callback);
};
window.spark = require('./spark.js');
$.modal.defaults.modalClass = "spark-modal";
function addLoginButton() {
var btn = document.createElement("button");
btn.id = 'spark-login-button';
btn.className = 'spark-login-button';
btn.appendChild(document.createTextNode("Login to Spark"));
btn.onclick = function() {
$('#spark-login-form').modal();
};
if( $('#spark-login').length ) {
$('#spark-login').append(btn);
} else {
document.body.appendChild(btn);
}
}
function addLoginForm() {
var form = document.createElement('form');
form.id = 'spark-login-form';
form.className = 'spark-login-modal';
form.appendChild(generateError());
form.appendChild(generateInput('email', 'text'));
form.appendChild(generateInput('password', 'password'));
form.appendChild(generateButton());
document.body.appendChild(form);
}
function addBehaviour(callback) {
$('#spark-login-form-button').click(function(e) {
e.preventDefault();
var user = $('#spark-login-form-email').val();
var pass = $('#spark-login-form-password').val();
var loginPromise = window.spark.login({ username: user, password: pass });
loginPromise.then(
function(data) {
callback(data);
$('#spark-login-form-email').val('');
$('#spark-login-form-password').val('');
displayErrorMessage('');
$('#spark-login-form-error').hide();
$.modal.close();
},
function(error) {
if (error.message === 'invalid_client' || error.message === 'invalid_grant') {
displayErrorMessage('Invalid username or password.');
} else if (error.cors === 'rejected') {
displayErrorMessage('Request rejected.');
} else {
displayErrorMessage('Unknown error.');
console.log(error);
}
}
);
});
}
function generateError() {
var div = document.createElement("div");
div.id = 'spark-login-form-error';
div.className = 'spark-login-error';
return div;
}
function generateInput(name, type) {
var input = document.createElement("input");
input.id = 'spark-login-form-' + name;
input.type = type;
input.className = 'spark-login-input';
input.placeholder = name;
return input;
}
function generateButton() {
var btn = document.createElement("button");
btn.id = 'spark-login-form-button';
btn.className = 'spark-login-button';
btn.appendChild(document.createTextNode("log in"));
return btn;
}
function displayErrorMessage(message) {
$('#spark-login-form-error').show();
$('#spark-login-form-error').text(message);
}