Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.43 KB | None | 0 0
  1. 'use strict';
  2.  
  3. (function(angular) {
  4. angular.module('nDaterangepicker', []);
  5.  
  6. function PropertyUtil() {
  7. var getProperties = function(obj) {
  8. var result = {};
  9.  
  10. for (var property in obj) {
  11. if (obj.hasOwnProperty(property) && typeof obj[property] != 'function') {
  12. result[property] = obj[property];
  13. }
  14. }
  15.  
  16. return result;
  17. };
  18.  
  19. return {
  20. getProperties: function(obj) {
  21. return getProperties(obj);
  22. }
  23. };
  24. }
  25.  
  26. angular.module('nDaterangepicker')
  27. .service('DateRangePickerLocaleService', function() {
  28. this.applyLabel = 'Apply';
  29. this.cancelLabel = 'Cancel';
  30. this.fromLabel = 'From';
  31. this.toLabel = 'To';
  32. this.weekLabel = 'W';
  33. this.customRangeLabel = 'Custom Range';
  34. this.daysOfWeek = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
  35. this.monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  36. this.firstDay = 0;
  37.  
  38. this.setApplyLabel = function(applyLabel) {
  39. this.applyLabel = applyLabel;
  40.  
  41. return this;
  42. };
  43.  
  44. this.getApplyLabel = function() {
  45. return this.applyLabel;
  46. };
  47.  
  48. this.setCancelLabel = function(cancelLabel) {
  49. this.cancelLabel = cancelLabel;
  50.  
  51. return this;
  52. };
  53.  
  54. this.getCancelLabel = function() {
  55. return this.cancelLabel;
  56. };
  57.  
  58. this.setFromLabel = function(fromLabel) {
  59. this.fromLabel = fromLabel;
  60.  
  61. return this;
  62. };
  63.  
  64. this.getFromLabel = function() {
  65. return this.fromLabel;
  66. };
  67.  
  68. this.setToLabel = function(toLabel) {
  69. this.toLabel = toLabel;
  70.  
  71. return this;
  72. };
  73.  
  74. this.getToLabel = function() {
  75. return this.toLabel;
  76. };
  77.  
  78. this.setWeekLabel = function(weekLabel) {
  79. this.weekLabel = weekLabel;
  80.  
  81. return this;
  82. };
  83.  
  84. this.getWeekLabel = function() {
  85. return this.weekLabel;
  86. };
  87.  
  88. this.setCustomRangeLabel = function(customRangeLabel) {
  89. this.customRangeLabel = customRangeLabel;
  90.  
  91. return this;
  92. };
  93.  
  94. this.getCustomRangeLabel = function() {
  95. return this.customRangeLabel;
  96. };
  97.  
  98. this.setDaysOfWeek = function(daysOfWeek) {
  99. this.daysOfWeek = daysOfWeek;
  100.  
  101. return this;
  102. };
  103.  
  104. this.getDaysOfWeek = function() {
  105. return this.daysOfWeek;
  106. };
  107.  
  108. this.setMonthNames = function(monthNames) {
  109. this.monthNames = monthNames;
  110.  
  111. return this;
  112. };
  113.  
  114. this.getMonthNames = function() {
  115. return this.monthNames;
  116. };
  117.  
  118. this.setFirstDay = function(firstDay) {
  119. this.firstDay = firstDay;
  120.  
  121. return this;
  122. };
  123.  
  124. this.getFirstDay = function() {
  125. return this.firstDay;
  126. };
  127.  
  128. this.getList = function() {
  129. return (PropertyUtil()).getProperties(this);
  130. };
  131. });
  132.  
  133. angular.module('nDaterangepicker')
  134. .service('DateRangePickerService', function() {
  135. this.startDate;
  136. this.endDate;
  137. this.minDate;
  138. this.maxDate;
  139. this.dateLimit = false;
  140. this.timeZone = null;
  141. this.showDropdowns = false;
  142. this.showWeekNumbers = false;
  143. this.timePicker = false;
  144. this.timePickerIncrement = 30;
  145. this.timePicker12Hour = false;
  146. this.timePickerSeconds = false;
  147. this.ranges = false;
  148. this.opens = 'right'; // 'left'/'right'/'center'
  149. this.buttonClasses = ['btn', 'btn-small btn-sm'];
  150. this.applyClass = 'btn-success';
  151. this.cancelClass = 'btn-default';
  152. this.format = 'MM/DD/YYYY';
  153. this.separator = ' - ';
  154. this.singleDatePicker = false;
  155. this.parentEl = '';
  156.  
  157. this.iconBtnClasses = [];
  158.  
  159. this.type = 'string';
  160. this.availableDateTypes = ['string', 'date', 'moment'];
  161.  
  162. this.readonly = true;
  163.  
  164. this.setStartDate = function(startDate) {
  165. this.startDate = startDate;
  166.  
  167. return this;
  168. };
  169.  
  170. this.getStartDate = function() {
  171. return this.startDate;
  172. };
  173.  
  174. this.setEndDate = function(endDate) {
  175. this.endDate = endDate;
  176.  
  177. return this;
  178. };
  179.  
  180. this.getEndDate = function() {
  181. return this.endDate;
  182. };
  183.  
  184. this.setMinDate = function(minDate) {
  185. this.minDate = minDate;
  186.  
  187. return this;
  188. };
  189.  
  190. this.getMinDate = function() {
  191. return this.minDate;
  192. };
  193.  
  194. this.setMaxDate = function(maxDate) {
  195. this.maxDate = maxDate;
  196.  
  197. return this;
  198. };
  199.  
  200. this.getMaxDate = function() {
  201. return this.maxDate;
  202. };
  203.  
  204. this.setDateLimit = function(dateLimit) {
  205. this.dateLimit = dateLimit;
  206.  
  207. return this;
  208. };
  209.  
  210. this.getDateLimit = function() {
  211. return this.dateLimit;
  212. };
  213.  
  214. this.setTimeZone = function(timeZone) {
  215. this.timeZone = timeZone;
  216.  
  217. return this;
  218. };
  219.  
  220. this.getTimeZone = function() {
  221. return this.timeZone;
  222. };
  223.  
  224. this.setShowDropdowns = function(showDropdowns) {
  225. this.showDropdowns = showDropdowns;
  226.  
  227. return this;
  228. };
  229.  
  230. this.isShowDropdowns = function() {
  231. return this.showDropdowns;
  232. };
  233.  
  234. this.setShowWeekNumbers = function(showWeekNumbers) {
  235. this.showWeekNumbers = showWeekNumbers;
  236.  
  237. return this;
  238. };
  239.  
  240. this.isShowWeekNumbers = function() {
  241. return this.showWeekNumbers;
  242. };
  243.  
  244. this.setTimePicker = function(timePicker) {
  245. this.timePicker = timePicker;
  246.  
  247. return this;
  248. };
  249.  
  250. this.isTimePicker = function() {
  251. return this.timePicker;
  252. };
  253.  
  254. this.setTimePickerIncrement = function(timePickerIncrement) {
  255. this.timePickerIncrement = timePickerIncrement;
  256.  
  257. return this;
  258. };
  259.  
  260. this.getTimePickerIncrement = function() {
  261. return this.timePickerIncrement;
  262. };
  263.  
  264. this.setTimePicker12Hour = function(timePicker12Hour) {
  265. this.timePicker12Hour = timePicker12Hour;
  266.  
  267. return this;
  268. };
  269.  
  270. this.isTimePicker12Hour = function() {
  271. return this.timePicker12Hour;
  272. };
  273.  
  274. this.setTimePickerSeconds = function(timePickerSeconds) {
  275. this.timePickerSeconds = timePickerSeconds;
  276.  
  277. return this;
  278. };
  279.  
  280. this.isTimePickerSeconds = function() {
  281. return this.timePickerSeconds;
  282. };
  283.  
  284. this.addRange = function(rangeName, range) {
  285. this.ranges[rangeName] = range;
  286.  
  287. return this;
  288. };
  289.  
  290. this.setRanges = function(ranges) {
  291. this.ranges = ranges;
  292.  
  293. return this;
  294. };
  295.  
  296. this.getRanges = function() {
  297. return this.ranges;
  298. };
  299.  
  300. this.setOpens = function(opens) {
  301. this.opens = opens;
  302.  
  303. return this;
  304. };
  305.  
  306. this.getOpens = function() {
  307. return this.opens;
  308. };
  309.  
  310. this.addButtonClass = function(buttonClass) {
  311. this.buttonClasses.push(buttonClass);
  312.  
  313. return this;
  314. };
  315.  
  316. this.setButtonClasses = function(buttonClasses) {
  317. this.buttonClasses = buttonClasses;
  318.  
  319. return this;
  320. };
  321.  
  322. this.getButtonClasses = function() {
  323. return this.buttonClasses;
  324. };
  325.  
  326. this.setApplyBtnClass = function(applyClass) {
  327. this.applyClass = applyClass;
  328.  
  329. return this;
  330. };
  331.  
  332. this.getApplyBtnClass = function() {
  333. return this.applyClass;
  334. };
  335.  
  336. this.setCancelBtnClass = function(cancelClass) {
  337. this.cancelClass = cancelClass;
  338.  
  339. return this;
  340. };
  341.  
  342. this.getCancelBtnClass = function() {
  343. return this.cancelClass;
  344. };
  345.  
  346. this.setFormat = function(format) {
  347. this.format = angular.uppercase(format);
  348.  
  349. return this;
  350. };
  351.  
  352. this.getFormat = function() {
  353. return this.format;
  354. };
  355.  
  356. this.setSeparator = function(separator) {
  357. this.separator = separator;
  358.  
  359. return this;
  360. };
  361.  
  362. this.getSeparator = function() {
  363. return this.separator;
  364. };
  365.  
  366. this.setParentEl = function(parentEl) {
  367. this.parentEl = parentEl;
  368.  
  369. return this;
  370. };
  371.  
  372. this.getParentEl = function() {
  373. return this.parentEl;
  374. };
  375.  
  376. this.addIconBtnClass = function(iconBtnClass) {
  377. this.iconBtnClasses.push(iconBtnClass);
  378.  
  379. return this;
  380. };
  381.  
  382. this.setIconBtnClasses = function(iconBtnClasses) {
  383. this.iconBtnClasses = iconBtnClasses;
  384.  
  385. return this;
  386. };
  387.  
  388. this.getIconBtnClasses = function() {
  389. return this.iconBtnClasses;
  390. };
  391.  
  392. this.setType = function(type) {
  393. if (this.isValidDateType(type)) {
  394. this.type = type;
  395. }
  396.  
  397. return this;
  398. };
  399.  
  400. this.isValidDateType = function(type) {
  401. if (-1 == this.availableDateTypes.indexOf(type)) {
  402. throw new TypeError('Got type (' + type + ') and expected one of the following (' + this.availableDateTypes.join(',') + ')');
  403. }
  404. return true;
  405. };
  406.  
  407. this.getType = function() {
  408. return this.type;
  409. };
  410.  
  411. this.setReadonly = function(readonly) {
  412. this.readonly = readonly;
  413.  
  414. return this;
  415. };
  416.  
  417. this.getReadonly = function() {
  418. return this.readonly;
  419. };
  420.  
  421. this.getList = function() {
  422. return (PropertyUtil()).getProperties(this);
  423. };
  424. });
  425.  
  426. angular.module('nDaterangepicker')
  427. .directive('shorthandDateRangePicker', function() {
  428. return {
  429. restrict: 'E',
  430. scope: {
  431. identifier: '@',
  432. name: '@',
  433. withBtn: '@',
  434. resetable: '@',
  435. model: '=',
  436. options: '='
  437. },
  438. template: function(tElement, tAttrs) {
  439. var template = '<input type="text" id="{{identifier}}" class="form-control" name="{{name}}" ' +
  440. 'ng-model="model" options="options" date-range-picker />';
  441.  
  442. if (tAttrs.withBtn == 'true') {
  443. template = '' +
  444. '<div class="input-group date-range-picker">' +
  445. template +
  446. '<span class="input-group-btn">' +
  447. '<label type="button" class="btn btn-default date" for="{{identifier}}">' +
  448. '<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>' +
  449. '</label>' +
  450. '</span>';
  451. }
  452. else {
  453. template = '' +
  454. '<div class="date-range-picker">' +
  455. template;
  456. }
  457.  
  458. if (tAttrs.resetable == 'true') {
  459. template += '<div class="input-group">' +
  460. '<button type="button" class="btn btn-link" title="Reset" ng-click="reset()" ng-show="model.startDate != null">Reset</button>' +
  461. '</div>';
  462. }
  463.  
  464. template += '</div>';
  465.  
  466. return template;
  467. },
  468. link: function(scope, iElement, iAttrs, controller, transcludeFn) {
  469. scope.reset = function() {
  470. scope.$broadcast(scope.identifier + 'Reset');
  471. };
  472. }
  473. };
  474. });
  475.  
  476. angular.module('nDaterangepicker')
  477. .directive('dateRangePicker', function($timeout, DateRangePickerService, DateRangePickerLocaleService) {
  478. return {
  479. restrict: 'A',
  480. require: '^ngModel',
  481. scope: {
  482. options: '='
  483. },
  484. controller: function($scope, $element, $attrs, $transclude) {
  485. var locale = angular.extend({}, DateRangePickerLocaleService.getList());
  486.  
  487. if ($scope.options && $scope.options.locale) {
  488. locale = angular.extend(locale, $scope.options.locale);
  489. delete $scope.options.locale;
  490. }
  491.  
  492. $scope.internalOptions = angular.extend({}, DateRangePickerService.getList(), {locale: locale}, $scope.options);
  493.  
  494. DateRangePickerService.isValidDateType($scope.internalOptions.type);
  495. },
  496. link: function(scope, iElement, iAttrs, ngModelCtrl, transcludeFn) {
  497. var el = angular.element(iElement),
  498. _init,
  499. _getPicker,
  500. _setViewValue,
  501. _setIsSingleDatePicker,
  502.  
  503. _formatted,
  504. _getMoment,
  505. _toType,
  506. _getDateToSet,
  507.  
  508. _validate,
  509. _validateMin,
  510. _validateMax,
  511. _isEmpty;
  512.  
  513. // Set as readonly. No need for user to interact with it other than through UI (input fields present inside)
  514. if (scope.internalOptions.readonly === true) {
  515. if (typeof el.attr('readonly') === 'undefined') {
  516. el.attr('readonly', 'readonly');
  517. }
  518. }
  519.  
  520. // Reset date
  521. if (scope.options && scope.options.identifier) {
  522. scope.$on(scope.options.identifier + 'Reset', function(event) {
  523. return $timeout(function() {
  524. return scope.$apply(function() {
  525. var picker = _getPicker(),
  526. dateToSet = _getDateToSet();
  527.  
  528. picker.setStartDate(dateToSet.format(scope.internalOptions.format));
  529. picker.setEndDate(dateToSet.format(scope.internalOptions.format));
  530.  
  531. _setViewValue(null, null);
  532.  
  533. return ngModelCtrl.$render();
  534. });
  535. });
  536. });
  537. }
  538.  
  539. ngModelCtrl.$formatters.push(function(viewVal) {
  540. if (angular.isUndefined(_getPicker())) {
  541. _setIsSingleDatePicker(viewVal);
  542. _init();
  543. }
  544. else {
  545. return viewVal;
  546. }
  547.  
  548. var _setRange = function(startDate, endDate) {
  549. var picker = _getPicker();
  550.  
  551. if (!_getMoment(startDate).isValid()) {
  552. throw new Error('Either invalid startDate was passed or invalid format. Please check!');
  553. }
  554.  
  555. if (!_getMoment(endDate).isValid()) {
  556. throw new Error('Either invalid endDate was passed or invalid format. Please check!');
  557. }
  558.  
  559. picker.setStartDate(startDate);
  560. picker.setEndDate(endDate);
  561. };
  562.  
  563. if (_isEmpty(viewVal)) {
  564. var dateToSet = _getDateToSet();
  565. _setRange(dateToSet, dateToSet);
  566.  
  567. return '';
  568. }
  569. else {
  570. if (scope.internalOptions.singleDatePicker) {
  571. _setRange(viewVal, viewVal);
  572. }
  573. else {
  574. _setRange(viewVal.startDate, viewVal.endDate);
  575. }
  576.  
  577. return viewVal;
  578. }
  579. });
  580.  
  581. ngModelCtrl.$parsers.push(function(viewVal) {
  582. if (angular.isUndefined(scope.internalOptions.readonly) || scope.internalOptions.readonly !== true) {
  583. return viewVal;
  584. }
  585.  
  586. if (scope.internalOptions.singleDatePicker) {
  587. return viewVal;
  588. }
  589. else if (!angular.isObject(viewVal) || !(viewVal.hasOwnProperty('startDate') && viewVal.hasOwnProperty('endDate'))) {
  590. return ngModelCtrl.$modelValue;
  591. }
  592.  
  593. return viewVal;
  594. });
  595.  
  596. ngModelCtrl.$isEmpty = function(value) {
  597. return _isEmpty(value);
  598. };
  599.  
  600. ngModelCtrl.$render = function() {
  601. if (angular.isUndefined(scope.internalOptions.readonly) || scope.internalOptions.readonly !== true) {
  602. return ngModelCtrl.$viewValue;
  603. }
  604.  
  605. if (!ngModelCtrl.$modelValue) {
  606. return el.val('');
  607. }
  608. if (ngModelCtrl.$modelValue.startDate === null) {
  609. return el.val('');
  610. }
  611.  
  612. _validate(ngModelCtrl.$modelValue);
  613.  
  614. return el.val(_formatted(ngModelCtrl.$modelValue));
  615. };
  616.  
  617. _init = function() {
  618. return el.daterangepicker(scope.internalOptions, function(start, end, label) {
  619. return $timeout(function() {
  620. return scope.$apply(function() {
  621. _setViewValue(start, end);
  622.  
  623. return ngModelCtrl.$render();
  624. });
  625. });
  626. });
  627. };
  628.  
  629. _getPicker = function() {
  630. return el.data('daterangepicker');
  631. };
  632.  
  633. _setViewValue = function(startDate, endDate) {
  634. if (!scope.internalOptions.singleDatePicker) {
  635. ngModelCtrl.$setViewValue({
  636. startDate: startDate !== null ? _toType(startDate) : null,
  637. endDate: endDate !== null ? _toType(endDate) : null
  638. });
  639. }
  640. else {
  641. ngModelCtrl.$setViewValue(startDate !== null ? _toType(startDate) : null);
  642. }
  643. };
  644.  
  645. _setIsSingleDatePicker = function(value) {
  646. if (value) {
  647. scope.internalOptions.singleDatePicker = !value.hasOwnProperty('startDate') && !value.hasOwnProperty('endDate');
  648. }
  649. else {
  650. scope.internalOptions.singleDatePicker = true;
  651. }
  652. };
  653.  
  654. _formatted = function(viewVal) {
  655. var f = function(date) {
  656. return _getMoment(date).format(scope.internalOptions.format);
  657. },
  658. result = '';
  659.  
  660. if (scope.internalOptions.singleDatePicker) {
  661. result = f(viewVal);
  662. }
  663. else {
  664. result = [f(viewVal.startDate), f(viewVal.endDate)].join(scope.internalOptions.separator);
  665. }
  666.  
  667. return result;
  668. };
  669.  
  670. _getMoment = function(date) {
  671. if (moment.isMoment(date)) {
  672. return date;
  673. }
  674. else {
  675. var result;
  676.  
  677. if (date instanceof Date) {
  678. result = moment(date);
  679. }
  680. else {
  681. result = moment(date, scope.internalOptions.format);
  682. }
  683.  
  684. return result;
  685. }
  686. };
  687.  
  688. _toType = function(date) {
  689. var momentObj = _getMoment(date);
  690.  
  691. switch (scope.internalOptions.type) {
  692. case 'moment': {
  693. return momentObj;
  694. }
  695.  
  696. case 'date': {
  697. return momentObj.toDate();
  698. }
  699.  
  700. default:
  701. case 'string': {
  702. return momentObj.format(scope.internalOptions.format);
  703. }
  704. }
  705. };
  706.  
  707. _getDateToSet = function() {
  708. var dateToSet,
  709. currentDate = moment(),
  710. maxDate = scope.internalOptions.maxDate,
  711. minDate = scope.internalOptions.minDate;
  712.  
  713. if (maxDate && typeof maxDate != 'undefined' && currentDate.isAfter(maxDate)) {
  714. dateToSet = _getMoment(maxDate);
  715. }
  716. else if (minDate && typeof minDate != 'undefined' && currentDate.isBefore(minDate)) {
  717. dateToSet = _getMoment(minDate);
  718. }
  719. else {
  720. dateToSet = currentDate;
  721. }
  722.  
  723. return dateToSet;
  724. };
  725.  
  726. _validate = function(value) {
  727. var startDate = typeof value.startDate != 'undefined' ? value.startDate : value,
  728. endDate = typeof value.endDate != 'undefined' ? value.endDate : value;
  729.  
  730. if (scope.internalOptions.minDate && startDate) {
  731. _validateMin(scope.internalOptions.minDate, startDate);
  732. } else {
  733. ngModelCtrl.$setValidity('minDate', true);
  734. }
  735. if (scope.internalOptions.maxDate && endDate) {
  736. _validateMax(scope.internalOptions.maxDate, endDate);
  737. } else {
  738. ngModelCtrl.$setValidity('maxDate', true);
  739. }
  740. };
  741.  
  742. _validateMin = function(min, start) {
  743. var valid;
  744.  
  745. min = _getMoment(min);
  746. start = _getMoment(start);
  747. valid = min.isBefore(start) || min.isSame(start, 'day');
  748. ngModelCtrl.$setValidity('minDate', valid);
  749.  
  750. return valid;
  751. };
  752.  
  753. _validateMax = function(max, end) {
  754. var valid;
  755.  
  756. max = _getMoment(max);
  757. end = _getMoment(end);
  758. valid = max.isAfter(end) || max.isSame(end, 'day');
  759. ngModelCtrl.$setValidity('maxDate', valid);
  760.  
  761. return valid;
  762. };
  763.  
  764. _isEmpty = function(value) {
  765. return !value || (value.startDate === null || value.endDate === null);
  766. };
  767.  
  768. el.on('apply.daterangepicker', function(event, picker) {
  769. return $timeout(function() {
  770. return scope.$apply(function() {
  771. _setViewValue(picker.startDate, picker.endDate);
  772.  
  773. return ngModelCtrl.$render();
  774. });
  775. });
  776. });
  777.  
  778. el.keyup(function(e) {
  779. if (e.keyCode == 27) { // esc
  780. _getPicker().hide();
  781. }
  782. });
  783. }
  784. };
  785. });
  786. })(angular);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement