Add the Widget macro. This enables assets to be widgetized (easily embedded in

another page). Usage as such: ^Widget(assetId, width, height, templateId);
assetId is the ID of the asset to widgetize, width and height are the size of
the iframe, templateId is the template ID of the template to use for the widget
itself. This will pop up an icon that shows you some markup to put on another
page to embed the asset in widget form. If no template given, will use the
ajaxInlineView of the asset.
This commit is contained in:
Chris Nehren 2008-01-09 23:24:16 +00:00
parent 36b622622e
commit 81736fb322
6 changed files with 242 additions and 0 deletions

View file

@ -18,6 +18,10 @@
- Added migration utility for Gallery
- Removed old .tmpl files from upgrades. ALL templates in upgrades must now
be in Packages!
- add: widgetize asset macro. called like so: ^Widget(assetId, width,
height, templateId); where assetId is the asset of the widget to
templatize and templateId is the template for the widget itself. If
templateId isn't specified, uses ajaxInlineView.
7.4.20
- fix: Assets with no committed versions may be left as orphans when parent is purged

View file

@ -2575,6 +2575,65 @@ true.
sub isValidRssItem { 1 }
#-------------------------------------------------------------------
=head2 www_widgetView ( )
Returns the view() method of the asset object suitable for widgetizing.
=cut
sub www_widgetView {
my $self = shift;
my $session = $self->session;
my $style = $session->style;
return $session->privilege->noAccess() unless $self->canView;
my $templateId = $session->form->process('templateId');
if($templateId eq 'none') {
$self->prepareView;
}
else {
$self->prepareWidgetView($templateId);
}
$self->_outputWidgetJs;
return $self->view;
}
#-------------------------------------------------------------------
=head2 prepareWidgetView ( )
Prepares the widget view for this asset. Specifically, sets up some JS to
ensure that links selected / forms submitted in the widgetized form of the
asset open in a new window.
=cut
sub prepareWidgetView {
my $self = shift;
my $templateId = shift;
my $template = WebGUI::Asset::Template->new($self->session, $templateId);
my $session = $self->session;
my $extras = $session->config->get('extrasURL');
my $yahooDomJs = $extras . '/yui/build/yahoo-dom-event/yahoo-dom-event.js';
my $widgetJs = $extras . '/widgetLinkTargets.js';
$template->prepare;
$self->{_viewTemplate} = $template;
}
sub _outputWidgetJs {
my $self = shift;
my $session = $self->session;
my $extras = $session->config->get('extrasURL');
my $yahooDomJs = $extras . '/yui/build/yahoo-dom-event/yahoo-dom-event.js';
my $widgetJs = $extras . '/widgetLinkTargets.js';
$session->output->print("<script type='text/javascript' src='" . $yahooDomJs . "'></script>");
$session->output->print("<script type='text/javascript' src='" . $widgetJs . "'></script>");
}
1;

120
lib/WebGUI/Macro/Widget.pm Executable file
View file

