Skip to content

Commit

Permalink
make submittable widgets handle 'enter' key press in addition to watc…
Browse files Browse the repository at this point in the history
…hing a native submission for <form> tags
  • Loading branch information
Inviz committed Dec 12, 2011
1 parent e8743cb commit df327ea
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions Source/Mixin/Submittable.js
Expand Up @@ -35,7 +35,7 @@ LSD.Mixin.Submittable = new Class({
as: 'submittable',
scope: {
'default': {
filter: '[default]'
filter: '[-]'
}
},
options: {
Expand All @@ -52,12 +52,10 @@ LSD.Mixin.Submittable = new Class({
},
callbacks: {
fill: function() {
if (LSD.toLowerCase(this.element.tagName) == 'form')
this.properties.watch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
this.properties.watch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
},
empty: function() {
if (LSD.toLowerCase(this.element.tagName) == 'form')
this.properties.unwatch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
this.properties.unwatch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
}
}
}
Expand Down Expand Up @@ -111,10 +109,14 @@ LSD.Mixin.Submittable = new Class({
its value is used for submission data.
*/
LSD.Mixin.Submittable.watchNativeSubmission = function(state) {
if (state) {
this.allocate('submit').inject(this.element, 'top').addEvent('click', this.bind('submit'))
if (LSD.toLowerCase(this.element.tagName) == 'form') {
if (state) {
this.allocate('submit').inject(this.element, 'top').addEvent('click', this.bind('submit'))
} else {
this.release('submit').dispose().removeEvent('click', this.bind('submit'));
}
} else {
this.release('submit').dispose().removeEvent('click', this.bind('submit'));
this.element[state ? 'addEvent' : 'removeEvent']('keydown', this.bind(LSD.Mixin.Submittable.listenKeyPress))
}
/*
novalidate html attribute disables internal form validation
Expand All @@ -124,4 +126,11 @@ LSD.Mixin.Submittable.watchNativeSubmission = function(state) {
this.element[state ? 'setAttribute' : 'removeAttribute']('novalidate', '');
};

LSD.Mixin.Submittable.listenKeyPress = function(event) {
if (event.key == 'enter') {
this.submit()
event.preventDefault();
}
};

LSD.Behavior.define(':submittable', 'submittable');

0 comments on commit df327ea

Please sign in to comment.