Search in sources :

Example 6 with IonProperty

use of com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty in project convertigo by convertigo.

the class UIDynamicElement method initAttributes.

@Override
protected StringBuilder initAttributes() {
    StringBuilder attributes = super.initAttributes();
    IonBean ionBean = getIonBean();
    if (ionBean != null) {
        UIForm form = getUIForm();
        boolean underForm = form != null;
        Map<String, String> vm = new HashMap<String, String>();
        for (IonProperty property : ionBean.getProperties().values()) {
            String name = property.getName();
            Object value = property.getValue();
            if (!value.equals(false)) {
                MobileSmartSourceType msst = property.getSmartType();
                String smartValue = msst.getValue();
                if (Mode.PLAIN.equals(msst.getMode())) {
                    if (property.getType().equalsIgnoreCase("string")) {
                        if (!isComposedValue(smartValue)) {
                            smartValue = "\'" + MobileSmartSourceType.escapeStringForTpl(smartValue) + "\'";
                        }
                    }
                }
                vm.put(name, smartValue);
            } else {
                vm.put(name, "null");
            }
        }
        StrSubstitutor sub = new StrSubstitutor(vm, "$[", "]");
        sub.setEnableSubstitutionInVariables(true);
        for (IonProperty property : ionBean.getProperties().values()) {
            String name = property.getName();
            String attr = property.getAttr();
            Object value = property.getValue();
            boolean isComposite = property.isComposite();
            // case value is set
            if (!value.equals(false)) {
                if (attr.equals("class")) {
                    // already handle in initAttrClasses
                    continue;
                }
                if (name.equals("AutoDisable")) {
                    if (underForm) {
                        String formIdentifier = form.getIdentifier();
                        if (!formIdentifier.isBlank()) {
                            attributes.append(" [disabled]=\"").append(formIdentifier).append(".invalid\"");
                        }
                    }
                } else {
                    String smartValue = property.getSmartValue();
                    smartValue = sub.replace(smartValue);
                    if (!isComposite) {
                        attributes.append(" ");
                        if (attr.isEmpty()) {
                            attributes.append(smartValue);
                        } else if (attr.indexOf("%%") != -1) {
                            attributes.append(attr.replaceFirst("%%", smartValue));
                        } else {
                            attributes.append(attr).append("=");
                            attributes.append("\"").append(smartValue).append("\"");
                        }
                    }
                }
            } else // case value is not set
            {
                if (!isComposite) {
                    if (underForm) {
                        if (name.equals("DoubleBinding")) {
                            String defval = null;
                            if (tagName.equals("ion-checkbox") || tagName.equals("ion-toggle")) {
                                defval = vm.get("Checked");
                            } else {
                                defval = vm.get("Value");
                            }
                            if (defval != null && !defval.equals("null")) {
                                attributes.append(" [ngModel]=\"" + defval + "\"");
                            } else {
                                attributes.append(" ngModel");
                            }
                        }
                    }
                }
            }
        }
    }
    return attributes;
}
Also used : StrSubstitutor(org.apache.commons.text.StrSubstitutor) IonProperty(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty) HashMap(java.util.HashMap) IonBean(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 7 with IonProperty

use of com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty in project convertigo by convertigo.

the class UIDynamicElement method getFormatedLabel.

protected String getFormatedLabel() {
    if (ionBean != null) {
        String source = ionBean.getDisplayFormat();
        if (!source.isEmpty()) {
            String sourceName = "displayFormat", message = null;
            Context javascriptContext = org.mozilla.javascript.Context.enter();
            Scriptable scope = javascriptContext.initStandardObjects(null);
            for (IonProperty property : ionBean.getProperties().values()) {
                String p_name = property.getName();
                // Object p_value = property.getValue();
                String smartValue = property.getSmartType().getLabel().replace("{{", "").replace("}}", "").replace("?.", ".");
                Scriptable jsObject = ((smartValue == null) ? null : org.mozilla.javascript.Context.toObject(smartValue, scope));
                scope.put(p_name, scope, jsObject);
            }
            try {
                Object ob = RhinoUtils.evalInterpretedJavascript(javascriptContext, scope, source, sourceName, 1, null);
                if (ob instanceof Function) {
                    Object returnedValue = ((Function) ob).call(javascriptContext, scope, scope, new Object[] {});
                    return returnedValue.toString();
                }
            } catch (EcmaError e) {
                message = "Unable to evaluate code for '" + sourceName + "'.\n" + "UIDynamicElement: \"" + getName() + "\"\n" + "A Javascript runtime error has occured at line " + e.lineNumber() + ", column " + e.columnNumber() + ": " + e.getMessage() + " \n" + e.lineSource();
            } catch (EvaluatorException e) {
                message = "Unable to evaluate code for '" + sourceName + "'.\n" + "UIDynamicElement: \"" + getName() + "\"\n" + "A Javascript evaluation error has occured: " + e.getMessage();
            } catch (JavaScriptException e) {
                message = "Unable to evaluate code for '" + sourceName + "'.\n" + "UIDynamicElement: \"" + getName() + "\"\n" + "A Javascript error has occured: " + e.getMessage();
            } finally {
                if (javascriptContext != null) {
                    org.mozilla.javascript.Context.exit();
                }
                if (message != null) {
                    System.out.println(message);
                    Engine.logBeans.warn(message);
                }
            }
        }
    }
    return null;
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) IonProperty(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty) EcmaError(org.mozilla.javascript.EcmaError) EvaluatorException(org.mozilla.javascript.EvaluatorException) JSONObject(org.codehaus.jettison.json.JSONObject) Scriptable(org.mozilla.javascript.Scriptable) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 8 with IonProperty

use of com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty in project convertigo by convertigo.

the class UIDynamicElement method getContributor.

@Override
protected Contributor getContributor() {
    return new Contributor() {

        @Override
        public Map<String, String> getActionTsFunctions() {
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, String> getActionTsImports() {
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, File> getCompBeanDir() {
            return new HashMap<String, File>();
        }

        @Override
        public Map<String, String> getModuleTsImports() {
            Map<String, String> imports = new HashMap<String, String>();
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                Map<String, List<String>> map = ionBean.getConfig().getModuleTsImports();
                if (map.size() > 0) {
                    for (String from : map.keySet()) {
                        for (String component : map.get(from)) {
                            imports.put(component.trim(), from);
                        }
                    }
                }
            }
            return imports;
        }

        @Override
        public Set<String> getModuleNgImports() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgImports();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgProviders() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgProviders();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgDeclarations() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgDeclarations();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgComponents() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgComponents();
            }
            return new HashSet<String>();
        }

        @Override
        public Map<String, String> getPackageDependencies() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getPackageDependencies();
            }
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, String> getConfigPlugins() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                Map<String, String> map = ionBean.getConfig().getConfigPlugins();
                for (String plugin : map.keySet()) {
                    try {
                        JSONObject json = new JSONObject(map.get(plugin));
                        if (json.has("variables")) {
                            boolean hasChanged = false;
                            JSONObject jsonVars = json.getJSONObject("variables");
                            @SuppressWarnings("unchecked") Iterator<String> it = jsonVars.keys();
                            while (it.hasNext()) {
                                String varkey = it.next();
                                String varval = jsonVars.getString(varkey);
                                if (varval.startsWith("@")) {
                                    // value = @propertyName
                                    String propertyName = varval.substring(1);
                                    if (ionBean.hasProperty(propertyName)) {
                                        IonProperty ionProperty = ionBean.getProperty(propertyName);
                                        Object p_value = ionProperty.getValue();
                                        String value = "";
                                        if (!p_value.equals(false)) {
                                            MobileSmartSourceType msst = ionProperty.getSmartType();
                                            String smartValue = msst.getValue();
                                            if (Mode.PLAIN.equals(msst.getMode())) {
                                                value = smartValue;
                                            }
                                        }
                                        jsonVars.put(varkey, value);
                                        hasChanged = true;
                                    }
                                }
                            }
                            if (hasChanged) {
                                json.put("variables", jsonVars);
                                map.put(plugin, json.toString());
                            }
                        }
                    } catch (Exception e) {
                    }
                }
                return map;
            }
            return new HashMap<String, String>();
        }

        @Override
        public Set<String> getModuleNgRoutes(String pageSegment) {
            return new HashSet<String>();
        }
    };
}
Also used : IonProperty(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty) HashMap(java.util.HashMap) IonBean(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List) JSONObject(org.codehaus.jettison.json.JSONObject) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with IonProperty

