Advertisement
kator

app.js

Dec 17th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var url = 'http://localhost:8080/';
  2.  
  3. var app = angular.module('myApp', ['ngResource', 'ui.grid', 'ngTouch', 'ngRoute']);
  4.  
  5. //Factories
  6. app.factory('TeamFactory', function ($resource) {
  7.     return $resource(url + 'druzyny/:id', {id: '@id'}, {
  8.         'update': {method: 'PUT'}
  9.     });
  10. });
  11. app.factory('Factory', function ($resource) {
  12.     return $resource(url + '/:id', {id: '@id'}, {});
  13. });
  14.  
  15. app.factory('PlayerFactory', function ($resource) {
  16.     return $resource(url + 'pilkarze/:id', {id: '@id'}, {
  17.         'update': {method: 'PUT'}
  18.     });
  19. });
  20.  
  21. //Templatki
  22. var editTemplate = {
  23.     name: 'edit',
  24.     displayName: 'Edycja',
  25.     enableSorting: false,
  26.     enableFiltering: false,
  27.     enableHiding: false,
  28.     cellTemplate: '<button id="editBtn" type="button" class="btn-small" ng-click="grid.appScope.edit(row.entity)" >Edytuj</button> '
  29. };
  30.  
  31. var delTemplate = {
  32.     name: 'delete',
  33.     displayName: 'Usuwanie',
  34.     enableSorting: false,
  35.     enableFiltering: false,
  36.     enableHiding: false,
  37.     cellTemplate: '<button id="deleteBtn" type="button" class="btn-small" ng-click="grid.appScope.delete(row.entity)" >Usun</button> '
  38. };
  39.  
  40. //CRUD
  41. var addF = function (entry, Factory, reload) {
  42.     entry.id = 0;
  43.     entry = copyOf(entry);
  44.     Factory.save(angular.toJson(entry),
  45.         reload(),
  46.         alert(resp));
  47. };
  48.  
  49. var deleteF = function (entry, Factory, reload) {
  50.     Factory.delete(entry,
  51.         reload(),
  52.         alert(resp));
  53. };
  54.  
  55. var updateF = function (entry, Factory, reload) {
  56.     entry = copyOf(entry);
  57.     var json = angular.toJson(entry);
  58.     Factory.update({id: entry.id}, json,
  59.         reload(),
  60.         alert(resp));
  61. };
  62.  
  63. var maxId = function (data) {
  64.     var max = 0;
  65.     data.forEach(function (entry) {
  66.         max = (max > entry.id) ? max : entry.id;
  67.     });
  68.     return max + 1;
  69. };
  70.  
  71. var copyOf = function (object) {
  72.     return angular.copy(object);
  73. };
  74.  
  75. //Controlers
  76. app.controller('indexCtrl', function ($scope, $rootScope, $route, $window) {
  77.  
  78.     $scope.strony = [
  79.         {name: 'Druzyny', url: 'data/teams.html', hidden: false},
  80.         {name: 'Zawodnicy', url: 'data/players.html', hidden: false},
  81.         {name: 'Pusta Strona', url: 'data/empty.html', hidden: true}
  82.     ];
  83.  
  84.     $scope.updateView = function (src) {
  85.         $scope.view = src;
  86.     };
  87.  
  88.     var alert = function (msg) {
  89.         $window.alert(msg);
  90.     }
  91. });
  92.  
  93.  
  94. app.controller('teamCtrl', function ($scope, TeamFactory) {
  95.     //required vars
  96.     var columns = [
  97.         {
  98.             name: 'id',
  99.             width: '6%',
  100.             enableFiltering: false
  101.         },
  102.         {
  103.             name: 'nazwa',
  104.             enableCellEdit: true,
  105.             enableFiltering: true
  106.         },
  107.         editTemplate,
  108.         delTemplate
  109.     ];
  110.     var Factory = TeamFactory;
  111.  
  112.     function empty() {
  113.         return {id: 0, nazwa: ''}
  114.     }
  115.  
  116.     $scope.showEdit = false;
  117.     $scope.entry = empty();
  118.     $scope.edited = empty();
  119.  
  120.     var data = [empty()];
  121.  
  122.     var reload = function () {
  123.         data = Factory.query(function () {
  124.             $scope.grid.data = data;
  125.         }, function (resp) {
  126.             console.log(resp);
  127.         });
  128.     };
  129.  
  130.     reload();
  131.  
  132.     $scope.grid = {
  133.         enableFiltering: true,
  134.         enableSorting: true,
  135.         enableCellSelection: true,
  136.         enableCellEditOnFocus: true,
  137.         columnDefs: columns,
  138.         data: data
  139.     };
  140.  
  141.     //CRUD
  142.     $scope.add = function (row) {
  143.         row.id = 0;
  144.         addF(row, Factory, reload);
  145.         $scope.entry = empty();
  146.     };
  147.  
  148.     $scope.edit = function (row) {
  149.         $scope.showEdit = true;
  150.         $scope.edited = copyOf(row);
  151.     };
  152.  
  153.     $scope.delete = function (row) {
  154.         deleteF(row, Factory, reload);
  155.     };
  156.  
  157.     $scope.update = function (row) {
  158.         updateF(row, Factory, reload);
  159.         $scope.showEdit = false;
  160.         $scope.edited = empty();
  161.     };
  162.  
  163. });
  164.  
  165. app.controller('playerCtrl', function ($scope, PlayerFactory) {
  166.     var Factory = PlayerFactory;
  167.     var columns = [
  168.         {
  169.             field: 'id',
  170.             displayName: 'Id',
  171.             width: "*",
  172.             enableFiltering: false
  173.         },
  174.         {
  175.             field: 'pesel',
  176.             displayName: 'Pesel',
  177.             width: "*"
  178.         },
  179.         {
  180.             field: 'imie',
  181.             displayName: 'Imie',
  182.             width: "*",
  183.         },
  184.         {
  185.             field: 'nazwisko',
  186.             displayName: 'Nazwisko',
  187.             width: "*"
  188.         },
  189.         editTemplate,
  190.         delTemplate
  191.     ];
  192.  
  193.     function empty() {
  194.         return {
  195.             id: 0,
  196.             pesel: '',
  197.             imie: '',
  198.             nazwisko: ''
  199.         }
  200.  
  201.     }
  202.  
  203.     $scope.showEdit = false;
  204.     $scope.entry = empty();
  205.     $scope.edited = empty();
  206.  
  207.     var data = [empty()];
  208.  
  209.     var reload = function () {
  210.         data = Factory.query(function () {
  211.             $scope.grid.data = data;
  212.         }, function (resp) {
  213.             console.log(resp);
  214.         });
  215.     };
  216.  
  217.     reload();
  218.  
  219.     $scope.grid = {
  220.         enableFiltering: true,
  221.         enableSorting: true,
  222.         enableCellSelection: true,
  223.         enableCellEditOnFocus: true,
  224.         columnDefs: columns,
  225.         data: data
  226.     };
  227.  
  228.     //CRUD
  229.     $scope.add = function (row) {
  230.         row.id = 0;
  231.         addF(row, Factory, reload);
  232.         $scope.entry = empty();
  233.     };
  234.  
  235.     $scope.edit = function (row) {
  236.         $scope.showEdit = true;
  237.         $scope.edited = copyOf(row);
  238.     };
  239.  
  240.     $scope.delete = function (row) {
  241.         deleteF(row, Factory, reload);
  242.     };
  243.  
  244.     $scope.update = function (row) {
  245.         updateF(row, Factory, reload);
  246.         $scope.showEdit = false;
  247.         $scope.edited = empty();
  248.     };
  249.  
  250. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement