Skip to content

Commit 1f11d1a

Browse files
programcsharpjehhynes
authored andcommitted
Switch validation to blur
Make tab/enter edit work Enhance focus behavior
1 parent 6181322 commit 1f11d1a

File tree

1 file changed

+102
-53
lines changed

1 file changed

+102
-53
lines changed

Griddly/Scripts/editly.js

Lines changed: 102 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -102,61 +102,136 @@
102102
// }).popover("show");
103103
//};
104104

105+
var hideValidation = function ()
106+
{
107+
validator.settings.unhighlight(editor);
108+
109+
// TODO: do we need this if app has proper bs highlight method?
110+
editor.parents(".form-group").removeClass("has-error");
111+
112+
template.popover("hide");
113+
};
114+
115+
var hideEditor = function ()
116+
{
117+
template.hide();
118+
119+
hideValidation();
120+
};
121+
122+
var doValidation = function ()
123+
{
124+
var isValid = editor.valid();
125+
126+
if (isValid)
127+
{
128+
hideValidation();
129+
}
130+
else
131+
{
132+
validator.settings.highlight(editor);
133+
134+
// TODO: do we need this if app has proper bs highlight method?
135+
editor.parents(".form-group").addClass("has-error");
136+
137+
window.setTimeout(function ()
138+
{
139+
$(template)
140+
.popover(
141+
{
142+
container: "body",
143+
content: function () { return validator.errorList[0].message; }
144+
})
145+
.popover("show");
146+
}, 100);
147+
}
148+
149+
return isValid;
150+
};
151+
152+
var handleBlur = function ()
153+
{
154+
// TODO: is there a more efficient way for this?
155+
if (!$(document.activeElement).parents().is(template))
156+
{
157+
if (editor.valid())
158+
{
159+
active.text(editor.val());
160+
161+
hideEditor();
162+
}
163+
else
164+
{
165+
editor.focus();
166+
167+
doValidation();
168+
}
169+
}
170+
};
171+
105172
template
106173
.show()
107174
.offset(active.offset())
108175
.width(template == editor ? active.width() : active.outerWidth())
109-
.height(template == editor ? active.height() : active.outerHeight());
176+
.height(template == editor ? active.height() : active.outerHeight())
177+
.off("blur")
178+
.on("blur", function()
179+
{
180+
setTimeout(handleBlur, 1);
181+
});
110182

111183
editor.val(active.text())
112184
.removeClass('error')
113185
.css(active.css(self.options.cloneProperties))
114186
.height(active.height())
115187
.focus()
116-
.off("blur")
117-
.on("blur", function()
118-
{
119-
active.text(editor.val());
120-
template.hide();
121-
template.popover("hide");
122-
})
123188
.off("keydown")
124189
.on("keydown", function (e)
125190
{
126191
if (e.which === ENTER)
127192
{
128-
//active.text(editor.val());
129-
//setActiveText();
193+
if (doValidation())
194+
{
195+
template.hide();
196+
active.focus();
197+
198+
var possibleMove = movement(active, e.shiftKey ? ARROW_UP : ARROW_DOWN);
130199

131-
template.hide();
132-
active.focus();
200+
if (possibleMove.length > 0)
201+
{
202+
possibleMove.focus();
133203

134-
e.preventDefault();
135-
e.stopPropagation();
204+
e.preventDefault();
205+
e.stopPropagation();
206+
}
207+
}
136208
}
137209
else if (e.which === ESC)
138210
{
139-
editor.val(active.text());
211+
template.off("blur");
140212

141-
e.preventDefault();
142-
e.stopPropagation();
213+
hideEditor();
214+
//editor.val(active.text());
143215

144-
template.hide();
145-
active.focus();
216+
//template.hide();
217+
//active.focus();
146218
}
147219
else if (e.which === TAB)
148220
{
149-
active.focus();
221+
if (doValidation())
222+
{
223+
active.focus();
150224

151-
// Have to move here because dropdown eats it somehow if not
152-
var possibleMove = movement(active, e.shiftKey ? ARROW_LEFT : ARROW_RIGHT);
225+
// Have to move here because dropdown eats it somehow if not
226+
var possibleMove = movement(active, e.shiftKey ? ARROW_LEFT : ARROW_RIGHT);
153227

154-
if (possibleMove.length > 0)
155-
{
156-
possibleMove.focus();
228+
if (possibleMove.length > 0)
229+
{
230+
possibleMove.focus();
157231

158-
e.preventDefault();
159-
e.stopPropagation();
232+
e.preventDefault();
233+
e.stopPropagation();
234+
}
160235
}
161236
}
162237
else if (this.selectionEnd - this.selectionStart === this.value.length ||
@@ -177,32 +252,6 @@
177252
if (template == editor)
178253
editor.width(active.width());
179254

180-
if (editor.valid())
181-
{
182-
validator.settings.unhighlight(editor);
183-
184-
// TODO: do we need this if app has proper bs highlight method?
185-
editor.parents(".form-group").removeClass("has-error");
186-
}
187-
else
188-
{
189-
validator.settings.highlight(editor);
190-
191-
// TODO: do we need this if app has proper bs highlight method?
192-
editor.parents(".form-group").addClass("has-error");
193-
194-
window.setTimeout(function ()
195-
{
196-
$(template)
197-
.popover(
198-
{
199-
container: "body",
200-
content: function () { return validator.errorList[0].message; }
201-
})
202-
.popover("show");
203-
}, 100);
204-
}
205-
206255
if (select)
207256
{
208257
editor.select();

0 commit comments

Comments
 (0)