use of com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty in project convertigo by convertigo.

the class UIDynamicAction method computeJsonModel.

@Override
public String computeJsonModel() {
    JSONObject jsonModel = new JSONObject();
    // if (isEnabled()) {
    try {
        jsonModel.put("in", new JSONObject().put("props", new JSONObject()).put("vars", new JSONObject())).put("out", new JSONObject());
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            JSONObject jsonProps = jsonModel.getJSONObject("in").getJSONObject("props");
            jsonProps.put("tplVersion", "");
            jsonProps.put("actionName", "");
            jsonProps.put("actionFunction", "");
            for (IonProperty property : ionBean.getProperties().values()) {
                jsonProps.put(property.getName(), "");
            }
        }
        JSONObject jsonVars = jsonModel.getJSONObject("in").getJSONObject("vars");
        Iterator<UIComponent> it = getUIComponentList().iterator();
        while (it.hasNext()) {
            UIComponent component = (UIComponent) it.next();
            if (component instanceof UIControlVariable) {
                UIControlVariable var = (UIControlVariable) component;
                jsonVars.put(var.getVarName(), "");
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // }
    return jsonModel.toString();
}
Also used : IonProperty(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty) JSONObject(org.codehaus.jettison.json.JSONObject) IonBean(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

IonProperty (com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty)9 IonBean (com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean)8 JSONObject (org.codehaus.jettison.json.JSONObject)8 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 HashMap (java.util.HashMap)2 JSONException (org.codehaus.jettison.json.JSONException)2 EvaluatorException (org.mozilla.javascript.EvaluatorException)2 JavaScriptException (org.mozilla.javascript.JavaScriptException)2 UIDynamicElement (com.twinsoft.convertigo.beans.ngx.components.UIDynamicElement)1 AbstractDialogCellEditor (com.twinsoft.convertigo.eclipse.property_editors.AbstractDialogCellEditor)1 NgxSmartSourcePropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.NgxSmartSourcePropertyDescriptor)1 StringComboBoxPropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor)1 InvalidSourceException (com.twinsoft.convertigo.engine.InvalidSourceException)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1