Project

General

Profile

« Previous | Next » 

Revision 61419

[Trunk | Monitor Dashboard]:
1. indicators.component.html: Added buttons for exporting and importing indicators of a specific subcategory.
2. indicators.component.ts: Added methods for exporting and importing indicators of a specific subcategory (import of indicators is not creating anything for now).

View differences:

indicators.component.ts
43 43
  styleUrls: ['indicators.component.css']
44 44
})
45 45
export class IndicatorsComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
46
  
46
  filesToUpload: Array<File>;
47
  errorMessage = "";
48
  enableUpload: boolean = true;
49

  
47 50
  @Input()
48 51
  public properties: EnvProperties = null;
49 52
  @Input()
......
110 113
              private router: Router,
111 114
              private cdr: ChangeDetectorRef,
112 115
              private sanitizer: DomSanitizer) {
116
    this.filesToUpload = [];
113 117
  }
114 118
  
115 119
  ngOnInit(): void {
......
1187 1191
    this.showCheckForSchemaEnhancements = this.isAdministrator && url && !this.properties.useOldStatisticsSchema && this.indicatorUtils.checkForSchemaEnhancements(url);
1188 1192
  }
1189 1193

  
1194
  public export_indicators() {
1195
    console.debug("Export indicators");
1196

  
1197
    let indicators = [];
1198
    let index: number = 0;
1199

  
1200
    this.displayNumbers.forEach(section => {
1201
      section.indicators.forEach(indicator => {
1202
        indicator.indicatorPaths.forEach(indicatorPath => {
1203
          console.debug("export number: ", this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath)));
1204
          indicators[index] = {
1205
            "type": indicator.type, "name": indicator.name,
1206
            "description": indicator.description, "additionalDescription": indicator.additionalDescription,
1207
            "visibility": indicator.visibility, "width": indicator.width, "height": indicator.height,
1208
            "url": this.statisticsService.getNumberUrl(indicatorPath.source, this.indicatorUtils.getFullUrl(this.stakeholder, indicatorPath))};
1209
          index++;
1210
        });
1211
      });
1212
    });
1213

  
1214
    this.displayCharts.forEach(section => {
1215
      section.indicators.forEach(indicator => {
1216
        indicator.indicatorPaths.forEach(indicatorPath => {
1217
          console.debug("export chart: "+this.getUrlByStakeHolder(indicatorPath));
1218
          indicators[index] = {
1219
            "type": indicator.type, "name": indicator.name,
1220
            "description": indicator.description, "additionalDescription": indicator.additionalDescription,
1221
            "visibility": indicator.visibility, "width": indicator.width, "height": indicator.height,
1222
            "url": this.getUrlByStakeHolder(indicatorPath)};
1223
          index++;
1224
        });
1225
      });
1226
    });
1227

  
1228
    let topic = this.stakeholder ? this.stakeholder.topics[this.topicIndex] : null;
1229
    let category = topic ? topic.categories[this.categoryIndex] : null;
1230
    let subCategory = category ? category.subCategories[this.subcategoryIndex] : null;
1231

  
1232
    var jsonFileUrl = window.URL.createObjectURL(new Blob([JSON.stringify(indicators)], {type: 'application/json'}));
1233
    var a = window.document.createElement('a');
1234
    window.document.body.appendChild(a);
1235
    a.setAttribute('style', 'display: none');
1236
    a.href = jsonFileUrl;
1237
    a.download = this.stakeholder.alias + "_"+topic.alias + "_" + category.alias + "_" + subCategory.alias + ".json";
1238
    a.click();
1239
    window.URL.revokeObjectURL(jsonFileUrl);
1240
    a.remove(); // remove the element
1241
  }
1242

  
1243
  fileChangeEvent(fileInput: any) {
1244
    this.filesToUpload = <Array<File>>fileInput.target.files;
1245
    this.upload();
1246
  }
1247

  
1248
  upload() {
1249
    this.enableUpload = false;
1250
    // this.errorMessage = "";
1251
    console.debug(this.filesToUpload);
1252
    if (this.filesToUpload.length == 0) {
1253
      console.error("There is no selected file to upload.");
1254
      // this.errorMessage = "There is no selected file to upload.";
1255
      return;
1256
    } else {
1257
      if (this.filesToUpload[0].name.indexOf(".json") == -1 || (this.filesToUpload[0].type != "application/json")) {
1258
        // this.errorMessage = "No valid file type. The required type is JSON";
1259
        console.error("No valid file type. The required type is JSON");
1260
        return;
1261
      }
1262
    }
1263
    // this.loading.open();
1264

  
1265
    this.makeFileRequest(this.properties.utilsService + '/upload?type=json', [], this.filesToUpload).then(async (result: string) => {
1266
      // const rows = (result as any).split('\n');  // I have used space, you can use any thing.
1267
      let invalid_rows = 0;
1268

  
1269
      // let chartSection = null;//this.createSection(-1, "chart");
1270
      // let numberSection = null;//this.createSection(-1, "number");
1271
      // await this.newSectionReady;
1272

  
1273
      let json_result = JSON.parse(result);
1274
      for (let i = 0; i < (json_result.length); i++) {
1275
        if (json_result[i] && json_result[i] != null && json_result[i] != "") {
1276
          this.processIndicatorEntry(json_result[i]);
1277
        } else {
1278
          invalid_rows++;
1279
        }
1280
      }
1281
      this.endOfFetching();
1282
    }, (error) => {
1283
      this.enableUpload = true;
1284
      console.error("An error occurred");
1285
    });
1286
  }
1287

  
1288
  makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
1289
    return new Promise((resolve, reject) => {
1290
      const formData: any = new FormData();
1291
      const xhr = new XMLHttpRequest();
1292
      for (let i = 0; i < files.length; i++) {
1293
        formData.append("uploads[]", files[i], files[i].name);
1294
      }
1295
      xhr.onreadystatechange = function () {
1296
        if (xhr.readyState == 4) {
1297
          if (xhr.status == 200) {
1298
            resolve(xhr.response);
1299
          } else {
1300
            reject(xhr.response);
1301
          }
1302
        }
1303
      };
1304
      xhr.open("POST", url, true);
1305
      xhr.send(formData);
1306
    });
1307
  }
1308

  
1309
  endOfFetching() {
1310
    //   this.showReport = true;
1311
    this.enableUpload = true;
1312
  }
1313

  
1314
  processIndicatorEntry(indicator) {
1315
    if(indicator.type == "chart") {
1316
      console.debug("TODO Create chart for url: "+indicator.url);
1317
      //this.editChartIndicatorOpen(chartSection, null, false, indicator.url);
1318
    } else if(indicator.type == "number") {
1319
      console.debug("TODO Create number for url: "+indicator.url);
1320
      //this.editNumberIndicatorOpen(numberSection);
1321
    }
1322
    console.debug(indicator);
1323
    // this.saveIndicator(false);
1324
  }
1190 1325
}

Also available in: Unified diff