@ -0,0 +1,120 @@
package WebGUI::Macro::Widget;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2007 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
#-------------------------------------------------------------------
sub process {
# get passed parameters
my $session = shift;
my $assetId = shift;
my $width = shift || 600;
my $height = shift || 400;
my $templateId = shift || 'none';
# Get location for CSS and JS files
my $conf = $session->config;
my $extras = $conf->get("extrasURL");
# add CSS and JS to the page
my $style = $session->style;
$style->setLink($extras."/yui/build/container/assets/container.css",{
rel=>"stylesheet",
type=>"text/css",
}
);
$style->setScript($extras."/wgwidget.js",{
type=>"text/javascript"
}
);
$style->setScript($extras."/yui/build/yahoo-dom-event/yahoo-dom-event.js",{
type=>"text/javascript"
}
);
$style->setScript($extras."/yui/build/container/container-min.js",{
type=>"text/javascript"
}
);
# construct the absolute URL
my $asset = WebGUI::Asset->new($session, $assetId);
my $fullUrl = "http://" . $conf->get("sitename")->[0] . $asset->getUrl;
# construct path to wgwidget.js
my $wgWidgetPath = 'http://' . $conf->get('sitename')->[0] . $extras . '/wgwidget.js';
# and yahoo-dom-event.js
my $yahooDomJsPath = 'http://' . $conf->get('sitename')->[0] . $extras . '/yui/build/yahoo-dom-event/yahoo-dom-event.js';
my $imgSrc = $extras . '/gear.png';
my $output = <<"EOHTML";
<script>
var codeGeneratorButton;
var handleButtonShow = function() {
codeGeneratorButton.show();
var tag = document.getElementById('jsWidgetCode');
tag.focus();
tag.select();
}
function initButton() {
var jsCode = "";
jsCode += "&lt;script type='text/javascript' src='$wgWidgetPath'&gt; &lt;/script&gt;";
jsCode += "&lt;script type='text/javascript'&gt;";
jsCode += "document.write(WebGUI.widgetBox.widget('$fullUrl', '$assetId', $width, $height, '$templateId')); &lt;/script&gt;";
// Instantiate the Dialog
codeGeneratorButton = new YAHOO.widget.SimpleDialog("codeGeneratorButton", {
width: "500px",
height: "200px",
fixedcenter: true,
visible: false,
draggable: true,
close: true,
text: "<textarea id='jsWidgetCode' rows='5' cols='50'>" + jsCode + "</textarea>",
icon: YAHOO.widget.SimpleDialog.ICON_INFO,
constraintoviewport: true,
modal: true,
zIndex: 9999,
buttons: [{text: "Dismiss", handler:dismissButton, isDefault: true}]
}
);
codeGeneratorButton.setHeader("Widget code");
// Render the Dialog
codeGeneratorButton.render(document.body);
YAHOO.util.Event.addListener("show", "click", handleButtonShow, codeGeneratorButton, true);
}
var dismissButton = function () {
this.hide();
}
YAHOO.util.Event.addListener(window, "load", initButton);
</script>
<!-- <button id="show">Get code for this content</button> -->
<a href="#" id="show"><img src="$imgSrc" /></a>
EOHTML
return $output;
}
1;

BIN
www/extras/gear.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 657 B

40
www/extras/wgwidget.js Executable file
View file

@ -0,0 +1,40 @@
var WebGUI = {
widgetBox : {
parentNodeId : null,
url : null,
widget : function( url, parentId, width, height, templateId ) {
if(url == "") {
return "<iframe scrolling='no'><body>No content available from "+url+"</body></iframe>";
}
if(width == undefined) {
width = 600;
}
if(height == undefined) {
height = 400;
}
this.url = url + "?func=widgetView&templateId=" + templateId;
this.parentNodeId = parentId;
this.markup = "";
this.markup += "<iframe scrolling='no' frameborder='0' id = '";
this.markup += this.parentNodeId;
this.markup += "' src='";
this.markup += this.url;
this.markup += "' width='";
this.markup += width;
this.markup += "' height='";
this.markup += height;
this.markup += "'>";
this.markup += "</iframe>";
return this.markup;
}
}
}

19
www/extras/widgetLinkTargets.js Executable file
View file

@ -0,0 +1,19 @@
var WebGUI = {
widgetBox : {
function retargetLinksAndForms() {
// get all the <a> elements, change their target appropriately
var allLinks = document.getElementsByTagName('a');
for(var i = 0; i < allLinks.length; i++) {
allLinks[i].target = '_blank';
}
// same for <form>s
var allForms = document.getElementsByTagName('form');
for(var i = 0; i < allForms.length; i++) {
allForms[i].target = '_blank';
}
}
}
};
YAHOO.util.Event.addListener(window, "load", WebGUI.widget.retargetLinksAndForms);