Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. input.js ======================================================================================
  2.  
  3.  
  4. 'use strict';
  5.  
  6. var PRISTINE_CLASS = 'ng-pristine',
  7. DIRTY_CLASS = 'ng-dirty';
  8.  
  9. var Module = angular.module('datePicker');
  10.  
  11. Module.constant('dateTimeConfig', {
  12. template: function (attrs) {
  13. return '' +
  14. '<div ' +
  15. 'date-picker="' + attrs.ngModel + '" ' +
  16. (attrs.view ? 'view="' + attrs.view + '" ' : '') +
  17. (attrs.maxView ? 'max-view="' + attrs.maxView + '" ' : '') +
  18. (attrs.autoClose ? 'auto-close="' + attrs.autoClose + '" ' : '') +
  19. (attrs.template ? 'template="' + attrs.template + '" ' : '') +
  20. (attrs.minView ? 'min-view="' + attrs.minView + '" ' : '') +
  21. (attrs.partial ? 'partial="' + attrs.partial + '" ' : '') +
  22. (attrs.step ? 'step="' + attrs.step + '" ' : '') +
  23. 'class="date-picker-date-time"></div>';
  24. },
  25. format: 'yyyy-MM-dd HH:mm',
  26. views: ['date', 'year', 'month', 'hours', 'minutes'],
  27. dismiss: false,
  28. position: 'relative'
  29. });
  30.  
  31. Module.directive('dateTimeAppend', function () {
  32. return {
  33. link: function (scope, element) {
  34. element.bind('click', function () {
  35. element.find('input')[0].focus();
  36. });
  37. }
  38. };
  39. });
  40.  
  41. Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfig', '$parse', 'datePickerUtils',
  42. function ($compile, $document, $filter, dateTimeConfig, $parse, datePickerUtils) {
  43. var body = $document.find('body');
  44. var dateFilter = $filter('date');
  45.  
  46. return {
  47. require: 'ngModel',
  48. scope:true,
  49. link: function (scope, element, attrs, ngModel) {
  50. var format = attrs.format || dateTimeConfig.format;
  51. var parentForm = element.inheritedData('$formController');
  52. var views = $parse(attrs.views)(scope) || dateTimeConfig.views.concat();
  53. var view = attrs.view || views[0];
  54. var index = views.indexOf(view);
  55. var dismiss = attrs.dismiss ? $parse(attrs.dismiss)(scope) : dateTimeConfig.dismiss;
  56. var picker = null;
  57. var position = attrs.position || dateTimeConfig.position;
  58. var container = null;
  59.  
  60. if (index === -1) {
  61. views.splice(index, 1);
  62. }
  63.  
  64. views.unshift(view);
  65.  
  66.  
  67. function formatter(value) {
  68. return dateFilter(value, format);
  69. }
  70.  
  71. function parser() {
  72. return ngModel.$modelValue;
  73. }
  74.  
  75. ngModel.$formatters.push(formatter);
  76. ngModel.$parsers.unshift(parser);
  77.  
  78.  
  79. //min. max date validators
  80. if (angular.isDefined(attrs.minDate)) {
  81. var minVal;
  82. ngModel.$validators.min = function (value) {
  83. return !datePickerUtils.isValidDate(value) || angular.isUndefined(minVal) || value >= minVal;
  84. };
  85. attrs.$observe('minDate', function (val) {
  86. minVal = new Date(val);
  87. ngModel.$validate();
  88. });
  89. }
  90.  
  91. if (angular.isDefined(attrs.maxDate)) {
  92. var maxVal;
  93. ngModel.$validators.max = function (value) {
  94. return !datePickerUtils.isValidDate(value) || angular.isUndefined(maxVal) || value <= maxVal;
  95. };
  96. attrs.$observe('maxDate', function (val) {
  97. maxVal = new Date(val);
  98. ngModel.$validate();
  99. });
  100. }
  101. //end min, max date validator
  102.  
  103. var template = dateTimeConfig.template(attrs);
  104.  
  105. function updateInput(event) {
  106. event.stopPropagation();
  107. if (ngModel.$pristine) {
  108. ngModel.$dirty = true;
  109. ngModel.$pristine = false;
  110. element.removeClass(PRISTINE_CLASS).addClass(DIRTY_CLASS);
  111. if (parentForm) {
  112. parentForm.$setDirty();
  113. }
  114. ngModel.$render();
  115. }
  116. }
  117.  
  118. function clear() {
  119. if (picker) {
  120. picker.remove();
  121. picker = null;
  122. }
  123. if (container) {
  124. container.remove();
  125. container = null;
  126. }
  127. }
  128.  
  129. function showPicker() {
  130. if (picker) {
  131. return;
  132. }
  133. // create picker element
  134. picker = $compile(template)(scope);
  135. scope.$digest();
  136.  
  137. scope.$on('setDate', function (event, date, view) {
  138. updateInput(event);
  139. if (dismiss && views[views.length - 1] === view) {
  140. clear();
  141. }
  142. });
  143.  
  144. scope.$on('hidePicker', function () {
  145. element.triggerHandler('blur');
  146. });
  147.  
  148. scope.$on('$destroy', clear);
  149.  
  150. // move picker below input element
  151.  
  152. if (position === 'absolute') {
  153. var pos = angular.extend(element.offset(), { height: element[0].offsetHeight });
  154. picker.css({ top: pos.top + pos.height, left: pos.left, display: 'block', position: position});
  155. body.append(picker);
  156. } else {
  157. // relative
  158. container = angular.element('<div date-picker-wrapper></div>');
  159. element[0].parentElement.insertBefore(container[0], element[0]);
  160. container.append(picker);
  161. // this approach doesn't work
  162. // element.before(picker);
  163. picker.css({top: element[0].offsetHeight + 'px', display: 'block'});
  164. }
  165.  
  166. picker.bind('mousedown', function (evt) {
  167. evt.preventDefault();
  168. });
  169. }
  170.  
  171. element.bind('focus', showPicker);
  172. element.bind('blur', clear);
  173.  
  174. }
  175. };
  176. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement