From 5c8f10d65c5cf9c086b2b4a97027bd1b5e492398 Mon Sep 17 00:00:00 2001 From: Tom Huang Date: Tue, 21 Jun 2016 00:37:06 +0800 Subject: [PATCH] start Signed-off-by: Tom Huang --- .editorconfig | 15 + .gitattributes | 1 + .gitignore | 2 + .jshintrc | 31 ++ CHANGELOG.md | 74 +++ LICENSE | 36 ++ README.md | 331 +++++++++++++ bower.json | 28 ++ component.json | 17 + css/pikaday.css | 222 +++++++++ css/site.css | 42 ++ css/theme.css | 56 +++ examples/amd.html | 50 ++ examples/bound-container.html | 52 ++ examples/calendars.html | 57 +++ examples/container.html | 52 ++ examples/date-range.html | 83 ++++ examples/jquery-amd.html | 54 +++ examples/jquery.html | 46 ++ examples/moment.html | 53 ++ examples/positions.html | 106 ++++ examples/theme.html | 83 ++++ examples/trigger.html | 46 ++ examples/weeknumbers.html | 41 ++ index.html | 43 ++ package.json | 45 ++ pikaday.js | 1079 +++++++++++++++++++++++++++++++++++++++++ plugins/pikaday.jquery.js | 52 ++ scss/pikaday.scss | 217 +++++++++ tests/methods.js | 47 ++ tests/module.js | 24 + 31 files changed, 3085 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 bower.json create mode 100644 component.json create mode 100644 css/pikaday.css create mode 100644 css/site.css create mode 100644 css/theme.css create mode 100644 examples/amd.html create mode 100644 examples/bound-container.html create mode 100644 examples/calendars.html create mode 100644 examples/container.html create mode 100644 examples/date-range.html create mode 100644 examples/jquery-amd.html create mode 100644 examples/jquery.html create mode 100644 examples/moment.html create mode 100644 examples/positions.html create mode 100644 examples/theme.html create mode 100644 examples/trigger.html create mode 100644 examples/weeknumbers.html create mode 100644 index.html create mode 100644 package.json create mode 100644 pikaday.js create mode 100644 plugins/pikaday.jquery.js create mode 100644 scss/pikaday.scss create mode 100644 tests/methods.js create mode 100644 tests/module.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7f30d1b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; http://EditorConfig.org +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{js,css}] +indent_style = space +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38f211d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.c9revisions diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..69844c5 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,31 @@ +{ + "node": true, + "browser": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "eqeqeq": true, + "immed": true, + "indent": 4, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true, + "expr": true, + "globals": { + "define": false, + /* MOCHA */ + "describe" : false, + "it" : false, + "before" : false, + "beforeEach" : false, + "after" : false, + "afterEach" : false + } +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..adf4ba6 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,74 @@ +# Changelog + +## 1.4.0 - TBA + +## 1.3.3 - 2015-02-17 + +- Add theme class support, see #260 +- Fix on iOS, see #101, #121 +- Add date range example, see #337 +- Fix dates used in examples, see #329 + +## 1.3.2 - 2015-02-17 + +- Add options to disable days, see #171 +- Add SCSS file, see #130 + +## 1.3.1 - 2015-02-04 + +- Fix for bug introduced in #166; fixes #224, #230 + +## 1.3.0 - 2015-01-09 + +- Fix #79 moving through months can cause calendar to "bounce around" +- Add configurable number of shown months +- #51 Add unit tests with ci: [testling](https://ci.testling.com/rikkert/pikaday) +- Fix #94 blur input field after date selection +- Add configuable container to render calendar into +- Add option to show week numbers, see #147 +- Positioning is now optional, see #192 + +## 1.2.0 - 2014-01-28 + +- Fix #8 Automatically adjust datepicker position +- Merge #45 Adjusting position +- Merge #11 Allow using a different element to open and bind the datepicker to +- Use new Bower package filename: bower.json +- Merge #90 Configuration options for the title, useful for Asian languages. +- Merge #96 Use proper UMD-wrapper +- Merge #92 Export as CommonJS module +- Merge #119 Use a px text-indent on .pika-prev/next to prevent Firefox bug +- Merge #60 Add customizable position option +- Add Component package definition + +## 1.1.0 - 2013-03-29 + +- Add Ruby on Rails [gem](https://rubygems.org/gems/pikaday-gem) +- Add project configuration files. (jshint and editorconfig) +- Add AMD support to pikaday and pikaday.jquery + +- Fix #52 check if date is valid when moment.js is available +- Fix #54 add CSS support for IE8 + +Notes: + +- jQuery plugin needs pikaday.js loaded first +- See the examples/ dir: + - amd.html module loading example + - jquery-amd.html module loading the jQuery plugin example + - jquery.html jQuery plugin example + - moment.html moment.js library example + +## 1.0.0 - 2013-03-15 + +- Fix issue #41 (minDate does not include the specified date) +- Add Bower package definition + +## 0.0.2 - 2012-10-10 + +- jQuery plugin version + +## 0.0.1 - 2012-10-08 + +- Basic documentation written +- GitHub repository is ready to go… diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b2caa06 --- /dev/null +++ b/LICENSE @@ -0,0 +1,36 @@ +Copyright (c) 2014 David Bushell BSD & MIT license + +The MIT License (MIT) + +Copyright (c) 2014 David Bushell + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +The BSD License + +Copyright (c) 2014 David Bushell +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..58c8fd2 --- /dev/null +++ b/README.md @@ -0,0 +1,331 @@ +Pikaday +======== + +### A refreshing JavaScript Datepicker + +* Lightweight (less than 5kb minified and gzipped) +* No dependencies (but plays well with [Moment.js][moment]) +* Modular CSS classes for easy styling + +[**Try Pikaday Demo →**][Pikaday] + +![Pikaday Screenshot][screenshot] + +**Production ready?** Since version 1.0.0 Pikaday is stable and used in production. If you do however find bugs or have feature requests please submit them to the [GitHub issue tracker][issues]. +Also see the [changelog](CHANGELOG.md) + + +## Usage + +**Pikaday** can be bound to an input field: + +```html + +``` + +Add the JavaScript to the end of your document: + +```html + + +``` + +If you're using **jQuery** make sure to pass only the first element: + +```javascript +var picker = new Pikaday({ field: $('#datepicker')[0] }); +``` + +If the Pikaday instance is not bound to a field you can append the element anywhere: + +```javascript +var field = document.getElementById('datepicker'); +var picker = new Pikaday({ + onSelect: function(date) { + field.value = picker.toString(); + } +}); +field.parentNode.insertBefore(picker.el, field.nextSibling); +``` + +For advanced formatting load [Moment.js][moment] prior to Pikaday: +See the [moment.js example][] for a full version. + +```html + + + + + +``` + +### Configuration + +As the examples demonstrate above +Pikaday has many useful options: + +* `field` bind the datepicker to a form field +* `trigger` use a different element to trigger opening the datepicker, see [trigger example][] (default to `field`) +* `bound` automatically show/hide the datepicker on `field` focus (default `true` if `field` is set) +* `position` preferred position of the datepicker relative to the form field, e.g.: `top right`, `bottom right` **Note:** automatic adjustment may occur to avoid datepicker from being displayed outside the viewport, see [positions example][] (default to 'bottom left') +* `reposition` can be set to false to not reposition datepicker within the viewport, forcing it to take the configured `position` (default: true) +* `container` DOM node to render calendar into, see [container example][] (default: undefined) +* `format` the default output format for `.toString()` and `field` value (requires [Moment.js][moment] for custom formatting) +* `defaultDate` the initial date to view when first opened +* `setDefaultDate` make the `defaultDate` the initial selected value +* `firstDay` first day of the week (0: Sunday, 1: Monday, etc) +* `minDate` the minimum/earliest date that can be selected (this should be a native Date object - e.g. `new Date()` or `moment().toDate()`) +* `maxDate` the maximum/latest date that can be selected (this should be a native Date object - e.g. `new Date()` or `moment().toDate()`) +* `disableWeekends` disallow selection of Saturdays or Sundays +* `disableDayFn` callback function that gets passed a Date object for each day in view. Should return true to disable selection of that day. +* `yearRange` number of years either side (e.g. `10`) or array of upper/lower range (e.g. `[1900,2015]`) +* `showWeekNumber` show the ISO week number at the head of the row (default `false`) +* `isRTL` reverse the calendar for right-to-left languages +* `i18n` language defaults for month and weekday names (see internationalization below) +* `yearSuffix` additional text to append to the year in the title +* `showMonthAfterYear` render the month after year in the title (default `false`) +* `numberOfMonths` number of visible calendars +* `mainCalendar` when `numberOfMonths` is used, this will help you to choose where the main calendar will be (default `left`, can be set to `right`). Only used for the first display or when a selected date is not already visible +* `theme` define a classname that can be used as a hook for styling different themes, see [theme example][] (default `null`) +* `onSelect` callback function for when a date is selected +* `onOpen` callback function for when the picker becomes visible +* `onClose` callback function for when the picker is hidden +* `onDraw` callback function for when the picker draws a new month + +## jQuery Plugin + +The normal version of Pikaday does not require jQuery, however there is a jQuery plugin if that floats your boat (see `plugins/pikaday.jquery.js` in the repository). This version requires jQuery, naturally, and can be used like other plugins: +See the [jQuery example][] for a full version. + +```html + + + + +``` + +## AMD support + +If you use a modular script loader than Pikaday is not bound to the global object and will fit nicely in your build process. You can require Pikaday just like any other module. +See the [AMD example][] for a full version. + +```javascript +require(['pikaday'], function(Pikaday) { + var picker = new Pikaday({ field: document.getElementById('datepicker') }); +}); +``` +The same applies for the jQuery plugin mentioned above. +See the [jQuery AMD example][] for a full version. + +```javascript +require(['jquery', 'pikaday.jquery'], function($) { + $('#datepicker').pikaday(); +}); +``` + +## CommonJS module support + +If you use a CommonJS compatible environment you can use the require function to import Pikaday. + + +```javascript +var pikaday = require('pikaday'); +``` + +When you bundle all your required modules with [Browserify][browserify] and you don't use [Moment.js][moment] specify the ignore option: + +`browserify main.js -o bundle.js -i moment` + +## Ruby on Rails + +If you're using **Ruby on Rails**, make sure to check out the [Pikaday gem][gem]. + +## Methods + +You can control the date picker after creation: + +```javascript +var picker = new Pikaday({ field: document.getElementById('datepicker') }); +``` + +### Get and set date + +`picker.toString('YYYY-MM-DD')` + +Returns the selected date in a string format. If [Moment.js][moment] exists (recommended) then Pikaday can return any format that Moment understands, otherwise you're stuck with JavaScript's default. + +`picker.getDate()` + +Returns a basic JavaScript `Date` object of the selected day, or `null` if no selection. + +`picker.setDate('2015-01-01')` + +Set the current selection. This will be restricted within the bounds of `minDate` and `maxDate` options if they're specified. You can optionally pass a boolean as the second parameter to prevent triggering of the onSelect callback (true), allowing the date to be set silently. + +`picker.getMoment()` + +Returns a [Moment.js][moment] object for the selected date (Moment must be loaded before Pikaday). + +`picker.setMoment(moment('14th February 2014', 'DDo MMMM YYYY'))` + +Set the current selection with a [Moment.js][moment] object (see `setDate` for details). + +### Change current view + +`picker.gotoDate(new Date(2014, 1))` + +Change the current view to see a specific date. This example will jump to February 2014 ([month is a zero-based index][mdn_date]). + +`picker.gotoToday()` + +Shortcut for `picker.gotoDate(new Date())` + +`picker.gotoMonth(2)` + +Change the current view by month (0: January, 1: Februrary, etc). + +`picker.nextMonth()` +`picker.prevMonth()` + +Go to the next or previous month (this will change year if necessary). + +`picker.gotoYear()` + +Change the year being viewed. + +`picker.setMinDate()` + +Update the minimum/earliest date that can be selected. + +`picker.setMaxDate()` + +Update the maximum/latest date that can be selected. + +`picker.setStartRange()` + +Update the range start date. For using two Pikaday instances to select a date range. + +`picker.setEndRange()` + +Update the range end date. For using two Pikaday instances to select a date range. + +### Show and hide datepicker + +`picker.isVisible()` + +Returns `true` or `false`. + +`picker.show()` + +Make the picker visible. + +`picker.adjustPosition()` + +Recalculate and change the position of the picker. + +`picker.hide()` + +Hide the picker making it invisible. + +`picker.destroy()` + +Hide the picker and remove all event listeners — no going back! + +### Internationalization + +The default `i18n` configuration format looks like this: + +```javascript +i18n: { + previousMonth : 'Previous Month', + nextMonth : 'Next Month', + months : ['January','February','March','April','May','June','July','August','September','October','November','December'], + weekdays : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], + weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] +} +``` + +You must provide 12 months and 7 weekdays (with abbreviations). Always specify weekdays in this order with Sunday first. You can change the `firstDay` option to reorder if necessary (0: Sunday, 1: Monday, etc). You can also set `isRTL` to `true` for languages that are read right-to-left. + + +## Extensions + +### Timepicker + +Pikaday is a pure datepicker. It will not support picking a time of day. However, there have been efforts to add time support to Pikaday. +See [#1][issue1] and [#18][issue18]. These reside in their own fork. + +You can use the work [@owenmead][owenmead] did most recently at [owenmead/Pikaday][owen Pika] +A more simple time selection approach done by [@xeeali][xeeali] at [xeeali/Pikaday][xeeali Pika] is based on version 1.2.0. +Also [@stas][stas] has a fork [stas/Pikaday][stas Pika], but is now quite old + + +## Browser Compatibility + +* IE 7+ +* Chrome 8+ +* Firefox 3.5+ +* Safari 3+ +* Opera 10.6+ + +[![browser compatibility](https://ci.testling.com/rikkert/pikaday.png) +](https://ci.testling.com/rikkert/pikaday) + + +* * * + +## Authors + +* David Bushell [http://dbushell.com][Bushell] [@dbushell][Bushell Twitter] +* Ramiro Rikkert [GitHub][Rikkert] [@RamRik][Rikkert Twitter] + +Thanks to [@shoogledesigns][shoogledesigns] for the name. + +Copyright © 2014 David Bushell | BSD & MIT license + + [Pikaday]: http://dbushell.github.com/Pikaday/ "Pikaday" + [moment]: http://momentjs.com/ "moment.js" + [browserify]: http://browserify.org/ "browserify" + [screenshot]: https://raw.github.com/dbushell/Pikaday/gh-pages/screenshot.png "Screenshot" + [issues]: https://github.com/dbushell/Pikaday/issues "Issue tracker" + [gem]: https://rubygems.org/gems/pikaday-gem "RoR gem" + [mdn_date]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date "Date" + [Bushell]: http://dbushell.com/ "dbushell.com" + [Bushell Twitter]: https://twitter.com/dbushell "@dbushell" + [Rikkert]: https://github.com/rikkert "Rikkert GitHub" + [Rikkert Twitter]: https://twitter.com/ramrik "@ramrik" + [shoogledesigns]: https://twitter.com/shoogledesigns/status/255209384261586944 "@shoogledesigns" + [issue1]: https://github.com/dbushell/Pikaday/issues/1 "Issue 1" + [issue18]: https://github.com/dbushell/Pikaday/issues/18 "Issue 18" + [stas]: https://github.com/stas "@stas" + [stas Pika]: https://github.com/stas/Pikaday "Pikaday" + [owenmead]: https://github.com/owenmead "@owenmead" + [owen Pika]: https://github.com/owenmead/Pikaday "Pikaday" + [xeeali]: https://github.com/xeeali "@xeeali" + [xeeali Pika]: https://github.com/xeeali/Pikaday "Pikaday" + [moment.js example]: http://dbushell.github.com/Pikaday/examples/moment.html "Pikaday w/ moment.js" + [jQuery example]: http://dbushell.github.com/Pikaday/examples/jquery.html "Pikaday w/ jQuery" + [AMD example]: http://dbushell.github.com/Pikaday/examples/amd.html "Pikaday w/ AMD" + [jQuery AMD example]: http://dbushell.github.com/Pikaday/examples/jquery-amd.html "Pikaday w/ jQuery + AMD" + [trigger example]: http://dbushell.github.com/Pikaday/examples/trigger.html "Pikaday using custom trigger" + [positions example]: http://dbushell.github.com/Pikaday/examples/positions.html "Pikaday using different position options" + [container example]: http://dbushell.github.com/Pikaday/examples/container.html "Pikaday using custom calendar container" + [theme example]: http://dbushell.github.com/Pikaday/examples/theme.html "Pikaday using multiple themes" diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..07d5b11 --- /dev/null +++ b/bower.json @@ -0,0 +1,28 @@ +{ + "name": "pikaday", + "description": "A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS", + "keywords": [ + "datepicker", + "calendar", + "date" + ], + "main": [ + "./pikaday.js", + "./css/pikaday.css" + ], + "dependencies": {}, + "ignore": [ + "**/.*", + "node_modules", + "components" + ], + "license" : [ + "BSD", + "MIT" + ], + "homepage": "http://dbushell.github.io/Pikaday/", + "repository": { + "type": "git", + "url": "https://github.com/dbushell/Pikaday.git" + } +} diff --git a/component.json b/component.json new file mode 100644 index 0000000..c3d9e9f --- /dev/null +++ b/component.json @@ -0,0 +1,17 @@ +{ + "name": "pikaday", + "repo": "dbushell/Pikaday", + "description": "A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS", + "version": "1.3.3", + "keywords": [ + "datepicker", + "calendar", + "date" + ], + "main": "pikaday.js", + "scripts": ["pikaday.js"], + "styles": ["css/pikaday.css"], + "dependencies": {}, + "demo": "http://dbushell.github.io/Pikaday/", + "license": "MIT" +} diff --git a/css/pikaday.css b/css/pikaday.css new file mode 100644 index 0000000..2557752 --- /dev/null +++ b/css/pikaday.css @@ -0,0 +1,222 @@ +@charset "UTF-8"; + +/*! + * Pikaday + * Copyright © 2014 David Bushell | BSD & MIT license | http://dbushell.com/ + */ + +.pika-single { + z-index: 9999; + display: block; + position: relative; + color: #333; + background: #fff; + border: 1px solid #ccc; + border-bottom-color: #bbb; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; +} + +/* +clear child float (pika-lendar), using the famous micro clearfix hack +http://nicolasgallagher.com/micro-clearfix-hack/ +*/ +.pika-single:before, +.pika-single:after { + content: " "; + display: table; +} +.pika-single:after { clear: both } +.pika-single { *zoom: 1 } + +.pika-single.is-hidden { + display: none; +} + +.pika-single.is-bound { + position: absolute; + box-shadow: 0 5px 15px -5px rgba(0,0,0,.5); +} + +.pika-lendar { + float: left; + width: 240px; + margin: 8px; +} + +.pika-title { + position: relative; + text-align: center; +} + +.pika-label { + display: inline-block; + *display: inline; + position: relative; + z-index: 9999; + overflow: hidden; + margin: 0; + padding: 5px 3px; + font-size: 14px; + line-height: 20px; + font-weight: bold; + background-color: #fff; +} +.pika-title select { + cursor: pointer; + position: absolute; + z-index: 9998; + margin: 0; + left: 0; + top: 5px; + filter: alpha(opacity=0); + opacity: 0; + color: black; +} + +.pika-prev, +.pika-next { + display: block; + cursor: pointer; + position: relative; + outline: none; + border: 0; + padding: 0; + width: 20px; + height: 30px; + /* hide text using text-indent trick, using width value (it's enough) */ + text-indent: 20px; + white-space: nowrap; + overflow: hidden; + background-color: transparent; + background-position: center center; + background-repeat: no-repeat; + background-size: 75% 75%; + opacity: .5; + *position: absolute; + *top: 0; +} + +.pika-prev:hover, +.pika-next:hover { + opacity: 1; +} + +.pika-prev, +.is-rtl .pika-next { + float: left; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAUklEQVR42u3VMQoAIBADQf8Pgj+OD9hG2CtONJB2ymQkKe0HbwAP0xucDiQWARITIDEBEnMgMQ8S8+AqBIl6kKgHiXqQqAeJepBo/z38J/U0uAHlaBkBl9I4GwAAAABJRU5ErkJggg=='); + *left: 0; +} + +.pika-next, +.is-rtl .pika-prev { + float: right; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAU0lEQVR42u3VOwoAMAgE0dwfAnNjU26bYkBCFGwfiL9VVWoO+BJ4Gf3gtsEKKoFBNTCoCAYVwaAiGNQGMUHMkjGbgjk2mIONuXo0nC8XnCf1JXgArVIZAQh5TKYAAAAASUVORK5CYII='); + *right: 0; +} + +.pika-prev.is-disabled, +.pika-next.is-disabled { + cursor: default; + opacity: .2; +} + +.pika-select { + display: inline-block; + *display: inline; +} + +.pika-table { + width: 100%; + border-collapse: collapse; + border-spacing: 0; + border: 0; +} + +.pika-table th, +.pika-table td { + width: 14.285714285714286%; + padding: 0; +} + +.pika-table th { + color: #999; + font-size: 12px; + line-height: 25px; + font-weight: bold; + text-align: center; +} + +.pika-button { + cursor: pointer; + display: block; + box-sizing: border-box; + -moz-box-sizing: border-box; + outline: none; + border: 0; + margin: 0; + width: 100%; + padding: 5px; + color: #666; + font-size: 12px; + line-height: 15px; + text-align: right; + background: #f5f5f5; +} + +.pika-week { + font-size: 11px; + color: #999; +} + +.is-today .pika-button { + color: #33aaff; + font-weight: bold; +} + +.is-selected .pika-button { + color: #fff; + font-weight: bold; + background: #33aaff; + box-shadow: inset 0 1px 3px #178fe5; + border-radius: 3px; +} + +.is-inrange .pika-button { + background: #D5E9F7; +} + +.is-startrange .pika-button { + color: #fff; + background: #6CB31D; + box-shadow: none; + border-radius: 3px; +} + +.is-endrange .pika-button { + color: #fff; + background: #33aaff; + box-shadow: none; + border-radius: 3px; +} + +.is-disabled .pika-button { + pointer-events: none; + cursor: default; + color: #999; + opacity: .3; +} + +.pika-button:hover { + color: #fff; + background: #ff8000; + box-shadow: none; + border-radius: 3px; +} + +/* styling for abbr */ +.pika-table abbr { + border-bottom: none; + cursor: help; +} + diff --git a/css/site.css b/css/site.css new file mode 100644 index 0000000..a5111a2 --- /dev/null +++ b/css/site.css @@ -0,0 +1,42 @@ +html { margin: 0; padding: 0; } +body { font-size: 100%; margin: 0; padding: 1.75em; font-family: 'Helvetica Neue', Arial, sans-serif; } + +h1 { font-size: 1.75em; margin: 0 0 0.6em 0; } +h2 { font-size: 1.25em; margin: 0 0 0.6em 0; } + +a { color: #2996cc; } +a:hover { text-decoration: none; } + +p { line-height: 1.5em; } +.small { color: #666; font-size: 0.875em; } +.large { font-size: 1.25em; } + + +label { + font-weight: bold; +} + +input[type="text"] { + margin: 0.5em 0 3em 0; + padding: 0.5em; +} + +button#datepicker-button { + margin-bottom: 3em; +} + +.flow-element { + display: inline-block; + margin-right: 3em; + vertical-align: top; +} +.flow-element:last-child { + margin-right: 0; +} +.flow-element input { + margin-top: 0; +} + +#container { + min-height: 15em; +} \ No newline at end of file diff --git a/css/theme.css b/css/theme.css new file mode 100644 index 0000000..3a0c101 --- /dev/null +++ b/css/theme.css @@ -0,0 +1,56 @@ +/** + * This theme is an example to show how you can create your own. + */ +.pika-single.dark-theme { + color: #fff; + background: #333; + border: 1px solid #666; + border-bottom-color: #999; +} + +.dark-theme .pika-label { + background-color: #333; +} + +.dark-theme .pika-prev, +.dark-theme .is-rtl .pika-next { + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAQ0lEQVR4Ae3KIQ4AIBTD0N0/IeHGI3UIRA3ut/Zl+ltXc5++htVAmIAwAWECwgSEKbgthEoIlRAqIVRCqINQB9nDgQd7ktwFo6UpWQAAAABJRU5ErkJggg=='); +} + +.dark-theme .pika-next, +.dark-theme .is-rtl .pika-prev { + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAP0lEQVQ4y+3TMQoAMAgEwfwfAvvjTZ1uGzuvHhBPPGczEG+FRqqRaqQaqUaqkX6QBmmjacvQ6qEVTjsh+xizebvlaWptGXZAAAAAAElFTkSuQmCC'); +} + +.dark-theme .pika-table th { + color: #999; +} + +.dark-theme .pika-button { + color: #fff; + background: #222; +} + +.dark-theme .pika-week { + color: #999; +} + +.dark-theme .is-today .pika-button { + color: #33aaff; +} + +.dark-theme .is-selected .pika-button { + color: #fff; + background: #33aaff; + box-shadow: inset 0 1px 3px #178fe5; +} + +.dark-theme .is-disabled .pika-button { + color: #999; + opacity: .3; +} + +.dark-theme .pika-button:hover { + color: #fff !important; + background: #ff8000 !important; +} diff --git a/examples/amd.html b/examples/amd.html new file mode 100644 index 0000000..74835b5 --- /dev/null +++ b/examples/amd.html @@ -0,0 +1,50 @@ + + + + + + Pikaday - AMD example + + + + + + Fork me on GitHub + +

