I’ve been keeping a little notepad file on my machine for a while some of common code snippets that help when building SDL Tridion Gui / Alchemy extensions. I thought it would be great to share them with the community in the hope that they can help someone, and others can contribute.
Please leave comments with your snippets and i’ll add them to the list (and give you credit!).
Anquilla
1. Items
1.1 Getting an item / items
var items = p.items; if (items != null) { var totalItems = items.length; for (var i = 0; i < totalItems; i++) { var itemId = items[i]; var item = $models.getItem(itemId); if (item) { // examples uses $models.getItemType(items[i]) item.getStaticTitle() '); }
1.2 Getting an item type:
switch ($models.getItemType(items[i])) { case $const.ItemType.FOLDER: //return true; case $const.ItemType.COMPONENT: //return true; case $const.ItemType.PAGE: //return true; case $const.ItemType.STRUCTUREGROUP: //return true; case $const.ItemType.KEYWORD: //return true; } var icon = item.getItemIcon();
1.3 Is Shared
console.log("Item is shared: " + item.isShared());
If an item is shared it is not local to this publication
1.4 Item Types
console.log("item type: " + item.getItemType());
This will output:
item type: tcm:16
1.5 If the item is of a particular type (often used in IsEnabled in the Command)
if (item != null) { switch ($models.getItemType(item)) { case $const.ItemType.PUBLICATION: return true; } } return false;
Pop up
Here’s the typical code in the command to create a Tridion popup window
var p = this.properties; var selectedItem = this._getSelectedItem(selection); var url = "/Path/File.aspx#selectedItem=" + selectedItem.replace("tcm:", ""); var parameters = "width=800px, height=600px,scrollbars=1"; var args = { popupType: Tridion.Controls.PopupManager.Type.EXTERNAL }; p.popup = $popupManager.createExternalContentPopup(url, parameters, args); $evt.addEventHandler(p.popup, "close", this.getDelegate(this.closePopup)); p.popup.open();
Note: Uses the method shown below in 1.6
1.6 Selecting an item
There’s two options here, if you want to select only a single item, or multiple items:
_getSelectedItems: function (selection) { $assert.isObject(selection); switch (selection.getCount()) { case 0: return (selection.getParentItemUri) ? selection.getParentItemUri() : null; case 1: return selection.getItems(); default: return selection.getItems(); } }, _getSelectedItem: function (selection) { $assert.isObject(selection); switch (selection.getCount()) { case 0: return (selection.getParentItemUri) ? selection.getParentItemUri() : null; case 1: return selection.getItem(0); default: return null; } },
2.0 Useful API functionality
2.1 $url
$url.getHashParam("your querystring value");