forked from mozilla/persona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
153 lines (122 loc) · 3.62 KB
/
page.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
/*jshint browser:true, jQuery: true, forin: true, laxbreak:true */
/*global BrowserID: true*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
BrowserID.Modules = BrowserID.Modules || {};
BrowserID.Modules.PageModule = (function() {
"use strict";
var ANIMATION_TIME = 250,
bid = BrowserID,
dom = bid.DOM,
screens = bid.Screens,
mediator = bid.Mediator;
function onSubmit(event) {
event.stopPropagation();
event.preventDefault();
if (this.validate()) {
this.submit();
}
return false;
}
var PageController = BrowserID.Class({
init: function(options) {
options = options || {};
var self=this;
self.domEvents = [];
if(options.bodyTemplate) {
self.renderDialog(options.bodyTemplate, options.bodyVars);
}
if(options.waitTemplate) {
self.renderWait(options.waitTemplate, options.waitVars);
}
if(options.errorTemplate) {
self.renderError(options.errorTemplate, options.errorVars);
}
},
checkRequired: function(options) {
var list = [].slice.call(arguments, 1);
for(var item, index = 0; item = list[index]; ++index) {
if(!options.hasOwnProperty(item)) {
throw "missing config option: " + item;
}
}
},
start: function(options) {
var self=this;
self.bind("form", "submit", onSubmit);
self.bind("#thisIsNotMe", "click", self.close.bind(self, "notme"));
},
stop: function() {
this.unbindAll();
dom.removeClass("body", "waiting");
},
destroy: function() {
this.stop();
},
bind: function(target, type, callback, context) {
var self=this,
cb = callback.bind(context || this);
dom.bindEvent(target, type, cb);
self.domEvents.push({
target: target,
type: type,
cb: cb
});
},
unbindAll: function() {
var self=this,
evt;
while(evt = self.domEvents.pop()) {
dom.unbindEvent(evt.target, evt.type, evt.cb);
}
},
renderDialog: function(body, body_vars) {
screens.wait.hide();
screens.error.hide();
screens.form.show(body, body_vars);
dom.focus("input:visible:not(:disabled):eq(0)");
},
renderWait: function(body, body_vars) {
screens.wait.show(body, body_vars);
},
renderError: function(body, body_vars, oncomplete) {
screens.error.show(body, body_vars);
bid.ErrorDisplay.start();
$("#error").stop().css('opacity', 1).hide().fadeIn(ANIMATION_TIME, function() {
if(oncomplete) oncomplete(false);
});
},
validate: function() {
return true;
},
submit: function() {
},
close: function(message, data) {
this.destroy();
if (message) {
this.publish(message, data);
}
},
publish: function(message, data) {
mediator.publish(message, data);
},
/**
* Get a curried function to an error dialog.
* @method getErrorDialog
* @method {object} action - info to use for the error dialog. Should have
* @method {function} [onerror] - callback to call after the
* error has been displayed.
* two fields, message, description.
*/
getErrorDialog: function(action, onerror) {
var self=this;
return function(lowLevelInfo) {
self.renderError("error", $.extend({
action: action
}, lowLevelInfo), onerror);
};
}
});
return PageController;
}());