Pikaday - AMD example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + \ No newline at end of file diff --git a/examples/bound-container.html b/examples/bound-container.html new file mode 100644 index 0000000..a994028 --- /dev/null +++ b/examples/bound-container.html @@ -0,0 +1,52 @@ + + + + + + Pikaday - Container example + + + + + + Fork me on GitHub + +

Pikaday - Container example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ +
+
+ + +
+
+
+
+
+ +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + + \ No newline at end of file diff --git a/examples/calendars.html b/examples/calendars.html new file mode 100644 index 0000000..b93e665 --- /dev/null +++ b/examples/calendars.html @@ -0,0 +1,57 @@ + + + + + + Pikaday - ranges example + + + + + + Fork me on GitHub + +

Pikaday

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ 2 months +
+ 3 months, main aligned to right + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2013 David Bushell | BSD & MIT license | Example by MoOx

+ + + + + + diff --git a/examples/container.html b/examples/container.html new file mode 100644 index 0000000..d6f11c8 --- /dev/null +++ b/examples/container.html @@ -0,0 +1,52 @@ + + + + + + Pikaday - Container example + + + + + + Fork me on GitHub + +

Pikaday - Container example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ +
+
+ + +
+
+
+
+
+ +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + + \ No newline at end of file diff --git a/examples/date-range.html b/examples/date-range.html new file mode 100644 index 0000000..01e1143 --- /dev/null +++ b/examples/date-range.html @@ -0,0 +1,83 @@ + + + + + + Pikaday - Date Range example + + + + + +

