Customization of parsys to restrict number of compnents in CQ 5.6.1

  1. Copy the parsys component from(/libs/foundation/components/parsys) to your poroject(/apps/<project nmae>/components/).
     2.  In the design dialog add number filed configuration to enter the number of components.

           Crate node noofcomp of type cq:widget under /apps/<project nmae>/components/parsys
              /design_dialog/items.
           Add the following properties

           xtype : numberfield
           name : noOfComp
           maxValue : 20
           fieldLabel : No of components


     3.   Create ajax.jsp and write the below logic.
           <%@page import="com.day.cq.wcm.foundation.ParagraphSystem"%>
           <%@include file="/libs/foundation/global.jsp"%>
           <%
                 ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest);
                 int totalComponents=parSys.paragraphs().size();
                 int restrictCompoNo=Integer.parseInt(currentStyle.get("noOfComp","20"));

                if(totalComponents >= restrictCompoNo){
                      out.println("true");
                  }else{
                            out.println("false");
                    }
               %>

    4.  You need to overlay the EditBase.js and Sidekick.js

          Create the following structure   /apps/cq/ui/widgets/source/widgets/wcm

         then copy the EditBase.js and Sidekick.js form libs . Place under /wcm /
   
         In Sidekic.js at line 4098 in set timeout method add this snippet.

            var parentPath=editComponent.path;
            var ajaxUrl=parentPath.substr(0,parentPath.lastIndexOf("/"))+".ajax";
            var notAllowToCreate = CQ.HTTP.eval(ajaxUrl);
           
            if(!notAllowToCreate){
                editComponent.createParagraph(definition);

            }else{
                CQ.Ext.Msg.show({
                                   msg: 'You reached  the maximun limit. You can set the limit in disign mode.',
                                   buttons: CQ.Ext.Msg.OK,
                                   icon: CQ.Ext.MessageBox.WARNING
                                });

               }
                dropTarget.editComponent.hideTarget();


         In EditBase.js at line 1181  in set timeout method add this snippet.
       
           var parentPath=e.path;
            var ajaxUrl=parentPath.substr(0,parentPath.lastIndexOf("/"))+".ajax";
            var notAllowToCreate = CQ.HTTP.eval(ajaxUrl);    
            if(!notAllowToCreate){
                e.createParagraph(definition);

            }else{
                CQ.Ext.Msg.show({
                    msg: 'You reached  the maximun limit. You can set the limit in disign mode.',
                    buttons: CQ.Ext.Msg.OK,
                    icon: CQ.Ext.MessageBox.WARNING
                });
               
            }
            e.dialogs[CQ.wcm.EditBase.INSERT].hide();
            e.insertDialogMask.hide();


8 comments:

  1. Hi Subbu, Thank you for posting this useful tutorial. I have to implement smiler thing. Could you please tell me where I need to keep ajax.jsp? Thank you very much for your help!

    Elena

    ReplyDelete
    Replies
    1. Hi Elena Heath

      You have to create ajax.jsp in the below path

      /apps//components/parsys/ajax.jsp

      Delete
  2. Thanks ! It works like a charm !

    ReplyDelete
  3. Will this work in sightly as well?

    ReplyDelete
  4. Will this work for touchUI as well?

    ReplyDelete