Skip to content

Commit

Permalink
Merge commit '5d099fe415996979ceb2deb15e9558e7e6c962f4' into amd-impr…
Browse files Browse the repository at this point in the history
…ovements

Conflicts:
	distribution/backbone-forms.amd.min.js
	distribution/backbone-forms.min.js
	test/editors/list.js
	test/form.js

Closes #89, closes #77
  • Loading branch information
powmedia committed Jul 26, 2012
1 parent d78524a commit ebfdc6c
Show file tree
Hide file tree
Showing 26 changed files with 4,671 additions and 354 deletions.
407 changes: 269 additions & 138 deletions README.md

Large diffs are not rendered by default.

422 changes: 397 additions & 25 deletions distribution.amd/backbone-forms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion distribution.amd/backbone-forms.min.js

Large diffs are not rendered by default.

108 changes: 90 additions & 18 deletions distribution.amd/editors/jquery-ui.js
Expand Up @@ -46,6 +46,8 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
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);
Expand All @@ -65,6 +67,35 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {

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 {};
});
}

});
Expand Down Expand Up @@ -107,6 +138,8 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
hours: hoursOptions.join(),
mins: minsOptions.join()
}));

this._observeDatepickerEvents();

//Store references to selects
this.$hours = $('select:eq(0)', this.el);
Expand Down Expand Up @@ -239,6 +272,8 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
text: false,
icons: { primary: 'ui-icon-trash' }
});

if (this.hasFocus) this.trigger('blur', this);

return this;
},
Expand Down Expand Up @@ -271,13 +306,13 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
* Add a new item to the list if it is completed in the editor
*/
addNewItem: function(event) {
event.preventDefault();
if (event) event.preventDefault();

var self = this;

this.openEditor(null, function(value) {
this.openEditor(null, function(value, editor) {
//Fire 'addItem' cancellable event
triggerCancellableEvent(self, 'addItem', [value], function() {
triggerCancellableEvent(self, 'addItem', [value, editor], function() {
var text = self.itemToString(value);

//Create DOM element
Expand All @@ -300,6 +335,10 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
text: false,
icons: { primary: 'ui-icon-trash' }
});

self.trigger('add', self, value);
self.trigger('item:change', self, editor);
self.trigger('change', self);
});
});
},
Expand All @@ -314,14 +353,17 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
li = $(event.target).closest('li'),
originalValue = $.data(li[0], 'data');

this.openEditor(originalValue, function(newValue) {
this.openEditor(originalValue, function(newValue, editor) {
//Fire 'editItem' cancellable event
triggerCancellableEvent(self, 'editItem', [newValue], function() {
triggerCancellableEvent(self, 'editItem', [newValue, editor], function() {
//Update display
$('.bbf-list-text', li).html(self.itemToString(newValue));

//Store data
$.data(li[0], 'data', newValue);

self.trigger('item:change', self, editor);
self.trigger('change', self);
});
});
},
Expand All @@ -339,6 +381,9 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
function remove() {
triggerCancellableEvent(self, 'removeItem', [data], function() {
li.remove();

self.trigger('remove', self, data);
self.trigger('change', self);
});
}

Expand All @@ -365,24 +410,15 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
value: data
}).render();

var container = $(this.editorTemplate());
var container = this.editorContainer = $(this.editorTemplate());
$('.bbf-list-editor', container).html(editor.el);

var close = function() {
$(document).unbind('keydown', handleEnterPressed);

container.dialog('close');

editor.remove();
container.remove();
};

var saveAndClose = function() {
var errs = editor.validate();
if (errs) return;

callback(editor.getValue());
close();
callback(editor.getValue(), editor);
container.dialog('close');
}

var handleEnterPressed = function(event) {
Expand All @@ -398,9 +434,27 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
title: data ? 'Edit item' : 'New item',
buttons: {
'OK': saveAndClose,
'Cancel': close
'Cancel': function() {
container.dialog('close');
}
},
close: function() {
self.editorContainer = null;

$(document).unbind('keydown', handleEnterPressed);

editor.remove();
container.remove();

self.trigger('item:close', self, editor);
self.trigger('item:blur', self, editor);
self.trigger('blur', self);
}
});

this.trigger('item:open', this, editor);
this.trigger('item:focus', this, editor);
this.trigger('focus', this);

//Save and close dialog on Enter keypress
$(document).bind('keydown', handleEnterPressed);
Expand All @@ -419,6 +473,24 @@ define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
setValue: function(value) {
this.value = value;
this.render();
},

focus: function() {
if (this.hasFocus) return;

var item = this.$('li .bbf-list-edit').first();
if (item.length > 0) {
item.click();
}
else {
this.addNewItem();
}
},

blur: function() {
if (!this.hasFocus) return;

if (this.editorContainer) this.editorContainer.dialog('close');
}

});
Expand Down

0 comments on commit ebfdc6c

Please sign in to comment.