nb-moment-calendar
v0.8.2
Published
NB Moment Calendar core
Downloads
27
Maintainers
Readme
NB MOMENT CALENDAR
This is the base class for generator calendar using moment.js and lodash
This created because of another project I am working on - NB Angular Calendar
By separating them, the developer can have access just the underlying data.
UPDATE: Dec 2016 from V0.7.0 completely rewritten in ES6, and the export module will be in UMD format.
Install
bower install nb-moment-calendar --save
In our bower.json
file, I have set to use the locale version instead; because using the standard version cause a bug when try to
change the start of week from Sunday to Monday (google it, lots of people have the same problem). If you don't want to include a big file.
You can still do it by override it in your bower - with the locale files that you need.
{
"override": {
"moment": [
"moment.js",
"locale/en-gb.js"
]
}
}
Or use it with your node / browsersify / webpack project
npm install nb-moment-calendar --save
// in your js
const NBMomentCalendar = require('nb-moment-calendar');
const calInstance = new NBMomentCalendar['default'](); // note this is due to the UMD export
// ESPECIALLY FOR BROWSER
var moment = new NBMomentCalendar['default']();
This is why I EXTREMELY HIGHLY RECOMMEND YOU TO START USING ES6!!!!
import NBMomentCalendar from 'nb-moment-calendar';
no fuss!
DEMO
Clone this repo, cd into the root directory then
$ npm run demo
The file is demo.js
.
Usage
~We have a top level name space NB
(In browser) and this package is MomentDateGenerator
~
var nbMomentCalObj = new NBMomentCalendar.default();
Internally we guarantee the config
get run at least once. But you could also provide your configuration
as soon as you have init the instance.
By default we have the following options
var options = {
locale: 'en-gb',
localeConfig: {
weekdaysMin: ['Mo' , 'Tu' , 'We' , 'Th' , 'Fr' , 'Sa' , 'Su'],
week : {
dow : 1 // Monday is the first day of the week
}
}
};
The following method pass to the init method like this:
moment.locale(this.options.locale , this.options.localeConfig);
The this.options
will merge with your configuration first (see API below)
API
List of API available
getMoment(options)
Setup the moment configuration, note that moment is mutable, once we call the config, and we will reuse the same moment object internally. Until it gets re-init again.
note the previous ~~setOptions~~ move to a private method. This is the only method to call during init.
getConfig()
return the config options.
getMonth(year , month)
This is a breaking change in V0.7.0. It was call get
before. But using ES6 get has a differnet meaning
- year (int) 4 digit (or any moment accepted year format)
- month (int) month in digit (not index!)
This method is a wrapper of getMergeMonth
and lookup if there is a cache version first.
This will return (array) weeks (array by week index) in the format below
{
'day': _date.format('D'),
'wend': (wend===0 || wend===6),
'fday': _date.format('YYYY-MM-DD'),
'wday': _date.isoWeekday()
}
We don't provide the underlying moment
object because most of the time, you don't need it. So save some memory size.
And if you need it, at the beginning of the call, if you use the getMoment
method, you already got the original object.
Then just create a clone to work with it.
var nbCalMoment = calInstance.getMoment(options);
var yourMomentClone = nbCalMoment().clone();
getYear(year , headingFormat)
- year (int) year in 4 digit
Return (array) 12 months of padded month data. In the following format
{
year: year
month: month,
heading: 'MMM YYYY' || headingFormat
data: [blocks of months]
}
getMonths(year , month , months , headingFormat)
- year (int) start year
- month (int) start month
- months (int) total months you want to retrieve from the start month
- headingFormat (string) how you want to format the month heading
This will return the number of padded months you specified. In the same format as getYear
getRawMonths(year , month , months)
- year (int) start year
- month (int) start month
- months (int) total months you want to retrieve from the start month
This will return the number of months (no padding) you specified.
Develop
There is a simple test file in the same folder, just run:
$ gulp dev
Will open your default browser.
And you can generate the compress version with. We are using rollup since the V0.7.0
$ npm run build
Joel Chu © 2015
-- EOF --