EES sample plugin

Hi ,
I have installed the EasyEdit.plugins.lastUpdatedBy plugin but when I change screens Details , Content metadata etc



the last updated by data is duplicated. This does not occur when I refresh the page.

see below.



Status: Live

Last updated by: username<<<

Last updated by: username<<<

Published: 11 Jan 2012Last update: 23 Mar 2012

Hey Darren


Can you please paste the entire contents of the plugin code here so we can have a look and see if anything stands out.



Cheers



Scotty

[quote]
Hey Darren



Can you please paste the entire contents of the plugin code here so we can have a look and see if anything stands out.



Cheers



Scotty

[/quote]



/** * Sample EES plugin - Adds 'Last updated by' to the info box */





EasyEdit.plugins.lastUpdatedBy = {

init: function()

{ var self = this;

// Add a function to the screen load event

EasyEditEventManager.bind('EasyEditScreenLoad',self.addLastUpdatedBy);

},

addLastUpdatedBy: function()

{



// Get the current asset

EasyEditAssetManager.getCurrentAsset(function(asset){

// Append some HTML after the current status in the info box

jQuery('#ees_assetCurrentStatus').after('<span>Last updated by: ' + asset.attribute('updated_username') + '</span><br>');





});

}

};

Thanks Darren


Looks like our manuals needs an update, the current example is not compatible (or is, but is bugged a little) with phase 3 release. Also probably no reason we need to be calling the function on each screen load any more, as apposed to just once after ees loads.



Try the below correction out, and I will pass on for online manuals update.


    
    
    EasyEdit.plugins.lastUpdatedBy = {
    init: function() {
        var self = this;
        
        // Add a function to the after ees load event
        EasyEditEventManager.bind('EasyEditAfterLoad',self.addLastUpdatedBy);
    },
    addLastUpdatedBy: function(){
        // Get the current asset
        EasyEditAssetManager.getCurrentAsset(function(asset){
            // Append some HTML after the current status in the info box
            var $lastUpdatedBy = jQuery('#ees_assetLastUpdatedBy');
            var $currentStatus = jQuery('#ees_assetCurrentStatus');
            var updatedUserName = 'Last updated by: ' + asset.attribute('updated_username');
            if($lastUpdatedBy.length){
                $lastUpdatedBy.text(updatedUserName);
            } else {
                $currentStatus.after('' + updatedUserName + '');
            }
        });
    }
    };
    



Cheers

Scotty

[quote]
Thanks Darren



Looks like our manuals needs an update, the current example is not compatible (or is, but is bugged a little) with phase 3 release. Also probably no reason we need to be calling the function on each screen load any more, as apposed to just once after ees loads.







Cheers



Scotty

[/quote]





thanks Scotty



that has fixed it.

[quote]
thanks Scotty



that has fixed it.

[/quote]



Sorry Darren, gave you too much code there (copy and past fail) much of all that would be helpful if the function was called on screen load, but now its changes to after ees loads (so is only called once) the below should be sufficient…


    
    
    EasyEdit.plugins.lastUpdatedBy = {
    init: function() {
        var self = this;
        
        // Add a function to the after ees load event
        EasyEditEventManager.bind('EasyEditAfterLoad',self.addLastUpdatedBy);
    },
    addLastUpdatedBy: function(){
        // Get the current asset
        EasyEditAssetManager.getCurrentAsset(function(asset){
            // Append some HTML after the current status in the info box
            jQuery('#ees_assetCurrentStatus').after('Last updated by: ' + asset.attribute('updated_username') + '');
        });
    }
    };
    


Cheers

Scotty

[quote]
Sorry Darren, gave you too much code there (copy and past fail) much of all that would be helpful if the function was called on screen load, but now its changes to after ees loads (so is only called once) the below should be sufficient…







Cheers



Scotty

[/quote]



thanks



on another note is there any any keyword for file size?



tried this "asset_file_size_in_bytes" but does seem to work



I noticed in the code that it is a bit more complicated to find out.

[quote]
thanks



on another note is there any any keyword for file size?



tried this "asset_file_size_in_bytes" but does seem to work



I noticed in the code that it is a bit more complicated to find out.

[/quote]



How/where are you using this keyword?

[quote]
How/where are you using this keyword?

[/quote]



I wanted to add it to the status area, (similar to last updatedby)

for file assets.

The only way to find it at the moment is to preview the asset.

or you could add it to the details page as default :slight_smile:

Keyword replacements do not work from/within EES javascript plugins.


You'll have to extend you're plugin to store the asset objects 'size' property and then use this in some way.



You already have a call to get current asset there which returns the current asset object, if you good with web diagnostic tools like Firebug, you can run the following from the console to inspect the object and re-use what you need…


    
    
    // Run this from firebug console and inspect the returned object, you see there is an 'attr' whn run against a file asset that you might be able to use
    EasyEditAssetManager.getCurrentAsset(function(asset){
    console.log(asset);
    });
    


You can extend the existing plugin you have to do something like...

    
// Only do the following stuff if this is a file type, this snippet of code just checks ot see if its an image, you'd want to extend this.
var assetType = asset.attribute('type_code');
if(assetType === 'image'){
var fileSize = asset.attribute('size');
// Do some stuff with fileSize variable here
}




Cheers



Scotty.
    
    
    // Run this from firebug console and inspect the returned object, you see there is an 'attr' whn run against a file asset that you might be able to use
    EasyEditAssetManager.getCurrentAsset(function(asset){
    console.log(asset);
    });
    

