Pārlūkot izejas kodu

Refactor AdjusterService to class

jmacura 5 gadi atpakaļ
vecāks
revīzija
78126db4c0
3 mainītis faili ar 112 papildinājumiem un 103 dzēšanām
  1. 2 2
      src/adjuster/adjuster.module.js
  2. 78 101
      src/adjuster/adjuster.service.js
  3. 32 0
      src/adjuster/factors.js

+ 2 - 2
src/adjuster/adjuster.module.js

@@ -1,5 +1,5 @@
 import adjusterComponent from './adjuster.component.js';
-import adjusterService from './adjuster.service.js';
+import {AdjusterService} from './adjuster.service.js';
 
 angular
   .module('pra.adjuster', ['hs.core', 'hs.map'])
@@ -9,6 +9,6 @@ angular
     };
   })
 
-  .service('PraAdjusterService', adjusterService)
+  .service('PraAdjusterService', AdjusterService)
 
   .component('pra.adjuster', adjusterComponent);

+ 78 - 101
src/adjuster/adjuster.service.js

@@ -1,111 +1,90 @@
 // import attractivity from '../Attractivity.json';
 import nuts from '../nuts.js';
+import {factors} from './factors.js';
 
-export default function (HsCore, HsUtilsService, $rootScope, $http) {
-  'ngInject';
-  const me = {
-    factors: [
-      {
-        name: 'Natural',
-        column: 'N_index',
-        weight: 1,
-      },
-      {
-        name: 'Social & Human',
-        column: 'S_index',
-        weight: 1,
-      },
-      {
-        name: 'Anthropic',
-        column: 'A_index',
-        weight: 1,
-      },
-      {
-        name: 'Economical',
-        column: 'E_index',
-        weight: 1,
-      },
-      {
-        name: 'Cultural',
-        column: 'C_index',
-        weight: 1,
-      },
-      {
-        name: 'Institutional',
-        column: 'I_index',
-        weight: 1,
-      },
-    ],
-    apply() {
-      HsUtilsService.debounce(function () {
-        $http({
-          method: 'post',
-          url: 'https://publish.lesprojekt.cz/nodejs/scores',
-          data: {
-            factors: me.factors.map((f) => {
-              return {
-                factor: f.factor,
-                weight: f.weight,
-                datasets: f.datasets
-                  .filter((ds) => ds.included)
-                  .map((ds) => ds.name),
-              };
-            }),
-          },
-        }).then((response) => {
-          me.attractivity = response.data;
-          let max = 0;
-          me.attractivity.forEach((a) => {
-            if (a.aggregate > max) {
-              max = a.aggregate;
-            }
-          });
-          const normalizer = 1 / max;
-          me.attractivity.forEach((a) => {
-            a.aggregate *= normalizer;
-          });
-          me.attractivity.forEach((a) => {
-            me.nutsCodeRecordRelations[a.code] = a;
-          });
-          nuts.nuts3Source.forEachFeature((feature) => {
-            feature.set(
-              'total',
-              me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate
-            );
+export class AdjusterService {
+  constructor(HsCore, HsUtilsService, $rootScope, $http) {
+    'ngInject';
+    this.HsCore = HsCore;
+    this.HsUtilsService = HsUtilsService;
+    this.$rootScope = $rootScope;
+    this.$http = $http;
+    this.factors = factors;
+    this.nutsCodeRecordRelations = {};
+    this.init();
+  }
+
+  apply() {
+    const f = () => {
+      this.$http({
+        method: 'post',
+        url: 'https://publish.lesprojekt.cz/nodejs/scores',
+        data: {
+          factors: this.factors.map((f) => {
+            return {
+              factor: f.factor,
+              weight: f.weight,
+              datasets: f.datasets
+                .filter((ds) => ds.included)
+                .map((ds) => ds.name),
+            };
+          }),
+        },
+      }).then((response) => {
+        this.attractivity = response.data;
+        let max = 0;
+        this.attractivity.forEach((a) => {
+          if (a.aggregate > max) {
+            max = a.aggregate;
+          }
+        });
+        const normalizer = 1 / max;
+        this.attractivity.forEach((a) => {
+          a.aggregate *= normalizer;
+        });
+        this.attractivity.forEach((a) => {
+          this.nutsCodeRecordRelations[a.code] = a;
+        });
+        nuts.nuts3Source.forEachFeature((feature) => {
+          feature.set(
+            'total',
+            this.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate
+          );
+          feature.set(
+            'totalForHumans',
+            (
+              this.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate *
+              100
+            ).toFixed(2)
+          );
+          this.factors.forEach((factor) => {
             feature.set(
-              'totalForHumans',
+              factor.factor,
               (
-                me.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate *
-                100
+                this.nutsCodeRecordRelations[feature.get('NUTS_ID')][
+                  factor.factor
+                ] * 100
               ).toFixed(2)
             );
-            me.factors.forEach((factor) => {
-              feature.set(
-                factor.factor,
-                (
-                  me.nutsCodeRecordRelations[feature.get('NUTS_ID')][
-                    factor.factor
-                  ] * 100
-                ).toFixed(2)
-              );
-            });
           });
         });
-        /*
-
-            */
-      }, 300)();
-    },
-  };
-  me.nutsCodeRecordRelations = {};
+      });
+    };
+    this.HsUtilsService.debounce(f, 300)();
+  }
 
-  $http({url: 'https://publish.lesprojekt.cz/nodejs/datasets'}).then(
-    (response) => {
-      me.factors = response.data.map((dataset) => {
+  init() {
+    this.$http({
+      url: 'https://publish.lesprojekt.cz/nodejs/datasets',
+    }).then((response) => {
+      this.factors = response.data.map((dataset) => {
         return {factor: dataset.Factor, weight: 1, datasets: []};
       });
-      me.factors = HsUtilsService.removeDuplicates(me.factors, 'factor');
-      me.factors.forEach((factor) => {
+      this.factors = this.HsUtilsService.removeDuplicates(
+        this.factors,
+        'factor'
+      );
+      this.factors.forEach((factor) => {
         factor.datasets = response.data
           .filter((ds) => ds.Factor === factor.factor)
           .map((ds) => {
@@ -115,9 +94,7 @@ export default function (HsCore, HsUtilsService, $rootScope, $http) {
             };
           });
       });
-      me.apply();
-    }
-  );
-
-  return me;
+      this.apply();
+    });
+  }
 }

+ 32 - 0
src/adjuster/factors.js

@@ -0,0 +1,32 @@
+export const factors = [
+  {
+    name: 'Natural',
+    column: 'N_index',
+    weight: 1,
+  },
+  {
+    name: 'Social & Human',
+    column: 'S_index',
+    weight: 1,
+  },
+  {
+    name: 'Anthropic',
+    column: 'A_index',
+    weight: 1,
+  },
+  {
+    name: 'Economical',
+    column: 'E_index',
+    weight: 1,
+  },
+  {
+    name: 'Cultural',
+    column: 'C_index',
+    weight: 1,
+  },
+  {
+    name: 'Institutional',
+    column: 'I_index',
+    weight: 1,
+  },
+];