Skip to content

Commit

Permalink
fix(datetime): fixes date time in 3.0 + perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Mar 21, 2017
1 parent 02f8f11 commit 99142f8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 26 deletions.
28 changes: 16 additions & 12 deletions src/components/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PickerColumn } from '../picker/picker-options';
import { Form } from '../../util/form';
import { Ion } from '../ion';
import { Item } from '../item/item';
import { deepCopy, isBlank, isPresent, isTrueProperty, isArray, isString, assert } from '../../util/util';
import { deepCopy, isBlank, isPresent, isTrueProperty, isArray, isString, assert, clamp } from '../../util/util';
import { dateValueRange, renderDateTime, renderTextFormat, convertFormatToKey, getValueFromFormat, parseTemplate, parseDate, updateDate, DateTimeData, convertDataToISO, daysInMonth, dateSortValue, dateDataSortValue, LocaleData } from '../../util/datetime-util';

export const DATETIME_VALUE_ACCESSOR: any = {
Expand Down Expand Up @@ -514,14 +514,12 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
picker.refresh();
});

picker.present(pickerOptions);

this._isOpen = true;
picker.onDidDismiss(() => {
this._isOpen = false;
});

picker.refresh();
picker.present(pickerOptions);
}

/**
Expand Down Expand Up @@ -565,7 +563,7 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
values = dateValueRange(format, this._min, this._max);
}

let column: PickerColumn = {
const column: PickerColumn = {
name: key,
selectedIndex: 0,
options: values.map(val => {
Expand All @@ -578,8 +576,8 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces

// cool, we've loaded up the columns with options
// preselect the option for this column
var optValue = getValueFromFormat(this._value, format);
var selectedIndex = column.options.findIndex(opt => opt.value === optValue);
const optValue = getValueFromFormat(this._value, format);
const selectedIndex = column.options.findIndex(opt => opt.value === optValue);
if (selectedIndex >= 0) {
// set the select index for this column's options
column.selectedIndex = selectedIndex;
Expand All @@ -589,10 +587,10 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
picker.addColumn(column);
});

const min = <any>this._min;
const max = <any>this._max;

// Normalize min/max
const min = <any>this._min;
const max = <any>this._max;
const columns = this._picker.getColumns();
['month', 'day', 'hour', 'minute']
.filter(name => !columns.find(column => column.name === name))
Expand Down Expand Up @@ -620,22 +618,28 @@ export class DateTime extends Ion implements AfterContentInit, ControlValueAcces
const lb = lowerBounds.slice();
const ub = upperBounds.slice();
const options = column.options;
let indexMin = options.length - 1;
let indexMax = 0;

for (var i = 0; i < options.length; i++) {
var opt = options[i];
var value = opt.value;
lb[index] = opt.value;
ub[index] = opt.value;

opt.disabled = (
var disabled = opt.disabled = (
value < lowerBounds[index] ||
value > upperBounds[index] ||
dateSortValue(ub[0], ub[1], ub[2], ub[3], ub[4]) < min ||
dateSortValue(lb[0], lb[1], lb[2], lb[3], lb[4]) > max
);
if (!disabled) {
indexMin = Math.min(indexMin, i);
indexMax = Math.max(indexMax, i);
}
}

opt = column.options[column.selectedIndex];
let selectedIndex = column.selectedIndex = clamp(indexMin, column.selectedIndex, indexMax);
opt = column.options[selectedIndex];
if (opt) {
return opt.value;
}
Expand Down
10 changes: 10 additions & 0 deletions src/components/datetime/test/issues/pages/root-page/root-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,15 @@
></ion-datetime>
</ion-item>

<ion-item>
<ion-label>Current year Date</ion-label>
<ion-datetime min="2017-03-20"></ion-datetime>
</ion-item>

<ion-item>
<ion-label>Last year Date</ion-label>
<ion-datetime min="2016-03-20"></ion-datetime>
</ion-item>


</ion-content>
19 changes: 9 additions & 10 deletions src/components/picker/picker-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ export class PickerColumnCmp {
// get the height of one option
this.optHeight = (colEle.firstElementChild ? colEle.firstElementChild.clientHeight : 0);

// set the scroll position for the selected option
this.setSelected(this.col.selectedIndex, 0);

// Listening for pointer events
this.events.pointerEvents({
element: this.elementRef.nativeElement,
Expand Down Expand Up @@ -384,6 +381,7 @@ export class PickerColumnCmp {
}
}
}
this.col.prevSelected = selectedIndex;

if (saveY) {
this.y = y;
Expand All @@ -409,19 +407,20 @@ export class PickerColumnCmp {
refresh() {
let min = this.col.options.length - 1;
let max = 0;

for (var i = 0; i < this.col.options.length; i++) {
if (!this.col.options[i].disabled) {
const options = this.col.options;
for (var i = 0; i < options.length; i++) {
if (!options[i].disabled) {
min = Math.min(min, i);
max = Math.max(max, i);
}
}

var selectedIndex = clamp(min, this.col.selectedIndex, max);

if (selectedIndex !== this.col.selectedIndex) {
const selectedIndex = clamp(min, this.col.selectedIndex, max);
if (this.col.prevSelected !== selectedIndex) {
var y = (selectedIndex * this.optHeight) * -1;
this.update(y, 150, true, true);
this._plt.cancelRaf(this.rafId);
this.velocity = 0;
this.update(y, 150, true, false);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/components/picker/picker-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { PickerColumnCmp } from './picker-column';
encapsulation: ViewEncapsulation.None,
})
export class PickerCmp {

@ViewChildren(PickerColumnCmp) _cols: QueryList<PickerColumnCmp>;
d: PickerOptions;
enabled: boolean;
Expand Down Expand Up @@ -116,6 +117,10 @@ export class PickerCmp {
});
}

ionViewDidLoad() {
this.refresh();
}

ionViewWillEnter() {
this._gestureBlocker.block();
}
Expand All @@ -125,9 +130,7 @@ export class PickerCmp {
}

refresh() {
this._cols.forEach(column => {
column.refresh();
});
this._cols.forEach(column => column.refresh());
}

_colChange(selectedOption: PickerColumnOption) {
Expand Down
1 change: 1 addition & 0 deletions src/components/picker/picker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PickerColumn {
name?: string;
align?: string;
selectedIndex?: number;
prevSelected?: number;
prefix?: string;
suffix?: string;
options?: PickerColumnOption[];
Expand Down
5 changes: 4 additions & 1 deletion src/components/picker/picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter, Output } from '@angular/core';

import { App } from '../app/app';
import { Config } from '../../config/config';
import { isPresent } from '../../util/util';
import { isPresent, assert } from '../../util/util';
import { NavOptions } from '../../navigation/nav-util';
import { PickerCmp } from './picker-component';
import { PickerOptions, PickerColumn } from './picker-options';
Expand Down Expand Up @@ -67,6 +67,9 @@ export class Picker extends ViewController {
}

refresh() {
assert(this._cmp, 'componentRef must be valid');
assert(this._cmp.instance.refresh, 'instance must implement refresh()');

this._cmp && this._cmp.instance.refresh && this._cmp.instance.refresh();
}

Expand Down
1 change: 1 addition & 0 deletions src/navigation/nav-controller-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ export class NavControllerBase extends Ion implements NavController {

_viewAttachToDOM(view: ViewController, componentRef: ComponentRef<any>, viewport: ViewContainerRef) {
assert(view._state === STATE_INITIALIZED, 'view state must be INITIALIZED');
assert(componentRef, 'componentRef can not be null');

// fire willLoad before change detection runs
this._willLoad(view);
Expand Down
2 changes: 2 additions & 0 deletions src/navigation/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export class ViewController {
* @hidden
*/
init(componentRef: ComponentRef<any>) {
assert(componentRef, 'componentRef can not be null');

this._cmp = componentRef;
this.instance = this.instance || componentRef.instance;
this._detached = false;
Expand Down

2 comments on commit 99142f8

@sachithd
Copy link

@sachithd sachithd commented on 99142f8 Apr 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi

Apologies if this is not the correct place to comment but since V3.0.1 the Datetime control behaves differently.

I use datetime control as follows
<ion-datetime displayFormat="YYYY-MM-DD" [(ngModel)]="mDate">

On V2 the mDate value gives a String (Eg: 2014-04-17) when selected from the datetime control and I can set the value of the control the same way by specifying a string. mDate = '2014-04-15' etc

But on V3 , the mDate returns an object when you select a value from the control.

I'm not sure if this is a bug or if this is the expected behaviour. Please advise. I can't find this documented any where.

Thanks

PS
I'm testing some other functionality (by Brandy Carney ) as well so I'm on the following version
npm install --save --save-exact ionic-angular@3.0.1-201704062237

@jgw96
Copy link
Contributor

@jgw96 jgw96 commented on 99142f8 Apr 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manucorporat would you be able to comment on the above issue?

Please sign in to comment.