Skip to content

Instantly share code, notes, and snippets.

@rcurlette
Last active December 18, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcurlette/5831253 to your computer and use it in GitHub Desktop.
Save rcurlette/5831253 to your computer and use it in GitHub Desktop.
Readonly.js file containing code to enable [readonly] and [hide] functionality in Tridion Schemas
// Created by Robert Curlette, 18 June, 2013
// Not possible with support of Tridion StackExchange community and these posts:
// http://tridion.stackexchange.com/questions/1810/component-fields-available-in-gui-extension-with-fieldbuilder
// http://tridion.stackexchange.com/questions/1252/gui-extension-to-hide-field-in-2011-sp1
// http://tridion.stackexchange.com/questions/1380/make-schema-fields-read-only-in-tridion-2011-sp1
// http://tridion.stackexchange.com/questions/1915/how-to-get-component-metadata-fields-with-fieldbuilder
var $j = jQuery.noConflict();
if ($display)
{
(function()
{
$evt.addEventHandler($display, "start", Extension$onDisplayStartedReadonly);
function Extension$onDisplayStartedReadonly()
{
$evt.removeEventHandler($display, "start", Extension$onDisplayStartedReadonly);
var view = $display.getView();
if (view && Tridion.OO.implementsInterface(view, "Tridion.Cme.Views.Component"))
{
var fieldBuilder = view.properties.controls.fieldBuilder;
$evt.addEventHandler(fieldBuilder, "load", Extension$onDisplayStartedReadonly$_collectProperties);
}
};
function Extension$onDisplayStartedReadonly$_collectProperties(event)
{
var fieldBuilder = event && event.source;
if (fieldBuilder)
{
// General Content fields
var fieldsContainer = fieldBuilder.properties.input;
var genFieldsNode = fieldsContainer.getElement();
processFields(genFieldsNode);
// Metadata fields
var metaFieldBuilder = GetMetadataFieldbuilder();
var metaFieldsContainer = metaFieldBuilder.properties.input;
var metaFieldsNode = metaFieldsContainer.getElement();
processFields(metaFieldsNode);
}
};
// Disable fields with [readonly] or hide fields with [hide] in the field description
function processFields(fieldsNode)
{
$j(fieldsNode).children().each(function (index, elm)
{
var cntrl = $j('div.input', elm)[0].control;
var a = $j(cntrl.getElement()).parent();
// Read-only for fields containing [readonly] in the field description
if ($j(a.children()[0]).text().indexOf("[readonly]") >= 0)
{
a.children()[1].children[0].disabled = true;
}
// Hide with fields containing [hide] in the field description
if ($j(a.children()[0]).text().indexOf("[hide]") >= 0)
{
$j(cntrl.getElement()).parent().hide();
}
});
}
// Method created by Alex Klock and provided as an answer in this StackExchange post, http://tridion.stackexchange.com/questions/1915/how-to-get-component-metadata-fields-with-fieldbuilder
function GetMetadataFieldbuilder()
{
var fieldBuilder;
var tabs = $display.getView().properties.controls.TabControl,
cardCount = tabs.properties.cards.length,
fieldBuilder,
card;
while (cardCount--) {
card = tabs.properties.cards[cardCount];
if (card.getId() === "MetadataTab") {
fieldBuilder = card.properties.controls.fieldBuilder;
}
}
return fieldBuilder;
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment