Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function renderCategoriesListForAddition(parentCategory_ID, containerID){
  2.     var parentCategory = CategoriesManager.requestCategoryByID(parentCategory_ID);
  3.     parentCategory.done(function(category){
  4.         parentCategory = category;
  5.     });
  6.     var call = CategoriesManager.requestListOfCategories(parentCategory_ID);
  7.     call.done(function(categoriesList){
  8.         $('#'+containerID).html('<ons-list-header>Choose the destination of your new category</ons-list-header>');
  9.         var addHere = '<ons-list-item tappable onclick="insertCategory(\''+parentCategory_ID+'\', \''+containerID+'\')">';
  10.         addHere += '<div class="left">';
  11.         addHere += '<ons-icon icon="md-plus"></ons-icon>';
  12.         addHere += '</div>';
  13.         addHere += '<div class="center">';
  14.         addHere += 'Add Here';
  15.         addHere += '</div>';
  16.         addHere += '</ons-list-item>';
  17.         if(parentCategory_ID){
  18.             if(!parentCategory.parentCategory_ID){
  19.                 parentCategory.parentCategory_ID = '';
  20.             }
  21.             var onclick = "renderCategoriesListForAddition('"+parentCategory.parentCategory_ID+"', '"+containerID+"')";
  22.             var goBack = '<ons-list-item tappable onclick="'+onclick+'">';
  23.             goBack += '<div class="left">';
  24.             goBack += '';
  25.             goBack += '</div>';
  26.             goBack += '<div class="center">'+parentCategory.name+'</div>';
  27.             goBack += '</ons-list-item>';
  28.             $('#'+containerID).append(goBack);         
  29.         }
  30.         $('#'+containerID).append(addHere);
  31.         for(var i = 0; i < categoriesList.length; i++){
  32.             appendListItem(categoriesList[i], containerID);
  33.         }
  34.     });
  35.    
  36.     var appendListItem = function(category, containerID){
  37.         $('#'+containerID).append(getListItem(category));
  38.     }
  39.    
  40.     var getListItem = function(category){
  41.         var result = '';
  42.         result += '<ons-list-item modifier="chevron" tappable ';
  43.         result += 'onclick="renderCategoriesListForAddition(\''+category.ID+'\', \''+containerID+'\')"> ';
  44.        
  45.         result += '<div class="left"></div>'
  46.         result += '<div class="center">';
  47.         result += category.name;
  48.         result += '</div>';
  49.         result += '</ons-list-item>';
  50.         return result;
  51.     }
  52.    
  53.  
  54. }
  55.  
  56.  function insertCategory(parentCategory_ID, containerID){
  57.         ons.notification.prompt({
  58.             title: 'New Category',
  59.             message: 'Enter a name for your new category',
  60.             cancelable: 'cancelable'
  61.         }).then(function(categoryName){
  62.             if(!categoryName){
  63.                 ons.notification.alert({
  64.                     title: 'Error',
  65.                     message: 'Category name cannot be empty'
  66.                 });
  67.             } else {
  68.                 var addition = CategoriesManager.saveCategory(categoryName, '', parentCategory_ID, '');
  69.                 addition.done(function(category){
  70.                     ons.notification.alert({
  71.                         title: 'Success',
  72.                         message: 'Your new category has been added'
  73.                     }).then(function(){
  74.                         if(!parentCategory_ID){
  75.                             parentCategory_ID = ''; // make sure it's exactly an empty string
  76.                         }
  77.                         renderCategoriesListForAddition(parentCategory_ID, containerID);
  78.                     });
  79.                 });
  80.             }
  81.         });
  82.        
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement