change admin console to get asset info via ajax call after frame is loaded

this is step one in making the asset control toolbars work
correctly without having an "Admin Mode"
This commit is contained in:
Doug Bell 2011-04-14 15:08:35 -05:00
parent eec3986cf7
commit f5ebb6e916
3 changed files with 116 additions and 99 deletions

View file

@ -122,6 +122,38 @@ sub getAdminPluginTemplateVars {
#---------------------------------------------------------------------- #----------------------------------------------------------------------
=head2 getAssetData ( asset )
Get the required data for an asset, including the properties and helpers.
Returns a hashref.
=cut
sub getAssetData {
my ( $self, $asset ) = @_;
# Populate the required fields to fill in
my %fields = (
assetId => $asset->getId,
url => $asset->getUrl,
lineage => $asset->lineage,
title => $asset->menuTitle,
revisionDate => $asset->revisionDate,
childCount => $asset->getChildCount,
assetSize => $asset->assetSize,
lockedBy => ($asset->isLockedBy ? $asset->lockedBy->username : ''),
canEdit => $asset->canEdit && $asset->canEditIfLocked,
helpers => $asset->getHelpers,
icon => $asset->getIcon("small"),
className => $asset->getName,
revisions => [ $asset->getRevisionDates ],
);
return \%fields;
}
#----------------------------------------------------------------------
=head2 getAssetTypes ( ) =head2 getAssetTypes ( )
Get a hash of className => info pairs containing information about the Get a hash of className => info pairs containing information about the
@ -480,6 +512,22 @@ sub www_findUser {
#---------------------------------------------------------------------- #----------------------------------------------------------------------
=head2 www_getAssetData ( )
Get data for an asset, including properties and asset helpers.
=cut
sub www_getAssetData {
my ( $self ) = @_;
my $session = $self->session;
my $assetId = $session->form->get('assetId');
my $asset = WebGUI::Asset->newById( $session, $assetId );
return JSON->new->encode( $self->getAssetData( $asset ) );
}
#----------------------------------------------------------------------
=head2 www_getClipboard ( ) =head2 www_getClipboard ( )
Get the assets currently on the user's clipboard Get the assets currently on the user's clipboard
@ -496,13 +544,7 @@ sub www_getClipboard {
my $assets = WebGUI::Asset->getRoot( $session )->getAssetsInClipboard( $userOnly ); my $assets = WebGUI::Asset->getRoot( $session )->getAssetsInClipboard( $userOnly );
my @assetInfo = (); my @assetInfo = ();
for my $asset ( @{$assets} ) { for my $asset ( @{$assets} ) {
push @assetInfo, { push @assetInfo, $self->getAssetData( $asset );
assetId => $asset->getId,
url => $asset->getUrl,
title => $asset->menuTitle,
revisionDate => $asset->revisionDate,
icon => $asset->getIcon("small"),
};
} }
return JSON->new->encode( \@assetInfo ); return JSON->new->encode( \@assetInfo );
@ -557,43 +599,16 @@ sub www_getTreeData {
for my $assetId ( @{ $p->getPageData } ) { for my $assetId ( @{ $p->getPageData } ) {
my $asset = WebGUI::Asset->newById( $session, $assetId ); my $asset = WebGUI::Asset->newById( $session, $assetId );
push @{ $assetInfo->{ assets } }, $self->getAssetData( $asset );
# Populate the required fields to fill in
my %fields = (
assetId => $asset->getId,
url => $asset->getUrl,
lineage => $asset->lineage,
title => $asset->menuTitle,
revisionDate => $asset->revisionDate,
childCount => $asset->getChildCount,
assetSize => $asset->assetSize,
lockedBy => ($asset->isLockedBy ? $asset->lockedBy->username : ''),
canEdit => $asset->canEdit && $asset->canEditIfLocked,
helpers => $asset->getHelpers,
icon => $asset->getIcon("small"),
className => $asset->getName,
);
push @{ $assetInfo->{ assets } }, \%fields;
} }
$assetInfo->{ totalAssets } = $p->getRowCount; $assetInfo->{ totalAssets } = $p->getRowCount;
$assetInfo->{ sort } = $session->form->get( 'orderByColumn' ); $assetInfo->{ sort } = $session->form->get( 'orderByColumn' );
$assetInfo->{ dir } = lc $session->form->get( 'orderByDirection' ); $assetInfo->{ dir } = lc $session->form->get( 'orderByDirection' );
$assetInfo->{ currentAsset } = { $assetInfo->{ currentAsset } = $self->getAssetData( $asset );
assetId => $asset->getId,
url => $asset->getUrl,
title => $asset->getTitle,
icon => $asset->getIcon("small"),
helpers => $asset->getHelpers,
};
$assetInfo->{ crumbtrail } = []; $assetInfo->{ crumbtrail } = [];
for my $asset ( @{ $asset->getLineage( ['ancestors'], { returnObjects => 1 } ) } ) { for my $asset ( @{ $asset->getLineage( ['ancestors'], { returnObjects => 1 } ) } ) {
push @{ $assetInfo->{crumbtrail} }, { push @{ $assetInfo->{crumbtrail} }, $self->getAssetData( $asset );
title => $asset->getTitle,
url => $asset->getUrl
};
} }
$session->response->content_type( 'application/json' ); $session->response->content_type( 'application/json' );
@ -708,24 +723,7 @@ sub www_searchAssets {
for my $result ( @{ $p->getPageData } ) { for my $result ( @{ $p->getPageData } ) {
my $assetId = $result->{assetId}; my $assetId = $result->{assetId};
my $asset = WebGUI::Asset->newById( $session, $assetId ); my $asset = WebGUI::Asset->newById( $session, $assetId );
push @{ $assetInfo->{ assets } }, $self->getAssetData( $asset );
# Populate the required fields to fill in
my %fields = (
assetId => $asset->getId,
url => $asset->getUrl,
lineage => $asset->lineage,
title => $asset->menuTitle,
revisionDate => $asset->revisionDate,
childCount => $asset->getChildCount,
assetSize => $asset->assetSize,
lockedBy => ($asset->isLockedBy ? $asset->lockedBy->username : ''),
canEdit => $asset->canEdit && $asset->canEditIfLocked,
helpers => $asset->getHelpers,
icon => $asset->getIcon('small'),
className => $asset->getName,
);
push @{ $assetInfo->{ assets } }, \%fields;
} }
$assetInfo->{ totalAssets } = $p->getRowCount; $assetInfo->{ totalAssets } = $p->getRowCount;

View file

@ -248,34 +248,15 @@ if ($self->session->user->isRegistered || $self->session->setting->get("preventP
} }
# TODO: Figure out if user is still in the admin console # Give the API jocks something to use
$var{'head.tags'} .= '<script type="text/javascript">'; if ( $session->asset ) {
my $asset = $session->asset; $var{'head.tags'} .= sprintf <<'ADMINJS', $session->asset->getId
<script type="text/javascript">
# If user is in an operation, find the right asset if ( typeof window.WG == "undefined" ) { window.WG = {} }
if ( !$asset && $session->form->get('op') ) { WG.currentAssetId = '%s';
$asset = WebGUI::Asset->newByUrl( $session ); </script>
}
if ( $asset ) {
my $i18n = WebGUI::International->new( $session );
my $assetName = $i18n->get( @{ $asset->assetName } );
my $assetDef = {
assetId => $asset->getId,
title => $asset->getTitle,
url => $asset->getUrl,
icon => $asset->getIcon(1),
type => $assetName,
helpers => $asset->getHelpers,
revisions => $asset->getRevisionDates,
};
$var{'head.tags'} .= sprintf <<'ADMINJS', JSON->new->encode( $assetDef );
if ( window.parent && window.parent.admin ) {
window.parent.admin.navigate( %s );
}
ADMINJS ADMINJS
} }
$var{'head.tags'} .= '</script>';
# Removing the newlines will probably annoy people. # Removing the newlines will probably annoy people.
# Perhaps turn it off under debug mode? # Perhaps turn it off under debug mode?

View file

@ -11,9 +11,11 @@ if ( typeof WebGUI == "undefined" ) {
WebGUI = {}; WebGUI = {};
} }
WebGUI.Admin = function(cfg){ WebGUI.Admin = function(cfg){
var self = this;
// Public properties // Public properties
this.cfg = cfg; this.cfg = cfg;
this.currentAssetDef = null; this.currentAssetDef = null;
this.currentAssetId = "";
this.currentTab = "view"; // "view" or "tree" or other ID this.currentTab = "view"; // "view" or "tree" or other ID
this.treeDirty = true; this.treeDirty = true;
@ -37,8 +39,20 @@ WebGUI.Admin = function(cfg){
// Custom events // Custom events
this.afterNavigate = new YAHOO.util.CustomEvent( "afterNavigate", this ); this.afterNavigate = new YAHOO.util.CustomEvent( "afterNavigate", this );
// Keep track of the view iframe
var viewframe = document.getElementById('adminViewFrame');
// If it already loaded, run the right function
if ( viewframe.hasLoaded && window.frames[viewframe.name].WG ) {
this.navigate( window.frames[viewframe.name].WG.currentAssetId );
}
// Next and every subsequent time it loads, run the right function again
YAHOO.util.Event.on( viewframe, 'load', function(){
if ( window.frames[viewframe.name].WG ) {
self.navigate( window.frames[viewframe.name].WG.currentAssetId );
}
} );
// Private methods // Private methods
var self = this;
// Initialize these things AFTER the i18n is fetched // Initialize these things AFTER the i18n is fetched
var _init = function () { var _init = function () {
self.afterNavigate.subscribe( self.requestUpdateCurrentVersionTag, self ); self.afterNavigate.subscribe( self.requestUpdateCurrentVersionTag, self );
@ -60,10 +74,6 @@ WebGUI.Admin = function(cfg){
homeUrl : self.cfg.homeUrl homeUrl : self.cfg.homeUrl
} ); } );
self.afterNavigate.subscribe( self.locationBar.afterNavigate, self.locationBar ); self.afterNavigate.subscribe( self.locationBar.afterNavigate, self.locationBar );
if ( self.currentAssetDef ) {
self.locationBar.navigate( self.currentAssetDef );
}
}; };
// Get I18N // Get I18N
@ -245,23 +255,51 @@ WebGUI.Admin.prototype.makeGotoAsset
* page is reached * page is reached
*/ */
WebGUI.Admin.prototype.navigate WebGUI.Admin.prototype.navigate
= function ( assetDef ) { = function ( assetId ) {
// Don't do the same asset twice // Don't do the same asset twice
if ( this.currentAssetDef && this.currentAssetDef.assetId == assetDef.assetId ) { if ( this.currentAssetId && this.currentAssetId == assetId ) {
// But still fire the event // But still fire the event
this.afterNavigate.fire( assetDef ); this.afterNavigate.fire( this.currentAssetDef );
return; return;
} }
if ( !this.currentAssetDef || this.currentAssetDef.assetId != assetDef.assetId ) { if ( !this.currentAssetId || this.currentAssetId != assetId ) {
this.currentAssetDef = assetDef; // request asset update
this.treeDirty = 1; this.currentAssetId = assetId;
this.updateAssetHelpers( assetDef ); var self = this;
this.updateAssetHistory( assetDef ); this.requestAssetDef( assetId, function( assetDef ) {
} self.currentAssetDef = assetDef;
self.treeDirty = 1;
self.updateAssetHelpers( assetDef );
self.updateAssetHistory( assetDef );
// Fire event // Fire event
this.afterNavigate.fire( assetDef ); this.afterNavigate.fire( assetDef );
} );
}
};
/**
* requestAssetDef( assetId, callback )
* Request more information about an asset. The callback takes a single
* argument which is an object containing the asset information and is
* called in the scope of the admin function
*/
WebGUI.Admin.prototype.requestAssetDef
= function ( assetId, callback ) {
var connectCallback = {
success : function (o) {
var assetDef = YAHOO.lang.JSON.parse( o.responseText );
callback.call( this, assetDef );
},
failure : function (o) {
},
scope: this
};
var url = '?op=admin;method=getAssetData;assetId=' + assetId;
var ajax = YAHOO.util.Connect.asyncRequest( 'GET', url, connectCallback );
}; };
/** /**
@ -447,7 +485,7 @@ WebGUI.Admin.prototype.updateAssetHelpers
= function ( assetDef ) { = function ( assetDef ) {
var typeEl = document.getElementById( 'helper_asset_name' ); var typeEl = document.getElementById( 'helper_asset_name' );
typeEl.style.backgroundImage = 'url(' + assetDef.icon + ')'; typeEl.style.backgroundImage = 'url(' + assetDef.icon + ')';
typeEl.innerHTML = assetDef.type; typeEl.innerHTML = assetDef.className;
// Clear old helpers // Clear old helpers
var helperEl = document.getElementById( 'helper_list' ); var helperEl = document.getElementById( 'helper_list' );
@ -477,7 +515,7 @@ WebGUI.Admin.prototype.addHelperHandler
YAHOO.util.Event.on( elem, "click", function(){ self.gotoAsset( helper.url ) }, self, true ); YAHOO.util.Event.on( elem, "click", function(){ self.gotoAsset( helper.url ) }, self, true );
} }
else { else {
YAHOO.util.Event.on( elem, "click", function(){ self.requestHelper( helperId, self.currentAssetDef.assetId ) }, self, true ); YAHOO.util.Event.on( elem, "click", function(){ self.requestHelper( helperId, self.currentAssetId ) }, self, true );
} }
}; };