Merge "JSCS cleanup - horizon/static/framework conf and util"

This commit is contained in:
Jenkins 2015-06-19 14:51:04 +00:00 committed by Gerrit Code Review
commit bf9acb39a5
14 changed files with 581 additions and 607 deletions

View File

@ -1,8 +1,8 @@
/*global angular*/
(function () {
'use strict';
angular.module('horizon.framework.conf', [])
angular
.module('horizon.framework.conf', [])
.constant('horizon.framework.conf.spinner_options', {
inline: {
lines: 10,
@ -33,4 +33,4 @@
trail: 50
}
});
}());
})();

View File

@ -1,16 +1,17 @@
(function () {
'use strict';
angular.module('horizon.framework', [
angular
.module('horizon.framework', [
'horizon.framework.conf',
'horizon.framework.util',
'horizon.framework.widgets'
])
.constant('horizon.framework.basePath', '/static/framework/')
.config(['$interpolateProvider', '$httpProvider',
.config([
'$interpolateProvider',
'$httpProvider',
function ($interpolateProvider, $httpProvider) {
// Replacing the default angular symbol
// allow us to mix angular with django templates
$interpolateProvider.startSymbol('{$');
@ -35,5 +36,6 @@
}
};
});
}]);
}
]);
})();

View File

@ -1,4 +1,4 @@
(function() {
(function () {
'use strict';
/**
@ -41,12 +41,12 @@
* </tr>
* ```
*/
.directive('bindScope', function() {
.directive('bindScope', function () {
return {
restrict: 'A',
link: function(scope, element, attrs, ctrl, transclude) {
link: function (scope, element, attrs, ctrl, transclude) {
if (transclude) {
transclude(scope, function(clone) {
transclude(scope, function (clone) {
var detailElt = element.find('[bind-scope-target]');
if (detailElt.length) {
detailElt.append(clone);
@ -56,5 +56,4 @@
}
};
});
})();

View File

@ -1,22 +1,21 @@
(function() {
(function () {
'use strict';
describe('horizon.framework.util.bind-scope module', function() {
it('should have been defined', function() {
describe('horizon.framework.util.bind-scope module', function () {
it('should have been defined', function () {
expect(angular.module('horizon.framework.util.bind-scope')).toBeDefined();
});
});
describe('bind-scope directive', function() {
describe('bind-scope directive', function () {
var $scope, $element;
beforeEach(module('horizon.framework'));
beforeEach(module('horizon.framework.widgets'));
beforeEach(module('horizon.framework.util.bind-scope'));
beforeEach(module('horizon.framework.util.bind-scope', function($compileProvider) {
$compileProvider.directive('testBindScope', function() {
beforeEach(module('horizon.framework.util.bind-scope', function ($compileProvider) {
$compileProvider.directive('testBindScope', function () {
return {
restrict: 'E',
scope: {
@ -30,7 +29,7 @@
});
}));
beforeEach(inject(function($injector) {
beforeEach(inject(function ($injector) {
var $compile = $injector.get('$compile');
$scope = $injector.get('$rootScope').$new();
@ -48,17 +47,15 @@
$scope.$digest();
}));
it('should have 3 list items', function() {
it('should have 3 list items', function () {
expect($element.find('li').length).toBe(3);
});
it('should have 3 list items with values "cat", "dog" and "fish"', function() {
it('should have 3 list items with values "cat", "dog" and "fish"', function () {
var listItems = $element.find('li');
expect(listItems[0].textContent.trim()).toBe('cat');
expect(listItems[1].textContent.trim()).toBe('dog');
expect(listItems[2].textContent.trim()).toBe('fish');
});
});
})();

View File

@ -32,8 +32,8 @@
* Evaluates given input for standard truthiness and returns translation
* of 'Yes' and 'No' for true/false respectively.
*/
.filter('yesno', ['horizon.framework.util.i18n.gettext', function(gettext) {
return function(input) {
.filter('yesno', ['horizon.framework.util.i18n.gettext', function (gettext) {
return function (input) {
return (input ? gettext("Yes") : gettext("No"));
};
}])
@ -45,8 +45,8 @@
* Expects numeric value and suffixes translated 'GB' with spacing.
* Returns empty string if input is not a number or is null.
*/
.filter('gb', function() {
return function(input) {
.filter('gb', function () {
return function (input) {
if (isNaN(input) || null === input) {
return '';
} else {
@ -62,8 +62,8 @@
* Expects numeric value and suffixes translated 'MB' with spacing.
* Returns empty string if input is not a number or is null.
*/
.filter('mb', function() {
return function(input) {
.filter('mb', function () {
return function (input) {
if (isNaN(input) || null === input) {
return '';
} else {
@ -78,12 +78,12 @@
* @description
* Capitalizes leading characters of individual words.
*/
.filter('title', function() {
return function(input) {
.filter('title', function () {
return function (input) {
if (typeof input !== 'string') {
return input;
}
return input.replace(/(?:^|\s)\S/g, function(a) {
return input.replace(/(?:^|\s)\S/g, function (a) {
return a.toUpperCase();
});
};
@ -95,8 +95,8 @@
* @description
* Replaces all underscores with spaces.
*/
.filter('noUnderscore', function() {
return function(input) {
.filter('noUnderscore', function () {
return function (input) {
if (typeof input !== 'string') {
return input;
}
@ -112,8 +112,8 @@
* in given mapping, return key. This is useful when translations for
* codes are present.
*/
.filter('decode', function() {
return function(input, mapping) {
.filter('decode', function () {
return function (input, mapping) {
var val = mapping[input];
return angular.isDefined(val) ? val : input;
};
@ -126,22 +126,22 @@
* Returns a human-readable approximation of the input of bytes,
* converted to a useful unit of measure. Uses 1024-based notation.
*/
.filter('bytes', function() {
return function(input) {
.filter('bytes', function () {
return function (input) {
var kb = 1024;
var mb = kb*1024;
var gb = mb*1024;
var tb = gb*1024;
var mb = kb * 1024;
var gb = mb * 1024;
var tb = gb * 1024;
if (isNaN(input) || null === input || input < 0) {
return '';
} else if (input >= tb) {
return interpolate(gettext("%s TB"), [Number(input/tb).toFixed(2)]);
return interpolate(gettext("%s TB"), [Number(input / tb).toFixed(2)]);
} else if (input >= gb) {
return interpolate(gettext("%s GB"), [Number(input/gb).toFixed(2)]);
return interpolate(gettext("%s GB"), [Number(input / gb).toFixed(2)]);
} else if (input >= mb) {
return interpolate(gettext("%s MB"), [Number(input/mb).toFixed(2)]);
return interpolate(gettext("%s MB"), [Number(input / mb).toFixed(2)]);
} else if (input >= kb) {
return interpolate(gettext("%s KB"), [Number(input/kb).toFixed(2)]);
return interpolate(gettext("%s KB"), [Number(input / kb).toFixed(2)]);
} else {
return interpolate(gettext("%s bytes"), [Math.floor(input)]);
}
@ -155,11 +155,11 @@
* Displays translated count in table footer.
* Takes only finite numbers.
*/
.filter('itemCount', function() {
return function(input) {
.filter('itemCount', function () {
return function (input) {
var isNumeric = (input !== null && isFinite(input));
var number = isNumeric ? Math.round(input): 0;
var count = (number > 0) ? number: 0;
var number = isNumeric ? Math.round(input) : 0;
var count = (number > 0) ? number : 0;
var format = ngettext('Displaying %s item', 'Displaying %s items', count);
return interpolate(format, [count]);
};
@ -171,11 +171,10 @@
* @description
* Returns translated text.
*/
.filter('trans', ['horizon.framework.util.i18n.gettext', function(gettextFunc) {
return function(input) {
.filter('trans', ['horizon.framework.util.i18n.gettext', function (gettextFunc) {
return function (input) {
// NOTE: uses 'gettextFunc' to avoid message collection.
return gettextFunc(input);
};
}]);
}());

View File

@ -1,6 +1,7 @@
describe('horizon.framework.util.filters', function () {
(function () {
'use strict';
describe('horizon.framework.util.filters', function () {
beforeEach(module('horizon.framework.util.i18n'));
beforeEach(module('horizon.framework.util.filters'));
@ -36,7 +37,6 @@ describe('horizon.framework.util.filters', function () {
expect(yesnoFilter(0)).toBe('No');
expect(yesnoFilter('')).toBe('No');
});
});
describe('gb', function () {
@ -58,7 +58,6 @@ describe('horizon.framework.util.filters', function () {
it('returns empty string for null', function () {
expect(gbFilter(null)).toBe('');
});
});
describe('mb', function () {
@ -80,7 +79,6 @@ describe('horizon.framework.util.filters', function () {
it('returns empty string for null', function () {
expect(mbFilter(null)).toBe('');
});
});
describe('title', function () {
@ -107,7 +105,6 @@ describe('horizon.framework.util.filters', function () {
it('handles strings beginning with numbers', function () {
expect(titleFilter('3abc')).toBe('3abc');
});
});
describe('noUnderscore', function () {
@ -128,7 +125,6 @@ describe('horizon.framework.util.filters', function () {
expect(noUnderscoreFilter('')).toBe('');
expect(noUnderscoreFilter(21)).toBe(21);
});
});
describe("decode", function () {
@ -148,7 +144,6 @@ describe('horizon.framework.util.filters', function () {
it("Returns input when key is not present", function () {
expect(decodeFilter('NOT_PRESENT', {'PRESENT': 'Here'})).toBe('NOT_PRESENT');
});
});
describe('bytes', function () {
@ -183,11 +178,9 @@ describe('horizon.framework.util.filters', function () {
expect(bytesFilter('Yo!')).toBe('');
expect(bytesFilter(null)).toBe('');
});
});
describe('itemCount', function () {
it('should return translated text with item count',
inject(function (itemCountFilter) {
expect(itemCountFilter(null)).toBe('Displaying 0 items');
@ -207,14 +200,12 @@ describe('horizon.framework.util.filters', function () {
expect(itemCountFilter(-1.2)).toBe('Displaying 0 items');
})
);
});
describe('trans', function() {
it('should return translated text', inject(function(transFilter) {
describe('trans', function () {
it('should return translated text', inject(function (transFilter) {
expect(transFilter("Howdy")).toBe("Howdy");
}));
});
});
});
})();

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
(function () {
'use strict';
angular.module("horizon.framework.util.i18n", [])
angular.module('horizon.framework.util.i18n', [])
/*
* @name horizon.framework.util.i18n.gettext
@ -37,19 +36,14 @@
* (translation) from filters, which are arguably more
* presentation-oriented.
*/
.factory("horizon.framework.util.i18n.gettext", ['$window', function($window) {
.factory('horizon.framework.util.i18n.gettext', ['$window', function ($window) {
// If no global function, revert to just returning given text.
var gettextFunc = $window.gettext || function(x) { return x; };
var gettextFunc = $window.gettext || function (x) { return x; };
// Eventually, could delete the window gettext references here,
// or provide an appropriate method.
return function() {
return function () {
return gettextFunc.apply(this, arguments);
};
}]);
})();

View File

@ -13,52 +13,46 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
(function () {
'use strict';
describe('horizon.framework.util.i18n', function() {
describe('horizon.framework.util.i18n', function () {
beforeEach(module('horizon.framework.util.i18n'));
describe('gettext', function() {
describe('gettext', function () {
var factory;
describe('Normal operation', function() {
beforeEach(inject(function($injector) {
describe('Normal operation', function () {
beforeEach(inject(function ($injector) {
factory = $injector.get("horizon.framework.util.i18n.gettext");
}));
it('defines the factory', function() {
it('defines the factory', function () {
expect(factory).toBeDefined();
});
it('function returns what is given', function() {
it('function returns what is given', function () {
expect(factory("Hello")).toBe('Hello');
});
});
describe("injected window.gettext", function() {
beforeEach(module(function($provide) {
var $window = {gettext: function(x) { return x.replace(/good/, 'bad'); }};
describe("injected window.gettext", function () {
beforeEach(module(function ($provide) {
var $window = { gettext: function (x) { return x.replace(/good/, 'bad'); } };
$provide.value('$window', $window);
}));
// Get the factory by name.
beforeEach(inject(function($injector) {
beforeEach(inject(function ($injector) {
factory = $injector.get("horizon.framework.util.i18n.gettext");
}));
it('uses the window gettext when available', function() {
it('uses the window gettext when available', function () {
// we can't spy on the window gettext due to (appropriate)
// indirection. But we can make sure it was called.
expect(factory("good cop")).toBe("bad cop");
});
});
});
});
})();

View File

@ -11,5 +11,4 @@
'horizon.framework.util.validators'
])
.constant('horizon.framework.util.basePath', '/static/framework/util/');
})();

View File

@ -15,6 +15,7 @@
* |---------------------------------------------------------------------------------|
* | {@link horizon.framework.util.validators.directive:validateNumberMax `validateNumberMax`} |
* | {@link horizon.framework.util.validators.directive:validateNumberMin `validateNumberMin`} |
* | {@link horizon.framework.util.validators.directive:notBlank `notBlank`} |
* | {@link horizon.framework.util.validators.directive:hzPasswordMatch `hzPasswordMatch`} |
*
*/
@ -68,8 +69,10 @@
return value;
};
// Re-validate if value is changed through the UI
// or model (programmatically)
/**
* Re-validate if value is changed through the UI
* or model (programmatically)
*/
ctrl.$parsers.push(maxValidator);
ctrl.$formatters.push(maxValidator);
@ -128,8 +131,10 @@
return value;
};
// Re-validate if value is changed through the UI
// or model (programmatically)
/**
* Re-validate if value is changed through the UI
* or model (programmatically)
*/
ctrl.$parsers.push(minValidator);
ctrl.$formatters.push(minValidator);
@ -140,6 +145,12 @@
};
}])
/**
* @ngdoc directive
* @name notBlank
* @element ng-model
* @description Ensure that the value is not blank
*/
.directive('notBlank', function () {
return {
require: 'ngModel',
@ -185,29 +196,29 @@
* Note that id and name are required for the password input.
* This directive uses the form model and id for validation check.
*/
.directive('hzPasswordMatch', function(){
.directive('hzPasswordMatch', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: { pw: '=hzPasswordMatch' },
link: function(scope, element, attr, ctrl){
link: function (scope, element, attr, ctrl) {
// helper function to check that password matches
function passwordCheck(){
scope.$apply(function(){
function passwordCheck() {
scope.$apply(function () {
var match = (ctrl.$modelValue === scope.pw.$modelValue);
ctrl.$setValidity('match', match);
});
}
// this ensures that typing in either input
// will trigger the password match
var pwElement = $('#'+scope.pw.$name);
/**
* this ensures that typing in either input
* will trigger the password match
*/
var pwElement = $('#' + scope.pw.$name);
pwElement.on('keyup change', passwordCheck);
element.on('keyup change', passwordCheck);
} // end of link
}; // end of return
}); // end of directive
}());
})();

View File

@ -1,22 +1,20 @@
(function() {
(function () {
'use strict';
describe('horizon.framework.util.validators module', function() {
it('should have been defined', function() {
describe('horizon.framework.util.validators module', function () {
it('should have been defined', function () {
expect(angular.module('horizon.framework.util.validators')).toBeDefined();
});
});
describe('validators directive', function() {
describe('validators directive', function () {
beforeEach(module('horizon.framework.widgets'));
beforeEach(module('horizon.framework.util.validators'));
describe('validateNumberMax directive', function() {
describe('validateNumberMax directive', function () {
var $scope, $form;
beforeEach(inject(function($injector) {
beforeEach(inject(function ($injector) {
var $compile = $injector.get('$compile');
$scope = $injector.get('$rootScope').$new();
@ -33,32 +31,30 @@
$scope.$digest();
}));
it('should pass validation initially when count is 0 and max is 1', function() {
it('should pass validation initially when count is 0 and max is 1', function () {
expect($form.count.$valid).toBe(true);
expect($form.$valid).toBe(true);
});
it('should not pass validation if count increased to 2 and max is 1', function() {
it('should not pass validation if count increased to 2 and max is 1', function () {
$form.count.$setViewValue(2);
$scope.$digest();
expect($form.count.$valid).toBe(false);
expect($form.$valid).toBe(false);
});
it('should pass validation if count is empty', function() {
it('should pass validation if count is empty', function () {
$form.count.$setViewValue('');
$scope.$digest();
expect($form.count.$valid).toBe(true);
expect($form.$valid).toBe(true);
});
});
describe('validateNumberMin directive', function() {
describe('validateNumberMin directive', function () {
var $scope, $form;
beforeEach(inject(function($injector) {
beforeEach(inject(function ($injector) {
var $compile = $injector.get('$compile');
$scope = $injector.get('$rootScope').$new();
@ -75,12 +71,12 @@
$scope.$digest();
}));
it('should not pass validation initially when count is 0 and min is 1', function() {
it('should not pass validation initially when count is 0 and min is 1', function () {
expect($form.count.$valid).toBe(false);
expect($form.$valid).toBe(false);
});
it('should pass validation if count increased to 2 and min is 1', function() {
it('should pass validation if count increased to 2 and min is 1', function () {
$form.count.$setViewValue(2);
$scope.$digest();
expect($scope.count).toBe(2);
@ -88,16 +84,15 @@
expect($form.$valid).toBe(true);
});
it('should pass validation if count is empty', function() {
it('should pass validation if count is empty', function () {
$form.count.$setViewValue('');
$scope.$digest();
expect($form.count.$valid).toBe(true);
expect($form.$valid).toBe(true);
});
});
describe('hzPasswordMatch directive', function() {
describe('hzPasswordMatch directive', function () {
var $compile, $rootScope;
var element, password, cpassword;
@ -108,7 +103,7 @@
'hz-password-match="form.password">' +
'</form>';
beforeEach(inject(function($injector){
beforeEach(inject(function ($injector) {
$compile = $injector.get('$compile');
$rootScope = $injector.get('$rootScope').$new();
@ -122,60 +117,57 @@
$rootScope.$digest();
}));
it('should be initially empty', function() {
it('should be initially empty', function () {
expect(password.val()).toEqual('');
expect(password.val()).toEqual(cpassword.val());
expect(cpassword.hasClass('ng-valid')).toBe(true);
});
it('should not match if user changes only password', function(done) {
it('should not match if user changes only password', function (done) {
$rootScope.user.password = 'password';
$rootScope.$digest();
cpassword.change();
setTimeout(function(){
setTimeout(function () {
expect(cpassword.val()).not.toEqual(password.val());
expect(cpassword.hasClass('ng-invalid')).toBe(true);
done();
}, 1000);
});
it('should not match if user changes only confirmation password', function(done) {
it('should not match if user changes only confirmation password', function (done) {
$rootScope.user.cpassword = 'password';
$rootScope.$digest();
cpassword.change();
setTimeout(function(){
setTimeout(function () {
expect(cpassword.val()).not.toEqual(password.val());
expect(cpassword.hasClass('ng-invalid')).toBe(true);
done();
}, 1000);
});
it('should match if both passwords are the same', function(done) {
it('should match if both passwords are the same', function (done) {
$rootScope.user.password = 'password';
$rootScope.user.cpassword = 'password';
$rootScope.$digest();
cpassword.change();
setTimeout(function(){
setTimeout(function () {
expect(cpassword.val()).toEqual(password.val());
expect(cpassword.hasClass('ng-valid')).toBe(true);
done();
}, 1000);
});
it('should not match if both passwords are different', function(done) {
it('should not match if both passwords are different', function (done) {
$rootScope.user.password = 'password123';
$rootScope.user.cpassword = 'password345';
$rootScope.$digest();
cpassword.change();
setTimeout(function(){
setTimeout(function () {
expect(cpassword.val()).not.toEqual(password.val());
expect(cpassword.hasClass('ng-invalid')).toBe(true);
done();
}, 1000);
});
}); // end of hzPasswordMatch directive
});
})();

View File

@ -97,5 +97,4 @@
};
}
]);
})();

View File

@ -8,10 +8,8 @@
});
describe('workflow factory', function () {
var workflow,
spec,
decorators = [
var workflow, spec;
var decorators = [
function (spec) {
angular.forEach(spec.steps, function (step) {
if (step.requireSomeServices) {
@ -55,5 +53,4 @@
expect(angular.isFunction(steps[2].checkReadiness)).toBe(true);
});
});
})();