|
1 | 1 | /*!
|
2 | 2 | * knockout-daterangepicker
|
3 |
| - * version: 0.0.3 |
| 3 | + * version: 0.0.4 |
4 | 4 | * authors: Sensor Tower team
|
5 | 5 | * license: MIT
|
6 | 6 | * https://sensortower.github.io/daterangepicker
|
|
102 | 102 | })();
|
103 | 103 |
|
104 | 104 | $.fn.daterangepicker = function(options, callback) {
|
| 105 | + if (options == null) { |
| 106 | + options = {}; |
| 107 | + } |
105 | 108 | this.each(function() {
|
106 | 109 | var $element;
|
107 | 110 | $element = $(this);
|
108 | 111 | if (!$element.data('daterangepicker')) {
|
109 | 112 | options.anchorElement = $element;
|
110 | 113 | if (callback) {
|
111 |
| - options.callback = $.proxy(callback, this); |
| 114 | + options.callback = callback; |
112 | 115 | }
|
| 116 | + options.callback = $.proxy(options.callback, this); |
113 | 117 | return $element.data('daterangepicker', new DateRangePickerView(options));
|
114 | 118 | }
|
115 | 119 | });
|
|
126 | 130 |
|
127 | 131 | ko.virtualElements.allowedBindings.stopBinding = true;
|
128 | 132 |
|
129 |
| - ko.bindingHandlers.daterangepicker = { |
130 |
| - init: function(element, valueAccessor, allBindings) { |
131 |
| - var observable, options; |
132 |
| - observable = valueAccessor(); |
133 |
| - options = allBindings.get('daterangepickerOptions') || {}; |
134 |
| - return $(element).daterangepicker(options, function(startDate, endDate, period) { |
135 |
| - return observable([startDate, endDate, period]); |
136 |
| - }); |
137 |
| - }, |
138 |
| - update: function(element, valueAccessor, allBindings) { |
139 |
| - var $element, dateFormat, endDate, endDateText, period, ref, startDate, startDateText; |
140 |
| - $element = $(element); |
141 |
| - ref = valueAccessor()(), startDate = ref[0], endDate = ref[1], period = ref[2]; |
142 |
| - dateFormat = 'MMM D, YYYY'; |
143 |
| - startDateText = moment(startDate).format(dateFormat); |
144 |
| - endDateText = moment(endDate).format(dateFormat); |
145 |
| - return ko.ignoreDependencies(function() { |
146 |
| - if ($element.data('daterangepicker').single()) { |
147 |
| - $element.val(startDateText); |
148 |
| - } else { |
149 |
| - $element.val(startDateText + " – " + endDateText); |
150 |
| - } |
151 |
| - $element.data('daterangepicker').period(period); |
152 |
| - $element.data('daterangepicker').startDate(startDate); |
153 |
| - return $element.data('daterangepicker').endDate(endDate); |
154 |
| - }); |
155 |
| - } |
156 |
| - }; |
| 133 | + ko.bindingHandlers.daterangepicker = (function() { |
| 134 | + return $.extend(this, { |
| 135 | + _optionsKey: 'daterangepickerOptions', |
| 136 | + _formatKey: 'daterangepickerFormat', |
| 137 | + init: function(element, valueAccessor, allBindings) { |
| 138 | + var observable, options; |
| 139 | + observable = valueAccessor(); |
| 140 | + options = ko.unwrap(allBindings.get(this._optionsKey)) || {}; |
| 141 | + return $(element).daterangepicker(options, function(startDate, endDate, period) { |
| 142 | + return observable([startDate, endDate]); |
| 143 | + }); |
| 144 | + }, |
| 145 | + update: function(element, valueAccessor, allBindings) { |
| 146 | + var $element, dateFormat, endDate, endDateText, ref, startDate, startDateText; |
| 147 | + $element = $(element); |
| 148 | + ref = valueAccessor()(), startDate = ref[0], endDate = ref[1]; |
| 149 | + dateFormat = ko.unwrap(allBindings.get(this._formatKey)) || 'MMM D, YYYY'; |
| 150 | + startDateText = moment(startDate).format(dateFormat); |
| 151 | + endDateText = moment(endDate).format(dateFormat); |
| 152 | + return ko.ignoreDependencies(function() { |
| 153 | + var text; |
| 154 | + if (!$element.data('daterangepicker').standalone()) { |
| 155 | + text = $element.data('daterangepicker').single() ? startDateText : startDateText + " – " + endDateText; |
| 156 | + $element.val(text).text(text); |
| 157 | + } |
| 158 | + $element.data('daterangepicker').startDate(startDate); |
| 159 | + return $element.data('daterangepicker').endDate(endDate); |
| 160 | + }); |
| 161 | + } |
| 162 | + }); |
| 163 | + })(); |
157 | 164 |
|
158 | 165 | DateRange = (function() {
|
159 | 166 | function DateRange(title, startDate, endDate) {
|
|
289 | 296 | this.expanded = this._expanded(options.expanded);
|
290 | 297 | this.standalone = this._standalone(options.standalone);
|
291 | 298 | this.locale = this._locale(options.locale);
|
292 |
| - this.opens = this._opens(options.opens); |
| 299 | + this.orientation = this._orientation(options.orientation); |
293 | 300 | this.forceUpdate = options.forceUpdate;
|
294 | 301 | this.minDate = this._minDate(options.minDate);
|
295 | 302 | this.maxDate = this._maxDate(options.maxDate);
|
|
414 | 421 | };
|
415 | 422 | };
|
416 | 423 |
|
417 |
| - Config.prototype._opens = function(val) { |
| 424 | + Config.prototype._orientation = function(val) { |
418 | 425 | if (val !== 'right' && val !== 'left') {
|
419 | 426 | val = 'right';
|
420 | 427 | }
|
|
518 | 525 | };
|
519 | 526 |
|
520 | 527 | Config.prototype._parentElement = function(val) {
|
521 |
| - if (this.anchorElement) { |
| 528 | + if (this.standalone()) { |
| 529 | + return this.anchorElement; |
| 530 | + } else { |
522 | 531 | return $(val || 'body');
|
523 | 532 | }
|
524 | 533 | };
|
|
971 | 980 | var j, len, obj, period, ref;
|
972 | 981 | obj = {
|
973 | 982 | single: this.single(),
|
974 |
| - opened: this.opened(), |
| 983 | + opened: this.standalone() || this.opened(), |
975 | 984 | expanded: this.standalone() || this.single() || this.expanded(),
|
976 | 985 | standalone: this.standalone(),
|
977 |
| - 'opens-left': this.opens() === 'left', |
978 |
| - 'opens-right': this.opens() === 'right', |
| 986 | + 'orientation-left': this.orientation() === 'left', |
| 987 | + 'orientation-right': this.orientation() === 'right', |
979 | 988 | 'hide-periods': this.periods().length === 1
|
980 | 989 | };
|
981 | 990 | ref = Period.allPeriods;
|
|
1051 | 1060 | if (this.opened()) {
|
1052 | 1061 | return this.close();
|
1053 | 1062 | } else {
|
1054 |
| - return open(); |
| 1063 | + return this.open(); |
1055 | 1064 | }
|
1056 | 1065 | };
|
1057 | 1066 |
|
|
1077 | 1086 | left: 'auto',
|
1078 | 1087 | right: 'auto'
|
1079 | 1088 | };
|
1080 |
| - switch (this.opens()) { |
| 1089 | + switch (this.orientation()) { |
1081 | 1090 | case 'left':
|
1082 | 1091 | if (this.containerElement.offset().left < 0) {
|
1083 | 1092 | style.left = '9px';
|
|
0 commit comments