Last active
August 29, 2015 14:06
-
-
Save justincjahn/d914e7d99d36bd658b4a to your computer and use it in GitHub Desktop.
Ember.js Bootstrap Date Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| /** | |
| * A specialized set of input elements that provide a date range picker. | |
| * | |
| * Provides validation on the end date to prevent selecting dates earlier | |
| * than specified in the start date. | |
| * | |
| * Usage: | |
| * | |
| * ```handlebars | |
| * {{date-range-picker startDate=myStartVariable endDate=myEndVariable}} | |
| * ``` | |
| */ | |
| App.DateRangePickerComponent = Ember.Component.extend({ | |
| classNames: [ | |
| 'input-group', | |
| 'input-daterange' | |
| ], | |
| format: "mm/dd/yyyy", | |
| register: function () { | |
| var self = this; | |
| var options = { | |
| format: this.get('format'), | |
| autoclose: true, | |
| todayHighlight: true, | |
| keyboardNavigation: false, | |
| language: 'en' | |
| }; | |
| // Wire up the datepicker on the startDate input | |
| this.$().find('.date-start').datepicker(options).on('changeDate', function (ev) { | |
| // Update the end date | |
| self.set('startDate', ev.date); | |
| // Set the endDate input's startDate to the provided value | |
| self.$().find('.date-end').datepicker('setStartDate', ev.date); | |
| // If the selected date is newer than the end date, then push it forward. | |
| var endDate = self.get('endDate'); | |
| if (!Ember.isEmpty(endDate) && endDate < ev.date) { | |
| self.$().find('.date-end').datepicker('setDate', ev.date); | |
| } | |
| }); | |
| // Wire up the datepicker on the endDate input | |
| this.$().find('.date-end').datepicker(options).on('changeDate', function (ev) { | |
| self.set('endDate', ev.date); | |
| }); | |
| // Wire up the clear button on the endDate input | |
| this.$().find('.clear-button').click(function () { | |
| self.set('endDate', null); | |
| $(this).prev('input').val(''); | |
| }); | |
| // Set initial date(s) | |
| if (!Ember.isEmpty(this.get('startDate'))) { | |
| this.$().find('.date-start').datepicker('setDate', this.get('startDate')); | |
| } | |
| // Set initial date(s) | |
| if (!Ember.isEmpty(this.get('endDate'))) { | |
| this.$().find('.date-end').datepicker('setDate', this.get('endDate')); | |
| } | |
| }.on('didInsertElement') | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .clear-button { | |
| position: absolute; | |
| right: 0; | |
| top: 0; | |
| height: 14px; | |
| margin: auto; | |
| font-size: 14px; | |
| cursor: pointer; | |
| color: #ccc; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment