Skip to content

Commit

Permalink
Supporting button action confirmation and when-done redirects.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Apr 11, 2016
1 parent a9358bc commit 501c5a9
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 66 deletions.
40 changes: 37 additions & 3 deletions assets/angular/emit.js
@@ -1,6 +1,33 @@
Rapidoid.initializer(function($scope) {

$scope._emit = function(eventId, eventArgs) {
$scope._emit = function(event, eventId, eventArgs) {

var btn = $(event.currentTarget);
var confirm = btn.data("confirm");

if (confirm) {
swal({
title: "Are you sure?",
text: confirm,
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
closeOnConfirm: true
}, function(){
doEmit(event, eventId, eventArgs);
});

} else {
doEmit(event, eventId, eventArgs);
}

};

function doEmit(event, eventId, eventArgs) {

var btn = $(event.currentTarget);
var go = btn.data("go");

eventId = eventId || 'bind';
eventArgs = eventArgs || [];
Expand Down Expand Up @@ -45,14 +72,19 @@ Rapidoid.initializer(function($scope) {

$.post(window.location.href, inputs).done(function(data) {

if (go) {
Rapidoid.goAt(go);
return;
}

if (typeof data === 'string' || data instanceof String) {
$scope.ajaxBodyContent = data;
$scope.$apply();
return;
}

if (data._redirect_) {
_goAt(data._redirect_);
Rapidoid.goAt(data._redirect_);
return;
}

Expand All @@ -75,6 +107,7 @@ Rapidoid.initializer(function($scope) {
}
}
}

} else {
if (data._sel_ === undefined) {
swal("Application error!", "The command couldn't be executed!", "error");
Expand All @@ -90,10 +123,11 @@ Rapidoid.initializer(function($scope) {
}
}
}

}).fail(function(data) {
swal("Communication error!", "Couldn't connect to the server!", "error");
console.log(data);
});
};
}

});
22 changes: 13 additions & 9 deletions rapidoid-commons/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -739,21 +739,25 @@ public static void logProperties(Properties props) {
}
}

public static String uriFor(Object entity) {
if (!JPA.isEntity(entity)) {
public static String uriFor(Object target) {
if (!JPA.isEntity(target)) {
return "";
}

Object id = JPA.getIdentifier(entity);

if (id != null) {
String name = Str.camelToSnake(English.plural(Cls.entityName(entity)));
String frm = Conf.ROOT.is("generate") ? "%s%s.html" : "/%s/%s/view";
return U.frmt(frm, name, id);
return uriFor(typeUri(target.getClass()), target);
}

} else {
public static String uriFor(String baseUri, Object target) {
if (!JPA.isEntity(target)) {
return "";
}

Object id = JPA.getIdentifier(target);
return id != null ? uri(baseUri, id + "") : "";
}

public static String typeUri(Class<?> entityType) {
return "/" + English.plural(Str.uncapitalized(entityType.getSimpleName()));
}

}
32 changes: 25 additions & 7 deletions rapidoid-gui/src/main/java/org/rapidoid/gui/Btn.java
Expand Up @@ -35,18 +35,20 @@ public class Btn extends AbstractCommand<Btn> {

private String kind = "default";

private String href;
private String go;

private String class_;

private String confirm;

@Override
protected Tag render() {
handleEventIfMatching();

String cls = U.or(class_, "btn btn-" + kind);

if (href != null) {
return a(contents).href(href).class_(cls);
if (go != null && handler == null && confirm() == null) {
return a(contents).href(go).class_(cls);
}

ButtonTag btn = button(contents).type("button").class_(cls);
Expand All @@ -55,6 +57,14 @@ protected Tag render() {
btn = btn.cmd(command(), cmdArgs());
}

if (confirm() != null) {
btn = btn.attr("data-confirm", confirm());
}

if (go() != null) {
btn = btn.attr("data-go", go());
}

return btn;
}

Expand Down Expand Up @@ -101,12 +111,12 @@ public Btn kind(String kind) {
return this;
}

public String href() {
return href;
public String go() {
return go;
}

public Btn href(String linkTo) {
this.href = linkTo;
public Btn go(String linkTo) {
this.go = linkTo;
return this;
}

Expand All @@ -124,4 +134,12 @@ public Btn onClick(Runnable onClickHandler) {
return this;
}

public String confirm() {
return confirm;
}

public Btn confirm(String confirm) {
this.confirm = confirm;
return this;
}
}
Expand Up @@ -35,7 +35,7 @@ public abstract class AbstractCommand<W extends AbstractCommand<?>> extends Abst

private String[] cmdArgs;

private Runnable handler;
protected Runnable handler;

private boolean handled;

Expand Down
Expand Up @@ -373,7 +373,7 @@ public static Btn cmd(String cmd, Object... args) {

public static Btn navigate(String cmd) {
String caption = Str.capitalized(cmd);
return btn(caption).href(cmd);
return btn(caption).go(cmd);
}

public static Btn[] cmds(String... commands) {
Expand Down Expand Up @@ -513,11 +513,11 @@ public static Tag[] mediaList(List<Object> found) {

for (Object result : found) {
Object id = Beany.getId(result);
String url = UTILS.uriFor(result);
String uri = UTILS.uriFor(result);

Tag left = h6("(ID", NBSP, "=", NBSP, id, ")");
Object header = span(result.getClass().getSimpleName());
items[ind++] = media(left, header, small(Beany.beanToNiceText(result, true)), url);
items[ind++] = media(left, header, small(Beany.beanToNiceText(result, true)), uri);
}

return items;
Expand Down
Expand Up @@ -46,7 +46,7 @@ public class TagRenderer {

private static final byte[] COMMA_SEP = ", ".getBytes();
private static final byte[] INDENT = " ".getBytes();
private static final byte[] EMIT = "_emit('".getBytes();
private static final byte[] EMIT = "_emit($event, '".getBytes();
private static final byte[] EMIT_SEP = "', [".getBytes();
private static final byte[] EMIT_CLOSE = "])".getBytes();
private static final byte[] _H = " _h=\"".getBytes();
Expand Down
40 changes: 37 additions & 3 deletions rapidoid-html/src/main/resources/default/static/rapidoid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 501c5a9

Please sign in to comment.