Skip to content

Commit

Permalink
Added ability to load category and categories
Browse files Browse the repository at this point in the history
Bug fixes, improvements.
  • Loading branch information
rcorral committed Dec 8, 2011
1 parent a43a8db commit 5b2a0f9
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 9 deletions.
184 changes: 178 additions & 6 deletions www/k2/assets/js/functions.js
Expand Up @@ -98,7 +98,7 @@ japp.load_k2_items = function( limitstart, limit, fresh ) {
* Loads JForm fields from the server
* It avoid writing a lot of HTML
*/
japp.load_k2_item_fields = function( fieldset, fresh ){
japp.load_k2_form_fields = function( form, fieldset, fresh ){
// Set the context for caching purposes
var context = 'k2.item.fields.' + fieldset;

Expand All @@ -109,14 +109,15 @@ japp.load_k2_item_fields = function( fieldset, fresh ){
this._ajax(
{
app: 'k2',
resource: 'itemform',
resource: 'formfields',
form: form,
fieldset: fieldset
}, function( data ) {
// If there is no data, and we are expecting data, then try request again
if ( japp._object_empty( data ) ) {
// This function will only try to get data 3 times
// First two parameters are filters for the request, and the last is a callback
japp._try_server_request_again( 'load_k2_item_fields', fieldset,
japp._try_server_request_again( 'load_k2_item_fields_' + form, fieldset,
function(){ japp.load_k2_item_fields( fieldset, fresh ); });
} else {
// Add response to cache
Expand Down Expand Up @@ -150,8 +151,8 @@ japp.get_k2_categories = function( fresh ){
function( data ) {
// Try again?
if ( japp._object_empty( data ) ) {
japp._try_server_request_again( 'get_k2_kategories', '',
function(){ japp.get_k2_kategories( fresh ) } );
japp._try_server_request_again( 'get_k2_categories', '',
function(){ japp.get_k2_categories( fresh ) } );
} else {
jcache.set( context, data );
}
Expand Down Expand Up @@ -201,7 +202,8 @@ japp.load_k2_item = function( id ) {
jQuery('#item-catid').val(item.catid).selectmenu();
jQuery('#item-featured').val(item.featured).slider('refresh');
jQuery('#item-published').val(item.published).slider('refresh');
jQuery('#item-text').val( item.introtext + ( item.fulltext ? '<hr id="system-readmore" />' + item.fulltext : '' ) );
jQuery('#item-text').val( item.introtext + ( item.fulltext ? '<hr id="system-readmore" />'
+ item.fulltext : '' ) );
jQuery('#item-language').val(item.language).selectmenu();
jQuery('#item-created-by').val(item.created_by).selectmenu();
jQuery('#item-created-by-alias').val(item.created_by_alias);
Expand Down Expand Up @@ -509,3 +511,173 @@ japp.delete_k2_tag = function() {
}
}, { async: false, type: 'DELETE' });
}

/* Categories */
japp.get_k2_extrafieldsgroups = function( fresh ){
var context = 'k2.extrafieldgroups';
if ( japp.cache && !fresh && jcache.get( context ) ) {
return jcache.get( context );
}

this._ajax(
{
app: 'k2',
resource: 'extrafieldsgroups'
},
function( data ) {
// Try again?
if ( japp._object_empty( data ) ) {
japp._try_server_request_again( 'get_k2_extrafieldsgroups', '',
function(){ japp.get_k2_extrafieldsgroups( fresh ) } );
} else {
jcache.set( context, data );
}
}, { async: false });

return jcache.get( context );
}

japp.load_k2_categories = function( limitstart, limit, fresh ) {
if ( this._is_loading('k2_categories') ) {
return false;
}

var el = jQuery('#k2-categories-list ul');
this._started_loading('k2_categories');

if ( typeof limitstart == 'undefined' ) { limitstart = jQuery(el).attr('g:limitstart') || 0; }
if ( typeof limit == 'undefined' ) { limit = 20; }

var func = function( data ) {
jQuery('#ajax-loading-img').remove();
jcache.set( context, data, {expiry: date_times.seconds( date_times.hour/2 )} );

if ( !data.length ) {
japp.unbind_scroll_listener(); // Remove listener for image at the bottom of page
japp._stopped_loading('k2_categories', true);
return;
}

el = jQuery(el);
acl = japp.get_joomla_accesslevels();

// Add each category to lsit
jQuery(data).each(function(){
state = japp.get_item_state( 1 /* this.published */ );
access = '';
for (var i = 0; i < acl.length; i++) {
if ( acl[i].value == this.access ) {
access = acl[i].text;
};
};
jQuery(el).append('<li><a href="category.html?id=' + this.id + '">'
+ '<h3>' + this.treename + '</h3>'
+ '<p><span class="item-' + state.toLowerCase() + '">'
+ state + '</span> / <span class="item-'
+ access.toLowerCase() + '">'
+ access + '</span></p>'
+ '</a></li>');
});

jQuery(el).listview('refresh').attr('g:limitstart',
parseInt( limitstart ) + parseInt( limit ) );

jQuery(el).append('<li id="ajax-loading-img"><img src="'
+ japp.ajax_loader + '" /></li>');

if ( 0 == limitstart ) {
japp.scroll_bottom_listener( '#ajax-loading-img',
function(){ japp.load_k2_categories(); } );
}

japp._stopped_loading('k2_categories', true);
};

var context = 'k2.categories.' + limitstart + '.' + limit;
if ( japp.cache && !fresh && jcache.get( context ) ) {
func( jcache.get( context ) );
} else {
this._ajax(
{
app: 'k2',
resource: 'categories',
limitstart: limitstart,
limit: limit
}, func );
}
};

japp.load_k2_category = function( id ) {
category = this.get_k2_category( id );

// Populate all category fields
jQuery('#category-name').val(category.name);
jQuery('#category-alias').val(category.alias);
jQuery('#category-parent').val(category.parent).selectmenu();
jQuery('#params_inheritFrom').selectmenu();
jQuery('#category-extraFieldsGroup').val(category.extraFieldsGroup).selectmenu();
jQuery('#category-published').val(category.published).slider('refresh');
jQuery('#category-access').val(category.access).selectmenu();
jQuery('#category-language').val(category.language).selectmenu();
jQuery('#category-description').val(category.description);
for ( param in category.params ) {
if ( 'theme' == param ) {
_el = jQuery('#params' + param);
_el.val(category.params[param]);
if ( typeof _el[0] != 'undefined' ) {
if ( 'SELECT' == _el[0].tagName ) {
_el.selectmenu('refresh');
}
}
} else {
jQuery('[name="params[' +param+ ']"]' ).each(function(){
if ( 'radio' != jQuery(this).attr('type') ) {
_el = jQuery(this);
_el.val(category.params[param]);
if ( typeof _el[0] != 'undefined' ) {
if ( 'SELECT' == _el[0].tagName ) {
_el.selectmenu('refresh');
}
}
} else if ( jQuery(this).val() == category.params[param] ) {
_el = jQuery(this);
_el.attr('checked', true).checkboxradio('refresh');
} else {
jQuery(this).attr('checked', false).checkboxradio('refresh');
return;
}

_el.val(category.params[param]);
});
}
};
jQuery('#category-id').val(category.id);

this._stop_loader();
}

japp.get_k2_category = function( id, fresh ) {
var context = 'k2.category.' + id;
if ( japp.cache && !fresh && jcache.get( context ) ) {
return jcache.get( context );
}

this._ajax(
{
app: 'k2',
resource: 'category',
cid: id
},
function( data ) {
// Try again?
if ( japp._object_empty( data ) ) {
japp._try_server_request_again( 'get_k2_category', id,
function(){ japp.get_k2_category( id, fresh ); });
} else {
jcache.set( context, data );
}
}, { async: false });

return jcache.get( context );
}

73 changes: 71 additions & 2 deletions www/k2/assets/js/main.js
Expand Up @@ -22,8 +22,8 @@ jQuery('#page-k2-items').live('pageshow',function(event){
* 'pageshow' gets triggered after the page has been initialized
*/
jQuery('#page-k2-item').live('pagebeforecreate',function(event){
japp.load_k2_item_fields('item-view-options-listings');
japp.load_k2_item_fields('item-view-options');
japp.load_k2_form_fields('item', 'item-view-options-listings');
japp.load_k2_form_fields('item', 'item-view-options');
}).live('pageshow',function(event){
try {
// This only needs to be done for item pages, where you have a lot of form fields
Expand Down Expand Up @@ -119,3 +119,72 @@ jQuery('#page-k2-tag').live('pageshow',function(event){
setTimeout('func();', 250);
} catch(e){japp._stop_loader();}
});

jQuery('#page-k2-categories').live('pageshow',function(event){
try { // Wrap in try/catch in case there is a JS error, the app won't crash
el = jQuery('#k2-categories-list ul');
if ( !el.html() ) {
japp._start_loader();
japp.load_k2_categories();
}
} catch(e){japp._stop_loader();}
});

jQuery('#page-k2-category').live('pagebeforecreate',function(event){
japp.load_k2_form_fields('category', 'category-item-layout');
japp.load_k2_form_fields('category', 'category-view-options');
japp.load_k2_form_fields('category', 'item-image-options');
japp.load_k2_form_fields('category', 'item-view-options-listings');
japp.load_k2_form_fields('category', 'item-view-options');
japp.load_k2_form_fields('category', 'category-metadata-information');
}).live('pageshow',function(event){
try {
// This only needs to be done for category pages, where you have a lot of form fields
// Wrap everything in a function and trigger it later
func = function() {
// Get the category id
id = _gup( 'id' );

// Load data for dynamic dropdowns and other html elements
categories = japp.get_k2_categories();
extrafieldsgroups = japp.get_k2_extrafieldsgroups();
access = japp.get_joomla_accesslevels();
languages = japp.get_content_language();

categories.unshift({value:0, text:'-- None --'});
extrafieldsgroups.unshift({'id':0, name:'-- None --'});

// Populate dynamic html elements
_populate_select( '#category-parent', categories, 'row.value', 'row.text' );
_populate_select( '#params_inheritFrom', categories, 'row.value', 'row.text' );
_populate_select( '#category-extraFieldsGroup', extrafieldsgroups, 'row.id', 'row.name' );
_populate_select( '#category-access', access, 'row.value', 'row.text' );
_populate_select( '#category-language', languages, 'row.value', 'row.text' );

// New category
if ( !id ) {
// Stop loader and initialize all select menus
japp._stop_loader();
jQuery('#category-parent,#category-params-inheritFrom,#category-extraFieldsGroup,'
+ '#category-access,#category-language').selectmenu();

// Set page title
jQuery('.page-title').html( 'New category' );

return;
}

japp.load_k2_category( id );

// Set page title
jQuery('.page-title').html( 'Edit category' );

jQuery('#category-delete').css('display', 'block');
}

// Start the loader
japp._start_loader();
// This needs to be triggered this way, otherwise the device doesn't like it
setTimeout('func();', 250);
} catch(e){japp._stop_loader();}
});
26 changes: 26 additions & 0 deletions www/k2/categories.html
@@ -0,0 +1,26 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript" charset="utf-8" src="../assets/js/loader.js"></script>
</head>
<body>
<div data-role="page" id="page-k2-categories">

<div data-role="header" data-theme="b" data-position="fixed">
<a href="index.html" data-rel="back" data-icon="arrow-l">K2</a>
<h1>Categories</h1>
<a href="category.html" data-icon="plus" class="ui-btn-right">New</a>
</div><!-- /header -->

<div data-role="content">
<div id="k2-categories-list">
<ul data-role="listview" data-filter="true"></ul>
</div>
</div><!-- /content -->
</div><!-- /page -->

</body>
</html>

0 comments on commit 5b2a0f9

Please sign in to comment.