Obsolete Pages{{Obsolete}}
The official documentation is at: http://docs.alfresco.com
Components consist of at least one WebScript (Freemarker template plus optional JavaScript logic), plus optional client-side stylesheet(s) and JavaScript files. They may also reference assets such as images, Adobe Flash movies and sounds.
30ComponentStandards_FileStructure.png
Component WebScripts follow the same standard format as other V3.0 WebScripts with one notable exception: an optional head script which is injected into the <head> part of a template's page. The head script is generally used to include client-side JavaScript (.js) and StyleSheet (.css) assets, but can contain any other HTML mark-up normally reserved for the <head> block. Note that different browsers may treat duplicate tags unpredictably, e.g. if the final rendered page contains two <title> tags.
The html script should contain inline JavaScript initialization code, to instantiate any client-side JavaScript objects the component relies upon. Typically for Share, this involves passing the DOM ID generated by the framework, runtime options such as URL arguments and the i18n message bundle (.properties' script)' in JSON format. To preserve the validity of XHTML mark-up, client-side JavaScript should be wrapped in CDATA blocks:
<script type='text/javascript'>//</script>
It is very important for component authors to allow their components to be instantiated more than once per page. This means making sure no DOM IDs are used in the WebScript body, unless prefixed with the Framework-supplied htmlid, for example
...and also creating JavaScript objects with the new directive, rather than statically.
<webscript>
<shortname>Alfresco SampleComponent</shortname>
<description>Alfresco Samples: Sample Component</description>
<url>/components/alfresco/sample</url>
</webscript>
<link rel='stylesheet' type='text/css' href='${url.context}/components/alfresco/sample.css' />
<script type='text/javascript' src='${url.context}/components/alfresco/sample.js'></script>
<script type='text/javascript'>//</script>
message.welcome=The sample component loaded and is working as it should
/*
* Copyright (C) 2005-2008 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ('FLOSS') applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing
*/
/*
* Alfresco.SampleComponent component logic.
* @namespace Alfresco
* @class Alfresco.SampleComponent
*/
(function()
{
/**
* YUI Library aliases
*/
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;
/**
* Alfresco Share aliases
*/
var $html = Alfresco.util.encodeHTML;
/**
* Alfresco.SampleComponent constructor.
* @return {Alfresco.SampleComponent} the new component instance
* @constructor
*/
Alfresco.SampleComponent = function SampleComponent_constructor(htmlId)
{
/* Standard component properties */
this.name = 'Alfresco.SampleComponent';
this.id = htmlId;
/* Register with the ComponentManager */
Alfresco.util.ComponentManager.register(this);
/* Request YUI Components */
Alfresco.util.YUILoaderHelper.require(['container'], this.onComponentsLoaded, this);
return this;
};
Alfresco.SampleComponent.prototype =
{
/**
* Default runtime options for this component
* @property options
* @type object
*/
options:
{
/**
* Sample test string
*
* @property testString
* @type string
* @default ''
*/
testString: ''
},
/**
* Allows multiple runtime options to be set simultaneously
* @method setOptions
* @param obj {object} options to override form defaults
* @return {Alfresco.SampleComponent} returns 'this' for method chaining
*/
setOptions: function SampleComponent_setOptions(obj)
{
/* Use a YUI helper function to override default options */
this.options = YAHOO.lang.merge(this.options, obj);
return this;
},
/**
* Set messages for this component.
*
* @method setMessages
* @param obj {object} Object literal specifying a set of messages
* @return {Alfresco.SampleComponent} returns 'this' for method chaining
*/
setMessages: function SampleComponent_setMessages(obj)
{
Alfresco.util.addMessages(obj, this.name);
return this;
},
/**
* Fired by YUILoaderHelper when required component script files have
* been loaded into the browser.
* @method onComponentsLoaded
*/
onComponentsLoaded: function SampleComponent_onComponentsLoaded()
{
/* Register the onReady callback when the component parent element has been loaded */
Event.onContentReady(this.id, this.onReady, this, true);
},
/**
* Fired by YUI when parent div has been constructed and ready for use.
* @method onReady
*/
onReady: function SampleComponent_onReady()
{
/* TODO: Initialisation code, e.g. create YUI components. */
var sampleDiv = Dom.get(this.id + '-sample');
if (sampleDiv !== null)
{
sampleDiv.innerHTML = $html(this.options.testString);
}
Alfresco.util.PopupManager.displayMessage(
{
text: this._msg('message.welcome', this.name)
});
},
/**
* Gets a custom message
*
* @method _msg
* @param messageId {string} The messageId to retrieve
* @return {string} The custom message
* @private
*/
_msg: function SampleComponent__msg(messageId)
{
return Alfresco.util.message.call(this, messageId, 'Alfresco.SampleComponent', Array.prototype.slice.call(arguments).slice(1));
}
};
})();
A Module is a variant of a Component. The benefit of creating a module instead of a component is that it decreases the page processing and page load on the user's browser and the processing on the server. A lot of pages in Share provides dialogs or popups that may never be used before the user browses to another page. To avoid unnecessary page loading only parts of the modules are loaded into the page in it's initial state. If the user decides to use the functionality from a module, the rest of the functionality for that module will be loaded into the current page and then presented to the user.
A good example of this is the Create Site dialog module in the Alfresco Share application. The Create Site functionality is accessible anywhere in the share application from the Header component's 'Sites'-menu. Since the Create Site dialog only will be used a fraction of the times it could be, it is written as a module instead of a component.
The following section describes how a module is used, let's continue to use the Create Site module and Header component as an example:
Alfresco.module.getCreateSiteInstance().show();
By using this approach neither the server or the user has to bother with loading the module's template and i18n messages for every page. The only thing that needs to be loaded is the javascript and the css file, which will be cached in the browser anyway and not cause any additional page load.
Hopefully the basics of a module is quite clear at this point, to clarify a few things here are the main differences between a module and a component.
<script type='text/javascript'>new Alfresco.Header('${args.htmlid}')</script>
Alfresco.module.getCreateSiteInstance()
<script type='text/javascript'>new Alfresco.Header('${args.htmlid}').setMessages(${messages});</script>
<script type='text/javascript'>Alfresco.util.addMessages(${messages}, 'Alfresco.module.CreateSite');</script>
Note! When the Create Site javascript instance loads the template from the server it will pass in the following attribute to make sure the scripts inside the template will get run in all browser.
Alfresco.util.Ajax.request({
url: Alfresco.constants.URL_SERVICECONTEXT + 'modules/create-site',
...
execScripts: true
});
Note! Even thought the Create Site template doesn't do it, it is possible for it to find it's javascript instance by making a call to the Alfresco component registry.
var createSiteInstance = Alfresco.util.ComponentManager.find({id: '${args.htmlid}'})[0];
<webscript>
<shortname>Alfresco SampleModule</shortname>
<description>Alfresco Samples: Sample Module</description>
<url>/modules/alfresco/sample</url>
</webscript>
A module has no head file, instead it's dependencies such as javascript and css files must be imported in the head file for the component that uses the module.
${msg('header.welcome')}
${msg('label.about')}
<script type='text/javascript'>//</script>
header.welcome=Welcome to the Sample Module
label.about=This is a sample of a module to demonstrate how modules can be used to decrease the page load.
message.templateFailure=Could not load template for sample module
/**
* Copyright (C) 2005-2008 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ('FLOSS') applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing
*/
/**
* Sample module
*
* A sample module
*
* @namespace Alfresco.module
* @class Alfresco.module.SampleModule
*/
(function()
{
/**
* YUI Library aliases
*/
var Dom = YAHOO.util.Dom,
Event = YAHOO.util.Event;
/**
* Alfresco Share aliases
*/
var $html = Alfresco.util.encodeHTML;
/**
* Sample module constructor.
*
* @param htmlId {string} A unique id for this module
* @return {Alfresco.module.SmapleModule The new SampleModule instance
* @constructor
* @private
*/
Alfresco.module.SampleModule = function(containerId)
{
this.name = 'Alfresco.module.SampleModule';
this.id = containerId;
this.widgets = {};
var instance = Alfresco.util.ComponentManager.find(
{
id: this.id
});
if (instance !== undefined && instance.length > 0)
{
throw new Error('An instance of Alfresco.module.SampleModule already exists.');
}
// Register this component
Alfresco.util.ComponentManager.register(this);
// Load YUI Components
Alfresco.util.YUILoaderHelper.require(['container'], this.onComponentsLoaded, this);
return this;
};
Alfresco.module.SampleModule.prototype =
{
/**
* Object container for storing YUI widget instances.
*
* @property widgets
* @type object
*/
widgets: null,
/**
* Fired by YUILoaderHelper when required component script files have
* been loaded into the browser.
*
* @method onComponentsLoaded
*/
onComponentsLoaded: function SM_onComponentsLoaded()
{
// Don't do anything since the template isn't loaded at this point
},
/**
* Shows the SampleModule dialog to the user.
*
* @method show
*/
show: function SM_show()
{
if (this.widgets.panel)
{
/**
* The panel gui has been showed before and its gui has already
* been loaded and created
*/
this._showPanel();
}
else
{
/**
* Load the gui from the server and let the templateLoaded() method
* handle the rest.
*/
Alfresco.util.Ajax.request(
{
url: Alfresco.constants.URL_SERVICECONTEXT + 'modules/sample-module',
dataObj:
{
htmlid: this.id
},
successCallback:
{
fn: this.onTemplateLoaded,
scope: this
},
execScripts: true,
failureMessage: Alfresco.util.message('message.templateFailure', this.name)
});
}
},
/**
* Called when the SampleModule html template has been returned from the server.
* Creates the YUI panel and shows it.
*
* @method onTemplateLoaded
* @param response {object} a Alfresco.util.Ajax.request response object
*/
onTemplateLoaded: function SM_onTemplateLoaded(response)
{
// Inject the template from the XHR request into a new DIV element
var containerDiv = document.createElement('div');
containerDiv.innerHTML = response.serverResponse.responseText;
// The panel is created from the HTML returned in the XHR request, not the container
var panelDiv = Dom.getFirstChild(containerDiv);
this.widgets.panel = new YAHOO.widget.Panel(panelDiv,
{
modal: true,
draggable: false,
fixedcenter: true,
close: false,
visible: false
});
// Add the panel to the Dom
this.widgets.panel.render(document.body);
// Show the panel
this._showPanel();
},
/**
* Prepares the gui and shows the panel.
*
* @method _showPanel
* @private
*/
_showPanel: function CS__showPanel()
{
// Show the upload panel
this.widgets.panel.show();
}
};
})();
Alfresco.module.getSampleModuleInstance = function()
{
var instanceId = 'alfresco-sampleModule-instance',
instance = Alfresco.util.ComponentManager.find(
{
id: instanceId
});
if (instance !== undefined && instance.length != 0)
{
instance = instance[0];
}
else
{
instance = new Alfresco.module.SampleModule(instanceId);
}
return instance;
};
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.