Skip to content

Commit

Permalink
Merge pull request #348 from marmelab/date_timezone
Browse files Browse the repository at this point in the history
[RFR] Introducing datetime field
  • Loading branch information
jeromemacias committed Mar 6, 2015
2 parents 50129b8 + 03712db commit cb705d6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ A field is the representation of a property of an entity.
### General Field Settings

* `nga.field(name, type)`
Create a new field of the given type. Default type is 'string', so you can omit it. Bundled types include `number`, `string`, `text`, `boolean`, `wysiwyg`, `email`, `date`, `choice`, `choices`, `json`, `file`, and `template`
Create a new field of the given type. Default type is 'string', so you can omit it. Bundled types include `number`, `string`, `text`, `boolean`, `wysiwyg`, `email`, `date`, `datetime`, `choice`, `choices`, `json`, `file`, and `template`

* `label(string label)`
Define the label of the field. Defaults to the uppercased field name.
Expand All @@ -330,21 +330,12 @@ Define if the field is editable in the edition form. Usefult to display a field
* `order(number|null)`
Define the position of the field in the view.

* `format(string ['yyyy-MM-dd' by default])`
Define the format for `date` type.

* `parse(function [no change by default])`
Filter applied to modify date object returned by date picker if needed.

* `isDetailLink(boolean)`
Tell if the value is a link in the list view. Default to true for the identifier and references field, false otherwise. The link points to the edition view, except for read-only entities, where it points to the show view.

* `detailLinkRoute(string)`
Define the route for a link in the list view, i.e. `isDetailLink` of the field is true. The default is `edit`, hence the link points to the edition view. The other option is `show` to point to the show view.

* `choices([{value: '', label: ''}, ...])`
Define array of choices for `choice` type. A choice has both a value and a label.

* `map(function)`
Define a custom function to transform the value. It receive the value and the corresponding entry. Works in list, edit views and references.

Expand Down Expand Up @@ -382,9 +373,32 @@ A list of CSS classes to be added to the corresponding field. If you provide a f
* `defaultValue(*)`
Define the default value of the field in the creation form.

### `choice` and `choices` Field Settings

* `choices([{value: '', label: ''}, ...])`
Define array of choices for `choice` type. A choice has both a value and a label.

### `date` Field Settings

* `format(string ['yyyy-MM-dd' by default])`

* `parse(function [remove hours, minutes and timezone by default])`
Filter applied to modify date object returned by date picker if needed.

### `datetime` Field Settings

* `format(string ['yyyy-MM-dd HH:mm:ss' by default])`

* `parse(function [no change by default])`
Filter applied to modify date object returned by date picker if needed.

### `template` Field Settings

* `template(*)`
Define the template to be displayed for fields of type `template` (can be a string or a function).

### `file` Field Settings

* `uploadInformation`
Give upload information for `file` field type
- `url`: url for server side upload
Expand Down
11 changes: 1 addition & 10 deletions examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,7 @@
nga.field('q', 'string').label('').attributes({'placeholder': 'Global Search'}),
nga.field('created_at', 'date')
.label('Posted')
.attributes({'placeholder': 'Filter by date'})
.format('yyyy-MM-dd')
.parse(function(date) {
// the backend is dumb and doesn't interpret date objects
// so we convert the date to a string without timezone
var dateObject = date instanceof Date ? date : new Date(date);
dateObject.setMinutes(dateObject.getMinutes() - dateObject.getTimezoneOffset());
var dateString = dateObject.toJSON();
return dateString ? dateString.substr(0,10) : null;
}),
.attributes({'placeholder': 'Filter by date'}),
nga.field('today', 'boolean').map(function() {
var now = new Date(),
year = now.getFullYear(),
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/config/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ define(function (require) {
fvp.registerFieldView('choice', require('ng-admin/Crud/fieldView/ChoiceFieldView'));
fvp.registerFieldView('choices', require('ng-admin/Crud/fieldView/ChoicesFieldView'));
fvp.registerFieldView('date', require('ng-admin/Crud/fieldView/DateFieldView'));
fvp.registerFieldView('datetime', require('ng-admin/Crud/fieldView/DateFieldView'));
fvp.registerFieldView('email', require('ng-admin/Crud/fieldView/EmailFieldView'));
fvp.registerFieldView('file', require('ng-admin/Crud/fieldView/FileFieldView'));
fvp.registerFieldView('json', require('ng-admin/Crud/fieldView/JsonFieldView'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ define(function (require) {
var Field = require('ng-admin/Main/component/service/config/Field'),
utils = require('ng-admin/lib/utils');

function DateField(fieldName) {
function DateField() {
Field.apply(this, arguments);
this._format = 'yyyy-MM-dd';
this._parse = function (date) {
return date;
this._parse = function(date) {
if (date instanceof Date) {
// the datepicker returns a JS Date object, with hours, minutes and timezone
// in order to convert it back to date, we must remove the timezone, then
// remove hours and minutes
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
var dateString = date.toJSON();
return dateString ? dateString.substr(0,10) : null;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*global define*/

define(function (require) {
'use strict';

var DateField = require('ng-admin/Main/component/service/config/fieldTypes/DateField'),
utils = require('ng-admin/lib/utils');

function DateTimeField() {
DateField.apply(this, arguments);
this._format = 'yyyy-MM-dd HH:mm:ss';
this._parse = function(date) {
return date;
};
}

utils.inherits(DateTimeField, DateField);

return DateTimeField;
});
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Main/config/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define(function (require) {
nga.registerFieldType('choice', ChoiceField);
nga.registerFieldType('choices', ChoiceField);
nga.registerFieldType('date', require('ng-admin/Main/component/service/config/fieldTypes/DateField'));
nga.registerFieldType('datetime', require('ng-admin/Main/component/service/config/fieldTypes/DateTimeField'));
nga.registerFieldType('email', Field);
nga.registerFieldType('file', require('ng-admin/Main/component/service/config/fieldTypes/FileField'));
nga.registerFieldType('json', Field);
Expand Down

0 comments on commit cb705d6

Please sign in to comment.