From f5ebb6e9163504eff0d00ac0dd6e5e378d2d12f7 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 14 Apr 2011 15:08:35 -0500 Subject: [PATCH] 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" --- lib/WebGUI/Admin.pm | 108 ++++++++++++++++++------------------ lib/WebGUI/Session/Style.pm | 33 +++-------- www/extras/admin/admin.js | 74 ++++++++++++++++++------ 3 files changed, 116 insertions(+), 99 deletions(-) diff --git a/lib/WebGUI/Admin.pm b/lib/WebGUI/Admin.pm index a5c5a2158..ed365a939 100644 --- a/lib/WebGUI/Admin.pm +++ b/lib/WebGUI/Admin.pm @@ -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 ( ) 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 ( ) 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 @assetInfo = (); for my $asset ( @{$assets} ) { - push @assetInfo, { - assetId => $asset->getId, - url => $asset->getUrl, - title => $asset->menuTitle, - revisionDate => $asset->revisionDate, - icon => $asset->getIcon("small"), - }; + push @assetInfo, $self->getAssetData( $asset ); } return JSON->new->encode( \@assetInfo ); @@ -557,43 +599,16 @@ sub www_getTreeData { for my $assetId ( @{ $p->getPageData } ) { my $asset = WebGUI::Asset->newById( $session, $assetId ); - - # 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; + push @{ $assetInfo->{ assets } }, $self->getAssetData( $asset ); } $assetInfo->{ totalAssets } = $p->getRowCount; $assetInfo->{ sort } = $session->form->get( 'orderByColumn' ); $assetInfo->{ dir } = lc $session->form->get( 'orderByDirection' ); - $assetInfo->{ currentAsset } = { - assetId => $asset->getId, - url => $asset->getUrl, - title => $asset->getTitle, - icon => $asset->getIcon("small"), - helpers => $asset->getHelpers, - }; - + $assetInfo->{ currentAsset } = $self->getAssetData( $asset ); $assetInfo->{ crumbtrail } = []; for my $asset ( @{ $asset->getLineage( ['ancestors'], { returnObjects => 1 } ) } ) { - push @{ $assetInfo->{crumbtrail} }, { - title => $asset->getTitle, - url => $asset->getUrl - }; + push @{ $assetInfo->{crumbtrail} }, $self->getAssetData( $asset ); } $session->response->content_type( 'application/json' ); @@ -708,24 +723,7 @@ sub www_searchAssets { for my $result ( @{ $p->getPageData } ) { my $assetId = $result->{assetId}; my $asset = WebGUI::Asset->newById( $session, $assetId ); - - # 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; + push @{ $assetInfo->{ assets } }, $self->getAssetData( $asset ); } $assetInfo->{ totalAssets } = $p->getRowCount; diff --git a/lib/WebGUI/Session/Style.pm b/lib/WebGUI/Session/Style.pm index 92fe74b7e..b4f49980a 100644 --- a/lib/WebGUI/Session/Style.pm +++ b/lib/WebGUI/Session/Style.pm @@ -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 - $var{'head.tags'} .= ' ADMINJS } - $var{'head.tags'} .= ''; # Removing the newlines will probably annoy people. # Perhaps turn it off under debug mode? diff --git a/www/extras/admin/admin.js b/www/extras/admin/admin.js index 7e033b972..34aecf5d2 100644 --- a/www/extras/admin/admin.js +++ b/www/extras/admin/admin.js @@ -11,9 +11,11 @@ if ( typeof WebGUI == "undefined" ) { WebGUI = {}; } WebGUI.Admin = function(cfg){ + var self = this; // Public properties this.cfg = cfg; this.currentAssetDef = null; + this.currentAssetId = ""; this.currentTab = "view"; // "view" or "tree" or other ID this.treeDirty = true; @@ -37,8 +39,20 @@ WebGUI.Admin = function(cfg){ // Custom events 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 - var self = this; // Initialize these things AFTER the i18n is fetched var _init = function () { self.afterNavigate.subscribe( self.requestUpdateCurrentVersionTag, self ); @@ -60,10 +74,6 @@ WebGUI.Admin = function(cfg){ homeUrl : self.cfg.homeUrl } ); self.afterNavigate.subscribe( self.locationBar.afterNavigate, self.locationBar ); - if ( self.currentAssetDef ) { - self.locationBar.navigate( self.currentAssetDef ); - } - }; // Get I18N @@ -245,23 +255,51 @@ WebGUI.Admin.prototype.makeGotoAsset * page is reached */ WebGUI.Admin.prototype.navigate -= function ( assetDef ) { += function ( assetId ) { // 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 - this.afterNavigate.fire( assetDef ); + this.afterNavigate.fire( this.currentAssetDef ); return; } - if ( !this.currentAssetDef || this.currentAssetDef.assetId != assetDef.assetId ) { - this.currentAssetDef = assetDef; - this.treeDirty = 1; - this.updateAssetHelpers( assetDef ); - this.updateAssetHistory( assetDef ); - } + if ( !this.currentAssetId || this.currentAssetId != assetId ) { + // request asset update + this.currentAssetId = assetId; + var self = this; + this.requestAssetDef( assetId, function( assetDef ) { + self.currentAssetDef = assetDef; + self.treeDirty = 1; + self.updateAssetHelpers( assetDef ); + self.updateAssetHistory( assetDef ); - // Fire event - this.afterNavigate.fire( assetDef ); + // Fire event + 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 ) { var typeEl = document.getElementById( 'helper_asset_name' ); typeEl.style.backgroundImage = 'url(' + assetDef.icon + ')'; - typeEl.innerHTML = assetDef.type; + typeEl.innerHTML = assetDef.className; // Clear old helpers 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 ); } 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 ); } };