Monday, November 12, 2012

Color Picker in Liferay

jQuery :
---------
Using Lifery provided Color Picker in Own Portlets :

Liferay provides jQuery Script Color Picker, Steps to reuse the Liferay color picker in your own portlets:
Syntax:
<script type="text/javascript"> jQuery( function() { var colorPicker = new Liferay.ColorPicker( { option, option... } ); } ); </script>

Options:
context (Object): A DOM object which specifies the context in which to search for the item
hasImage: (Boolean) If set to true, it uses the "item" param or whatever image has the .use-color-
picker class as the image
item: (Object|String): A DOM object or a jQuery Selector string that specifies which field to insert the
selected value into
onChange (Function): A function that will get called whenever the color changes
onClose (Function): A function that will get called when the color picker is closed.

(Ref: How to use Liferay's Color Picker in own portlets Wiki)
Implementation:
Copy the Jquery Function in your Html file:
<script type="text/javascript"> 
 jQuery( function() { 
   var colorPicker = new Liferay.ColorPicker( {
     hasImage: true
     } 
    ); 
  } 
 ); 
</script>

(hasImage: true) Option is mandatory to use Color Picker in own portlets.

In Html file, copy the below input text field syntax:
<tr> <td> 
<input class="text-input" id="<portlet:namespace />color" name="<portlet:namespace />color" size="9" type="text" value="" />
<img alt="<liferay-ui:message key="color" />" border="0" class="use-colorpicker" height="18" hspace="10" src="<%= themeDisplay.getPathThemeImages() %>/color_picker.png" title="<liferay-ui:message key="color" />" vspace="0" width="18" style="cursor: pointer;" />
 </td> </tr>

Note:<Img> should have class="use-colorpicker" Css class.

AUI
-----
For Liferay 6 onwords...
You have to use AUI.
<script type="text/javascript">
AUI().ready(
  'aui-color-picker',
  function(A) {
   var colorText = A.one('#<portlet:namespace />color');
   window.ColorPicker = new A.ColorPicker({
    after: {
     colorChange: function(val) {
      var hex = '#' + this.get('hex');
      colorText.set('value', hex);
     }
    }
   }).render('#demo');
  }
 );
</script>

<tr> <td> 
<input class="text-input" id="<portlet:namespace />color" name="<portlet:namespace />color" size="9" type="text" value="" /><div id="demo"></div>
 </td> </tr>

No comments:

Post a Comment