Skip to content

Commit

Permalink
rm start-up need for .on('ready', fn)
Browse files Browse the repository at this point in the history
  • Loading branch information
bevacqua committed Jan 11, 2015
1 parent b262515 commit 48a1a67
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 103 deletions.
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
bower_components
dist
example
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"quotmark": "single",
"validthis": true,
"indent": 2,
"node": true
"node": true,
"browser": true
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.0.0 Invasion

- Removed necessity to wait until `'ready'` event was fired in order to interact with calendar
- Event listeners registered on a `'destroy'` event handler will no longer be immediately removed
- Introduced ability to pass `weekdayFormat` as an array with 7 strings

# 1.2.4 Tunnel Vision

- Fixed [an issue](https://github.com/bevacqua/rome/issues/30) where `autoHideOnBlur` wouldn't work on Firefox
Expand Down
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Option | Description
`timeFormat` | Format string used to display the time on the calendar
`timeInterval` | Seconds between each option in the time dropdown
`timeValidator` | Function to validate that a given time is considered valid. Receives a native `Date` parameter.
`weekdayFormat` | Format used to display weekdays. Takes `min` -> Mo, `short` -> Mon, or `long` -> Monday.
`weekdayFormat` | Format used to display weekdays. Takes `min` _(Mo)_, `short` _(Mon)_, `long` _(Monday)_, or an array with seven strings of your choosing.
`weekStart` | Day considered the first of the week. Range: Sunday `0` - Saturday `6`

Note that in the case of input fields, when `initialValue` isn't provided the initial value is inferred from `elem.value` instead. In the case of inline calendars, `Date.now` will be used as a default if none is provided.
Expand Down Expand Up @@ -236,14 +236,6 @@ Event | Arguments | Description
`show` | `[]` | The calendar has been displayed
`hide` | `[]` | The calendar has been hidden

Please note that you may need to wait for the calendar's `'ready'` event before interacting with it, otherwise most of the API will result in a no-op.

```js
rome(elem).once('ready', function () {
this.show();
});
```

#### Date and Time Validator

Please note that `dateValidator` and `timeValidator` both receive a native `Date` object as a parameter. These methods are expected to return `undefined` or `true` if the date is deemed valid, and `false` in case the date is invalid. If `dateValidator` returns `false`, the validation process will try to find a valid date near the desired date.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"main": "src/rome.moment.js",
"dependencies": {
"contra.emitter": "^1.0.0",
"contra.emitter": "^1.1.0",
"lodash.throttle": "^2.4.1",
"moment": "^2.8.2",
"raf": "^2.0.1"
Expand Down
28 changes: 16 additions & 12 deletions src/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ function calendar (calendarOptions) {
var time;
var timelist;

destroy(true);
init();
raf(ready);

raf(function () {
init();
});
return api;

function napi () { return api; }

Expand Down Expand Up @@ -78,14 +77,20 @@ function calendar (calendarOptions) {
api.setValue = setValue;
api.show = show;

hideCalendar();
eventListening();
if (o._input !== true) {
show();
}

api.emit('ready', clone(o));
eventListening();
ready();

return api;
}

function ready () {
api.emit('ready', clone(o));
}

function destroy (silent) {
if (container) {
container.parentNode.removeChild(container);
Expand All @@ -95,6 +100,7 @@ function calendar (calendarOptions) {
eventListening(true);
}

var destroyed = api.emitterSnapshot('destroyed');
api.destroyed = true;
api.destroy = napi;
api.emitValues = napi;
Expand All @@ -108,11 +114,11 @@ function calendar (calendarOptions) {
api.restore = init;
api.setValue = napi;
api.show = napi;
api.off();

if (silent !== true) {
api.emit('destroyed');
destroyed();
}
api.off();

return api;
}
Expand Down Expand Up @@ -305,7 +311,7 @@ function calendar (calendarOptions) {
var bound;
var direction = op === 'add' ? -1 : 1;
var offset = o.monthsInCalendar + direction * getMonthOffset(lastDayElement);
refCal[op]('months', offset);
refCal[op](offset, 'months');
bound = inRange(refCal.clone());
ref = bound || ref;
if (bound) { refCal = bound.clone(); }
Expand Down Expand Up @@ -648,8 +654,6 @@ function calendar (calendarOptions) {
function getMoment () {
return ref.clone();
}

return api;
}

module.exports = calendar;
2 changes: 1 addition & 1 deletion src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function defaults (options, cal) {
o.weekdayFormat = momentum.moment.weekdaysShort();
} else if (o.weekdayFormat === 'min') {
o.weekdayFormat = momentum.moment.weekdaysMin();
} else {
} else if (!Array.isArray(o.weekdayFormat) || o.weekdayFormat.length < 7) {
throw new Error('`weekdays` must be `min`, `short`, or `long`');
}
if (o.monthsInCalendar === no) { o.monthsInCalendar = 1; }
Expand Down
33 changes: 21 additions & 12 deletions src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
var throttle = require('lodash.throttle');
var raf = require('raf');
var clone = require('./clone');
var defaults = require('./defaults');
var calendar = require('./calendar');
var momentum = require('./momentum');
var classes = require('./classes');
var events = require('./events');

function inputCalendar (input, calendarOptions) {
var o;
var api = calendar(calendarOptions);
var api = calendar(asInput(calendarOptions));
var throttledTakeInput = throttle(takeInput, 50);
var throttledPosition = throttle(position, 30);
var ignoreInvalidation;
var ignoreShow;

bindEvents();
init(calendarOptions);

function init (superOptions) {
o = clone(superOptions);
return api;

function asInput (options) {
var o = options || {};
o._input = true;
return o;
}

function init (initOptions) {
o = defaults(asInput(initOptions || calendarOptions), api);

classes.add(api.container, o.styles.positioned);
events.add(api.container, 'mousedown', containerMouseDown);
Expand All @@ -42,12 +51,6 @@ function inputCalendar (input, calendarOptions) {

function destroy () {
eventListening(true);
raf(bindEvents);
}

function bindEvents () {
api.once('ready', init);
api.once('destroyed', destroy);
}

function eventListening (remove) {
Expand All @@ -61,6 +64,14 @@ function inputCalendar (input, calendarOptions) {
events[op](input, 'input', throttledTakeInput);
if (o.invalidate) { events[op](input, 'blur', invalidateInput); }
events[op](window, 'resize', throttledPosition);

if (remove) {
api.once('ready', init);
api.off('destroyed', destroy);
} else {
api.off('ready', init);
api.once('destroyed', destroy);
}
}

function containerClick () {
Expand Down Expand Up @@ -120,8 +131,6 @@ function inputCalendar (input, calendarOptions) {
return isEmpty() ? null : fn.apply(this, arguments);
};
}

return api;
}

module.exports = inputCalendar;
33 changes: 0 additions & 33 deletions src/polyfills/array.every.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/polyfills/array.filter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Array.prototype.filter) {
Array.prototype.filter = function (fn, ctx) {
var f = [];
Expand Down
2 changes: 2 additions & 0 deletions src/polyfills/array.foreach.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Array.prototype.forEach) {
Array.prototype.forEach = function (fn, ctx) {
if (this === void 0 || this === null || typeof fn !== 'function') {
Expand Down
2 changes: 2 additions & 0 deletions src/polyfills/array.indexof.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (what, start) {
if (this === undefined || this === null) {
Expand Down
2 changes: 2 additions & 0 deletions src/polyfills/array.isarray.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

Array.isArray || (Array.isArray = function (a) {
return '' + a !== a && Object.prototype.toString.call(a) === '[object Array]';
});
2 changes: 2 additions & 0 deletions src/polyfills/array.map.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Array.prototype.map) {
Array.prototype.map = function (fn, ctx) {
var context, result, i;
Expand Down
2 changes: 2 additions & 0 deletions src/polyfills/array.some.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Array.prototype.some) {
Array.prototype.some = function (fn, ctx) {
var context, i;
Expand Down
2 changes: 2 additions & 0 deletions src/polyfills/function.bind.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!Function.prototype.bind) {
Function.prototype.bind = function (context) {
if (typeof this !== 'function') {
Expand Down
65 changes: 32 additions & 33 deletions src/polyfills/object.keys.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
'use strict';

return function(obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var hasOwn = Object.prototype.hasOwnProperty;
var hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString');
var dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
];
var dontEnumsLength = dontEnums.length;

var result = [], prop, i;
if (!Object.keys) {
Object.keys = function(obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}

for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
var result = [], prop, i;

for (prop in obj) {
if (hasOwn.call(obj, prop)) {
result.push(prop);
}
}

if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwn.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
return result;
};
}());
}
}
return result;
};
}
2 changes: 2 additions & 0 deletions src/polyfills/string.trim.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
Expand Down
1 change: 0 additions & 1 deletion src/rome.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require('./polyfills/array.map');
require('./polyfills/array.filter');
require('./polyfills/array.isarray');
require('./polyfills/array.indexof');
require('./polyfills/array.every');
require('./polyfills/array.some');
require('./polyfills/string.trim');
require('./polyfills/object.keys');
Expand Down

0 comments on commit 48a1a67

Please sign in to comment.