Skip to content

Commit

Permalink
Refs #46. Update category counts after filtering extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Apr 19, 2013
1 parent 6d35fae commit 1c4004c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 60 deletions.
42 changes: 29 additions & 13 deletions controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,57 @@ class Slicerappstore_IndexController extends Slicerappstore_AppController
*/
function indexAction()
{
$modelLoader = new MIDAS_ModelLoader();
$extensionModel = $modelLoader->loadModel('Extension', 'slicerpackages');
$extensionModel = MidasLoader::loadModel('Extension', 'slicerpackages');

$layout = $this->_getParam('layout');
if(!isset($layout) || $layout != 'empty')
{
$this->view->availableReleases = $extensionModel->getAllReleases();
$this->view->layout = 'layout';
}

$this->view->layout = $this->view->json['layout'];

$avalue = function($k, $a, $default) { return array_key_exists($k, $a) ? $a[$k] : $default; };
$params = $this->_getAllParams();
$this->view->json['os'] = $avalue('os', $params, '');
$this->view->json['arch'] = $avalue('arch', $params, '');
$this->view->json['release'] = $avalue('release', $params, '');
$this->view->json['revision'] = $avalue('revision', $params, '');
$this->view->json['category'] = $avalue('category', $params, '');
$this->view->layout = $this->view->json['layout'];
}

/**
* If a filter param changes on the normal layout view, call into this
* to retrieve updated category counts based on the current filter
*/
function categoriesAction()
{
$this->disableLayout();
$this->disableView();

$avalue = function($k, $a, $default) { return array_key_exists($k, $a) ? $a[$k] : $default; };
$params = $this->_getAllParams();

$os = $avalue('os', $params, '');
$arch = $avalue('arch', $params, '');
$revision = $avalue('revision', $params, '');

$filterParams = array();
if($this->view->json['revision'])
if($revision)
{
$filterParams['slicer_revision'] = $this->view->json['revision'];
$filterParams['slicer_revision'] = $revision;
}
if($this->view->json['os'])
if($os)
{
$filterParams['os'] = $this->view->json['os'];
$filterParams['os'] = $os;
}
if($this->view->json['arch'])
if($arch)
{
$filterParams['arch'] = $this->view->json['arch'];
$filterParams['arch'] = $arch;
}
$this->view->json['categories'] = $extensionModel->getCategoriesWithCounts($filterParams);
ksort($this->view->json['categories']);
$extensionModel = MidasLoader::loadModel('Extension', 'slicerpackages');
$categories = $extensionModel->getCategoriesWithCounts($filterParams);
ksort($categories);
echo JsonComponent::encode($categories);
}

/**
Expand Down
119 changes: 72 additions & 47 deletions public/js/index/index.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ midas.slicerappstore.applyFilter = function() {
$.fn.scrollPagination.loadContent(
$('#extensionsContainer'), midas.slicerappstore.scrollPaginationOptions(/*pageLimit = */ 0), true);
}
midas.slicerappstore.fetchCategories();
}

/**
Expand Down Expand Up @@ -243,10 +244,77 @@ midas.slicerappstore.showCategory = function(category, count) {
lastToken += tokenId;
name += '.';
});
}
};

$(document).ready(function() {
/**
* Called when the categories have been loaded or refreshed
*/
midas.slicerappstore.categoriesLoaded = function () {
$('li.categoryControl').remove();
$.each(midas.slicerappstore.categories, function(category, count) {
midas.slicerappstore.showCategory(category, count);
});

// Enable the "All" category filter
midas.slicerappstore.selectedCategory = $('li#categoryAll').unbind('click').click(function() {
midas.slicerappstore.category = '';
midas.slicerappstore.selectedCategory.removeClass('selectedCategory');
midas.slicerappstore.selectedCategory = $(this);
$(this).addClass('selectedCategory');
midas.slicerappstore.applyFilter();
});

// Enable filtering by specific categories
$('li.categoryControl').unbind('click').click(function() {
midas.slicerappstore.category = $(this).attr('name');
midas.slicerappstore.selectedCategory.removeClass('selectedCategory');
midas.slicerappstore.selectedCategory = $(this);
$(this).addClass('selectedCategory');
midas.slicerappstore.applyFilter();
});

var selector = midas.slicerappstore.category == '' ?
'li#categoryAll' :
'li.categoryControl[name="'+midas.slicerappstore.category+'"]';
midas.slicerappstore.selectedCategory = $(selector).addClass('selectedCategory');

// Setup scroll pagination and fetch results based on the initial settings
if ($.support.pageVisibility){
if(!midas.slicerappstore.isPageHidden()){
midas.slicerappstore.initScrollPagination();
} else {
$(document).bind("show", function(){
midas.slicerappstore.initScrollPagination();
$(document).unbind("show");
});
}

}
else {
// If visibility API is not supported, fetch all extensions
midas.slicerappstore.applyFilter();
}
};

midas.slicerappstore.fetchCategories = function () {
// Refresh and render the category tree based on all available categories
$.ajax({
type: 'GET',
dataType: 'json',
url: json.global.webroot+'/slicerappstore/index/categories',
data: {
os: midas.slicerappstore.os,
arch: midas.slicerappstore.arch,
revision: midas.slicerappstore.revision
},
success: function (resp) {
midas.slicerappstore.categories = resp;
midas.slicerappstore.categoriesLoaded();
}
});
};

$(document).ready(function() {
midas.slicerappstore.os = json.os;
midas.slicerappstore.arch = json.arch;
midas.slicerappstore.release = json.release;
Expand Down Expand Up @@ -289,53 +357,10 @@ $(document).ready(function() {
});
}

// Render the category tree based on all available categories
$.each(json.categories, function(category, count) {
midas.slicerappstore.showCategory(category, count);
});

// Enable the "All" category filter
midas.slicerappstore.selectedCategory = $('li#categoryAll').click(function() {
midas.slicerappstore.category = '';
midas.slicerappstore.selectedCategory.removeClass('selectedCategory');
midas.slicerappstore.selectedCategory = $(this);
$(this).addClass('selectedCategory');
midas.slicerappstore.applyFilter();
});

// Enable filtering by specific categories
$('li.categoryControl').click(function() {
midas.slicerappstore.category = $(this).attr('name');
midas.slicerappstore.selectedCategory.removeClass('selectedCategory');
midas.slicerappstore.selectedCategory = $(this);
$(this).addClass('selectedCategory');
midas.slicerappstore.applyFilter();
});

var selector = midas.slicerappstore.category == '' ?
'li#categoryAll' :
'li.categoryControl[name="'+midas.slicerappstore.category+'"]';
midas.slicerappstore.selectedCategory = $(selector).addClass('selectedCategory');

// Setup scroll pagination and fetch results based on the initial settings
if ($.support.pageVisibility){
if(!midas.slicerappstore.isPageHidden()){
midas.slicerappstore.initScrollPagination();
} else {
$(document).bind("show", function(){
midas.slicerappstore.initScrollPagination();
$(document).unbind("show");
});
}

}
else {
// If visibility API is not supported, fetch all extensions
midas.slicerappstore.applyFilter();
}
midas.slicerappstore.fetchCategories();

$('img.kwLogo').click(function () {
var dlgWidth = Math.min($(window).width() * 0.8, 550);
var dlgWidth = Math.min($(window).width() * 0.8, 550);
midas.loadDialog('KWInfo', '/slicerappstore/index/kwinfo');
midas.showDialog('Slicer Extension Catalog', false, {width: dlgWidth})
});
Expand Down

0 comments on commit 1c4004c

Please sign in to comment.