Blog

Squiz Matrix Asset Map Sort Order Using Javascript

Wed, Sep. 21, 2011

In a past project I needed a way to change the asset map sort order by dragging items in an asset listing. I figured that it had to be possible, so with a little bit of javascript I wrote up the following function.

Hope it helps someone out!

/**
/**
* Changes the sort order of assets in Squiz Matrix
*
* @param number         id              ID of the asset to move
* @param number         parent          ID of the parent that the asset is under
* @param number         new_position    New position of asset (0 is first in the sort order)
*
* @return string
* @access public
*/
function changeSort(id, parent, new_position) {
        var host_url = location.protocol+'//'+location.host+'?SQ_ACTION=asset_map_request';
        var link_id;
        
        // Construct our XML to send
        var xml_get = '<command action="get assets"><asset assetid="'+parent+'" start="0" limit="150" linkid="10" /></command>';
        
        $.ajax({
                url: host_url,
                type: 'POST',
                processData: false,
                data: xml_get,
                contentType: "text/xml",
                dataType: 'xml',
                success: function(xml) {
                        $(xml).find('asset').each(function() {
                                if ($(this).attr('assetid') == id) {
                                        link_id = $(this).attr('linkid');
                                        $.ajax({
                                                url: host_url,
                                                type: 'POST',
                                                processData: false,
                                                data: '<command action="move asset" to_parent_assetid="'+parent+'" to_parent_pos="'+new_position+'"><asset assetid="'+id+'" linkid="'+link_id+'" parentid="'+parent+'" /></command>',
                                                contentType: "text/xml",
                                                dataType: 'xml',
                                                success: function(xml) {
                                                        //alert(xml);
                                                }
                                        });//end ajax
                                        
                                }//end if
                                
                        });//end each
                        
                }//end success
                
        });//end ajax
        
}//end changeSort()

Remember, this function requires jQuery.

Nicholas Hubbard
Owner

Comments

Anton Babushkin
Sep 21, 2011
Nice share Nic. This is very useful for little UI widgets that alter assets position so that they display in different order on the front-end when an asset listing is run (with Direct Links set to Yes). I've just started a GitHub gist and added yours in there. I'll add some of my snippets in there too. Would love to see a huge compilation of useful snippets (JS / PHP, Shell, etc)! https://gist.github.com/1233609
Nic Hubbard
Sep 21, 2011
Awesome idea to start a gist for this, thanks.

Add Comment