;(function() { var Form = Backbone.Form, Base = Form.editors.Base, createTemplate = Form.helpers.createTemplate, triggerCancellableEvent = Form.helpers.triggerCancellableEvent, exports = {}; /** * Additional editors that depend on jQuery UI */ //DATE exports['jqueryui.Date'] = Base.extend({ className: 'bbf-jui-date', initialize: function(options) { Base.prototype.initialize.call(this, options); //Cast to Date if (this.value && !_.isDate(this.value)) { this.value = new Date(this.value); } //Set default date if (!this.value) { var date = new Date(); date.setSeconds(0); date.setMilliseconds(0); this.value = date; } }, render: function() { var $el = this.$el; $el.html(''); var input = $('input', $el); input.datepicker({ dateFormat: 'dd/mm/yy', showButtonPanel: true }); this._observeDatepickerEvents(); //Make sure setValue of this object is called, not of any objects extending it (e.g. DateTime) exports['jqueryui.Date'].prototype.setValue.call(this, this.value); return this; }, /** * @return {Date} Selected date */ getValue: function() { var input = $('input', this.el), date = input.datepicker('getDate'); return date; }, setValue: function(value) { $('input', this.el).datepicker('setDate', value); }, focus: function() { if (this.hasFocus) return; this.$('input').datepicker('show'); }, blur: function() { if (!this.hasFocus) return; this.$('input').datepicker('hide'); }, _observeDatepickerEvents: function() { var self = this; this.$('input').datepicker('option', 'onSelect', function() { self.trigger('change', self); }) this.$('input').datepicker('option', 'onClose', function() { if (!self.hasFocus) return; self.trigger('blur', self); }); this.$('input').datepicker('option', 'beforeShow', function() { if (self.hasFocus) return {}; self.trigger('focus', self); return {}; }); } }); //DATETIME exports['jqueryui.DateTime'] = exports['jqueryui.Date'].extend({ className: 'bbf-jui-datetime', template: createTemplate(' : '), render: function() { function pad(n) { return n < 10 ? '0' + n : n } //Render the date element first exports['jqueryui.Date'].prototype.render.call(this); //Setup hour options var hours = _.range(0, 24), hoursOptions = []; _.each(hours, function(hour) { hoursOptions.push(''); }); //Setup minute options var minsInterval = this.schema.minsInterval || 15, mins = _.range(0, 60, minsInterval), minsOptions = []; _.each(mins, function(min) { minsOptions.push(''); }); //Render time selects this.$el.append(this.template({ hours: hoursOptions.join(), mins: minsOptions.join() })); this._observeDatepickerEvents(); //Store references to selects this.$hours = $('select:eq(0)', this.el); this.$mins = $('select:eq(1)', this.el); //Set time this.setValue(this.value); return this; }, /** * @return {Date} Selected datetime */ getValue: function() { var input = $('input', this.el), date = input.datepicker('getDate'); date.setHours(this.$hours.val()); date.setMinutes(this.$mins.val()); date.setMilliseconds(0); return date; }, setValue: function(date) { exports['jqueryui.Date'].prototype.setValue.call(this, date); this.$hours.val(date.getHours()); this.$mins.val(date.getMinutes()); } }); //LIST exports['jqueryui.List'] = Base.extend({ className: 'bbf-jui-list', //Note: The extra div around the