'use strict';

(function() {
  var app = angular.module('app');

  app.controller('KeywordController', function($scope, RepositoryFactory, resolveEntity) {
    /* Initialize Front End */

    // This happens immediately upon page load
    $scope.resolveEntity = resolveEntity;

    // Connect controller to back-end APIs for keyword-categories & keywords
    var KeywordCategoriesRepo = new RepositoryFactory({
      endpoint: 'keywords/categories',
      retrieveItems: function(data) {
        return data._items;
      }
    });

    var KeywordsRepo = new RepositoryFactory({
      endpoint: 'keywords',
      retrieveItems: function(data) {
        return data._items;
      }
    });

    // Immediately load all keyword-categories & categories from API
    // upon page load
    KeywordCategoriesRepo.readAll()
      .then(function(keywordCategories) {
        $scope.keywordCategories = keywordCategories;
        KeywordsRepo.readAll()
          .then(function(keywords) {
            $scope.keywords = keywords;
          });
      });

    // The grid
    $scope.keywordsGridOptions = {
      // use the data from `$scope.keywords`
      data: 'keywords',
      // if true, break cell edits with the 'select' element
      enableCellSelection: false,
      enableCellEdit: true,
      keepLastSelected: false,
      enableRowSelection: false,
      multiSelect: false,
      enableSorting: true,
      enableColumnResize: true,
      enableColumnReordering: true,
      showFilter: false,
      rowHeight: '40',
      columnDefs: [
        {
          field: 'id',
          displayName: 'ID',
          enableCellEdit: false,
          width: '80px'
        },
        {
          field: 'value',
          displayName: 'Value'
        },
        {
          field: 'keywordCategoryID',
          displayName: 'Category',
          cellTemplate: 'app/keywords/partials/keywordsCategoryGridCell.html',
          editableCellTemplate: 'app/keywords/partials/keywordCategoryGridCellEditor.html'
        },
        {
          field: '',
          displayName: 'Operations',
          cellTemplate: 'app/keywords/partials/operationsGridCell.html',
          enableCellEdit: false,
          sortable: false
        }
      ]
    };
  });

  /* Front-end Operations & Interactions */

  // tell jshint to shut up about these two variables not being defined
  /* globals $scope, KeywordsRepo */

  $scope.createKeyword = function(newKeyword) {
    $scope.broadcast('ngGridEventEndCellEdit');

    if (newKeyword.value.length > 0) {
      KeywordsRepo.createOne(newKeyword)
        .then(function() {
          KeywordsRepo.readAll()
            .then(function(keywords) {
              $scope.keywords = keywords;
            });
        });
    }
  };

  $scope.updateKeyword = function(keyword) {
    $scope.broadcast('ngGridEventEndCellEdit');
    KeywordsRepo.updateOne(keyword);
  };

  $scope.deleteKeyword = function(keyword) {
    $scope.broadcast('ngGridEventEndCellEdit');
    KeywordsRepo.deleteOne(keyword)
      .then(function() {
        KeywordsRepo.readAll()
          .then(function(keywords) {
            $scope.keywords = keywords;
          });
      });
  };

  $scope.stopEditingKeywordCategory = function() {
    $scope.broadcast('ngGridEventEndCellEdit');
  };

  $scope.$on('ngGridEventRows', function(newRows) {
    $scope.broadcast('ngGridEventEndCellEdit');
  });

})();

@@ -83,16 +83,17 @@
 'Keyword
+s
 Controll
@@ -2234,23 +2234,19 @@
 %0A    %7D;%0A
+%0A
   
-%7D);%0A%0A
   /* Fro
@@ -2288,116 +2288,8 @@
 %0A%0A  
-// tell jshint to shut up about these two variables not being defined%0A  /* globals $scope, KeywordsRepo */%0A%0A
   $s
@@ -2328,32 +2328,34 @@
 wKeyword) %7B%0A    
+ 
+ 
 $scope.broadcast
@@ -2379,24 +2379,26 @@
 ellEdit');%0A%0A
+  
     if (newK
@@ -2426,24 +2426,26 @@
  0) %7B%0A      
+  
 KeywordsRepo
@@ -2467,32 +2467,34 @@
 eyword)%0A        
+ 
+ 
 .then(function()
@@ -2488,32 +2488,34 @@
 en(function() %7B%0A
+  
           Keywor
@@ -2535,32 +2535,34 @@
 l()%0A            
+  
 .then(function(k
@@ -2578,32 +2578,34 @@
 %7B%0A              
+ 
+ 
 $scope.keywords 
@@ -2624,28 +2624,32 @@
             
+  
 %7D);%0A
+  
         %7D);%0A
@@ -2656,16 +2656,22 @@
     
+ 
+ 
 %7D%0A
+  
   %7D;%0A%0A
+  
   $s
@@ -2707,32 +2707,34 @@
 (keyword) %7B%0A    
+  
 $scope.broadcast
@@ -2757,32 +2757,34 @@
 CellEdit');%0A    
+  
 KeywordsRepo.upd
@@ -2800,22 +2800,26 @@
 yword);%0A
+  
   %7D;%0A%0A
+  
   $scope
@@ -2851,32 +2851,34 @@
 (keyword) %7B%0A    
+ 
+ 
 $scope.broadcast
@@ -2901,24 +2901,26 @@
 CellEdit');%0A
+  
     Keywords
@@ -2941,32 +2941,34 @@
 (keyword)%0A      
+  
 .then(function()
@@ -2962,32 +2962,34 @@
 en(function() %7B%0A
+  
         Keywords
@@ -2995,32 +2995,34 @@
 sRepo.readAll()%0A
+  
           .then(
@@ -3046,32 +3046,34 @@
 ) %7B%0A            
+  
 $scope.keywords 
@@ -3086,32 +3086,34 @@
 ords;%0A          
+  
 %7D);%0A      %7D);%0A  
@@ -3110,18 +3110,24 @@
     
+ 
+ 
 %7D);%0A
+  
   %7D;%0A%0A
+  
   $s
@@ -3169,32 +3169,34 @@
 unction() %7B%0A    
+ 
+ 
 $scope.broadcast
@@ -3223,22 +3223,26 @@
 Edit');%0A
+  
   %7D;%0A%0A
+  
   $scope
@@ -3285,24 +3285,26 @@
 Rows) %7B%0A    
+  
 $scope.broad
@@ -3335,16 +3335,25 @@
 Edit');%0A
+    %7D);%0A%0A
   %7D);%0A%0A%7D

Fix KeywordsController