fng-bootstrap-date 0.12.0-beta.34 → 0.12.0-beta.341

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/fng-bootstrap-date.js +156 -32
  2. package/package.json +1 -1
@@ -1,37 +1,161 @@
1
1
  (function () {
2
- 'use strict';
2
+ 'use strict';
3
3
 
4
- var uiBootstrapDateModule = angular.module('fng.uiBootstrapDate', ['ui.bootstrap']);
4
+ var uiBootstrapDateModule = angular.module('fng.uiBootstrapDate', ['ui.bootstrap']);
5
5
 
6
- uiBootstrapDateModule.controller('FngUIBootstrapDateCtrl', ['$scope', function ($scope) {
7
- $scope.popup = {
8
- opened: false
9
- };
6
+ uiBootstrapDateModule.controller('FngUIBootstrapDateCtrl', ['$scope', function ($scope) {
7
+ $scope.popup = {
8
+ opened: false
9
+ };
10
+
11
+ $scope.open = function () {
12
+ $scope.popup.opened = true;
13
+ }
14
+ }])
15
+ .directive('fngUiBootstrapDatePicker', ['$compile', '$timeout', 'PluginHelperService', 'FormMarkupHelperService', 'RecordHandlerService',
16
+ function ($compile, $timeout, PluginHelperService, FormMarkupHelperService, RecordHandlerService) {
17
+ return {
18
+ restrict: 'E',
19
+ replace: true,
20
+ priority: 1,
21
+ controller: 'FngUIBootstrapDateCtrl',
22
+ scope: true, // our own scope, but not isolated - prevents multiple instances on the same page from opening each other
23
+ link: function (scope, element, attrs) {
24
+ function afterTimeout() {
25
+ $timeout(function() {
26
+ if (element[0].parentElement) {
27
+ element.replaceWith(html);
28
+ } else {
29
+ afterTimeout();
30
+ }
31
+ });
32
+ }
33
+
34
+ var template;
35
+ var processedAttrs = PluginHelperService.extractFromAttr(attrs, 'fngUiBootstrapDatePicker', scope);
36
+ var overriddenDefaults = {
37
+ 'show-button-bar': false,
38
+ 'show-meridian': false,
39
+ 'date-format': 'dd/MM/yyyy'
40
+ };
41
+ overriddenDefaults = Object.assign({}, overriddenDefaults, processedAttrs.directiveOptions);
42
+ var overriddenDateDefaults = {
43
+ showWeeks: false
44
+ };
45
+ var jsonDateOptions = {
46
+ 'showWeeks': false
47
+ };
48
+ if (processedAttrs.directiveOptions['date-options']) {
49
+ jsonDateOptions = JSON.parse(processedAttrs.directiveOptions['date-options'].replace(/'/g, '"'));
50
+ }
51
+ scope.dateOptions = Object.assign(overriddenDateDefaults, jsonDateOptions);
52
+ ["minDate","maxDate"].forEach(v => {
53
+ if (scope.dateOptions[v] && typeof scope.dateOptions[v] === "string") {
54
+ // allow minDate and maxDate to be derived from another (assumed to be date-typed) field, but with
55
+ // the limitation that the value of that field must be available now and must not change
56
+ // (i.e., this is effectively a one-time binding)
57
+ // one possible use case would be to use the value of a hidden "when created" field as a minimum
58
+ // or maximum value for another (not hidden) date field
59
+ if (scope.dateOptions[v].startsWith("record.") || scope.dateOptions[v].startsWith("subDoc.")) {
60
+ scope.dateOptions[v] = RecordHandlerService.getData(scope, scope.dateOptions[v]);
61
+ } else {
62
+ scope.dateOptions[v] = new Date(scope.dateOptions[v]);
63
+ }
64
+ }
65
+ })
10
66
 
11
- $scope.open = function() {
12
- $scope.popup.opened = true;
13
- }
14
- }])
15
- .directive('fngUiBootstrapDatePicker', ['$compile', '$filter', 'pluginHelper', 'formMarkupHelper', 'cssFrameworkService',
16
- function ($compile, $filter, pluginHelper, formMarkupHelper, cssFrameworkService) {
17
- return {
18
- restrict: 'E',
19
- replace: true,
20
- priority: 1,
21
- controller: 'FngUIBootstrapDateCtrl',
22
- link: function (scope, element, attrs) {
23
- var template;
24
- var processedAttr = pluginHelper.extractFromAttr(attrs, 'fngUiBootstrapDatePicker');
25
- template = pluginHelper.buildInputMarkup(scope, attrs.model, processedAttr.info, processedAttr.options, false, false, function (buildingBlocks) {
26
- return formMarkupHelper.generateSimpleInput(
27
- buildingBlocks.common + ' uib-datepicker-popup="' + (processedAttr.directiveOptions.format || 'dd/MM/yy') + '" is-open="popup.opened" ng-click="open()" ' + formMarkupHelper.addTextInputMarkup(buildingBlocks, processedAttr.info, ''),
28
- processedAttr.info,
29
- processedAttr.options
30
- );
31
- });
32
- element.replaceWith($compile(template)(scope));
67
+ const isArray = processedAttrs.info.array;
68
+ template = PluginHelperService.buildInputMarkup(
69
+ scope,
70
+ attrs,
71
+ {
72
+ processedAttrs,
73
+ addButtons: isArray,
74
+ needsX: isArray,
75
+ },
76
+ function (buildingBlocks) {
77
+ var str = buildingBlocks.common.trim();
78
+ for (var opt in overriddenDefaults) {
79
+ if (opt !== 'date-options') {
80
+ str += ` ${opt}="${overriddenDefaults[opt]}"`;
81
+ }
82
+ }
83
+ if (processedAttrs.info.title) {
84
+ str += ` title="${processedAttrs.info.title}"`;
85
+ }
86
+ if (processedAttrs.info.arialabel) {
87
+ str += ` aria-label="${processedAttrs.info.arialabel}"`;
88
+ }
89
+ if (processedAttrs.info.required) {
90
+ str += " required";
91
+ }
92
+ const markup = FormMarkupHelperService.addTextInputMarkup(buildingBlocks, processedAttrs.info, '');
93
+ const disabled = PluginHelperService.genDisabledStr(scope, processedAttrs, "");
94
+ const dateFormat = processedAttrs.directiveOptions.format || processedAttrs.directiveOptions['date-format'] || 'dd/MM/yy';
95
+ str += ` ${markup}${disabled}datepicker-options="dateOptions" uib-datepicker-popup="${dateFormat}"`;
96
+
97
+ if (disabled && disabled.trim().toLowerCase() !== "disabled") {
98
+ scope.popup = { opened: false };
99
+ scope.open = function () {
100
+ scope.popup.opened = true;
101
+ }
102
+ str += ' is-open="popup.opened" ng-click="open()" ng-change="setDirty()" validdate '; // don't remove the trailing space here
103
+ }
104
+ return FormMarkupHelperService.generateSimpleInput(
105
+ str,
106
+ processedAttrs.info,
107
+ processedAttrs.options
108
+ );
109
+ }
110
+ );
111
+ var html = $compile(template)(scope);
112
+ if (element[0].parentElement) {
113
+ element.replaceWith(html);
114
+ } else {
115
+ afterTimeout();
116
+ }
117
+ }
118
+ };
119
+ }]
120
+ )
121
+ .directive('validdate', [function() {
122
+ return {
123
+ require: 'ngModel',
124
+ link: function(scope, elm, attrs, ctrl) {
125
+ var minDate;
126
+ var maxDate;
127
+ if (scope.dateOptions.minDate) {
128
+ minDate = new Date(scope.dateOptions.minDate).valueOf();
129
+ }
130
+ if (scope.dateOptions.maxDate) {
131
+ maxDate = new Date(scope.dateOptions.maxDate).valueOf();
132
+ }
133
+ if (minDate || maxDate) {
134
+ let dateFormat = attrs.format || attrs.dateFormat;
135
+ if (dateFormat !== 'dd/MM/yyyy') {
136
+ throw new Error('Unsupported date format in validdate: ' + dateFormat)
137
+ }
138
+ }
139
+
140
+ ctrl.$validators.validdate = function(modelValue, viewValue) {
141
+ if (ctrl.$isEmpty(modelValue)) {
142
+ // if empty should be considered invalid, the field should be set to required (which will
143
+ // then be validated elsewhere)
144
+ return true;
145
+ }
146
+ var retVal = true;
147
+ if (minDate || maxDate) {
148
+ var dateVal = modelValue.valueOf();
149
+ if (minDate && dateVal < minDate) {
150
+ retVal = false;
151
+ }
152
+ if (maxDate && dateVal > maxDate) {
153
+ retVal = false;
154
+ }
155
+ }
156
+ return retVal;
157
+ };
158
+ }
33
159
  }
34
- };
35
- }]
36
- )
37
- })();
160
+ }])
161
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fng-bootstrap-date",
3
- "version": "0.12.0-beta.34",
3
+ "version": "0.12.0-beta.341",
4
4
  "description": "Date input plugin for forms-angular, using the Bootstrap datepicker from Angular-ui-bootstrap",
5
5
  "main": "fng-bootstrap-date.js",
6
6
  "scripts": {