Skip to content

Instantly share code, notes, and snippets.

@SimeonC
Created September 20, 2015 21:01
Show Gist options
  • Save SimeonC/7e0b5d6cae1c79deb48d to your computer and use it in GitHub Desktop.
Save SimeonC/7e0b5d6cae1c79deb48d to your computer and use it in GitHub Desktop.
A demo for input trigger for sc-date-time-picker. Uses Angular-Strap for it's $modal service.
.modal(tabindex="-1" role="dialog" aria-hidden="true")
.modal-dialog.time-date
.modal-content
time-date-picker(
display-mode="{{displayMode}}"
default-mode="{{defaultMode}}"
default-date="{{defaultDate}}"
mindate="{{mindate}}"
maxdate="{{maxdate}}"
ng-model="model"
on-cancel="onCancel()"
on-save="onSave()"
)
.directive('dateTimePickerInput', [ '$modal', '$filter', function($modal: mgcrea.ngStrap.modal.IModalService, $filter: ng.IFilterService) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope: IBaseScope,
element: ng.IAugmentedJQuery,
attrs: ng.IAttributes,
ngModelCtrl: any): void {
if(element[0].tagName.toLowerCase() !== 'input' || attrs['type'].toLowerCase() !== 'text') {
throw new Error('date-time-picker-input directive must be instantiated as an attribute of a input with type="text"');
}
var _openModal: mgcrea.ngStrap.modal.IModal;
element.on('focus', function(){
if(_openModal) return;
ngModelCtrl.$setTouched();
_openModal = $modal({
templateUrl: '/views/common/dateTimeDialog.html',
scope: scope.$new(),
controller: [
'$scope', function ($scope: any) {
// See https://github.com/SimeonC/sc-date-time#options for details
var passthroughAttrs = ['displayMode', 'defaultDate', 'defaultMode', 'mindate', 'maxdate']
angular.forEach(passthroughAttrs, function (key) {
$scope[key] = attrs[key];
});
if(angular.isDate(ngModelCtrl.$modelValue)) $scope.model = new Date(ngModelCtrl.$modelValue);
$scope.onCancel = function () {
_openModal.hide();
element[0].focus();
element[0].blur();
_openModal = null;
};
$scope.onSave = function () {
ngModelCtrl.$setViewValue($scope.model);
ngModelCtrl.$setDirty();
element.val(formatDate($scope.model));
_openModal.hide();
element[0].focus();
element[0].blur();
_openModal = null;
};
}
],
show: true
});
});
if(attrs['ngMin']) {
ngModelCtrl.$validators['min'] = function (dateValue) {
return new Date(dateValue) >= new Date(attrs['ngMin']);
};
}
if(attrs['ngMax']) {
ngModelCtrl.$validators['max'] = function (dateValue) {
return new Date(dateValue) <= new Date(attrs['ngMax']);
};
}
ngModelCtrl.$formatters.push(formatDate);
function formatDate(value){
return $filter('date')(value, attrs['displayFormat'] || 'EEEE d MMMM yyyy, h:mm a');
};
}
};
} ];
});
input.better-placeholder.form-control(
type='text'
name="dateOfBirth"
ng-model='#{driver}.license.dateOfBirth'
placeholder="Date of Birth"
required
date-time-picker-input
display-mode="date"
display-format="d MMMM yyyy"
ng-max="getMinimumDrivingDate()"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment