-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (102 loc) · 2.62 KB
/
index.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
var load = require('script-loader')
var domify = require('domify')
var classes = require('classes')
var Emitter = require('emitter')
var debug = require('debug')
var template = require('./template').trim()
var nextTick = require('next-tick')
var persona = undefined
var log = debug('persona')
/**
* Singleton Factory
*/
module.exports = function() {
persona = persona || new Persona()
persona.load()
return persona
}
function Persona() {
}
Emitter(Persona.prototype)
Persona.prototype.load = function(fn) {
if (this.loaded) return
fn = fn || function() {}
this.loader = load("https://login.persona.org/include.js")
this.loader.onLoad(function() {
log('loaded persona')
this.init()
this.loaded = true
this.emit('loaded')
if (this.doLogin && !this.doLogout) this.login()
if (this.doLogout) this.logout()
}.bind(this))
return this
}
Persona.prototype.init = function() {
navigator.id.watch({
onlogin: function(assertion) {
log('logged in', arguments)
this.emit('login', assertion)
}.bind(this),
onlogout: function() {
log('logged out', arguments)
this.emit('logout')
}.bind(this)
});
}
Persona.prototype.button = function(options) {
var button = new PersonaButton(options)
nextTick(button.render.bind(button))
return button
}
Persona.prototype.login = function() {
if (!navigator.id) return this.doLogin = true // do later
return navigator.id.request()
}
Persona.prototype.logout = function() {
if (!navigator.id) return this.doLogout = true // do later
return navigator.id.request()
}
function PersonaButton(options) {
if (options instanceof HTMLElement) this.el = options
options = options || {}
this.color = options.color || 'black'
this.style = options.style || 'persona'
this.el = options.el || this.el || domify(template)[0]
this.el.addEventListener('click', function(e) {
e.preventDefault()
e.stopImmediatePropagation()
persona.login()
}.bind(this))
}
PersonaButton.prototype.black = function() {
this.color = 'black'
return this
}
PersonaButton.prototype.blue = function() {
this.color = 'blue'
return this
}
PersonaButton.prototype.red = function() {
this.color = 'red'
return this
}
PersonaButton.prototype.persona = function() {
this.style = 'persona'
return this
}
PersonaButton.prototype.email = function() {
this.style = 'email'
return this
}
PersonaButton.prototype.plain = function() {
this.style = 'plain'
return this
}
PersonaButton.prototype.render = function() {
classes(this.el)
.add('persona-button')
.add('persona-' + this.style)
.add('persona-' + this.color)
return this
}