Pikaday - Date Range example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ +

Pikaday source on GitHub

+ +
+ +
+ +
+ +
+ +
+ +
+ +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license

+ + + + + + \ No newline at end of file diff --git a/examples/jquery-amd.html b/examples/jquery-amd.html new file mode 100644 index 0000000..136bbf3 --- /dev/null +++ b/examples/jquery-amd.html @@ -0,0 +1,54 @@ + + + + + + Pikaday - jQuery + AMD example + + + + + + Fork me on GitHub + +

Pikaday - jQuery + AMD example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + \ No newline at end of file diff --git a/examples/jquery.html b/examples/jquery.html new file mode 100644 index 0000000..33612dd --- /dev/null +++ b/examples/jquery.html @@ -0,0 +1,46 @@ + + + + + + Pikaday - jQuery example + + + + + + Fork me on GitHub + +

Pikaday - jQuery example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + + + + \ No newline at end of file diff --git a/examples/moment.html b/examples/moment.html new file mode 100644 index 0000000..3977724 --- /dev/null +++ b/examples/moment.html @@ -0,0 +1,53 @@ + + + + + + Pikaday - moment.js example + + + + + + Fork me on GitHub + +

Pikaday + moment.js

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ +
+
+ +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + + \ No newline at end of file diff --git a/examples/positions.html b/examples/positions.html new file mode 100644 index 0000000..ad77322 --- /dev/null +++ b/examples/positions.html @@ -0,0 +1,106 @@ + + + + + + Pikaday alignment example + + + + + + Fork me on GitHub + +

