Commits

Konstantin Mochalov  committed 689ddc0

Select, delete

  • Participants
  • Parent commits a3af1e1

Comments (0)

Files changed (3)

File tasks/static/tasks.js

 angular.module('tasks', ['ngResource']).
   controller('TasksCtrl', ['$scope', '$resource', function($scope, $resource) {
     $scope.tasks_api = $resource('/tasks')
+    $scope.task_api = $resource('/tasks/:id')
 
     $scope.loading = false
+    $scope.selected_task_id = null
     $scope.table_headers = [
       ["id", "Id"],
       ["description", "Description"],
       $scope.reload()
     }
 
+    $scope.selectTask = function(id) {
+      $scope.selected_task_id = id
+    }
+
     $scope.reload = function() {
       $scope.loading = true
       $scope.tasks_api.get({
       })
     }
 
+    $scope.is_selected_task_in_loaded_tasks = function() {
+      return $scope.selected_task_id &&
+        _.contains(_.map($scope.tasks, function(t) {return t.id}),
+          $scope.selected_task_id)
+    }
+
+    $scope.task_row_style = function(id) {
+      if ($scope.selected_task_id == id) {
+        return 'active'
+      }
+    }
+
+    $scope.openEditor = function(id) {
+      console.log("Open editor", id)
+    }
+
+    $scope.delete = function(id) {
+      $scope.task_api.delete({id: id}, function() {
+        $scope.loading = true
+        $scope.reload()
+      })
+    }
+
     $scope.reload()
   }])

File tasks/templates/index.html

     <h1>Tasks</h1>
 
     <div class="btn-group">
-      <button type="button" class="btn btn-default">New</button>
-      <button type="button" class="btn btn-default">Edit</button>
-      <button type="button" class="btn btn-default">Delete</button>
+      <button type="button" 
+        class="btn btn-default"
+        ng-click="openEditor(-1)">
+        New</button>
+      <button type="button" 
+        class="btn btn-default"
+        ng-click="openEditor(selected_task_id)"
+        ng-disabled="! is_selected_task_in_loaded_tasks()">
+        Edit</button>
+      <button type="button"
+        class="btn btn-default"
+        ng-click="delete(selected_task_id)"
+        ng-disabled="! is_selected_task_in_loaded_tasks()">
+        Delete</button>
     </div>
 
-    <table class="table table-hover" id="tasks-table">
+    <table class="table" id="tasks-table">
       <thead>
         <tr class="sortable-columns">
           <th ng-repeat="th in table_headers" ng-class="selectedCls(th[0])" ng-click="changeSorting(th[0])">{{th[1]}}</th>
         </tr>
       </thead>
       <tbody>
-        <tr ng-repeat="task in tasks">
+        <tr ng-repeat="task in tasks" ng-dblclick="openEditor(task.id)" ng-class="task_row_style(task.id)" ng-click="selectTask(task.id)">
           <td>{{ task.id }}</td>
           <td>{{ task.description }} </td>
           <td>{{ task.start_date }}</td>
 
     <ul class="pagination">
       <li><a href="#">&laquo;</a></li>
-      <li 
+      <li
         ng-repeat="p in pages()" 
         ng-class="pager_page_class(p)"
         ng-click="selectPage(p)">

File test_tasks/settings.py

 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
-    'django.middleware.csrf.CsrfViewMiddleware',
+    #'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     # Uncomment the next line for simple clickjacking protection: