Skip to content

Commit

Permalink
#8 - generating application module plugin for Vue :)
Browse files Browse the repository at this point in the history
  • Loading branch information
grabslu committed Oct 27, 2016
1 parent 4f6adb2 commit 4b22380
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.licket.core.model.LicketModel.ofModelObject;
import static org.licket.core.view.ComponentView.fromComponentContainerClass;
import org.licket.core.module.application.LicketComponentModelReloader;
import org.licket.core.module.application.LicketRemoteCommunication;
import org.licket.core.module.application.LicketRemote;
import org.licket.core.module.forms.component.AbstractLicketForm;
import org.licket.core.view.form.LicketInput;
import org.licket.core.view.link.LicketFormSubmitButton;
Expand All @@ -18,7 +18,7 @@ public class AddContactForm extends AbstractLicketForm<Contact> {

private final ContactsService contactsService;

public AddContactForm(String id, ContactsService contactsService, LicketRemoteCommunication remoteCommunication,
public AddContactForm(String id, ContactsService contactsService, LicketRemote remoteCommunication,
LicketComponentModelReloader modelReloader) {
super(id, Contact.class, ofModelObject(new Contact()), fromComponentContainerClass(AddContactForm.class),
remoteCommunication, modelReloader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.licket.demo.model.Contacts.fromIterable;
import org.licket.core.model.LicketModel;
import org.licket.core.module.application.LicketComponentModelReloader;
import org.licket.core.module.application.LicketRemoteCommunication;
import org.licket.core.module.application.LicketRemote;
import org.licket.core.view.container.AbstractLicketContainer;
import org.licket.core.view.link.AbstractLicketActionLink;
import org.licket.core.view.link.ComponentActionCallback;
Expand All @@ -23,7 +23,7 @@ public class ContactsPanel extends AbstractLicketContainer<Contacts> {
private ContactsService contactsService;

@Autowired
private LicketRemoteCommunication remoteCommunication;
private LicketRemote remoteCommunication;

public ContactsPanel(String id, LicketComponentModelReloader modelReloader) {
super(id, Contacts.class, emptyModel(), fromComponentContainerClass(ContactsPanel.class), modelReloader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public VuePlugin applicationModulePlugin(@Autowired HttpCommunicationService htt
}

@Bean
public LicketRemoteCommunication communicationService(@Autowired HttpCommunicationService httpService) {
return new LicketRemoteCommunication(httpService);
public LicketRemote communicationService(@Autowired HttpCommunicationService httpService) {
return new LicketRemote(httpService);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.licket.core.module.application;

import static java.util.Arrays.stream;
import static org.licket.framework.hippo.NameBuilder.name;
import static org.licket.framework.hippo.PropertyNameBuilder.property;
import org.licket.core.view.hippo.angular.ngmodule.VuePlugin;
import org.licket.core.view.hippo.vue.extend.VueClass;
import org.licket.framework.hippo.PropertyNameBuilder;

import java.util.Arrays;
import java.util.function.Consumer;

/**
* @author activey
*/
Expand All @@ -22,4 +26,7 @@ public PropertyNameBuilder vueName() {
return property(name("app"), name("AppModule"));
}

public void forEachService(Consumer<VueClass> serviceConsumer) {
stream(moduleServices).forEach(serviceConsumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private ObjectLiteralBuilder changedModelEventData(NameBuilder compositeId, Name
}

@Override
public PropertyNameBuilder vueName() {
return property(name("app"), name("ComponentModelReloader"));
public NameBuilder vueName() {
return name("$licketModelReloader");
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package org.licket.core.module.application;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.licket.framework.hippo.BlockBuilder.block;
import static org.licket.framework.hippo.ExpressionStatementBuilder.expressionStatement;
import static org.licket.framework.hippo.FunctionCallBuilder.functionCall;
import static org.licket.framework.hippo.FunctionNodeBuilder.functionNode;
import static org.licket.framework.hippo.KeywordLiteralBuilder.thisLiteral;
import static org.licket.framework.hippo.NameBuilder.name;
import static org.licket.framework.hippo.PropertyNameBuilder.property;
import static org.licket.framework.hippo.StringLiteralBuilder.stringLiteral;

import org.licket.core.module.resource.HttpCommunicationService;
import org.licket.core.view.hippo.vue.annotation.Name;
import org.licket.core.view.hippo.vue.annotation.VueComponentFunction;
import org.licket.core.view.hippo.vue.extend.VueClass;
import org.licket.framework.hippo.BlockBuilder;
import org.licket.framework.hippo.FunctionCallBuilder;
import org.licket.framework.hippo.NameBuilder;
import org.licket.framework.hippo.PropertyNameBuilder;

/**
* @author grabslu
*/
public class LicketRemoteCommunication implements VueClass {
public class LicketRemote implements VueClass {

@Name("http")
private final HttpCommunicationService httpCommunicationService;

public LicketRemoteCommunication(HttpCommunicationService httpCommunicationService) {
public LicketRemote(HttpCommunicationService httpCommunicationService) {
this.httpCommunicationService = checkNotNull(httpCommunicationService,
"Http communication service reference must not be null!");
}
Expand All @@ -30,18 +36,31 @@ public void submitForm(@Name("formComponentCompositeId") NameBuilder formCompone
@Name("formData") NameBuilder formData,
@Name("responseListener") NameBuilder responseListener, BlockBuilder body) {
body.appendStatement(expressionStatement(httpCommunicationService
.executeHttpPostWithData("`/licket/form/submit/${formComponentCompositeId}`", formData, responseListener)));
.callHttpPostWithData("`/licket/form/submit/${formComponentCompositeId}`", formData, responseListener)));
}

@VueComponentFunction
public void handleActionLinkClick(@Name("linkComponentCompositeId") NameBuilder linkComponentCompositeId,
@Name("responseListener") NameBuilder responseListener, BlockBuilder body) {
body.appendStatement(expressionStatement(httpCommunicationService
.executeHttpPost("`/licket/link/click/${linkComponentCompositeId}`", responseListener)));
.callHttpPost("`/licket/link/click/${linkComponentCompositeId}`", responseListener)));
}

@Override
public PropertyNameBuilder vueName() {
return property(name("app"), name("ComponentCommunicationService"));
public NameBuilder vueName() {
return name("$licketRemoteService");
}

public FunctionCallBuilder callSubmitForm(String formId) {
return functionCall()
.target(property(vueName(), name("submitForm")))
.argument(stringLiteral(formId)).argument(property(thisLiteral(), name("model")))
.argument(functionNode().param(name("response")).body(block().appendStatement(
expressionStatement(
functionCall()
.target(property(name("vm"), name("afterSubmit")))
.argument(name("response"))
)
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import org.licket.core.module.application.ApplicationModulePlugin;
import org.licket.core.resource.HeadParticipatingResource;
import org.licket.core.resource.javascript.AbstractJavascriptDynamicResource;
import org.licket.core.view.hippo.vue.extend.VueClass;
import org.licket.core.view.hippo.vue.extend.VueExtendMethodsDecorator;
import org.licket.framework.hippo.BlockBuilder;
import org.licket.framework.hippo.FunctionNodeBuilder;
import org.licket.framework.hippo.ObjectLiteralBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import static org.licket.core.view.hippo.vue.extend.VueExtendMethodsDecorator.fromClass;
import static org.licket.framework.hippo.AssignmentBuilder.assignment;
import static org.licket.framework.hippo.BlockBuilder.block;
import static org.licket.framework.hippo.ExpressionStatementBuilder.expressionStatement;
import static org.licket.framework.hippo.FunctionCallBuilder.functionCall;
import static org.licket.framework.hippo.FunctionNodeBuilder.functionNode;
import static org.licket.framework.hippo.NameBuilder.name;
import static org.licket.framework.hippo.ObjectLiteralBuilder.objectLiteral;
import static org.licket.framework.hippo.ObjectPropertyBuilder.propertyBuilder;
import static org.licket.framework.hippo.PropertyNameBuilder.property;
import static org.licket.framework.hippo.ReturnStatementBuilder.returnStatement;

/**
* @author activey
Expand All @@ -41,9 +47,6 @@ protected void buildJavascriptTree(BlockBuilder scriptBlockBuilder) {
}

private FunctionNodeBuilder pluginInitializer() {
// TODO generate list of services from module
applicationModulePlugin.

return functionNode()
.param(name("Vue"))
.param(name("options"))
Expand All @@ -52,8 +55,31 @@ private FunctionNodeBuilder pluginInitializer() {
functionCall()
.target(property("Object", "defineProperties"))
.argument(property("Vue", "prototype"))
.argument(objectLiteral())
.argument(services())
)
);
}

private ObjectLiteralBuilder services() {
ObjectLiteralBuilder services = ObjectLiteralBuilder.objectLiteral();
applicationModulePlugin.forEachService(service -> {
services.objectProperty(
propertyBuilder()
.name(service.vueName())
.value(objectLiteral().objectProperty(
propertyBuilder()
.name("get")
.value(functionNode().body(
block()
.appendStatement(
returnStatement()
.returnValue(serviceMethods(service)))))))
);
});
return services;
}

private ObjectLiteralBuilder serviceMethods(VueClass service) {
return fromClass(service).decorate(objectLiteral());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import static org.licket.core.model.LicketModel.ofModelObject;
import static org.licket.framework.hippo.ArrayElementGetBuilder.arrayElementGet;
import static org.licket.framework.hippo.AssignmentBuilder.assignment;
import static org.licket.framework.hippo.BlockBuilder.block;
import static org.licket.framework.hippo.ExpressionStatementBuilder.expressionStatement;
import static org.licket.framework.hippo.FunctionCallBuilder.functionCall;
import static org.licket.framework.hippo.FunctionNodeBuilder.functionNode;
import static org.licket.framework.hippo.KeywordLiteralBuilder.thisLiteral;
import static org.licket.framework.hippo.NameBuilder.name;
import static org.licket.framework.hippo.PropertyNameBuilder.property;
import static org.licket.framework.hippo.ReturnStatementBuilder.returnStatement;
import static org.licket.framework.hippo.StringLiteralBuilder.stringLiteral;
import org.licket.core.model.LicketModel;
import org.licket.core.module.application.LicketComponentModelReloader;
import org.licket.core.module.application.LicketRemoteCommunication;
import org.licket.core.module.application.LicketRemote;
import org.licket.core.view.ComponentView;
import org.licket.core.view.LicketComponent;
import org.licket.core.view.container.AbstractLicketContainer;
Expand All @@ -23,18 +22,23 @@
import org.licket.core.view.hippo.vue.annotation.Name;
import org.licket.core.view.link.ComponentActionCallback;
import org.licket.core.view.render.ComponentRenderingContext;
import org.licket.framework.hippo.*;
import org.licket.framework.hippo.BlockBuilder;
import org.licket.framework.hippo.ExpressionStatementBuilder;
import org.licket.framework.hippo.NameBuilder;

/**
* @author activey
*/
@VueComponent
public abstract class AbstractLicketForm<T> extends AbstractLicketContainer<T> {

private LicketRemote licketRemote;

public AbstractLicketForm(String id, Class<T> modelClass, LicketModel<T> model, ComponentView componentView,
LicketRemoteCommunication communicationService,
LicketRemote licketRemote,
LicketComponentModelReloader modelReloader) {
super(id, modelClass, model, componentView, modelReloader);
this.licketRemote = checkNotNull(licketRemote, "Liket remote instance must not be null!");
}

public final void submitForm(T formModelObject, ComponentActionCallback actionCallback) {
Expand Down Expand Up @@ -90,16 +94,7 @@ protected void onAfterSubmit(ComponentActionCallback componentActionCallback) {}
@VueComponentFunction
public void submitForm(BlockBuilder functionBlock) {
functionBlock
.appendStatement(expressionStatement(functionCall()
.target(property(name("$licketRemote"), name("submitForm")))
.argument(stringLiteral(getCompositeId().getValue())).argument(property(thisLiteral(), name("model")))
.argument(functionNode().param(name("response")).body(block().appendStatement(
expressionStatement(
functionCall()
.target(property(name("vm"), name("afterSubmit")))
.argument(name("response"))
)
)))))
.appendStatement(ReturnStatementBuilder.returnStatement().returnValue(name("false")));
.appendStatement(expressionStatement(licketRemote.callSubmitForm(getCompositeId().getValue())))
.appendStatement(returnStatement().returnValue(name("false")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
public class HttpCommunicationService implements VueClass {

@Override
public PropertyNameBuilder vueName() {
return property(thisLiteral(), name("$http"));
public NameBuilder vueName() {
return name("$http");
}

public FunctionCallBuilder executeHttpPost(String url, NameBuilder responseListener) {
public FunctionCallBuilder callHttpPost(String url, NameBuilder responseListener) {
return functionCall()
.target(property(functionCall()
.target(httpPostFunction())
Expand All @@ -30,7 +30,7 @@ public FunctionCallBuilder executeHttpPost(String url, NameBuilder responseListe
.argument(responseListener);
}

public FunctionCallBuilder executeHttpPostWithData(String url, NameBuilder data, NameBuilder responseListener) {
public FunctionCallBuilder callHttpPostWithData(String url, NameBuilder data, NameBuilder responseListener) {
return functionCall()
.target(property(functionCall()
.target(httpPostFunction())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.licket.core.model.LicketModel.emptyModel;
import static org.licket.core.view.ComponentView.internal;
import static org.licket.framework.hippo.NameBuilder.name;
import static org.licket.framework.hippo.PropertyNameBuilder.property;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.PostConstruct;
Expand All @@ -16,7 +15,7 @@
import org.licket.core.model.LicketModel;
import org.licket.core.resource.ByteArrayResource;
import org.licket.core.view.render.ComponentRenderingContext;
import org.licket.framework.hippo.PropertyNameBuilder;
import org.licket.framework.hippo.NameBuilder;
import org.licket.surface.element.SurfaceElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -113,8 +112,8 @@ public final Optional<LicketComponent<?>> traverseUp(Predicate<LicketComponent<?
}

@Override
public PropertyNameBuilder vueName() {
return property(name("app"), name(getCompositeId().getNormalizedValue()));
public NameBuilder vueName() {
return name(getCompositeId().getNormalizedValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private AngularComponentStructureDecorator(LicketComponent<?> licketComponent) {

public final AssignmentBuilder decorate(AngularStructuralDecorator decorable) {
return assignment()
.left(licketComponent.vueName())
// .left(licketComponent.vueName())
.right(functionCall()
.target(createModuleFunctionCall(decorable))
.argument(decorable.body()));
Expand Down Expand Up @@ -80,7 +80,8 @@ private void appendProviders(ObjectLiteralBuilder structureBody) {
}

private ArrayLiteralBuilder componentServicesDependencies() {
return forAngularClassDependencies(licketComponent).decorate(arrayLiteral());
// return forAngularClassDependencies(licketComponent).decorate(arrayLiteral());
return arrayLiteral();
}

private ArrayLiteralBuilder componentChildren() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ArrayLiteralBuilder decorate(ArrayLiteralBuilder arrayLiteralBuilder) {
if (!child.getView().isExternalized()) {
return false;
}
arrayLiteralBuilder.element(child.vueName());
// arrayLiteralBuilder.element(child.vueName());
return false;
});
return arrayLiteralBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private ObjectLiteralBuilder componentProperties() {
}

private ObjectLiteralBuilder methods() {
return VueExtendMethodsDecorator.fromExtend(component).decorate(objectLiteral());
return VueExtendMethodsDecorator.fromClass(component).decorate(objectLiteral());
}

private FunctionNodeBuilder data() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.licket.core.view.hippo.vue.extend;

import org.licket.framework.hippo.PropertyNameBuilder;
import org.licket.framework.hippo.NameBuilder;

/**
* @author activey
*/
public interface VueClass {

PropertyNameBuilder vueName();
NameBuilder vueName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class VueExtendMethodsDecorator {

private VueClass vueClass;

public static VueExtendMethodsDecorator fromExtend(VueClass vueClass) {
public static VueExtendMethodsDecorator fromClass(VueClass vueClass) {
return new VueExtendMethodsDecorator(vueClass);
}

Expand Down
Loading

0 comments on commit 4b22380

Please sign in to comment.