gadget property, and populates Msg, UserPref and ViewParams dataContext
*
* @param Gadget $gadget
*/
public function setGadget(Gadget $gadget) {
$this->gadget = $gadget;
$this->dataContext['UserPrefs'] = $this->dataContext['ViewParams'] = $this->dataContext['Msg'] = array();
if (isset($this->gadget->gadgetSpec->locales)) {
foreach ($this->gadget->gadgetSpec->locales as $key => $val) {
$this->dataContext['Msg'][$key] = $val;
}
}
if (isset($this->gadget->gadgetSpec->userPrefs)) {
foreach ($this->gadget->gadgetSpec->userPrefs as $pref) {
$this->dataContext['UserPrefs'][$pref['name']] = isset($pref['value']) ? $pref['value'] : '';
}
}
if (isset($_GET['view-params'])) {
$viewParams = json_decode($_GET['view-params'], true);
if ($viewParams != $_GET['view-params'] && $viewParams) {
foreach ($viewParams as $key => $val) {
$this->dataContext['ViewParams'][$key] = $val;
}
}
}
}
/**
* If some templates could not be parsed, we paste the back into the html document
* so javascript can take care of them
*/
public function addTemplates($content) {
// If $this->gadget->gadgetSpec->templatesDisableAutoProcessing == true, unparsedTemplates will be empty, so the setting is ignored here
if (count($this->unparsedTemplates)) {
foreach ($this->unparsedTemplates as $key => $val) {
$content = str_replace("", $val . "\n", $content);
}
}
return $content;
}
/**
* This function parses the os-template and os-data script tags.
* It's of vital importance to call this function *before* the rewriteContent function
* since the html/dom parser of the later breaks, mangles and otherwise destroys the
* os template/data script tags. So we need to expand the templates to pure html
* before we can proceeed to dom parse the resulting document
*
* @param string $content html to parse
*/
public function parseTemplates($content) {
$osTemplates = array();
$osDataRequests = array();
// First extract all the os-data tags, and execute those in a single combined request, saves latency
// and is consistent with other server implementations
preg_match_all('/( node
$output = new DOMDocument(null, 'utf-8');
foreach ($childNode->childNodes as $node) {
$outNode = $output->importNode($node, true);
$outNode = $output->appendChild($outNode);
}
// Restore single tags to their html variant, and remove the xml header
$ret = str_replace(
array('', '
', ''),
array('', '
', ''),
$output->saveXML());
return $ret;
}
}
return false;
}
/**
* Rewrites the content, based on shindig's configuration (force_rewrite) and/or the gadget's
* spec params, it also injects the required html, css and javascript for the final gadget
* using the dom observer methods for the head and body
*
* @param unknown_type $content
* @return unknown
*/
public function rewriteContent($content) {
// Rewrite the content, this will rewrite resource links to proxied versions (if requested), sanitize if configured, and
// add the various javascript tags to the document
$rewriter = new GadgetRewriter($this->context);
$rewriter->addObserver('head', $this, 'addHeadTags');
$rewriter->addObserver('body', $this, 'addBodyTags');
return $rewriter->rewrite($content, $this->gadget, true);
}
/**
* Generates the body script content
*
* @return string script
*/
public function getBodyScript() {
$script = "gadgets.util.runOnLoadHandlers();";
if ($this instanceof GadgetHrefRenderer) {
$script .= "window.setTimeout(function(){gadgets.window.adjustHeight()}, 10);";
}
return $script;
}
/**
* Append the runOnLoadHandlers script to the gadget's document body
*
* @param DOMElement $node
* @param DOMDocument $doc
*/
public function addBodyTags(DOMElement &$node, DOMDocument &$doc) {
$script = $this->getBodyScript();
$scriptNode = $doc->createElement('script');
$scriptNode->setAttribute('type', 'text/javascript');
$scriptNode->appendChild($doc->createTextNode($script));
$scriptNode->nodeValue = str_replace('&', '&', $script);
$node->appendChild($scriptNode);
}
public function getJavaScripts() {
$registry = $this->context->getRegistry();
$forcedJsLibs = $this->getForcedJsLibs();
$forcedAppendJsLibs = Config::get('forcedAppendedJsLibs');
$externFeatures = $forcedJsLibs;
$inlineFeatures = array();
foreach ($this->gadget->features as $feature) {
if (! in_array($feature, $forcedJsLibs) &&
! in_array($feature, $forcedAppendJsLibs)) {
$inlineFeatures[] = $feature;
}
}
$sortedExternFeatures = array();
$sortedInlineFeatures = array();
$registry->sortFeatures($externFeatures, $sortedExternFeatures);
$registry->sortFeatures($inlineFeatures, $sortedInlineFeatures);
// append additional js libs from config to the end of the javascript block
// this allows custom overloading of other javascript libraries
foreach ($forcedAppendJsLibs as $jsLib) {
$sortedInlineFeatures[] = $jsLib;
}
// if some of the feature libraries are externalized (through a browser cachable