Skip to content

Commit

Permalink
chore(core): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
valorkin committed Jun 20, 2017
1 parent 2152fcc commit 6083fc5
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@

<button type="button" class="btn btn-info" (click)="toggleMode()">12H / 24H</button>

<hr>

<timepicker [(ngModel)]="mytime2" [meridians]="meridianText"></timepicker>

<pre class="alert alert-info">Time is: {{mytime2}}</pre>
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export class DemoTimepickerMeridianComponent {

public mytime: Date = new Date();

public mytime2: Date = new Date();

public meridianText = ['12h', '24h'];

public toggleMode(): void {
this.ismeridian = !this.ismeridian;
}
Expand Down
28 changes: 14 additions & 14 deletions src/spec/timepicker/timepicker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ describe('Component: timepicker', () => {
context.wheelSign(new Event('customWheel'));
});

it('should canBeChanged wheel', () => {
context.mousewheel = false;
context.canBeChanged('wheel');
});

it('should canBeChanged key', () => {
context.arrowkeys = false;
context.canBeChanged('key');
});

it('should canBeChanged true', () => {
context.readonlyInput = true;
context.canBeChanged();
});
// it('should canBeChanged wheel', () => {
// context.mousewheel = false;
// context.canBeChanged('wheel');
// });
//
// it('should canBeChanged key', () => {
// context.arrowkeys = false;
// context.canBeChanged('key');
// });
//
// it('should canBeChanged true', () => {
// context.readonlyInput = true;
// context.canBeChanged();
// });

it('should changeHours', () => {
context.changeHours(3);
Expand Down
22 changes: 15 additions & 7 deletions src/timepicker/reducer/timepicker.actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { TimeUnit } from '../timepicker.models';
import { Action } from '../../mini-ngrx/index';
import { TimeChangeEvent, TimepickerComponentState, TimeUnit } from '../timepicker.models';

@Injectable()
export class TimepickerActions {
Expand All @@ -9,6 +9,7 @@ export class TimepickerActions {
static readonly CHANGE_MINUTES = '[timepicker] change minutes';
static readonly CHANGE_SECONDS = '[timepicker] change seconds';
static readonly SET_TIME_UNIT = '[timepicker] set time unit';
static readonly UPDATE_CONTROLS = '[timepicker] update controls';

writeValue(value: Date | string) {
return {
Expand All @@ -17,24 +18,24 @@ export class TimepickerActions {
};
}

changeHours(value: number) {
changeHours(event: TimeChangeEvent) {
return {
type: TimepickerActions.CHANGE_HOURS,
payload: value
payload: event
};
}

changeMinutes(value: number) {
changeMinutes(event: TimeChangeEvent) {
return {
type: TimepickerActions.CHANGE_MINUTES,
payload: value
payload: event
};
}

changeSeconds(value: number): Action {
changeSeconds(event: TimeChangeEvent): Action {
return {
type: TimepickerActions.CHANGE_SECONDS,
payload: value
payload: event
};
}

Expand All @@ -44,4 +45,11 @@ export class TimepickerActions {
payload: value
};
}

updateControls(value: TimepickerComponentState): Action {
return {
type: TimepickerActions.UPDATE_CONTROLS,
payload: value
};
}
}
60 changes: 56 additions & 4 deletions src/timepicker/reducer/timepicker.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,90 @@
import { TimepickerActions } from './timepicker.actions';
import { changeTime, setTime } from '../timepicker.utils';
import { Action } from '../../mini-ngrx/index';
import { TimepickerComponentState, TimepickerControls } from '../timepicker.models';
import {
canChangeHours, canChangeMinutes, canChangeSeconds, canChangeValue,
timepickerControls
} from '../timepicker-controls.util';
import { TimepickerConfig } from '../timepicker.config';

export interface TimepickerState {
export class TimepickerState {
value: Date;
config: TimepickerComponentState;
controls: TimepickerControls;
}

export const initialState = {} as TimepickerState;
export const initialState = {
config: new TimepickerConfig(),
controls: {
canIncrementHours: true,
canIncrementMinutes: true,
canIncrementSeconds: true,

canDecrementHours: true,
canDecrementMinutes: true,
canDecrementSeconds: true
}
} as TimepickerState;

export function timepickerReducer(state = initialState, action: Action) {
switch (action.type) {
case(TimepickerActions.WRITE_VALUE): {
return Object.assign({}, state, {value: action.payload});
}

case (TimepickerActions.CHANGE_HOURS): {
const _newTime = changeTime(state.value, {hour: action.payload});
if (!canChangeValue(state.config, action.payload) ||
!canChangeHours(action.payload, state.controls)) {
return state;
}

const _newTime = changeTime(state.value, {hour: action.payload.step});

return Object.assign({}, state, {value: _newTime});
}

case (TimepickerActions.CHANGE_MINUTES): {
const _newTime = changeTime(state.value, {minute: action.payload});
if (!canChangeValue(state.config, action.payload) ||
!canChangeMinutes(action.payload, state.controls)) {
return state;
}

const _newTime = changeTime(state.value, {minute: action.payload.step});

return Object.assign({}, state, {value: _newTime});
}

case (TimepickerActions.CHANGE_SECONDS): {
if (!canChangeValue(state.config, action.payload) ||
!canChangeSeconds(action.payload, state.controls)) {
return state;
}

const _newTime = changeTime(state.value, {seconds: action.payload});

return Object.assign({}, state, {value: _newTime});
}

case (TimepickerActions.SET_TIME_UNIT): {
if (!canChangeValue(state.config)) {
return state;
}

const _newTime = setTime(state.value, action.payload);

return Object.assign({}, state, {value: _newTime});
}

case (TimepickerActions.UPDATE_CONTROLS): {
const _newControlsState = timepickerControls(state.value, action.payload);

return Object.assign({}, state, {
config: action.payload,
controls: _newControlsState
});
}

default:
return state;
}
Expand Down
Loading

0 comments on commit 6083fc5

Please sign in to comment.