Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve AjaxTextHinter #782

Merged
merged 4 commits into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions Frameworks/Ajax/Ajax/Sources/er/ajax/AjaxTextHinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,52 @@
import com.webobjects.appserver.WOResponse;
import com.webobjects.foundation.NSDictionary;

import er.extensions.appserver.ERXWOContext;

/**
* Hints input and textarea fields with ghosted text that serves as an explanation for the user what to enter.
* The style "ajax-hinted-text-with-default" defines the color for this.
*
* The default value for the field comes from setting default = "something"; on the input element. For instance,
* to set the default value on a text field, you would do <wo:WOTextField value = "$value" default = "Fill this in"/>.
*
* If you place this tag around your forms or input elements, all input elements and forms are automatically re-registered
* after an Ajax-Refresh when placed within an AjaxUpdateContainer.
*
* <pre>
* HTML:
* &lt;webobject name="Form"&gt;
* &lt;webobject name="SomeText" /&gt;
* &lt;/webobject&gt;
* &lt;webobject name="TextHinter"/ &gt;
*
* WOD:
* TextHinter : AjaxTextHinter {
* form = "EditForm";
* }
* Form : ERXWOForm {
* id = "EditForm";
* ....
* }
* SomeText: WOTextField {
* default = "Name oder Titel";
* ....
* } </pre>
* Example 1: Given form and all input elements are registered on load (old behaviour of AjaxTextHinter)
*
* <wo:Form id="myform" ...>
* <wo:WOTextField default="Login name"/>
* </wo:Form>
* <wo:AjaxTextHinter form="myform"/>
*
* Example 2: Form is within AjaxUpdateContainer and has to be re-registered after Ajax refresh.
* So you can nest multiple AjaxTextHinter tags, if neccessary
*
* <wo:AjaxUpdateContainer>
* <wo:AjaxTextHinter>
* <wo:Form id="myform" ...>
* <wo:WOTextField default="Login name"/>
* </wo:Form>
* </wo:AjaxTextHinter/>
* </wo:AjaxUpdateContainer>
*
* Exmaple 3: only some input elements are within AjaxUpdateContainer
*
* <wo:AjaxTextHinter>
* <wo:Form id="myform" ...>
* <wo:AjaxUpdateContainer>
* <wo:AjaxTextHinter>
* <wo:WOTextField default="Login name"/>
* </wo:AjaxTextHinter/>
* </wo:AjaxUpdateContainer>
* </wo:Form>
* </wo:AjaxTextHinter/>
*
* </pre>
*
* @binding form ID of the form to apply the hints to
* @author ak
*/
Expand All @@ -49,20 +70,38 @@ public WOActionResults handleRequest(WORequest request, WOContext context) {
@Override
protected void addRequiredWebResources(WOResponse response, WOContext context) {
addScriptResourceInHead(context, response, "prototype.js");
addScriptResourceInHead(context, response, "behaviour.js");
addScriptResourceInHead(context, response, "wonder.js");
}

@Override
public void appendToResponse(WOResponse response, WOContext context) {
super.appendToResponse(response, context);
String name = (String) valueForBinding("form", context.component());
if(name != null) {
name = "'" + name + "'";
} else {
name = "";
String formId = (String) valueForBinding("form", context.component());

String id = _containerID(context);

if(hasChildrenElements() && formId == null)
{
response.appendContentString("<div id='" + id + "'>");
appendChildrenToResponse(response, context);
response.appendContentString("</div>");
if(AjaxUtils.isAjaxRequest(context.request()))
response.appendContentString("<script>AjaxHintedText.register('div', '" + id + "');</script>");
else
response.appendContentString("<script>Event.observe(window, 'load', function() {AjaxHintedText.register('div', '" + id + "');});</script>");
} else
{
String formSelector = (formId != null) ? "form#" + formId : "form";

if(AjaxUtils.isAjaxRequest(context.request()))
response.appendContentString("<script>AjaxHintedText.registerForm('" + formSelector + "');</script>");
else
response.appendContentString("<script>Event.observe(window, 'load', function() {AjaxHintedText.registerForm('" + formSelector + "');});</script>");
}
response.appendContentString("<script>AjaxHintedText.register(" + name + ")</script>");

}

protected String _containerID(WOContext context) {
return ERXWOContext.safeIdentifierName(context, false);
}

}
}
33 changes: 22 additions & 11 deletions Frameworks/Ajax/Ajax/WebServerResources/wonder.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,26 +822,37 @@ Ajax.StoppedPeriodicalUpdater = Class.create(Ajax.Base, {
});

var AjaxHintedText = {
register : function(name) {
name = name ? "form#" + name : "form";
var e = new Object();
e[name + " input"] = AjaxHintedText.textBehaviour;
e[name + " textarea"] = AjaxHintedText.textBehaviour;
e[name + ""] = AjaxHintedText.formBehaviour;
Behaviour.register(e);
register: function(tag, id) {
$$(tag + "#" + id).each(function(script, index) {
Element.select($(script), 'input', 'textarea').each(function(el, index) {
AjaxHintedText.textBehaviour(el);
});
Element.select($(script), 'form').each(function(form, index) {
AjaxHintedText.formBehaviour(form);
});
});
},
registerForm : function(formselector) {
$$(formselector).each(function(form,index) {
Element.select(form,'input','textarea').each(function(el, index) {
AjaxHintedText.textBehaviour(el);
});
AjaxHintedText.formBehaviour(form);
});
},
textBehaviour : function(e) {
if(!e.getAttribute('default')) {
return;
}
e.setAttribute('default', unescape(e.getAttribute('default')));
e.showDefaultValue = function() {
if(e.value == "") {
if(e.value == "" ||
e.value.replace(/[\r\n]/g, "") == e.getAttribute('default').replace(/[\r\n]/g, "")) {
Element.addClassName(e, 'ajax-hinted-text-with-default');
e.value = e.getAttribute('default');
} else {
e.value = e.getAttribute('default');
} else {
Element.removeClassName(e, 'ajax-hinted-text-with-default');
}
}
}
e.showTextValue = function() {
Element.removeClassName(e, 'ajax-hinted-text-with-default');
Expand Down