Improve JavaScript coding style

To comitted code now passes the Airbnb pattern using jscs
This commit is contained in:
Francis Lachapelle 2014-09-24 16:06:12 -04:00
parent df37fd8b92
commit 342441ecf8
7 changed files with 844 additions and 831 deletions

View file

@ -1,4 +1,4 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* JavaScript for SOGoContacts */
(function() {
@ -7,32 +7,32 @@
angular.module('SOGo.Authentication', [])
.constant('passwordPolicyConfig', {
'PolicyPasswordChangeUnsupported': -3,
'PolicyPasswordSystemUnknown': -2,
'PolicyPasswordUnknown': -1,
'PolicyPasswordExpired': 0,
'PolicyAccountLocked': 1,
'PolicyChangeAfterReset': 2,
'PolicyPasswordModNotAllowed': 3,
'PolicyMustSupplyOldPassword': 4,
'PolicyInsufficientPasswordQuality': 5,
'PolicyPasswordTooShort': 6,
'PolicyPasswordTooYoung': 7,
'PolicyPasswordInHistory': 8,
'PolicyNoError': 65535
PolicyPasswordChangeUnsupported: -3,
PolicyPasswordSystemUnknown: -2,
PolicyPasswordUnknown: -1,
PolicyPasswordExpired: 0,
PolicyAccountLocked: 1,
PolicyChangeAfterReset: 2,
PolicyPasswordModNotAllowed: 3,
PolicyMustSupplyOldPassword: 4,
PolicyInsufficientPasswordQuality: 5,
PolicyPasswordTooShort: 6,
PolicyPasswordTooYoung: 7,
PolicyPasswordInHistory: 8,
PolicyNoError: 65535
})
// TODO: convert to a Factory recipe?
.provider('Authentication', function(passwordPolicyConfig) {
this.readCookie = function(name) {
var foundCookie = null;
var prefix = name + "=";
var pairs = document.cookie.split(';');
for (var i = 0; !foundCookie && i < pairs.length; i++) {
var currentPair = pairs[i];
var start = 0;
while (currentPair.charAt(start) == " ")
var foundCookie, prefix, pairs, i, currentPair, start;
foundCookie = null;
prefix = name + '=';
pairs = document.cookie.split(';');
for (i = 0; !foundCookie && i < pairs.length; i++) {
currentPair = pairs[i];
start = 0;
while (currentPair.charAt(start) == ' ')
start++;
if (start > 0)
currentPair = currentPair.substr(start);
@ -44,37 +44,38 @@
};
this.readLoginCookie = function() {
var loginValues = null;
var cookie = this.readCookie("0xHIGHFLYxSOGo");
var loginValues = null,
cookie = this.readCookie('0xHIGHFLYxSOGo'),
value;
if (cookie && cookie.length > 8) {
var value = decodeURIComponent(cookie.substr(8));
loginValues = value.base64decode().split(":");
value = decodeURIComponent(cookie.substr(8));
loginValues = value.base64decode().split(':');
}
return loginValues;
};
this.redirectUrl = function(username, domain) {
var userName = username;
var userName, address, baseAddress, altBaseAddress, parts, hostpart, protocol, newAddress;
userName = username;
if (domain)
userName += '@' + domain.value;
var address = "" + window.location.href;
var baseAddress = ApplicationBaseURL + "/" + encodeURIComponent(userName);
var altBaseAddress;
if (baseAddress[0] == "/") {
var parts = address.split("/");
var hostpart = parts[2];
var protocol = parts[0];
baseAddress = protocol + "//" + hostpart + baseAddress;
address = '' + window.location.href;
baseAddress = ApplicationBaseURL + '/' + encodeURIComponent(userName);
if (baseAddress[0] == '/') {
parts = address.split('/');
hostpart = parts[2];
protocol = parts[0];
baseAddress = protocol + '//' + hostpart + baseAddress;
}
var altBaseAddress;
var parts = baseAddress.split("/");
parts = baseAddress.split('/');
parts.splice(0, 3);
altBaseAddress = parts.join("/");
var newAddress;
altBaseAddress = parts.join('/');
newAddress;
if ((address.startsWith(baseAddress)
|| address.startsWith(altBaseAddress))
&& !address.endsWith("/logoff")) {
&& !address.endsWith('/logoff')) {
newAddress = address;
} else {
newAddress = baseAddress;
@ -91,8 +92,9 @@
};
this.$get = ['$q', '$http', function($q, $http) {
var self = this;
var service = {
var _this = this, service;
service = {
// login: function(username, password, domain, language, rememberLogin) {
// var d = $q.defer();
login: function(data) {
@ -114,48 +116,50 @@
$http({
method: 'POST',
url: '/SOGo/connect',
data: {'userName': username,
'password': password,
'domain': domain,
'language': language,
'rememberLogin': rememberLogin}
data: {
userName: username,
password: password,
domain: domain,
language: language,
rememberLogin: rememberLogin
}
}).success(function(data, status) {
// Make sure browser's cookies are enabled
var loginCookie = self.readLoginCookie();
var loginCookie = _this.readLoginCookie();
if (!loginCookie) {
d.reject(l("cookiesNotEnabled"));
d.reject(l('cookiesNotEnabled'));
}
else {
// Check password policy
if (typeof(data['expire']) != 'undefined' && typeof(data['grace']) != 'undefined') {
if (data['expire'] < 0 && data['grace'] > 0) {
d.reject({'grace': data['grace']});
if (typeof data.expire != 'undefined' && typeof data.grace != 'undefined') {
if (data.expire < 0 && data.grace > 0) {
d.reject({grace: data.grace});
//showPasswordDialog('grace', createPasswordGraceDialog, data['grace']);
} else if (data['expire'] > 0 && data['grace'] == -1) {
d.reject({'expire': data['expire']});
} else if (data.expire > 0 && data.grace == -1) {
d.reject({expire: data.expire});
//showPasswordDialog('expiration', createPasswordExpirationDialog, data['expire']);
}
else {
d.resolve(self.redirectUrl(username, domain));
d.resolve(_this.redirectUrl(username, domain));
}
}
else {
d.resolve(self.redirectUrl(username, domain));
d.resolve(_this.redirectUrl(username, domain));
}
}
}).error(function(data, status) {
var msg;
if (data && data['LDAPPasswordPolicyError']) {
var perr = data['LDAPPasswordPolicyError'];
var msg, perr;
if (data && data.LDAPPasswordPolicyError) {
perr = data.LDAPPasswordPolicyError;
if (perr == passwordPolicyConfig.PolicyNoError) {
msg = l("Wrong username or password.");
msg = l('Wrong username or password.');
}
else {
msg = l("Login failed due to unhandled error case: " + perr);
msg = l('Login failed due to unhandled error case: ' + perr);
}
}
else {
msg = l("Unhandled error response");
msg = l('Unhandled error response');
}
d.reject({error: msg});
});

View file

@ -1,3 +1,5 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
@ -53,8 +55,8 @@
};
Resource.prototype.newguid = function(uid) {
var deferred = this._q.defer();
var path = this._path + '/' + uid + '/newguid';
var deferred = this._q.defer(),
path = this._path + '/' + uid + '/newguid';
this._http
.get(path)
@ -71,11 +73,11 @@
* @param {string} name - the new resource's name
*/
Resource.prototype.create = function(action, name) {
var deferred = this._q.defer();
var path = this._path + '/' + action;
var deferred = this._q.defer(),
path = this._path + '/' + action;
this._http
.post(path, { 'name': name })
.post(path, { name: name })
.success(deferred.resolve)
.error(deferred.reject);
@ -83,9 +85,9 @@
};
Resource.prototype.save = function(uid, newValue, options) {
var deferred = this._q.defer();
var action = (options && options.action)? options.action : 'save';
var path = this._path + '/' + uid + '/' + action;
var deferred = this._q.defer(),
action = (options && options.action)? options.action : 'save',
path = this._path + '/' + uid + '/' + action;
this._http
.post(path, newValue)
@ -96,8 +98,8 @@
};
Resource.prototype.remove = function(uid) {
var deferred = this._q.defer();
var path = this._path + '/' + uid + '/delete';
var deferred = this._q.defer(),
path = this._path + '/' + uid + '/delete';
this._http
.get(path)

View file

@ -1,4 +1,4 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* JavaScript for common UI services */
(function() {
@ -45,7 +45,7 @@
*/
Dialog.confirm = function(title, content) {
var d = this.$q.defer();
var modal = this.$modal.open({
this.$modal.open({
template:
'<h2 data-ng-bind-html="title"></h2>' +
'<p data-ng-bind-html="content"></p>' +
@ -70,9 +70,9 @@
};
Dialog.prompt = function(title, inputPlaceholder, options) {
var o = options || {};
var d = this.$q.defer();
var modal = this.$modal.open({
var o = options || {},
d = this.$q.defer();
this.$modal.open({
template:
'<h2 ng-bind-html="title"></h2>' +
'<form><input type="' + (o.inputType || 'text')

View file

@ -1,4 +1,4 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* JavaScript for common UI services for mobile theme */
(function() {

View file

@ -1,3 +1,5 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
@ -16,6 +18,7 @@
// Get UID from the server.
var newCardData = Card.$$resource.newguid(this.pid);
this.$unwrap(newCardData);
this.isNew = true;
}
}
else {
@ -62,10 +65,10 @@
scope: { data: '=sgAddress' },
controller: ['$scope', function($scope) {
$scope.addressLines = function(data) {
var lines = [];
var lines = [],
locality_region = [];
if (data.street) lines.push(data.street);
if (data.street2) lines.push(data.street2);
var locality_region = [];
if (data.locality) locality_region.push(data.locality);
if (data.region) locality_region.push(data.region);
if (locality_region.length > 0) lines.push(locality_region.join(', '));
@ -137,7 +140,7 @@
//var action = 'saveAs' + this.tag.substring(1).capitalize();
return Card.$$resource.save([this.pid, this.id || '_new_'].join('/'),
this.$omit(),
{ 'action': action })
{ action: action })
.then(function(data) {
return data;
});
@ -156,13 +159,13 @@
};
Card.prototype.$fullname = function() {
var fn = this.fn || '';
var fn = this.fn || '', names;
if (fn.length == 0) {
var names = [];
names = [];
if (this.givenname && this.givenname.length > 0)
names.push(this.givenname);
if (this.nickname && this.nickname.length > 0)
names.push("<em>" + this.nickname + "</em>");
names.push('<em>' + this.nickname + '</em>');
if (this.sn && this.sn.length > 0)
names.push(this.sn);
if (names.length > 0)
@ -204,9 +207,9 @@
* @returns the first email address of type "pref" or the first address if none found
*/
Card.prototype.$preferredEmail = function(partial) {
var email;
var email, re;
if (partial) {
var re = new RegExp(partial);
re = new RegExp(partial);
email = _.find(this.emails, function(o) {
return re.test(o.value);
});
@ -239,8 +242,8 @@
* @returns the fullname along with a matching email address in parentheses
*/
Card.prototype.$shortFormat = function(partial) {
var fullname = this.$fullname();
var email = this.$preferredEmail(partial);
var fullname = this.$fullname(),
email = this.$preferredEmail(partial);
if (email && email != fullname)
fullname += ' (' + email + ')';
return fullname;
@ -365,20 +368,24 @@
* @param {Card} card
*/
Card.prototype.$updateMember = function(index, email, card) {
var ref = {'email': email, 'reference': card.c_name, 'fn': card.$fullname()};
var ref = {email: email, reference: card.c_name, fn: card.$fullname()};
this.refs[index] = ref;
};
// Unwrap a promise
Card.prototype.$unwrap = function(futureCardData) {
var self = this;
var _this = this;
if (futureCardData) {
this.$futureCardData = futureCardData;
this.$futureCardData.then(function(data) {
}
return this.$futureCardData.then(function(data) {
// The success callback. Calling _.extend from $timeout will wrap it into a try/catch call and return
// a promise resolved immediately.
Card.$timeout(function() {
angular.extend(self, data);
return Card.$timeout(function() {
angular.extend(_this, data);
console.debug(angular.toJson(data));
return _this;
});
});
};

View file

@ -1,4 +1,4 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* JavaScript for SOGoRootPage */
(function() {
@ -8,7 +8,7 @@
.controller('loginController', ['$scope', 'Authentication', function($scope, Authentication) {
$scope.warning = false;
$scope.creds = { 'username': null, 'password': null };
$scope.creds = { username: null, password: null };
$scope.login = function(creds) {
$scope.warning = false;
Authentication.login(creds)