Skip to content

Commit

Permalink
- Hidden Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
alegauss committed Aug 25, 2018
1 parent 312f9d9 commit a46fe75
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 7 deletions.
46 changes: 45 additions & 1 deletion src/main/java/com/viglet/shiohara/component/ShFormComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import com.viglet.shiohara.persistence.model.post.type.ShPostType;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;
import com.viglet.shiohara.persistence.model.widget.ShWidget;
import com.viglet.shiohara.persistence.repository.post.type.ShPostTypeRepository;
import com.viglet.shiohara.persistence.repository.widget.ShWidgetRepository;
import com.viglet.shiohara.widget.ShSystemWidget;
import com.viglet.shiohara.widget.ShWidgetImplementation;

@Component
Expand All @@ -28,7 +31,9 @@ public class ShFormComponent {
private ApplicationContext applicationContext;
@Autowired
private SpringTemplateEngine templateEngine;

@Autowired
private ShWidgetRepository shWidgetRepository;

public String byPostType(String shPostTypeName, HttpServletRequest request)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
final Context ctx = new Context();
Expand All @@ -54,6 +59,45 @@ public int compare(ShPostTypeAttr o1, ShPostTypeAttr o2) {
fields.add(object.render(shPostTypeAttr));
}

String token = null;
if (csrf != null) {
token = csrf.getToken();
}

ctx.setVariable("token", token);
ctx.setVariable("shPostType", shPostType);
ctx.setVariable("shPostTypeAttrs", shPostType.getShPostTypeAttrs());
ctx.setVariable("fields", fields);

return templateEngine.process("form", ctx);
}

public String byPostTypeAndFolder(String shPostTypeName, String folderId, HttpServletRequest request)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
final Context ctx = new Context();

ShPostType shPostType = shPostTypeRepository.findByName(shPostTypeName);
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());

List<String> fields = new ArrayList<String>();

List<ShPostTypeAttr> postTypeAttrByOrdinal = new ArrayList<ShPostTypeAttr>(shPostType.getShPostTypeAttrs());

Collections.sort(postTypeAttrByOrdinal, new Comparator<ShPostTypeAttr>() {

public int compare(ShPostTypeAttr o1, ShPostTypeAttr o2) {
return o1.getOrdinal() - o2.getOrdinal();
}
});

for (ShPostTypeAttr shPostTypeAttr : postTypeAttrByOrdinal) {
String className = shPostTypeAttr.getShWidget().getClassName();
ShWidgetImplementation object = (ShWidgetImplementation) Class.forName(className).newInstance();
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
fields.add(object.render(shPostTypeAttr));
}


String token = null;
if (csrf != null) {
token = csrf.getToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public void createDefaultRows() {
if (shWidgetRepository.findAll().isEmpty()) {

ShWidget shWidget = new ShWidget();
shWidget.setName(ShSystemWidget.HIDDEN);
shWidget.setDescription("Hidden Widget");
shWidget.setClassName("com.viglet.shiohara.widget.ShHiddenWidget");
shWidget.setImplementationCode("template/widget/hidden/hidden.html");
shWidget.setSettingPath("template/widget/hidden/setting/hidden-setting.html");
shWidget.setType("TEXT,TEXTAREA");

shWidgetRepository.save(shWidget);

shWidget = new ShWidget();
shWidget.setName(ShSystemWidget.TEXT);
shWidget.setDescription("Text Widget");
shWidget.setClassName("com.viglet.shiohara.widget.ShTextWidget");
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/viglet/shiohara/widget/ShHiddenWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.viglet.shiohara.widget;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
public class ShHiddenWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {

String widgetSettings = shPostTypeAttr.getWidgetSettings();
JSONObject settings = new JSONObject(widgetSettings);
String defaultValue = settings.getString("defaultValue");

final Context ctx = new Context();
ctx.setVariable("shPostTypeAttr", shPostTypeAttr);
ctx.setVariable("defaultValue", defaultValue);

return templateEngine.process("widget/hidden/hidden-widget", ctx);
}

@Override
public boolean validateForm(HttpServletRequest request, ShPostTypeAttr shPostTypeAttr) {
// TODO Auto-generated method stub
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.viglet.shiohara.widget;

public class ShSystemWidget {
public static final String HIDDEN = "Hidden";
public static final String TEXT = "Text";
public static final String ACE_JS = "Ace Editor - Javascript";
public static final String ACE_HTML = "Ace Editor - HTML";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ shioharaApp.controller('ShPostEditCtrl', [
angular
.forEach($scope.shPost.shPostAttrs,
function(shPostAttr, key) {
//console.log("Type: " + shPostAttr.shPostTypeAttr.shWidget.type);
if (angular.equals(shPostAttr.shPostTypeAttr.shWidget.type, "JSON")) {
shPostAttr.strValue = JSON.parse(shPostAttr.strValue);
console.log(shPostAttr.strValue);
}
});

shPostAttr.strValue = JSON.stringify(shPostAttr.strValue);
if ( $scope.shPost.shFolder != null) {

$scope.folderId = $scope.shPost.shFolder.id;
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/js/server-side/shObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ viglet.shiohara.shObject = function() {
this.formComponent = function(shPostTypeName) {
return shFormComponent.byPostType(shPostTypeName, request);
},
/**
* @desc Returns Form from PostType And Folder
* @public
*/
this.formComponentByFolder = function(shPostTypeName, folderId) {
return shFormComponent.byPostTypeAndFolder(shPostTypeName, folderId, request);
},
/**
* @desc Returns Search Result
* @public
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type='hidden' ng-model="shPostAttr.strValue"></input>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table class="table" style="margin: 0px;">
<tr>
<td style="background: #F9F9F9; width: 20%;">
<p>
<b>Default Value</b>
</p>
</td>
<td><input type="text" class="form-control" ng-model="shPostTypeAttr.widgetSettingsObject.defaultValue"></td>
</tr>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="hidden" th:name="${'__sh-post-type-attr-' + shPostTypeAttr.name}" th:value="${defaultValue}">
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
name="parcelas" id="parcelas">

<option value="01" th:text="'1x de R$ ' + ${#numbers.formatDecimal(shProduct.value, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$ ' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
</select>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
name="parcelas" id="parcelas">

<option value="01" th:text="'1x de R$ ' + ${#numbers.formatDecimal(shProduct.value, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$ ' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
</select>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
name="parcelas" id="parcelas">

<option value="01" th:text="'1x de R$ ' + ${#numbers.formatDecimal(shProduct.value, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
<option value="02" th:text="'2x de R$ ' + ${#numbers.formatDecimal(shProduct.value/2, 0, 'POINT', 2, 'COMMA')}"></option>
</select>
</div>

0 comments on commit a46fe75

Please sign in to comment.