Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter which rounds number with defined precision. #4261

Merged
merged 1 commit into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions dashboard/src/app/demo-components/demo-components.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
* @author Florent Benoit
*/
export class DemoComponentsCtrl {
booksByAuthor: {
[author: string]: Array<{title: string}>
};
button2Disabled: boolean;
listItemsDocs: string[];
listItemsTasks: Array<{
name: string,
done: boolean
}>;
numbersToRound: number[];
precisions: number[];

/**
* Default constructor that is using resource
Expand All @@ -24,9 +35,9 @@ export class DemoComponentsCtrl {

// selecter
this.booksByAuthor = {};
this.booksByAuthor['St Exupery'] = [{title:'The little prince'}];
this.booksByAuthor['V. Hugo'] = [{title:'Les miserables'}, {title:'The Hunchback of Notre-Dame'}];
this.booksByAuthor['A. Dumas'] = [{title:'The count of Monte Cristo'}, {title:'The Three Musketeers'}];
this.booksByAuthor['St Exupery'] = [{title: 'The little prince'}];
this.booksByAuthor['V. Hugo'] = [{title: 'Les miserables'}, {title: 'The Hunchback of Notre-Dame'}];
this.booksByAuthor['A. Dumas'] = [{title: 'The count of Monte Cristo'}, {title: 'The Three Musketeers'}];


this.button2Disabled = true;
Expand All @@ -35,6 +46,9 @@ export class DemoComponentsCtrl {

this.listItemsTasks = [{name : 'Task 1', done: false}, {name : 'Task 2', done: true}, {name : 'Task 3', done: false},
{name : 'Task 4', done: true}, {name : 'Task 5', done: false}];

this.numbersToRound = [ -1234.5678, 0, 1234.5678 ];
this.precisions = [ -2, 0, 2 ];
}


Expand Down
13 changes: 13 additions & 0 deletions dashboard/src/app/demo-components/demo-components.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,18 @@ <h6>This is h6

</md-tab>

<md-tab label="Filter">

<div layout="column">
Number round filter:
<div data-ng-repeat="number in demoComponentsCtrl.numbersToRound">
<div data-ng-repeat="precision in demoComponentsCtrl.precisions">
<span>Number <b>{{number}}</b> with precision <b>{{precision}}</b> is <b>{{number | cheNumberRoundFilter:precision}}</b></span>
</div>
</div>
</div>

</md-tab>

</md-tabs>
</md-content>
2 changes: 1 addition & 1 deletion dashboard/src/components/attribute/attribute-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {CheTypeCity} from './input-type/input-city.directive';

export class AttributeConfig {

constructor(register) {
constructor(register: che.IRegisterService) {

register.directive('focusable', CheFocusable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export abstract class CheInputType {
link($scope: ng.IScope, $element: ng.IAugmentedJQuery, attrs: {cheInputType: string, [prop: string]: string}): void {

$element.on('keydown', (event: KeyboardEvent) => {
// escape, enter, tab
if (event.keyCode === 27 || event.keyCode === 13 || event.keyCode === 9) {
return true;
}
// delete, backspace
if (event.keyCode === 46 || event.keyCode === 8) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions dashboard/src/components/components-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// components
import {ApiConfig} from './api/che-api-config';
import {AttributeConfig} from './attribute/attribute-config';
import {FilterConfig} from './filter/filter-config';
import {CheBrandingConfig} from './branding/che-branding-config';
import {CodeMirrorConstant} from './codemirror/codemirror';
import {GitHubService} from './github/github-service';
Expand All @@ -32,6 +33,7 @@ export class ComponentsConfig {
constructor(register) {
new ApiConfig(register);
new AttributeConfig(register);
new FilterConfig(register);
new CheBrandingConfig(register);
new CodeMirrorConstant(register);
new GitHubService(register);
Expand Down
20 changes: 20 additions & 0 deletions dashboard/src/components/filter/filter-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheNumberRoundFilter} from './number-round/number-round.filter';

export class FilterConfig {

constructor(register: che.IRegisterService) {
new CheNumberRoundFilter(register);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';

export class CheNumberRoundFilter {

constructor(register: che.IRegisterService) {
register.filter('cheNumberRoundFilter', () => {
return (number: number, precision: number) => {
number = parseFloat(number);
precision = parseInt(precision, 10);

if (isNaN(number)) {
return 'NaN';
}
if (isNaN(precision)) {
precision = 0;
}

let factor = Math.pow(10, precision);
let tempNumber = number * factor;
let roundedTempNumber = Math.round(tempNumber);
return (roundedTempNumber / factor).toString();
};
});
}
}

91 changes: 91 additions & 0 deletions dashboard/src/components/filter/number-round/number-round.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2015-2017 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheHttpBackend} from '../../api/test/che-http-backend';

/**
* Test for CheNumberRoundFilter
*
* @author Oleksii Kurinnyi
*/

describe('CheNumberRoundFilter', () => {
let $scope, $compile;

let testNumbers = [
{
number: -1234.5678,
precision: -2,
result: -1200
},
{
number: -1234.5678,
precision: 0,
result: -1235
},
{
number: -1234.5678,
precision: 2,
result: -1234.57
},
{
number: 1234.5678,
precision: -2,
result: 1200
},
{
number: 1234.5678,
precision: 0,
result: 1235
},
{
number: 1234.5678,
precision: 2,
result: 1234.57
}
];

/**
* Backend for handling http operations
*/
let httpBackend;

beforeEach(angular.mock.module('userDashboard'));

beforeEach(inject((_$compile_: ng.ICompileService, $rootScope: ng.IRootScopeService, cheHttpBackend: CheHttpBackend) => {
$scope = $rootScope;
$compile = _$compile_;

httpBackend = cheHttpBackend.getHttpBackend();
// avoid tracking requests from branding controller
httpBackend.whenGET(/.*/).respond(200, '');
httpBackend.when('OPTIONS', '/api/').respond({});
}));

testNumbers.forEach((entry: {number: number, precision: number, result: number}) => {

(function (entry: {number: number, precision: number, result: number}) {
it(`Number ${entry.number} with precision ${entry.precision} should be equal to ${entry.result}`, () => {
$scope.entry = entry;

let element = angular.element(
'<div>{{entry.number | cheNumberRoundFilter:entry.precision}}</div>'
);
$compile(element)($scope);
$scope.$digest();

expect(element.text()).toEqual(entry.result.toString());
});
})(entry);

});

});
11 changes: 6 additions & 5 deletions dashboard/src/components/validator/city-name-validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheHttpBackend} from '../api/test/che-http-backend';

/**
* Test for city-name-validator directive
Expand Down Expand Up @@ -58,7 +59,7 @@ describe('city-name-validator', () => {

beforeEach(angular.mock.module('userDashboard'));

beforeEach(inject((_$compile_, $rootScope, cheHttpBackend) => {
beforeEach(inject((_$compile_: ng.ICompileService, $rootScope: ng.IRootScopeService, cheHttpBackend: CheHttpBackend) => {
$scope = $rootScope;
$compile = _$compile_;

Expand All @@ -68,9 +69,9 @@ describe('city-name-validator', () => {
httpBackend.when('OPTIONS', '/api/').respond({});
}));

validNames.forEach((validName) => {
validNames.forEach((validName: string) => {

(function shouldPass(validCityName) {
(function shouldPass(validCityName: string) {
it(`"${validCityName}" should be OK`, () => {
$scope.model = {value: ''};

Expand All @@ -92,9 +93,9 @@ describe('city-name-validator', () => {

});

invalidNames.forEach((invalidName) => {
invalidNames.forEach((invalidName: string) => {

(function shouldFail(invalidName) {
(function shouldFail(invalidName: string) {
it(`"${invalidName}" should fail`, () => {
$scope.model = {value: ''};

Expand Down