Source: components/sl-date-picker.js

  1. import Ember from 'ember';
  2. import TooltipEnabled from '../mixins/sl-tooltip-enabled';
  3. import layout from '../templates/components/sl-date-picker';
  4. /**
  5. * @module
  6. * @augments ember/Component
  7. * @augments module:mixins/sl-tooltip-enabled
  8. */
  9. export default Ember.Component.extend( TooltipEnabled, {
  10. // -------------------------------------------------------------------------
  11. // Dependencies
  12. // -------------------------------------------------------------------------
  13. // Attributes
  14. /** @type {String[]} */
  15. classNames: [
  16. 'form-group',
  17. 'sl-date-picker'
  18. ],
  19. /** @type {Object} */
  20. layout,
  21. // -------------------------------------------------------------------------
  22. // Actions
  23. // -------------------------------------------------------------------------
  24. // Events
  25. // -------------------------------------------------------------------------
  26. // Properties
  27. /**
  28. * Whether or not to close the datepicker immediately when a date
  29. * is selected
  30. *
  31. * @type {Boolean}
  32. */
  33. autoclose: true,
  34. /**
  35. * Whether or not to show week numbers to the left of week rows
  36. *
  37. * @type {Boolean}
  38. */
  39. calendarWeeks: false,
  40. /**
  41. * When true, displays a "Clear" button at the bottom of the datepicker
  42. *
  43. * If "autoclose" is also set to true, this button will also close
  44. * the datepicker.
  45. *
  46. * @type {Boolean}
  47. */
  48. clearBtn: false,
  49. /**
  50. * Days of the week that should be disabled
  51. *
  52. * Values are 0 (Sunday) to 6 (Saturday). Multiple values should be
  53. * comma-separated.
  54. *
  55. * @type {Array|String}
  56. */
  57. daysOfWeekDisabled: [],
  58. /**
  59. * When true, the input field is disabled and the datepicker will never display
  60. *
  61. * @type {Boolean}
  62. */
  63. disabled: false,
  64. /**
  65. * The latest date that may be selected; all later dates will be disabled
  66. *
  67. * @type {?Date|String}
  68. */
  69. endDate: null,
  70. /**
  71. * Whether or not to force parsing of the input value when the picker is
  72. * closed
  73. *
  74. * When an invalid date is left in the input field by the user, the picker
  75. * will forcibly parse that value, and set the input's value to the new,
  76. * valid date, conforming to the given _format_.
  77. *
  78. * @type {Boolean}
  79. */
  80. forceParse: true,
  81. /**
  82. * The date format
  83. *
  84. * Combination of the following:
  85. * - d, dd: Numeric date, no leading zero and leading zero, respectively
  86. * - D, DD: Abbreviated and full weekday names, respectively
  87. * - m, mm: Numeric month, no leading zero and leading zero, respectively
  88. * - M, MM: Abbreviated and full month names, respectively
  89. * - yy, yyyy: 2- and 4-digit years, respectively
  90. *
  91. * @type {String}
  92. */
  93. format: 'mm/dd/yyyy',
  94. /**
  95. * The help text below the datepicker
  96. *
  97. * @type {String}
  98. */
  99. helpText: null,
  100. /**
  101. * The input field's id attribute
  102. *
  103. * Used to expose this value externally for use in this component and when
  104. * composing this component into others.
  105. *
  106. * @type {?String}
  107. */
  108. inputElementId: null,
  109. /**
  110. * A list of inputs to be used in a range picker
  111. *
  112. * The inputs will be attached to the selected element. Allows for
  113. * explicitly creating a range picker on a non-standard element.
  114. *
  115. * @type {?Array}
  116. */
  117. inputs: null,
  118. /**
  119. * Whether or not to allow date navigation by arrow keys
  120. *
  121. * @type {Boolean}
  122. */
  123. keyboardNavigation: true,
  124. /**
  125. * The label text above the datepicker's input field
  126. *
  127. * @type {String}
  128. */
  129. label: null,
  130. /**
  131. * The IETF code of the language to use for month and day names
  132. *
  133. * @type {String}
  134. */
  135. language: 'en',
  136. /**
  137. * Set a limit for the view mode; accepts "days", "months", or "years"
  138. *
  139. * @type {String}
  140. */
  141. minViewMode: 'days',
  142. /**
  143. * Enable multidate picking
  144. *
  145. * Each date in month view acts as a toggle button, keeping track of which
  146. * dates the user has selected in order. If a number is given, the picker
  147. * will limit how many dates can be selected to that number, dropping the
  148. * oldest dates from the list when the number is exceeded. true equates to
  149. * no limit. The input’s value (if present) is set to a string generated by
  150. * joining the dates, formatted, with multidateSeparator.
  151. *
  152. * @type {Boolean|Number}
  153. */
  154. multidate: false,
  155. /**
  156. * A space-separated string for the popup's anchor position
  157. *
  158. * Consists of one or two of "left" or "right", "top" or "bottom",
  159. * and "auto" (may be omitted).
  160. *
  161. * @type {String}
  162. */
  163. orientation: 'auto',
  164. /**
  165. * The placeholder text that the datepicker should show
  166. *
  167. * @type {?String}
  168. */
  169. placeholder: null,
  170. /**
  171. * The earliest date that may be selected; all earlier dates will
  172. * be disabled
  173. *
  174. * @type {?Date|String}
  175. */
  176. startDate: null,
  177. /**
  178. * The view that the datepicker should show when it is opened; accepts
  179. * "month", "year", or "decade"
  180. *
  181. * @type {String}
  182. */
  183. startView: 'month',
  184. /**
  185. * When true or "linked", displays a "Today" button at the bottom of the
  186. * datepicker to select the current date
  187. *
  188. * If true, the "Today" button will only move the current date into view.
  189. * If "linked", the current date will also be selected.
  190. *
  191. * @type {Boolean|String}
  192. */
  193. todayBtn: false,
  194. /**
  195. * Whether to highlight the current date or not
  196. *
  197. * @type {Boolean}
  198. */
  199. todayHighlight: false,
  200. /**
  201. * The date either selected by the datepicker or entered by the user
  202. *
  203. * @type {?String}
  204. */
  205. value: null,
  206. /**
  207. * Day of the week to start on; 0 (Sunday) to 6 (Saturday)
  208. *
  209. * @type {Number}
  210. */
  211. weekStart: 0,
  212. // -------------------------------------------------------------------------
  213. // Observers
  214. /**
  215. * Captures and sets the input field's id attribute.
  216. *
  217. * This is used to expose this value externally for use when composing this
  218. * component into others.
  219. *
  220. * @function
  221. * @returns {undefined}
  222. */
  223. setInputElementId: Ember.on(
  224. 'didInsertElement',
  225. function() {
  226. this.set(
  227. 'inputElementId',
  228. this.$( 'input.date-picker' ).prop( 'id' )
  229. );
  230. }
  231. ),
  232. /**
  233. * Setup the bootstrap-datepicker plugin and events
  234. *
  235. * @function
  236. * @returns {undefined}
  237. */
  238. setupDatepicker: Ember.on(
  239. 'didInsertElement',
  240. function() {
  241. const datepicker = this.$( 'input.date-picker' )
  242. .datepicker( this.get( 'options' ) );
  243. datepicker.on( 'changeDate', () => {
  244. this.sendAction();
  245. });
  246. }
  247. ),
  248. /**
  249. * Remove events
  250. *
  251. * @function
  252. * @returns {undefined}
  253. */
  254. unregisterEvents: Ember.on(
  255. 'willClearRender',
  256. function() {
  257. this.$( 'input.date-picker' ).off();
  258. }
  259. ),
  260. /**
  261. * Dynamically update the startDate and endDate values for the datepicker
  262. *
  263. * @function
  264. * @returns {undefined}
  265. */
  266. updateDateRange: Ember.observer(
  267. 'endDate',
  268. 'startDate',
  269. function() {
  270. const input = this.$( 'input.date-picker' );
  271. const datepicker = input.data( 'datepicker' );
  272. datepicker.setStartDate( this.get( 'startDate' ) );
  273. datepicker.setEndDate( this.get( 'endDate' ) );
  274. if ( 'Invalid Date' === datepicker.getDate().toString() ) {
  275. input.datepicker().val( '' );
  276. input.attr( 'placeholder', this.get( 'placeholder' ) );
  277. }
  278. }
  279. ),
  280. // -------------------------------------------------------------------------
  281. // Methods
  282. /**
  283. * Datepicker plugin options
  284. *
  285. * @function
  286. * @returns {Object}
  287. */
  288. options: Ember.computed(
  289. function() {
  290. return {
  291. autoclose: this.get( 'autoclose' ),
  292. calendarWeeks: this.get( 'calendarWeeks' ),
  293. clearBtn: this.get( 'clearBtn' ),
  294. daysOfWeekDisabled: this.get( 'daysOfWeekDisabled' ),
  295. endDate: this.get( 'endDate' ),
  296. forceParse: this.get( 'forceParse' ),
  297. format: this.get( 'format' ),
  298. inputs: this.get( 'inputs' ),
  299. keyboardNavigation: this.get( 'keyboardNavigation' ),
  300. language: this.get( 'language' ),
  301. multidate: this.get( 'multidate' ),
  302. orientation: this.get( 'orientation' ),
  303. startDate: this.get( 'startDate' ),
  304. startView: this.get( 'startView' ),
  305. todayBtn: this.get( 'todayBtn' ),
  306. todayHighlight: this.get( 'todayHighlight' ),
  307. weekStart: this.get( 'weekStart' )
  308. };
  309. }
  310. )
  311. });