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

[RFR] Fix default date format in date field #528

Merged
merged 1 commit into from
Jun 24, 2015
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"admin-config": "^0.2.1",
"angular": "~1.3.15",
"angular-bootstrap": "^0.12.0",
"angular-mocks": "^1.3.15",
"angular-mocks": "1.3.14",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To prevent from a $browser.cookies is undefined introduced in 1.3.15.

"angular-numeraljs": "^1.1.6",
"angular-sanitize": "^1.3.15",
"angular-ui-codemirror": "^0.3.0",
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/CrudModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CrudModule.service('RestWrapper', require('./misc/RestWrapper'));

CrudModule.directive('maJsonValidator', require('./validator/maJsonValidator'));

CrudModule.directive('datepickerPopup', require('./field/datepickerPopup'));
CrudModule.directive('maField', require('./field/maField'));
CrudModule.directive('maButtonField', require('./field/maButtonField'));
CrudModule.directive('maChoiceField', require('./field/maChoiceField'));
Expand Down
16 changes: 16 additions & 0 deletions src/javascripts/ng-admin/Crud/field/datepickerPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Fixes an issue with Bootstrap Date Picker
@see https://github.com/angular-ui/bootstrap/issues/2659 */
var datepickerPopup = function () {
return {
restrict: 'EAC',
require: 'ngModel',
link: function(scope, element, attr, controller) {
//remove the default formatter from the input directive to prevent conflict
controller.$formatters.shift();
}
}
};

datepickerPopup.$inject = [];

export default datepickerPopup;
107 changes: 52 additions & 55 deletions src/javascripts/ng-admin/Crud/field/maDateField.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
/*global define*/
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing changed here, just flatten the file to look more ES6-ish.

* Edition field for a date - a text input with a datepicker.
*
* @example <ma-date-field field="field" value="value"></ma-date-field>
*/
function maDateField() {
return {
scope: {
'field': '&',
'value': '='
},
restrict: 'E',
link: function(scope, element) {
var field = scope.field();
scope.name = field.name();
scope.rawValue = scope.value;
scope.$watch('rawValue', function(rawValue) {
scope.value = field.parse()(rawValue);
});
scope.format = field.format();
if (!scope.format) {
scope.format = field.type() === 'date' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss';
}

define(function (require) {
'use strict';
scope.v = field.validation();
scope.isOpen = false;
var input = element.find('input').eq(0);
var attributes = field.attributes();
for (var name in attributes) {
input.attr(name, attributes[name]);
}
scope.toggleDatePicker = function ($event) {
$event.preventDefault();
$event.stopPropagation();
scope.isOpen = !scope.isOpen;
};
},
template: `
<div class="input-group datepicker">
<input
type="text" ng-model="rawValue" id="{{ name }}" name="{{ name }}" class="form-control"
datepicker-popup="{{ format }}" is-open="isOpen" close-text="Close" ng-required="v.required" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="toggleDatePicker($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
`
};
}

/**
* Edition field for a date - a text input with a datepicker.
*
* @example <ma-date-field field="field" value="value"></ma-date-field>
*/
function maDateField() {
return {
scope: {
'field': '&',
'value': '='
},
restrict: 'E',
link: function(scope, element) {
var field = scope.field();
scope.name = field.name();
scope.rawValue = scope.value;
scope.$watch('rawValue', function(rawValue) {
scope.value = field.parse()(rawValue);
});
scope.format = field.format();
if (!scope.format) {
scope.format = field.type() === 'date' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss';
}
maDateField.$inject = [];

scope.v = field.validation();
scope.isOpen = false;
var input = element.find('input').eq(0);
var attributes = field.attributes();
for (var name in attributes) {
input.attr(name, attributes[name]);
}
scope.toggleDatePicker = function ($event) {
$event.preventDefault();
$event.stopPropagation();
scope.isOpen = !scope.isOpen;
};
},
template:
'<div class="input-group datepicker">' +
'<input type="text" ng-model="rawValue" id="{{ name }}" name="{{ name }}" class="form-control" ' +
'datepicker-popup="{{ format }}" is-open="isOpen" close-text="Close" ' +
'ng-required="v.required" />' +
'<span class="input-group-btn">' +
'<button type="button" class="btn btn-default" ng-click="toggleDatePicker($event)"><i class="glyphicon glyphicon-calendar"></i></button>' +
'</span>' +
'</div>'
};
}

maDateField.$inject = [];

return maDateField;
});
export default maDateField;
4 changes: 3 additions & 1 deletion src/javascripts/test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module.exports = function (config) {
browsers: [process.env.CI ? 'PhantomJS' : 'Chrome'],
frameworks: ['jasmine'],
files: [
'../../node_modules/angular/angular.min.js',
'../../node_modules/angular/angular.js',
'../../node_modules/angular-bootstrap/dist/ui-bootstrap.min.js',
'../../node_modules/angular-bootstrap/dist/ui-bootstrap-tpls.min.js',
'../../node_modules/angular-mocks/angular-mocks.js',
'../../node_modules/angular-numeraljs/dist/angular-numeraljs.min.js',
'../../node_modules/numeral/numeral.js',
Expand Down
18 changes: 16 additions & 2 deletions src/javascripts/test/unit/Crud/field/maDateFieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
describe('directive: date-field', function() {
'use strict';

var directive = require('../../../../ng-admin/Crud/field/maDateField');
var dateDirective = require('../../../../ng-admin/Crud/field/maDateField');
var datepickerPopupDirective = require('../../../../ng-admin/Crud/field/datepickerPopup');
var DateField = require('admin-config/lib/Field/DateField');
angular.module('testapp_DateField', []).directive('maDateField', directive);

angular.module('testapp_DateField', ['ui.bootstrap', 'ui.bootstrap.tpls'])
.directive('maDateField', dateDirective)
.directive('datepickerPopup', datepickerPopupDirective);

var $compile,
scope,
Expand All @@ -24,6 +28,16 @@ describe('directive: date-field', function() {
expect(element.find('input').eq(0).attr('type')).toBe('text');
});

it('should format default value correctly', function () {
scope.value = new Date(2014, 2, 1);
scope.field = (new DateField()).format('yyyy-MM-dd');

var element = $compile(directiveUsage)(scope);
scope.$digest();

expect(element.find('input').eq(0).val()).toBe('2014-03-01');
});

it("should use the supplied format as datepicker parameter", function () {
scope.field = new DateField().format('yyyy-MM');
var date = new Date('2015-01-23');
Expand Down