允许用户动态添加项目的自定义xtype不会正确显示输入字段

时间:2015-07-29 18:28:11

标签: cq5 aem

只是尝试进行自定义xtype开发。从以下教程开始:Adobe CQ help: Create your first custom xtype.稍微调整它以便我可以在两个字段中输入文本。这是我的代码:

/components/WeatherComponent02/clientlibs/CustomWidget.js

/*
 * This script is used to create a new xtype, which will be used to
 * provide the 'author' (as opposed to the end-user) the ability to
 * enter multiple links within the scope of the second weather component.
 *
 * This was taken from a online tutorial on creating a custom xtype in AEM.
 * The tutorial can be found at: https://helpx.adobe.com/
 * experience-manager/using/creating-custom-xtype.html
*/

var Ejst={};

Ejst.CustomWidget = CQ.Ext.extend(CQ.form.CompositeField, {

    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    hiddenField: null,

    /**
     * @private
     * @type CQ.Ext.form.ComboBox
     */
    allowField: null,

    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    otherField: null,

    constructor: function(config) {
        config = config || { };
        var defaults = {
            "border": false,
            "layout": "table",
            "columns":2
        };
        config = CQ.Util.applyDefaults(config, defaults);
        Ejst.CustomWidget.superclass.constructor.call(this, config);
    },

    // overriding CQ.Ext.Component#initComponent
    initComponent: function() {
        Ejst.CustomWidget.superclass.initComponent.call(this);

        this.hiddenField = new CQ.Ext.form.Hidden({
            name: this.name
        });
        this.add(this.hiddenField);

        /*
        this.allowField = new CQ.form.Selection({
            type:"select",
            cls:"ejst-customwidget-1",
            listeners: {
                selectionchanged: {
                    scope:this,
                    fn: this.updateHidden
                }
            },
            optionsProvider: this.optionsProvider
        });
        this.add(this.allowField);
        */
        this.allowField = new CQ.Ext.form.TextField({
            cls:"ejst-customwidget-1",
            listeners: {
                change: {
                    scope:this,
                    fn: this.updateHidden
                }
            }
        });
        this.add(this.allowField);


        this.otherField = new CQ.Ext.form.TextField({
            cls:"ejst-customwidget-2",
            listeners: {
                change: {
                    scope:this,
                    fn:this.updateHidden
                }
            }
        });
        this.add(this.otherField);

    },



    // overriding CQ.form.CompositeField#processPath
    processPath: function(path) {
        console.log("CustomWidget#processPath", path);
        this.allowField.processPath(path);
    },

    // overriding CQ.form.CompositeField#processRecord
    processRecord: function(record, path) {
        console.log("CustomWidget#processRecord", path, record);
        this.allowField.processRecord(record, path);
    },

    // overriding CQ.form.CompositeField#setValue
    setValue: function(value) {
        var parts = value.split("/");
        this.allowField.setValue(parts[0]);
        this.otherField.setValue(parts[1]);
        this.hiddenField.setValue(value);
    },

    // overriding CQ.form.CompositeField#getValue
    getValue: function() {
        return this.getRawValue();
    },

    // overriding CQ.form.CompositeField#getRawValue
    getRawValue: function() {
        if (!this.allowField) {
            return null;
        }
        return this.allowField.getValue() + "/" +
               this.otherField.getValue();
    },

    // private
    updateHidden: function() {
        this.hiddenField.setValue(this.getValue());
    }

});

// register xtype
CQ.Ext.reg('ejstcustom', Ejst.CustomWidget);

以下是WeatherComponent02.jsp的内容,它应该能够提取此特定自定义xtype中的条目,并填充视图。

/components/WeatherComponent02/WeatherComponent02.jsp

<% Node node = null;

//This app logic gets back all values the user entered into the custom xtype and
//writes out the values to the CQ web page - the number of fields is unknown since
//each custom xtype is located on a multi-field
if(resource.adaptTo(Node.class)!=null)
{

    node=resource.adaptTo(Node.class);

    PropertyIterator props=null;

    if (node.getProperties()!=null)
        props = node.getProperties();

    while (props.hasNext())
    {
        Property prop = props.nextProperty();
        String name = prop.getName();

        String value=null;
        String[] linkFields =null;

        if (prop.getDefinition().isMultiple() && (name.equalsIgnoreCase("multi")))
        {
            StringBuffer buf = new StringBuffer();

            //Get the values entered into the custom xtype values
            Value[] values = prop.getValues();
            for (int i = 0; i < values.length; i++)
            {
                buf.append(i > 0 ? "," : "");
                buf.append(values[i].getString());
            }

            value = buf.toString();
            String[] tokens = value.split(",");

            for (int i=0;i<tokens.length;i++)
            {

                linkFields = tokens[i].split("\\\\");
                for (int k=0; k<linkFields.length; k++)
                {


                    if (k==0){
                        /*
                         * format of linkFields[k]:
                         *      http://www.cnn.com/weather/Raleigh/CNN Weather
                         * where,
                         *      authored url: http://www.cnn.com/weather/Raleigh
                         *      authored display text: CNN Weather
                         *
                         * But somehow the component combines this two pieces of
                         * texts as: http://www.cnn.com/weather/Raleigh/CNN Weather
                         * Need to sort this out.
                         *
                         */

                        String[] words=linkFields[k].split("/");
                        String thisUrl="";
                        String display="";

                        /*
                         * WARNING: A hack to recreate the URL and Display Text.
                         *
                         * A serious flaw is that, the dialog box for this entries
                         * will still fail to display the url and display text
                         * originally entered by the author.
                         */

                        //form the url again
                        for(int j=0;j<words.length-1;j++){
                            thisUrl=thisUrl+words[j]+"/";
                        }

                        //get the text to be displayed
                        display=words[words.length-1];
                        %>

                        <!-- create the hyperlink with display text -->                        
                        <a href="<%= thisUrl %>" /> <%= display %> </a> <br>

                        <% continue;
                    }
                }
            }

        }
    }//end while

}//end if

%>

到目前为止一切都很好,一切都很甜蜜,我在视图中获得了所需的链接。但是,当作者回顾该组件对话框中的面板时,他/她将无法查看他/她的条目。附件是截图:

Manifestation of the bug while opening the dialog box

但是,最终视图本身显示还可以!即。

enter image description here

如果您有任何其他问题,请与我们联系。

1 个答案:

答案 0 :(得分:1)

Ahahh ..今天第二次回答我自己的问题.. :)你得到的东西太多了......实际上,这是AEM项目要求的一部分。

溶液: a)在setValue函数中写

parts=value.split("|");

b)在getRawValue中相似,

return ... +"/"+... ;

c)最后,在jsp文件中,用

替换相应的行
String[] words=linkFields[k].split("\\|"), 

这样做是用“|”字符而不是“/”作为在多字段中输入的单词的分隔符。以前,因为“/”被用作来自多字段的值的分隔符,所以我无法在文本框中呈现链接(即形式:http://www.cnn.com/weather)。现在,它们显示得很好。