the above does return anything as well, am I missing something here?


    
    // appended this to assetLastUpdatedBy plugin code
    
    var assetType = asset.attribute('type_code');
    var fileSize = asset.attribute('size');
    var docfileSize = asset.attribute('asset_file_size_in_bytes'); 
    
    var typeCodeArr = ["word_doc","image","pdf_file"];
    var typeL = typeCodeArr.length;
    
    for (var i=0; i<typeL; i++) 
    {
    
       if (assetType == typeCodeArr[i])
      {
      
      jQuery('#ees_assetLastUpdatedBy').after('
    File Size: ' + ByteSize.ByteHuman(fileSize) + '');
      break;
      }
    
    }
        });
    }//end addLastUpdatedBy
    }
    
    var ByteSize =
    {
      
      ByteHuman: function(bytes)
      {
    
        var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
        var e = Math.floor(Math.log(bytes)/Math.log(1024));
        return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
    
      }
    };
    


Hi Scotty I have the filesize for images working but that is not that much help because this is already displayed on the Details page (in preview area).

I cannot seem to find where the [font="Verdana"]{"asset_file_size_in_bytes":128226} on the preview screen of pdf, word docs etc comes from.

any hints?[/font]

Hey Darren


The 'asset' object (when retrieved using the get current asset object as shown the plugin example) is a JSON collection of data about the current asset. You can't interact with this using normal matrix content keyword replacements.



'asset_file_size_in_bytes' does not exist as a property in the asset object, only 'size' does, so pass 'size' into your conversion function.



Also, my snippet of code example…


    
    
    EasyEditAssetManager.getCurrentAsset(function(asset){
    console.log(asset);
    });
    


... was an example of how to display the asset object in the browser console (like in Firebug console) so that you could inspect what is inside it and pull out what you need. If you have not done this before I would advise a little research on 'console.log' and also how to dynamically run javascript against a page from a browser console.

Cheers

Scotty

Sorry Darren, I might have initially misunderstood.


My code exampe will work for image files (which we store the total file size in bytes as 'size') within our own asset object. But when it comes to other files we actually make a new call to Matrix (via the JS API getKeywordReplacements function) to get an accurate filesize for the preview screen.



If you want to go down that path please use a new call to the JS API asset (you can use the same API asset that is references from EES code) but PLEASE do not re-use any internal EES code/objects/functions to do this. if you do, you run the risk of you're plugin comepletely breaking with any EES updates and this is unsupportable.



Might need the online manuals for some JS API help if you havnt worked with it before.



Cheers



Scotty

[quote]
Sorry Darren, I might have initially misunderstood.



My code exampe will work for image files (which we store the total file size in bytes as 'size') within our own asset object. But when it comes to other files we actually make a new call to Matrix (via the JS API getKeywordReplacements function) to get an accurate filesize for the preview screen.



If you want to go down that path please use a new call to the JS API asset (you can use the same API asset that is references from EES code) but PLEASE do not re-use any internal EES code/objects/functions to do this. if you do, you run the risk of you're plugin comepletely breaking with any EES updates and this is unsupportable.



Might need the online manuals for some JS API help if you havnt worked with it before.



Cheers



Scotty

[/quote]



HI Scotty ,

yes I see can it is using the JS API getKeywordReplacements function when you preview the file, obviously making an extra function call .

thanks.

[quote]
HI Scotty ,

yes I see can it is using the JS API getKeywordReplacements function when you preview the file, obviously making an extra function call .

thanks.

[/quote]

Hi Scotty, I think I finally have it working!



let me know if its completely the wrong way to go about it !

    
    EasyEdit.plugins.lastUpdatedBy = {
    init: function() 
    {
        var self = this;
      
        // Add a function to the after ees load event
        EasyEditEventManager.bind('EasyEditAfterLoad',self.addLastUpdatedBy);
    
    },
    addLastUpdatedBy: function()
    {
            
      // Get the current asset
       EasyEditAssetManager.getCurrentAsset(function(asset){
    
    var assetType = asset.attribute('type_code');
    var assetID = asset.attribute('id');
    
       // Append some HTML after the current status in the info box
     jQuery('#ees_assetCurrentStatus').after('Last updated by: ' + asset.attribute('updated_username') + '');
    
    
    // Only do the following stuff if this is a file type
    
    var typeCodeArr = ["word_doc","image","pdf_file"];
    var typeL = typeCodeArr.length;
    
    for (var i=0; i<typeL; i++) 
    {
    
       if (assetType == typeCodeArr[i])
      {
      
       EasyEdit.plugins.lastUpdatedBy.addFileSize(asset.attribute('id')); 
       //alert(i);
        break;
      }
    
    }
    
    
        });//getCurrentAsset
    },
    //end addLastUpdatedBy
    
    
    addFileSize: function(vassetID)
    {
     
        var self = this,
            api = EasyEditAPI.getInstance();
        
        api.queue('getKeywordsReplacements', {
            
       asset_id: vassetID,
            keywords_array: ["%asset_file_size_in_bytes%"]
       }).run(function(results){
                  var fileSize = api.getResult([0],'asset_file_size_in_bytes');
     jQuery('#ees_assetLastUpdatedBy').after('
    File Size: ' + ByteSize.ByteHuman(fileSize)+ '
    ');
                },api.runMode.SYNC); // End add file size.
       // Error check
     
    var ByteSize =
    {
       
       ByteHuman: function(bytes)
       {
    
       var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
       var e = Math.floor(Math.log(bytes)/Math.log(1024));
       return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
    
       }
    };
       
      } // End addFileSize getKeywordsReplacements()
       
    }; 
    //end   lastUpdatedBy
    
    

Hey Darren


Looks like you're on your way, I noticed your using the EES api object…


    EasyEditAPI.getInstance();


... to make a call to matrix via the JS API, which should be re-written to interact with the JS API directly e.g. create your own (new) JS API object and do what you need to do. Trying to re-use EES objects at this level will get you into hot water later on when you try to upgrade.

Cheers

Scotty