var Calendar; exports.init = function() { var instances = []; this.$el = $(".input.datetime_select, .input.date_select"); this.$el.each(function() { instances.push(new Calendar(this)); }); }; Calendar = (function() { function Cal(input_element) { this.date_format = "DD-MM-YYYY"; this.$el = $(input_element); this.$fieldset = this.$el.children(".fragments"); this.replace_select_fields_with_hidden_fields(); this.create_new_input_field(); this.setup_pikaday_instance(); } Cal.prototype.replace_select_fields_with_hidden_fields = function() { var $select = this.$fieldset.find(".fragments-group select"), _this = this; $select.each(function() { var new_input_field, checked_value; checked_value = $(this).find("option:checked").val(); new_input_field = document.createElement("input"); new_input_field.setAttribute("type", "hidden"); new_input_field.setAttribute("name", this.name); new_input_field.setAttribute("value", checked_value); _this.$fieldset.append(new_input_field); }); $select.closest(".fragments-group").remove(); }; Cal.prototype.create_new_input_field = function() { var new_input_field, date, date_string; date = this.get_date_from_hidden_fields(); date_string = moment(date).format(this.date_format); new_input_field = document.createElement("input"); new_input_field.setAttribute("type", "text"); new_input_field.setAttribute("class", "calendar-input-field"); new_input_field.value = date_string; this.$fieldset.append(new_input_field); }; Cal.prototype.setup_pikaday_instance = function() { var cal, new_input, self = this; new_input = this.$el.find("input.calendar-input-field")[0]; date = this.get_date_from_hidden_fields(); cal = new Pikaday({ format: this.date_format, defaultDate: date, field: new_input, firstDay: 1, onSelect: function(date) { self.set_date_on_hidden_fields(date); } }); cal.setDate(date); this.$el.data("calendar", cal); }; Cal.prototype.get_date_from_hidden_fields = function() { var fields = [], values = [], new_date; this.$fieldset.find('input[type="hidden"]').each(function() { var name = $(this).attr("name"); fields.push(name); }); fields = fields.sort(); new_date = new Date(); for (var i=0,j=fields.length; i