[Go back to tutorial home](tutorial.md) # Step 8: Create your own palette *(Estimated time: 15mn)* Step after step we've added icons in the Pawn activity toolbar. In this new step we're going to improve our toolbar by exploring the creation of "Palette". In [step 6](tutorial_step6.md) of this tutorial, we've added a specific icon in the toolbar for the "Presence Palette". In the Sugar UI, the name "palette" refer to a popup menu in the toolbar. When the user click on the toolbar icon, the popup appears and shows items inside. ![](images/tutorial_step8_1.png) As we've seen briefly in [step 6](tutorial_step6.md#add-the-presence-palette), to handle this feature Sugar-Web exposes a Palette library. Let's see how we could use this library to create our own palette. ### Create the palette file To create a new palette, you should first create a new palette file. This file will contain the source code to handle palette behavior. So, create a new file named `pawnpalette.js` in the `Pawn.activity/lib` directory. ***Warning***: *The file should not be created in the `Pawn.activity/src` because palettes are considered to be new libraries.* Here's the source code to insert into the new Pawn palette file: define(["sugar-web/graphics/palette"], function(palette) { var pawnpalette = {}; pawnpalette.PawnPalette = function(invoker, primaryText) { palette.Palette.call(this, invoker, primaryText); var template = 'Hello palette!'; var containerElem = document.createElement('div'); containerElem.innerHTML = template; this.setContent([containerElem]); }; var addEventListener = function(type, listener, useCapture) { return this.getPalette().addEventListener(type, listener, useCapture); }; pawnpalette.PawnPalette.prototype = Object.create(palette.Palette.prototype, { addEventListener: { value: addEventListener, enumerable: true, configurable: true, writable: true } }); return pawnpalette; }); As we've already seen in [step 2](tutorial_step2.md#customize-content), the `define` function is a way to define a new module and express its dependencies. Here we're going to define a new module that depends on the JavaScript library `sugar-web/graphics/palette`. This `palette` library is precisely the base class for all Palette objects. So, our new module create a new `PawnPalette` object and initialize it by calling the Palette constructor with `palette.Palette.call`. You don't need to understand in detail how it works but the important part here is that the Palette content should be set by a call of the `palette.setContent` function. Here, the content is just a HTML div element with a simple string `'Hello palette!'` inside. In most complex palettes, the content could come from a HTML file or could be a generated set of items. ### Integrate the palette in toolbar Let's now integrate our new palette in the toolbar. It could be done by adding a new button in the toolbar like we've done in [step 3](tutorial_step3.md#Create-a-new-toolbar-button). But here we're going to redefine the behavior for the current **Add** button instead. To do that, let's update our `js/activity.js` file. As usual, to integrate new features, we will first update the dependancies list of libraries at the first line of the file. define(["sugar-web/activity/activity", ... ,"sugar-web/graphics/journalchooser","pawnpalette"], function (activity, ... ,presencepalette, datastore, journalchooser, pawnpalette) { This time you need to add the `pawnpalette` library created before. Add the string `"pawnpalette"` at the end of dependancies array and declare a new `pawnpalette` variable at the end of the function declaration. Let's now create the palette and associate it to the icon. Add the following code just before the `addEventListener('click',...)` call for the add button: var addpalette = new pawnpalette.PawnPalette(document.getElementById("add-button"), "Add pawn"); This source code call create the `PawnPalette` object and call the constructor with two parameters: * the first one is the toolbar button to associate the palette. As we said, we're reusing our **Add** button. * the second one is the header for the palette. It's an optional parameter used to display a label at the begining of the palette to explain the palette objective to the user. Here we set `"Add pawn"`. Let's run the Pawn activity. Now, when you click on the add toolbar button the palette is displayed. ![](images/tutorial_step8_2.png) Funny isn't it? As an exercise, look what happens when you do not provide a second parameter to the constructor. ### Customize the palette content Now that we've seen basic concepts of palette, it's time to improve the UI for our custom palette. Specifically, we're going to update our palette to allow users to add between 1 and 3 pawns at the same time. Here's the final look we would like to obtain: ![](images/tutorial_step8_3.png) We would like to have 3 items each one that give opportunity to users to add a different number of pawns. To explore palette features each items will have a different UI: a text, an image, a text and an image combined. To do that, we're going to use a HTML file to describe the palette UI. So let's create a new file named `pawnpalette.html` in the `Pawn.activity/lib` directory, at the same place of the `pawnpalette.js`. Put the content above in this new file: