From 964d8cf9e24d92cdac847d177fde5a7bbcc74135 Mon Sep 17 00:00:00 2001 From: Yung Han Khoe Date: Sat, 4 Apr 2009 17:10:00 +0000 Subject: [PATCH] added Carousel wobject --- lib/WebGUI/Asset/Wobject/Carousel.pm | 335 ++++++++++++++++++++++ lib/WebGUI/i18n/English/Asset_Carousel.pm | 38 +++ www/extras/wobject/Carousel/carousel.js | 38 +++ 3 files changed, 411 insertions(+) create mode 100644 lib/WebGUI/Asset/Wobject/Carousel.pm create mode 100644 lib/WebGUI/i18n/English/Asset_Carousel.pm create mode 100644 www/extras/wobject/Carousel/carousel.js diff --git a/lib/WebGUI/Asset/Wobject/Carousel.pm b/lib/WebGUI/Asset/Wobject/Carousel.pm new file mode 100644 index 000000000..6ddce2e3d --- /dev/null +++ b/lib/WebGUI/Asset/Wobject/Carousel.pm @@ -0,0 +1,335 @@ +package WebGUI::Asset::Wobject::Carousel; + +$VERSION = "1.0.0"; + +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 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; +use warnings; +use JSON; +use Tie::IxHash; +use WebGUI::International; +use WebGUI::Utility; +use base 'WebGUI::Asset::Wobject'; + +#------------------------------------------------------------------- + +=head2 definition ( ) + +defines wobject properties for New Wobject instances. You absolutely need +this method in your new Wobjects. If you choose to "autoGenerateForms", the +getEditForm method is unnecessary/redundant/useless. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = WebGUI::International->new($session, 'Asset_Carousel'); + my %properties; + tie %properties, 'Tie::IxHash'; + %properties = ( + templateId =>{ + fieldType =>"template", + defaultValue =>'CarouselTmpl0000000001', + tab =>"display", + noFormPost =>0, + namespace =>"Carousel", + hoverHelp =>$i18n->get('carousel template description'), + label =>$i18n->get('carousel template label'), + }, + items =>{ + noFormPost =>1, + fieldType =>'text', + autoGenerate =>0, + }, + ); + push(@{$definition}, { + assetName=>$i18n->get('assetName'), + icon=>'Carousel.gif', + autoGenerateForms=>1, + tableName=>'Carousel', + className=>'WebGUI::Asset::Wobject::Carousel', + properties=>\%properties + }); + return $class->SUPER::definition($session, $definition); +} + + +#------------------------------------------------------------------- + +=head2 duplicate ( ) + +duplicates a New Wobject. This method is unnecessary, but if you have +auxiliary, ancillary, or "collateral" data or files related to your +wobject instances, you will need to duplicate them here. + +=cut + +sub duplicate { + my $self = shift; + my $newAsset = $self->SUPER::duplicate(@_); + return $newAsset; +} + +#------------------------------------------------------------------- + +=head2 getEditForm ( ) + +returns the tabform object that will be used in generating the edit page for New Wobjects. +This method is optional if you set autoGenerateForms=1 in the definition. + +=cut + +sub getEditForm { + my $self = shift; + my $tabform = $self->SUPER::getEditForm(); + + $self->session->style->setScript($self->session->url->extras('yui/build/editor/editor-min.js'), {type => + 'text/javascript'}); + $self->session->style->setLink($self->session->url->extras('yui/build/editor/assets/skins/sam/editor.css'), {type + =>'text/css', rel=>'stylesheet'}); + $self->session->style->setScript($self->session->url->extras('wobject/Carousel/carousel.js'), {type => + 'text/javascript'}); + + my $tableRowStart = qq| + + + +