Pikaday alignement example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + + +
+ +
+ not really (too close from top) +
+ for real this time (same code as above) +
+ not really (too close from right) +
+ Here is the top right position with content before (no automatic adjustment used, same code as above) +
+ +
+ +
+ Here is the bottom right position with content before (no automatic adjustment used, , same code as above) + +

Also, take a look to the bottom right of this page to see an example with position automatically adjusted.

+ + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by MoOx

+ + + + + + diff --git a/examples/theme.html b/examples/theme.html new file mode 100644 index 0000000..eeb5483 --- /dev/null +++ b/examples/theme.html @@ -0,0 +1,83 @@ + + + + + + Pikaday - Theme example + + + + + + + Fork me on GitHub + +

Pikaday - Theme example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ No theme +
+ Custom theme + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + + diff --git a/examples/trigger.html b/examples/trigger.html new file mode 100644 index 0000000..b2c6208 --- /dev/null +++ b/examples/trigger.html @@ -0,0 +1,46 @@ + + + + + + Pikaday - custom trigger element example + + + + + + Fork me on GitHub + +

Pikaday - custom trigger example

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + +
+ +
+ + + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + + \ No newline at end of file diff --git a/examples/weeknumbers.html b/examples/weeknumbers.html new file mode 100644 index 0000000..595f5ac --- /dev/null +++ b/examples/weeknumbers.html @@ -0,0 +1,41 @@ + + + + + + Pikaday - weeknumbers example + + + + + + Fork me on GitHub + +

Pikaday - showing weeknumbers

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ + + + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license | Example by Ramiro Rikkert

+ + + + + diff --git a/index.html b/index.html new file mode 100644 index 0000000..dbaa7f2 --- /dev/null +++ b/index.html @@ -0,0 +1,43 @@ + + + + + + Pikaday + + + + + +

Pikaday

+ +

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS.

+ +

Pikaday source on GitHub

+ + +
+ + +

What is this?

+ +

Since version 1.0 Pikaday is a stable and battle tested date-picker. Feel free to use it however you like but please report any bugs or feature requests to the GitHub issue tracker, thanks!

+ +

Copyright © 2014 David Bushell | BSD & MIT license

+ + + + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a00a764 --- /dev/null +++ b/package.json @@ -0,0 +1,45 @@ +{ + "name": "pikaday", + "version": "1.3.3", + "description": "A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS", + "keywords": [ + "datepicker", + "calendar", + "date" + ], + "homepage": "http://dbushell.github.io/Pikaday/", + "bugs": "https://github.com/dbushell/Pikaday/issues", + "licenses": [ + { + "type": "BSD", + "url": "http://opensource.org/licenses/BSD-3-Clause" + }, + { + "type": "MIT", + "url": "http://opensource.org/licenses/MIT" + } + ], + "main": "pikaday.js", + "repository": { + "type": "git", + "url": "https://github.com/dbushell/Pikaday.git" + }, + "optionalDependencies": { + "moment": "2.x" + }, + "devDependencies": { + "mocha": "~1.18.2", + "expect.js": "^0.3.1" + }, + "testling": { + "harness": "mocha", + "files": "tests/*.js", + "browsers": [ + "ie/7..latest", + "chrome/latest", + "firefox/3.6", "firefox/latest", + "safari/latest", + "opera/latest" + ] + } +} diff --git a/pikaday.js b/pikaday.js new file mode 100644 index 0000000..06f7b37 --- /dev/null +++ b/pikaday.js @@ -0,0 +1,1079 @@ +/*! + * Pikaday + * + * Copyright © 2014 David Bushell | BSD & MIT license | https://github.com/dbushell/Pikaday + */ + +(function (root, factory) +{ + 'use strict'; + + var moment; + if (typeof exports === 'object') { + // CommonJS module + // Load moment.js as an optional dependency + try { moment = require('moment'); } catch (e) {} + module.exports = factory(moment); + } else if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(function (req) + { + // Load moment.js as an optional dependency + var id = 'moment'; + try { moment = req(id); } catch (e) {} + return factory(moment); + }); + } else { + root.Pikaday = factory(root.moment); + } +}(this, function (moment) +{ + 'use strict'; + + /** + * feature detection and helper functions + */ + var hasMoment = typeof moment === 'function', + + hasEventListeners = !!window.addEventListener, + + document = window.document, + + sto = window.setTimeout, + + addEvent = function(el, e, callback, capture) + { + if (hasEventListeners) { + el.addEventListener(e, callback, !!capture); + } else { + el.attachEvent('on' + e, callback); + } + }, + + removeEvent = function(el, e, callback, capture) + { + if (hasEventListeners) { + el.removeEventListener(e, callback, !!capture); + } else { + el.detachEvent('on' + e, callback); + } + }, + + fireEvent = function(el, eventName, data) + { + var ev; + + if (document.createEvent) { + ev = document.createEvent('HTMLEvents'); + ev.initEvent(eventName, true, false); + ev = extend(ev, data); + el.dispatchEvent(ev); + } else if (document.createEventObject) { + ev = document.createEventObject(); + ev = extend(ev, data); + el.fireEvent('on' + eventName, ev); + } + }, + + trim = function(str) + { + return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g,''); + }, + + hasClass = function(el, cn) + { + return (' ' + el.className + ' ').indexOf(' ' + cn + ' ') !== -1; + }, + + addClass = function(el, cn) + { + if (!hasClass(el, cn)) { + el.className = (el.className === '') ? cn : el.className + ' ' + cn; + } + }, + + removeClass = function(el, cn) + { + el.className = trim((' ' + el.className + ' ').replace(' ' + cn + ' ', ' ')); + }, + + isArray = function(obj) + { + return (/Array/).test(Object.prototype.toString.call(obj)); + }, + + isDate = function(obj) + { + return (/Date/).test(Object.prototype.toString.call(obj)) && !isNaN(obj.getTime()); + }, + + isWeekend = function(date) + { + var day = date.getDay(); + return day === 0 || day === 6; + }, + + isLeapYear = function(year) + { + // solution by Matti Virkkunen: http://stackoverflow.com/a/4881951 + return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0; + }, + + getDaysInMonth = function(year, month) + { + return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; + }, + + setToStartOfDay = function(date) + { + if (isDate(date)) date.setHours(0,0,0,0); + }, + + compareDates = function(a,b) + { + // weak date comparison (use setToStartOfDay(date) to ensure correct result) + return a.getTime() === b.getTime(); + }, + + extend = function(to, from, overwrite) + { + var prop, hasProp; + for (prop in from) { + hasProp = to[prop] !== undefined; + if (hasProp && typeof from[prop] === 'object' && from[prop] !== null && from[prop].nodeName === undefined) { + if (isDate(from[prop])) { + if (overwrite) { + to[prop] = new Date(from[prop].getTime()); + } + } + else if (isArray(from[prop])) { + if (overwrite) { + to[prop] = from[prop].slice(0); + } + } else { + to[prop] = extend({}, from[prop], overwrite); + } + } else if (overwrite || !hasProp) { + to[prop] = from[prop]; + } + } + return to; + }, + + adjustCalendar = function(calendar) { + if (calendar.month < 0) { + calendar.year -= Math.ceil(Math.abs(calendar.month)/12); + calendar.month += 12; + } + if (calendar.month > 11) { + calendar.year += Math.floor(Math.abs(calendar.month)/12); + calendar.month -= 12; + } + return calendar; + }, + + /** + * defaults and localisation + */ + defaults = { + + // bind the picker to a form field + field: null, + + // automatically show/hide the picker on `field` focus (default `true` if `field` is set) + bound: undefined, + + // position of the datepicker, relative to the field (default to bottom & left) + // ('bottom' & 'left' keywords are not used, 'top' & 'right' are modifier on the bottom/left position) + position: 'bottom left', + + // automatically fit in the viewport even if it means repositioning from the position option + reposition: true, + + // the default output format for `.toString()` and `field` value + format: 'YYYY-MM-DD', + + // the initial date to view when first opened + defaultDate: null, + + // make the `defaultDate` the initial selected value + setDefaultDate: false, + + // first day of week (0: Sunday, 1: Monday etc) + firstDay: 0, + + // the minimum/earliest date that can be selected + minDate: null, + // the maximum/latest date that can be selected + maxDate: null, + + // number of years either side, or array of upper/lower range + yearRange: 100, + + // show week numbers at head of row + showWeekNumber: false, + + // used internally (don't config outside) + minYear: 0, + maxYear: 9999, + minMonth: undefined, + maxMonth: undefined, + + startRange: null, + endRange: null, + + isRTL: false, + + // Additional text to append to the year in the calendar title + yearSuffix: '', + + // Render the month after year in the calendar title + showMonthAfterYear: false, + + // how many months are visible + numberOfMonths: 1, + + // when numberOfMonths is used, this will help you to choose where the main calendar will be (default `left`, can be set to `right`) + // only used for the first display or when a selected date is not visible + mainCalendar: 'left', + + // Specify a DOM element to render the calendar in + container: undefined, + + // internationalization + i18n: { + previousMonth : 'Previous Month', + nextMonth : 'Next Month', + months : ['January','February','March','April','May','June','July','August','September','October','November','December'], + weekdays : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], + weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] + }, + + // Theme Classname + theme: null, + + // callback function + onSelect: null, + onOpen: null, + onClose: null, + onDraw: null + }, + + + /** + * templating functions to abstract HTML rendering + */ + renderDayName = function(opts, day, abbr) + { + day += opts.firstDay; + while (day >= 7) { + day -= 7; + } + return abbr ? opts.i18n.weekdaysShort[day] : opts.i18n.weekdays[day]; + }, + + renderDay = function(opts) + { + if (opts.isEmpty) { + return ''; + } + var arr = []; + if (opts.isDisabled) { + arr.push('is-disabled'); + } + if (opts.isToday) { + arr.push('is-today'); + } + if (opts.isSelected) { + arr.push('is-selected'); + } + if (opts.isInRange) { + arr.push('is-inrange'); + } + if (opts.isStartRange) { + arr.push('is-startrange'); + } + if (opts.isEndRange) { + arr.push('is-endrange'); + } + return '' + + '' + + ''; + }, + + renderWeek = function (d, m, y) { + // Lifted from http://javascript.about.com/library/blweekyear.htm, lightly modified. + var onejan = new Date(y, 0, 1), + weekNum = Math.ceil((((new Date(y, m, d) - onejan) / 86400000) + onejan.getDay()+1)/7); + return '' + weekNum + ''; + }, + + renderRow = function(days, isRTL) + { + return '' + (isRTL ? days.reverse() : days).join('') + ''; + }, + + renderBody = function(rows) + { + return '' + rows.join('') + ''; + }, + + renderHead = function(opts) + { + var i, arr = []; + if (opts.showWeekNumber) { + arr.push(''); + } + for (i = 0; i < 7; i++) { + arr.push('' + renderDayName(opts, i, true) + ''); + } + return '' + (opts.isRTL ? arr.reverse() : arr).join('') + ''; + }, + + renderTitle = function(instance, c, year, month, refYear) + { + var i, j, arr, + opts = instance._o, + isMinYear = year === opts.minYear, + isMaxYear = year === opts.maxYear, + html = '
', + monthHtml, + yearHtml, + prev = true, + next = true; + + for (arr = [], i = 0; i < 12; i++) { + arr.push(''); + } + monthHtml = '
' + opts.i18n.months[month] + '
'; + + if (isArray(opts.yearRange)) { + i = opts.yearRange[0]; + j = opts.yearRange[1] + 1; + } else { + i = year - opts.yearRange; + j = 1 + year + opts.yearRange; + } + + for (arr = []; i < j && i <= opts.maxYear; i++) { + if (i >= opts.minYear) { + arr.push(''); + } + } + yearHtml = '
' + year + opts.yearSuffix + '
'; + + if (opts.showMonthAfterYear) { + html += yearHtml + monthHtml; + } else { + html += monthHtml + yearHtml; + } + + if (isMinYear && (month === 0 || opts.minMonth >= month)) { + prev = false; + } + + if (isMaxYear && (month === 11 || opts.maxMonth <= month)) { + next = false; + } + + if (c === 0) { + html += ''; + } + if (c === (instance._o.numberOfMonths - 1) ) { + html += ''; + } + + return html += '
'; + }, + + renderTable = function(opts, data) + { + return '' + renderHead(opts) + renderBody(data) + '
'; + }, + + + /** + * Pikaday constructor + */ + Pikaday = function(options) + { + var self = this, + opts = self.config(options); + + self._onMouseDown = function(e) + { + if (!self._v) { + return; + } + e = e || window.event; + var target = e.target || e.srcElement; + if (!target) { + return; + } + + if (!hasClass(target.parentNode, 'is-disabled')) { + if (hasClass(target, 'pika-button') && !hasClass(target, 'is-empty')) { + self.setDate(new Date(target.getAttribute('data-pika-year'), target.getAttribute('data-pika-month'), target.getAttribute('data-pika-day'))); + if (opts.bound) { + sto(function() { + self.hide(); + if (opts.field) { + opts.field.blur(); + } + }, 100); + } + return; + } + else if (hasClass(target, 'pika-prev')) { + self.prevMonth(); + } + else if (hasClass(target, 'pika-next')) { + self.nextMonth(); + } + } + if (!hasClass(target, 'pika-select')) { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; + return false; + } + } else { + self._c = true; + } + }; + + self._onChange = function(e) + { + e = e || window.event; + var target = e.target || e.srcElement; + if (!target) { + return; + } + if (hasClass(target, 'pika-select-month')) { + self.gotoMonth(target.value); + } + else if (hasClass(target, 'pika-select-year')) { + self.gotoYear(target.value); + } + }; + + self._onInputChange = function(e) + { + var date; + + if (e.firedBy === self) { + return; + } + if (hasMoment) { + date = moment(opts.field.value, opts.format); + date = (date && date.isValid()) ? date.toDate() : null; + } + else { + date = new Date(Date.parse(opts.field.value)); + } + if (isDate(date)) { + self.setDate(date); + } + if (!self._v) { + self.show(); + } + }; + + self._onInputFocus = function() + { + self.show(); + }; + + self._onInputClick = function() + { + self.show(); + }; + + self._onInputBlur = function() + { + // IE allows pika div to gain focus; catch blur the input field + var pEl = document.activeElement; + do { + if (hasClass(pEl, 'pika-single')) { + return; + } + } + while ((pEl = pEl.parentNode)); + + if (!self._c) { + self._b = sto(function() { + self.hide(); + }, 50); + } + self._c = false; + }; + + self._onClick = function(e) + { + e = e || window.event; + var target = e.target || e.srcElement, + pEl = target; + if (!target) { + return; + } + if (!hasEventListeners && hasClass(target, 'pika-select')) { + if (!target.onchange) { + target.setAttribute('onchange', 'return;'); + addEvent(target, 'change', self._onChange); + } + } + do { + if (hasClass(pEl, 'pika-single') || pEl === opts.trigger) { + return; + } + } + while ((pEl = pEl.parentNode)); + if (self._v && target !== opts.trigger && pEl !== opts.trigger) { + self.hide(); + } + }; + + self.el = document.createElement('div'); + self.el.className = 'pika-single' + (opts.isRTL ? ' is-rtl' : '') + (opts.theme ? ' ' + opts.theme : ''); + + addEvent(self.el, 'ontouchend' in document ? 'touchend' : 'mousedown', self._onMouseDown, true); + addEvent(self.el, 'change', self._onChange); + + if (opts.field) { + if (opts.container) { + opts.container.appendChild(self.el); + } else if (opts.bound) { + document.body.appendChild(self.el); + } else { + opts.field.parentNode.insertBefore(self.el, opts.field.nextSibling); + } + addEvent(opts.field, 'change', self._onInputChange); + + if (!opts.defaultDate) { + if (hasMoment && opts.field.value) { + opts.defaultDate = moment(opts.field.value, opts.format).toDate(); + } else { + opts.defaultDate = new Date(Date.parse(opts.field.value)); + } + opts.setDefaultDate = true; + } + } + + var defDate = opts.defaultDate; + + if (isDate(defDate)) { + if (opts.setDefaultDate) { + self.setDate(defDate, true); + } else { + self.gotoDate(defDate); + } + } else { + self.gotoDate(new Date()); + } + + if (opts.bound) { + this.hide(); + self.el.className += ' is-bound'; + addEvent(opts.trigger, 'click', self._onInputClick); + addEvent(opts.trigger, 'focus', self._onInputFocus); + addEvent(opts.trigger, 'blur', self._onInputBlur); + } else { + this.show(); + } + }; + + + /** + * public Pikaday API + */ + Pikaday.prototype = { + + + /** + * configure functionality + */ + config: function(options) + { + if (!this._o) { + this._o = extend({}, defaults, true); + } + + var opts = extend(this._o, options, true); + + opts.isRTL = !!opts.isRTL; + + opts.field = (opts.field && opts.field.nodeName) ? opts.field : null; + + opts.theme = (typeof opts.theme) === 'string' && opts.theme ? opts.theme : null; + + opts.bound = !!(opts.bound !== undefined ? opts.field && opts.bound : opts.field); + + opts.trigger = (opts.trigger && opts.trigger.nodeName) ? opts.trigger : opts.field; + + opts.disableWeekends = !!opts.disableWeekends; + + opts.disableDayFn = (typeof opts.disableDayFn) === 'function' ? opts.disableDayFn : null; + + var nom = parseInt(opts.numberOfMonths, 10) || 1; + opts.numberOfMonths = nom > 4 ? 4 : nom; + + if (!isDate(opts.minDate)) { + opts.minDate = false; + } + if (!isDate(opts.maxDate)) { + opts.maxDate = false; + } + if ((opts.minDate && opts.maxDate) && opts.maxDate < opts.minDate) { + opts.maxDate = opts.minDate = false; + } + if (opts.minDate) { + this.setMinDate(opts.minDate); + } + if (opts.maxDate) { + setToStartOfDay(opts.maxDate); + opts.maxYear = opts.maxDate.getFullYear(); + opts.maxMonth = opts.maxDate.getMonth(); + } + + if (isArray(opts.yearRange)) { + var fallback = new Date().getFullYear() - 10; + opts.yearRange[0] = parseInt(opts.yearRange[0], 10) || fallback; + opts.yearRange[1] = parseInt(opts.yearRange[1], 10) || fallback; + } else { + opts.yearRange = Math.abs(parseInt(opts.yearRange, 10)) || defaults.yearRange; + if (opts.yearRange > 100) { + opts.yearRange = 100; + } + } + + return opts; + }, + + /** + * return a formatted string of the current selection (using Moment.js if available) + */ + toString: function(format) + { + return !isDate(this._d) ? '' : hasMoment ? moment(this._d).format(format || this._o.format) : this._d.toDateString(); + }, + + /** + * return a Moment.js object of the current selection (if available) + */ + getMoment: function() + { + return hasMoment ? moment(this._d) : null; + }, + + /** + * set the current selection from a Moment.js object (if available) + */ + setMoment: function(date, preventOnSelect) + { + if (hasMoment && moment.isMoment(date)) { + this.setDate(date.toDate(), preventOnSelect); + } + }, + + /** + * return a Date object of the current selection + */ + getDate: function() + { + return isDate(this._d) ? new Date(this._d.getTime()) : null; + }, + + /** + * set the current selection + */ + setDate: function(date, preventOnSelect) + { + if (!date) { + this._d = null; + + if (this._o.field) { + this._o.field.value = ''; + fireEvent(this._o.field, 'change', { firedBy: this }); + } + + return this.draw(); + } + if (typeof date === 'string') { + date = new Date(Date.parse(date)); + } + if (!isDate(date)) { + return; + } + + var min = this._o.minDate, + max = this._o.maxDate; + + if (isDate(min) && date < min) { + date = min; + } else if (isDate(max) && date > max) { + date = max; + } + + this._d = new Date(date.getTime()); + setToStartOfDay(this._d); + this.gotoDate(this._d); + + if (this._o.field) { + this._o.field.value = this.toString(); + fireEvent(this._o.field, 'change', { firedBy: this }); + } + if (!preventOnSelect && typeof this._o.onSelect === 'function') { + this._o.onSelect.call(this, this.getDate()); + } + }, + + /** + * change view to a specific date + */ + gotoDate: function(date) + { + var newCalendar = true; + + if (!isDate(date)) { + return; + } + + if (this.calendars) { + var firstVisibleDate = new Date(this.calendars[0].year, this.calendars[0].month, 1), + lastVisibleDate = new Date(this.calendars[this.calendars.length-1].year, this.calendars[this.calendars.length-1].month, 1), + visibleDate = date.getTime(); + // get the end of the month + lastVisibleDate.setMonth(lastVisibleDate.getMonth()+1); + lastVisibleDate.setDate(lastVisibleDate.getDate()-1); + newCalendar = (visibleDate < firstVisibleDate.getTime() || lastVisibleDate.getTime() < visibleDate); + } + + if (newCalendar) { + this.calendars = [{ + month: date.getMonth(), + year: date.getFullYear() + }]; + if (this._o.mainCalendar === 'right') { + this.calendars[0].month += 1 - this._o.numberOfMonths; + } + } + + this.adjustCalendars(); + }, + + adjustCalendars: function() { + this.calendars[0] = adjustCalendar(this.calendars[0]); + for (var c = 1; c < this._o.numberOfMonths; c++) { + this.calendars[c] = adjustCalendar({ + month: this.calendars[0].month + c, + year: this.calendars[0].year + }); + } + this.draw(); + }, + + gotoToday: function() + { + this.gotoDate(new Date()); + }, + + /** + * change view to a specific month (zero-index, e.g. 0: January) + */ + gotoMonth: function(month) + { + if (!isNaN(month)) { + this.calendars[0].month = parseInt(month, 10); + this.adjustCalendars(); + } + }, + + nextMonth: function() + { + this.calendars[0].month++; + this.adjustCalendars(); + }, + + prevMonth: function() + { + this.calendars[0].month--; + this.adjustCalendars(); + }, + + /** + * change view to a specific full year (e.g. "2012") + */ + gotoYear: function(year) + { + if (!isNaN(year)) { + this.calendars[0].year = parseInt(year, 10); + this.adjustCalendars(); + } + }, + + /** + * change the minDate + */ + setMinDate: function(value) + { + setToStartOfDay(value); + this._o.minDate = value; + this._o.minYear = value.getFullYear(); + this._o.minMonth = value.getMonth(); + }, + + /** + * change the maxDate + */ + setMaxDate: function(value) + { + this._o.maxDate = value; + }, + + setStartRange: function(value) + { + this._o.startRange = value; + }, + + setEndRange: function(value) + { + this._o.endRange = value; + }, + + /** + * refresh the HTML + */ + draw: function(force) + { + if (!this._v && !force) { + return; + } + var opts = this._o, + minYear = opts.minYear, + maxYear = opts.maxYear, + minMonth = opts.minMonth, + maxMonth = opts.maxMonth, + html = ''; + + if (this._y <= minYear) { + this._y = minYear; + if (!isNaN(minMonth) && this._m < minMonth) { + this._m = minMonth; + } + } + if (this._y >= maxYear) { + this._y = maxYear; + if (!isNaN(maxMonth) && this._m > maxMonth) { + this._m = maxMonth; + } + } + + for (var c = 0; c < opts.numberOfMonths; c++) { + html += '
' + renderTitle(this, c, this.calendars[c].year, this.calendars[c].month, this.calendars[0].year) + this.render(this.calendars[c].year, this.calendars[c].month) + '
'; + } + + this.el.innerHTML = html; + + if (opts.bound) { + if(opts.field.type !== 'hidden') { + sto(function() { + opts.trigger.focus(); + }, 1); + } + } + + if (typeof this._o.onDraw === 'function') { + var self = this; + sto(function() { + self._o.onDraw.call(self); + }, 0); + } + }, + + adjustPosition: function() + { + var field, pEl, width, height, viewportWidth, viewportHeight, scrollTop, left, top, clientRect; + + if (this._o.container) return; + + this.el.style.position = 'absolute'; + + field = this._o.trigger; + pEl = field; + width = this.el.offsetWidth; + height = this.el.offsetHeight; + viewportWidth = window.innerWidth || document.documentElement.clientWidth; + viewportHeight = window.innerHeight || document.documentElement.clientHeight; + scrollTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; + + if (typeof field.getBoundingClientRect === 'function') { + clientRect = field.getBoundingClientRect(); + left = clientRect.left + window.pageXOffset; + top = clientRect.bottom + window.pageYOffset; + } else { + left = pEl.offsetLeft; + top = pEl.offsetTop + pEl.offsetHeight; + while((pEl = pEl.offsetParent)) { + left += pEl.offsetLeft; + top += pEl.offsetTop; + } + } + + // default position is bottom & left + if ((this._o.reposition && left + width > viewportWidth) || + ( + this._o.position.indexOf('right') > -1 && + left - width + field.offsetWidth > 0 + ) + ) { + left = left - width + field.offsetWidth; + } + if ((this._o.reposition && top + height > viewportHeight + scrollTop) || + ( + this._o.position.indexOf('top') > -1 && + top - height - field.offsetHeight > 0 + ) + ) { + top = top - height - field.offsetHeight; + } + + this.el.style.left = left + 'px'; + this.el.style.top = top + 'px'; + }, + + /** + * render HTML for a particular month + */ + render: function(year, month) + { + var opts = this._o, + now = new Date(), + days = getDaysInMonth(year, month), + before = new Date(year, month, 1).getDay(), + data = [], + row = []; + setToStartOfDay(now); + if (opts.firstDay > 0) { + before -= opts.firstDay; + if (before < 0) { + before += 7; + } + } + var cells = days + before, + after = cells; + while(after > 7) { + after -= 7; + } + cells += 7 - after; + for (var i = 0, r = 0; i < cells; i++) + { + var day = new Date(year, month, 1 + (i - before)), + isSelected = isDate(this._d) ? compareDates(day, this._d) : false, + isToday = compareDates(day, now), + isEmpty = i < before || i >= (days + before), + isStartRange = opts.startRange && compareDates(opts.startRange, day), + isEndRange = opts.endRange && compareDates(opts.endRange, day), + isInRange = opts.startRange && opts.endRange && opts.startRange < day && day < opts.endRange, + isDisabled = (opts.minDate && day < opts.minDate) || + (opts.maxDate && day > opts.maxDate) || + (opts.disableWeekends && isWeekend(day)) || + (opts.disableDayFn && opts.disableDayFn(day)), + dayConfig = { + day: 1 + (i - before), + month: month, + year: year, + isSelected: isSelected, + isToday: isToday, + isDisabled: isDisabled, + isEmpty: isEmpty, + isStartRange: isStartRange, + isEndRange: isEndRange, + isInRange: isInRange + }; + + row.push(renderDay(dayConfig)); + + if (++r === 7) { + if (opts.showWeekNumber) { + row.unshift(renderWeek(i - before, month, year)); + } + data.push(renderRow(row, opts.isRTL)); + row = []; + r = 0; + } + } + return renderTable(opts, data); + }, + + isVisible: function() + { + return this._v; + }, + + show: function() + { + if (!this._v) { + removeClass(this.el, 'is-hidden'); + this._v = true; + this.draw(); + if (this._o.bound) { + addEvent(document, 'click', this._onClick); + this.adjustPosition(); + } + if (typeof this._o.onOpen === 'function') { + this._o.onOpen.call(this); + } + } + }, + + hide: function() + { + var v = this._v; + if (v !== false) { + if (this._o.bound) { + removeEvent(document, 'click', this._onClick); + } + this.el.style.position = 'static'; // reset + this.el.style.left = 'auto'; + this.el.style.top = 'auto'; + addClass(this.el, 'is-hidden'); + this._v = false; + if (v !== undefined && typeof this._o.onClose === 'function') { + this._o.onClose.call(this); + } + } + }, + + /** + * GAME OVER + */ + destroy: function() + { + this.hide(); + removeEvent(this.el, 'mousedown', this._onMouseDown, true); + removeEvent(this.el, 'change', this._onChange); + if (this._o.field) { + removeEvent(this._o.field, 'change', this._onInputChange); + if (this._o.bound) { + removeEvent(this._o.trigger, 'click', this._onInputClick); + removeEvent(this._o.trigger, 'focus', this._onInputFocus); + removeEvent(this._o.trigger, 'blur', this._onInputBlur); + } + } + if (this.el.parentNode) { + this.el.parentNode.removeChild(this.el); + } + } + + }; + + return Pikaday; + +})); diff --git a/plugins/pikaday.jquery.js b/plugins/pikaday.jquery.js new file mode 100644 index 0000000..a95ddc5 --- /dev/null +++ b/plugins/pikaday.jquery.js @@ -0,0 +1,52 @@ +/*! + * Pikaday jQuery plugin. + * + * Copyright © 2013 David Bushell | BSD & MIT license | https://github.com/dbushell/Pikaday + */ + +(function (root, factory) +{ + 'use strict'; + + if (typeof exports === 'object') { + // CommonJS module + factory(require('jquery'), require('../pikaday')); + } else if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(['jquery', 'pikaday'], factory); + } else { + // Browser globals + factory(root.jQuery, root.Pikaday); + } +}(this, function ($, Pikaday) +{ + 'use strict'; + + $.fn.pikaday = function() + { + var args = arguments; + + if (!args || !args.length) { + args = [{ }]; + } + + return this.each(function() + { + var self = $(this), + plugin = self.data('pikaday'); + + if (!(plugin instanceof Pikaday)) { + if (typeof args[0] === 'object') { + var options = $.extend({}, args[0]); + options.field = self[0]; + self.data('pikaday', new Pikaday(options)); + } + } else { + if (typeof args[0] === 'string' && typeof plugin[args[0]] === 'function') { + plugin[args[0]].apply(plugin, Array.prototype.slice.call(args,1)); + } + } + }); + }; + +})); diff --git a/scss/pikaday.scss b/scss/pikaday.scss new file mode 100644 index 0000000..3b2a3c0 --- /dev/null +++ b/scss/pikaday.scss @@ -0,0 +1,217 @@ +/*! + * Pikaday + * Copyright © 2014 David Bushell | BSD & MIT license | http://dbushell.com/ + */ + +.pika-single { + z-index: 9999; + display: block; + position: relative; + color: #333; + background: #fff; + border: 1px solid #ccc; + border-bottom-color: #bbb; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + + &.is-hidden { + display: none; + } + + &.is-bound { + position: absolute; + box-shadow: 0 5px 15px -5px rgba(0,0,0,.5); + } +} + +// clear child float (pika-lendar), using the famous micro clearfix hack +// http://nicolasgallagher.com/micro-clearfix-hack/ +.pika-single { + *zoom: 1; + + &:before, + &:after { + content: " "; + display: table; + } + + &:after { clear: both } +} + +.pika-lendar { + float: left; + width: 240px; + margin: 8px; +} + +.pika-title { + position: relative; + text-align: center; + + select { + cursor: pointer; + position: absolute; + z-index: 9998; + margin: 0; + left: 0; + top: 5px; + filter: alpha(opacity=0); + opacity: 0; + } +} + +.pika-label { + display: inline-block; + *display: inline; + position: relative; + z-index: 9999; + overflow: hidden; + margin: 0; + padding: 5px 3px; + font-size: 14px; + line-height: 20px; + font-weight: bold; + background-color: #fff; +} + +.pika-prev, +.pika-next { + display: block; + cursor: pointer; + position: relative; + outline: none; + border: 0; + padding: 0; + width: 20px; + height: 30px; + text-indent: 20px; // hide text using text-indent trick, using width value (it's enough) + white-space: nowrap; + overflow: hidden; + background-color: transparent; + background-position: center center; + background-repeat: no-repeat; + background-size: 75% 75%; + opacity: .5; + *position: absolute; + *top: 0; + + &:hover { + opacity: 1; + } + + &.is-disabled { + cursor: default; + opacity: .2; + } +} + +.pika-prev, +.is-rtl .pika-next { + float: left; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAUklEQVR42u3VMQoAIBADQf8Pgj+OD9hG2CtONJB2ymQkKe0HbwAP0xucDiQWARITIDEBEnMgMQ8S8+AqBIl6kKgHiXqQqAeJepBo/z38J/U0uAHlaBkBl9I4GwAAAABJRU5ErkJggg=='); + *left: 0; +} + +.pika-next, +.is-rtl .pika-prev { + float: right; + background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAAU0lEQVR42u3VOwoAMAgE0dwfAnNjU26bYkBCFGwfiL9VVWoO+BJ4Gf3gtsEKKoFBNTCoCAYVwaAiGNQGMUHMkjGbgjk2mIONuXo0nC8XnCf1JXgArVIZAQh5TKYAAAAASUVORK5CYII='); + *right: 0; +} + +.pika-select { + display: inline-block; + *display: inline; +} + +.pika-table { + width: 100%; + border-collapse: collapse; + border-spacing: 0; + border: 0; + + th, + td { + width: 14.285714285714286%; + padding: 0; + } + + th { + color: #999; + font-size: 12px; + line-height: 25px; + font-weight: bold; + text-align: center; + } + + abbr { + border-bottom: none; + cursor: help; + } +} + +.pika-button { + cursor: pointer; + display: block; + -moz-box-sizing: border-box; + box-sizing: border-box; + outline: none; + border: 0; + margin: 0; + width: 100%; + padding: 5px; + color: #666; + font-size: 12px; + line-height: 15px; + text-align: right; + background: #f5f5f5; + + .is-today & { + color: #33aaff; + font-weight: bold; + } + + .is-selected & { + color: #fff; + font-weight: bold; + background: #33aaff; + box-shadow: inset 0 1px 3px #178fe5; + border-radius: 3px; + } + + .is-disabled & { + pointer-events: none; + cursor: default; + color: #999; + opacity: .3; + } + + &:hover { + color: #fff; + background: #ff8000; + box-shadow: none; + border-radius: 3px; + } +} + +.pika-week { + font-size: 11px; + color: #999; +} + +.is-inrange .pika-button { + background: #D5E9F7; +} + +.is-startrange .pika-button { + color: #fff; + background: #6CB31D; + box-shadow: none; + border-radius: 3px; +} + +.is-endrange .pika-button { + color: #fff; + background: #33aaff; + box-shadow: none; + border-radius: 3px; +} diff --git a/tests/methods.js b/tests/methods.js new file mode 100644 index 0000000..e8871f0 --- /dev/null +++ b/tests/methods.js @@ -0,0 +1,47 @@ +var Pikaday = require('../'), + expect = require('expect.js'); + +describe('Pikaday public method', function () +{ + 'use strict'; + + describe('#toString()', function () + { + it('should return empty string when date not set', function () + { + var pikaday = new Pikaday(); + expect(pikaday.toString()).to.be.empty; + }); + + it('should return date string, formatted by moment, when date is set', function() { + var date = new Date(2014, 3, 25), + pikaday = new Pikaday({ + format: 'DD-MM-YY' + }); + + pikaday.setDate(date); + expect(pikaday.toString()).to.eql('25-04-14'); + }); + }); + + describe('When specifying minDate option in Constructor', function () { + it('Should remove the time portion (flattening to midnight)', function () { + var date = new Date(2015, 1, 17, 22, 10, 5), + expected = new Date(2015, 1, 17, 0, 0, 0), + pikaday = new Pikaday({ minDate: date }); + + expect(pikaday._o.minDate).to.eql(expected); + }); + }); + + describe('#setMinDate()', function () { + it('should flatten date to midnight ignoring time portion (consistent with minDate option in ctor)', function () { + var date = new Date(2015, 1, 17, 22, 10, 5), + expected = new Date(2015, 1, 17, 0, 0, 0), + pikaday = new Pikaday(); + + pikaday.setMinDate(date); + expect(pikaday._o.minDate).to.eql(expected); + }); + }); +}); diff --git a/tests/module.js b/tests/module.js new file mode 100644 index 0000000..f353dd2 --- /dev/null +++ b/tests/module.js @@ -0,0 +1,24 @@ +var Pikaday = require('../'), + expect = require('expect.js'); + +describe('Pikaday', function () +{ + 'use strict'; + + it('should expose as a CommonJS module', function () + { + expect(Pikaday).to.be.a('function'); + }); + + it('should NOT leak to the global object', function () + { + expect(window).to.not.have.property('Pikaday'); + }); + + it('should be possible to create a new instance', function () + { + expect(function () { + new Pikaday(); + }).to.not.throwException(); + }); +}); \ No newline at end of file -- 2.0.0