LoginView.js
7.42 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2014 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals Screens */
define('ux/views/LoginView', [
'underscore',
'jquery',
'context',
'util/Util',
'views/BaseView'
], function(_, $, Context, Util, BaseView) {
'use strict';
var DEFAULT_OPTIONS = {
defaultUser: '',
defaultPassword: ''
};
var LoginView = BaseView.extend(/** @lends LoginView.prototype */{
templateLoginForm: function(width, height) {
var tmpl = _.template('<div class="screens-Login-screen" style="width:<%- width %>px; height:<%- height %>px">' +
'<div class="u-screens-alignHorizontal">' +
'<div class="u-screens-alignVertical">' +
'<div class="screens-Login-wrapper">' +
'<div class="screens-Login-title"/>' +
'<form id="login-form" autocomplete="off">' +
'<input disabled type="text" name="username" placeholder="username" spellcheck="false" value="">' +
'<input disabled type="password" name="password" placeholder="password" value="">' +
'<button type="submit" class="screens-Login-button" value="" />' +
'</form>' +
'</div>' +
'</div>' +
'</div>' +
'</div>');
return tmpl({
width: width,
height: height
});
},
template: function(width, height) {
var tmpl = _.template('<div class="screens-Login-screen" style="width:<%- width %>px; height:<%- height %>px"/>');
return tmpl({
width: width,
height: height
});
},
className: 'screens-Login',
events: {
'submit #login-form': '_handleLogin'
},
sistineOptions: {
events: {
'press .screens-Login-button': '_handleLoginButtonUI',
'tap .screens-Login-button': '_handleLogin'
},
recognizers: [
'press', 'tap'
]
},
/**
* @classdesc View that renders the login screen
* @class LoginView
* @extends BaseView
*
* @param {Object} [options] An object of configurable options.
* @param {String} [options.defaultUser] Default user name for prefilled inputs
* @param {String} [options.defaultPassword] Default password name for prefilled inputs
*/
constructor: function(options) {
this._initOptions(options, DEFAULT_OPTIONS);
LoginView.__super__.constructor.apply(this, arguments);
},
/**
* Set focus on the input
*/
focus: function() {
this.$el.find('#login-form input[name="username"]').focus();
},
render: function() {
// get tile that spans the position of the event. assume only horizontal tiles.
// based on the number of tiles, we figure out where to place the login form
// so that they don't end up in the middle of 2 screens
var tiles = Context.appView.getTiles();
var idx = 0;
if (tiles.length % 2 === 0) {
idx = (tiles.length * 0.5) - 1;
} else {
idx = Math.floor(tiles.length * 0.5);
}
for (var i = 0; i < tiles.length; i++) {
if (i !== idx) {
this.$el.append(this.template(tiles[i].width, tiles[i].height));
} else {
this.$el.append(this.templateLoginForm(tiles[i].width, tiles[i].height));
}
}
return this;
},
/**
* Resets the login form.
*
* @returns {LoginView} The login view
*/
reset: function() {
this._showErrorMode(false);
this.$el.find('#login-form').get(0).reset();
this.$el.find('#login-form input[name="username"]').val(this.options.defaultUser);
this.$el.find('#login-form input[name="password"]').val(this.options.defaultPassword);
return this;
},
enable: function() {
$('INPUT', this.$el).prop('disabled', false);
return this;
},
disable: function() {
$('INPUT', this.$el).prop('disabled', true);
return this;
},
/**
* hide this view
* @returns {LoginView} this
*/
hide: function() {
this.$el.hide();
return this.disable();
},
/**
* show this view
* @returns {LoginView} this
*/
show: function() {
// also enable inputs for iOS safari
this.$el.show();
return this.enable();
},
handleLoginFailed: function() {
this._showErrorMode(true);
},
/**
* reposition the login elements so that they are placed right in
* the middle of the screen vertically
*/
_centerElements: function() {
var wrapper = this.$el.find('.ctx-Login-wrapper');
var loginButton = this.$el.find('.ctx-Login-button');
var midButton = loginButton.position().top + loginButton.innerHeight() * 0.5;
var marginTop = (window.innerHeight * 0.5) - midButton - 8;
wrapper.css({marginTop: marginTop + 'px'});
},
_handleLogin: function(ev) {
var user = this.$el.find('#login-form input[name="username"]').val();
var pass = this.$el.find('#login-form input[name="password"]').val();
if (user && pass) {
// todo: maybe handle login-response with callback
Screens.ws.emit('login', {
user: user,
pass: pass
});
}
return false;
},
_handleLoginButtonUI: function(ev) {
Util.buttonPressedUIHandler(ev);
},
/**
* show error or normal mode
* @param {Boolean} isError is true, we are showing the error mode, otherwise, show normal mode
*/
_showErrorMode: function(isError) {
var loginTitleEl = this.$el.find('.ctx-Login-title');
var user = this.$el.find('#login-form input[name="username"]');
var pass = this.$el.find('#login-form input[name="password"]');
if (isError) {
loginTitleEl.addClass('error');
user.addClass('error');
pass.addClass('error');
loginTitleEl.text('Invalid username or password');
} else {
loginTitleEl.removeClass('error');
user.removeClass('error');
pass.removeClass('error');
loginTitleEl.text('sign in to aem');
}
}
});
// return module exports
return LoginView;
});