+ |; + + $tabform->getTab("properties")->raw($tableRowStart); + + if($self->getValue('items')){ + my @items = @{JSON->new->decode($self->getValue('items'))->{items}}; + + foreach my $item (@items){ + my $itemHTML = 'ID: ' + .'
\n"; + + $itemHTML .= + " \n"; + $tabform->getTab("properties")->raw($itemHTML); + } + } + else{ + my $itemHTML = 'ID: ' + .'
\n"; + + $itemHTML .= + "\n"; + $tabform->getTab("properties")->raw($itemHTML); + } + my $tableRowEnd = qq| + + + |; + $tabform->getTab("properties")->raw($tableRowEnd); + + return $tabform; +} + +#------------------------------------------------------------------- + +=head2 prepareView ( ) + +See WebGUI::Asset::prepareView() for details. + +=cut + +sub prepareView { + my $self = shift; + $self->SUPER::prepareView(); + #$self->session->errorHandler->warn('templateId: '.$self->get("parentId")); + my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateId")); + $template->prepare; + $self->{_viewTemplate} = $template; +} + +#------------------------------------------------------------------- + +=head2 processPropertiesFromFormPost ( ) + +Used to process properties from the form posted. + +=cut + +sub processPropertiesFromFormPost { + my $self = shift; + my $form = $self->session->form; + my (@items,$items); + + foreach my $param ($form->param) { + if ($param =~ m/^item_/){ + my $sequenceNumber = $param; + $sequenceNumber =~ s/^item_//; + if($form->process('itemId_'.$sequenceNumber)){ + push(@items,{ + sequenceNumber => $sequenceNumber, + text => $form->process($param), + itemId => $form->process('itemId_'.$sequenceNumber), + }); + } + } + } + + my @sortedItems = sort { $a->{sequenceNumber} cmp $b->{sequenceNumber} } @items; + @items = (); + for (my $i=0; $i{sequenceNumber} = $i + 1; + push(@items,$sortedItems[$i]); + } + + $items = JSON->new->encode({items => \@items}); + $self->update({items => $items}); + return undef; +} + +#------------------------------------------------------------------- + +=head2 purge ( ) + +removes collateral data associated with a Carousel when the system +purges it's data. This method is unnecessary, but if you have +auxiliary, ancillary, or "collateral" data or files related to your +wobject instances, you will need to purge them here. + +=cut + +sub purge { + my $self = shift; + #purge your wobject-specific data here. This does not include fields + # you create for your Carousel asset/wobject table. + return $self->SUPER::purge; +} + +#------------------------------------------------------------------- + +=head2 view ( ) + +method called by the www_view method. Returns a processed template +to be displayed within the page style. + +=cut + +sub view { + my $self = shift; + my $session = $self->session; + my (@item_loop); + + #This automatically creates template variables for all of your wobject's properties. + my $var = $self->get; + + if($self->getValue('items')){ + $var->{item_loop} = JSON->new->decode($self->getValue('items'))->{items}; + } + + #This is an example of debugging code to help you diagnose problems. + #WebGUI::ErrorHandler::warn($self->get("templateId")); + + return $self->processTemplate($var, undef, $self->{_viewTemplate}); +} + +#------------------------------------------------------------------- + +=head2 www_edit ( ) + +Web facing method which is the default edit page. This method is entirely +optional. Take it out unless you specifically want to set a submenu in your +adminConsole views. + +=cut + +#sub www_edit { +# my $self = shift; +# return $self->session->privilege->insufficient() unless $self->canEdit; +# return $self->session->privilege->locked() unless $self->canEditIfLocked; +# my $i18n = WebGUI::International->new($self->session, "Asset_Carousel"); +# return $self->getAdminConsole->render($self->getEditForm->print, $i18n->get("edit title")); +#} + + + +#------------------------------------------------------------------- +# Everything below here is to make it easier to install your custom +# wobject, but has nothing to do with wobjects in general +#------------------------------------------------------------------- +# cd /data/WebGUI/lib +# perl -MWebGUI::Asset::Wobject::Carousel -e install www.example.com.conf [ /path/to/WebGUI ] +# - or - +# perl -MWebGUI::Asset::Wobject::Carousel -e uninstall www.example.com.conf [ /path/to/WebGUI ] +#------------------------------------------------------------------- + + +use base 'Exporter'; +our @EXPORT = qw(install uninstall); +use WebGUI::Session; + +#------------------------------------------------------------------- +sub install { + my $config = $ARGV[0]; + my $home = $ARGV[1] || "/data/WebGUI"; + die "usage: perl -MWebGUI::Asset::Wobject::Carousel -e install www.example.com.conf\n" unless ($home && $config); + print "Installing asset.\n"; + my $session = WebGUI::Session->open($home, $config); + + my $assets = $session->config->get( "assets" ); + $assets->{ "WebGUI::Asset::Wobject::Carousel" } = { category => "utilities" }; + $session->config->set( "assets", $assets ); + #$session->config->addToArray("assets","WebGUI::Asset::Wobject::Carousel"); + $session->db->write("create table Carousel ( + assetId char(22) binary not null, + revisionDate bigint not null, + items mediumtext, + templateId char(22), + primary key (assetId, revisionDate) + )"); + $session->var->end; + $session->close; + print "Done. Please restart Apache.\n"; +} + +#------------------------------------------------------------------- +sub uninstall { + my $config = $ARGV[0]; + my $home = $ARGV[1] || "/data/WebGUI"; + die "usage: perl -MWebGUI::Asset::Wobject::Carousel -e uninstall www.example.com.conf\n" unless ($home && $config); + print "Uninstalling asset.\n"; + my $session = WebGUI::Session->open($home, $config); + $session->config->deleteFromArray("assets","WebGUI::Asset::Wobject::Carousel"); + my $rs = $session->db->read("select assetId from asset where className='WebGUI::Asset::Wobject::Carousel'"); + while (my ($id) = $rs->array) { + my $asset = WebGUI::Asset->new($session, $id, "WebGUI::Asset::Wobject::Carousel"); + $asset->purge if defined $asset; + } + $session->db->write("drop table Carousel"); + $session->var->end; + $session->close; + print "Done. Please restart Apache.\n"; +} + + +1; +#vim:ft=perl diff --git a/lib/WebGUI/i18n/English/Asset_Carousel.pm b/lib/WebGUI/i18n/English/Asset_Carousel.pm new file mode 100644 index 000000000..115f5d559 --- /dev/null +++ b/lib/WebGUI/i18n/English/Asset_Carousel.pm @@ -0,0 +1,38 @@ +package WebGUI::i18n::English::Asset_Carousel; + +use strict; + +our $I18N = { + 'assetName' => { + message => q|Carousel|, + lastUpdated => 0, + context => q|The name of this asset, used in the admin bar.| + }, + + 'carousel template label' => { + message => q|Carousel template|, + lastUpdated => 0, + context => q|Label of the carousel template field on the edit screen.| + }, + + 'carousel template description' => { + message => q|Select a template for this carousel.|, + lastUpdated => 0, + context => q|Description of the carousel template field, used as hover help.| + }, + + 'payload label' => { + message => q|Payload|, + lastUpdated => 0, + context => q|Label of the payload field on the edit screen.| + }, + + 'payload description' => { + message => q|Enter a javacript script tag, flash object html, etc to process this carousel's items.|, + lastUpdated => 0, + context => q|Description of the payload field, used as hover help.| + }, +}; + +1; +#vim:ft=perl diff --git a/www/extras/wobject/Carousel/carousel.js b/www/extras/wobject/Carousel/carousel.js new file mode 100644 index 000000000..7ccbd311a --- /dev/null +++ b/www/extras/wobject/Carousel/carousel.js @@ -0,0 +1,38 @@ +//YAHOO.util.Event.addListener(window, "load", function() { + function addItem () { + var items_td = document.getElementById('items_td'); + var textAreas = items_td.getElementsByClassName('carouselItemText'); + var textAreaCount = textAreas.length; + var newItemNumber = textAreaCount + 1; + + var Dom = YAHOO.util.Dom; + var myConfig = { + height: '80px', + width: '500px', + handleSubmit: true + }; + + var newItem_textarea = document.createElement('textarea'); + newItem_textarea.id = 'item'+newItemNumber; + newItem_textarea.name = 'item_'+newItemNumber; + newItem_textarea.className = 'carouselItemText'; + + var newItem_id_span = document.createElement('span'); + newItem_id_span.innerHTML = 'ID: '; + items_td.appendChild(newItem_id_span); + + var newItem_id = document.getElementById('newItem_id'); + newItem_id.type = 'text'; + newItem_id.id = 'itemId'+newItemNumber; + newItem_id.name = 'itemId_'+newItemNumber; + newItem_id.value = 'carousel_item_'+newItemNumber; + + items_td.appendChild(newItem_textarea); + + var newItem_break = document.createElement('br'); + items_td.appendChild(newItem_break); + + var myEditor = new YAHOO.widget.SimpleEditor('item'+newItemNumber, myConfig); + myEditor.render(); + } +//}); \ No newline at end of file