Created
April 13, 2016 06:35
-
-
Save noomorph/5c69742b6cd0b1d9a914f74813801032 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, moment) { | |
var PLUGIN = 'monthPicker'; | |
function formatValue(value, locale, format) { | |
return moment(value).locale(locale).format(format); | |
} | |
function template() { | |
var options = this.options, | |
actions = this.actions; | |
return $('<table/>', { 'class': options.css.root }).append( | |
$('<thead/>').append( | |
$('<tr/>') | |
.append($('<th/>', { | |
'class': options.css.prev, | |
'text': options.text.prev, | |
'click': actions.prev | |
})) | |
.append($('<th/>', { | |
'class': options.css.date, | |
'text': formatValue( | |
this.state.value, | |
this.options.locale, | |
this.options.displayFormat | |
) | |
})) | |
.append($('<th/>', { | |
'class': options.css.next, | |
'text': options.text.next, | |
'click': actions.next | |
})) | |
) | |
); | |
} | |
function partialUpdate() { | |
this.dom.find('.' + this.options.css.date).text( | |
formatValue(this.state.value, this.options.locale, this.options.displayFormat) | |
); | |
this.dom.trigger('change', this.state); | |
} | |
function incrementMonth(months) { | |
this.state = $.extend({}, this.state, { | |
value: moment(this.state.value).add(months, 'months').toDate() | |
}); | |
} | |
function compose(f, g) { | |
return function composition() { | |
return f.call(this, g.apply(this, arguments)); | |
}; | |
} | |
function toDate(dateString) { | |
return new Date(dateString.split('.').reverse().join(',')); | |
} | |
$.fn[PLUGIN] = function $monthPickerPlugin(customOptions) { | |
return this.each(function () { | |
var component = {}; | |
component.dom = $(this); | |
component.options = $.extend({}, $.fn[PLUGIN].defaults, component.dom.data(), customOptions); | |
component.state = { value: toDate(component.options.value) }; | |
component.actions = { | |
prev: compose(partialUpdate.bind(component), incrementMonth.bind(component, -1)), | |
next: compose(partialUpdate.bind(component), incrementMonth.bind(component, +1)) | |
}; | |
component.dom.html(component.options.template.call(component)); | |
component.dom.on('change', component.options.onChange); | |
}); | |
}; | |
$.fn[PLUGIN].defaults = { | |
value: new Date(), | |
locale: 'ru', | |
displayFormat: 'MMMM YYYY', | |
css: { | |
prev: 'prev', | |
date: 'date-place', | |
next: 'next', | |
root: 'table-condensed' | |
}, | |
text: { | |
prev: '«', | |
next: '»' | |
}, | |
template: template, | |
onChange: $.noop | |
}; | |
}(window.jQuery || window.$, window.moment)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.month-picker-container').monthPicker().on('change', function (e, state) { | |
var date = moment(state).format('MM.YYYY'); | |
ajaxSend({ | |
type: 'POST', | |
url: 'http://example.com/api/echo', | |
data: { data: date } | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment