.
*/
require_once 'Compiler.php';
/**
* UWA compilation utilities.
*/
class Compiler_Uwa extends Compiler
{
/**
* Main rendering function.
*
* @return string
*/
public function render()
{
$style = $this->_widget->getStyle();
$script = $this->_widget->getCompressedScript();
$preferences = $this->_widget->getPreferences();
$l = array();
$l[] = '';
$l[] = '';
$l[] = '';
$l[] = '
';
$l[] = '' . $this->_widget->getTitle() . '';
$l[] = '';
foreach ($this->_widget->getMetadata() as $key => $value) {
$l[] = '';
}
$l[] = '';
$coreLibrary = $this->_widget->getCoreLibrary();
$externalScripts = $this->_widget->getExternalScripts();
if (empty($externalScripts) ) {
$library = 'http://' . NV_HOST . '/js/UWA/load.js.php?env=Standalone';
} else if ($coreLibrary == 'uwa') {
$library = 'http://' . NV_HOST . '/js/c/UWA_Standalone.js';
} else if ($coreLibrary == 'uwa-mootools') {
$library = 'http://' . NV_HOST . '/js/c/UWA_Standalone_Mootools.js';
}
$l[] = '';
foreach ($externalScripts as $javascript) {
$l[] = '';
}
if (isset($preferences) && count($preferences) > 0) {
$l[] = '';
foreach ($preferences as $pref) {
$l[] = $this->_getPreferenceXml($pref);
}
$l[] = '';
}
if (isset($style) && strlen($style) > 0) {
$l[] = '';
}
if (isset($script) && strlen($script) > 0) {
$l[] = '';
}
$l[] = '';
$l[] = '';
$l[] = $this->_widget->getBody();
$l[] = '';
$l[] = '';
return implode("\n", $l);
}
public function _getPreferenceXml($preference)
{
$preference = $preference->toArray();
$xml = " $value) {
if ($key != "options") {
$k = htmlspecialchars($key);
$v = htmlspecialchars($value);
$xml .= " $k=\"$v\"";
}
}
switch ($preference['type']) {
case 'list':
$xml .= ">\n";
if (isset($preference['options']) && count($preference['options']) > 0) {
foreach ($preference['options'] as $opt) {
$xml .= sprintf(" \n", $opt['value'], $opt['label']);
}
}
$xml .= "";
break;
default:
$xml .= "/>";
break;
}
return $xml;
}
/**
* Renders the controller within a JavaScript closure.
*/
public function renderJavaScriptRaw()
{
$l = array();
$l[] = "(function(){";
$l[] = $this->_widget->getCompressedScript();
$l[] = "widget.setMetas(" . $this->_widget->getMetadataJson() . ");";
$l[] = "widget.setPreferences(" . $this->_widget->getPreferencesJson() . ");";
$l[] = "})();";
return implode("\n", $l);
}
/**
* Renders the controller as a JavaScript class.
*/
public function renderJavaScriptFunction()
{
$l = array();
$l[] = 'if (typeof UWA == "undefined") var UWA = {};';
if (isset($this->options['uwaId'])) {
$l[] = 'if (typeof UWA.Scripts == "undefined") UWA.Scripts = [];';
$l[] = sprintf("UWA.Scripts['%s']=UWA.script=function(widget){", $this->options['uwaId']);
} else {
$l[] = "UWA.script=function(widget){";
}
$l[] = sprintf('widget.uwaUrl = %s;', Zend_Json::encode($this->_widget->getUrl()));
$l[] = $this->_widget->getCompressedScript();
$metas = $this->_widget->getMetas();
$l[] = sprintf("widget.setMetas(%s);", Zend_Json::encode($metas));
$preferences = $this->_widget->getPreferencesArray();
if (count($preferences) > 0) {
$l[] = sprintf("widget.setPreferences(%s);", Zend_Json::encode($preferences));
}
if (isset($this->options['platform']) && in_array($this->options['platform'], array('live', 'opera', 'dashboard'))) {
$body = $this->_widget->getBody();
if (!empty($body) && $body != 'Loading...
') {
$l[] = sprintf('widget.setBody(%s);', Zend_Json::encode($body));
}
}
$l[] = "return widget;";
$l[] = "}";
return implode("\n", $l);
}
/**
* Renders the controller as a JavaScript closure.
*/
public function renderJs()
{
return $this->renderJavaScriptFunction();
}
/**
* Renders the styles information.
*/
public function renderCss()
{
return $this->_widget->getStyle();
}
}