1 | define('controllers/TeamImportDialogController', [
|
2 | 'ng-file-upload',
|
3 | 'services/ng-teams',
|
4 | 'services/dialogs',
|
5 | 'angular'
|
6 | ], function () {
|
7 | var moduleName = 'TeamImportDialog';
|
8 |
|
9 | return angular.module(moduleName, ['ngFileUpload']).controller('TeamImportDialogController', [
|
10 | '$scope', '$dialogs', '$teams',
|
11 | function ($scope, $dialogs, $teams) {
|
12 | var BRIAN_LEES_SCHEDULER_FORMAT = /^Version Number,\d+,*\s+Block Format,\d+,*\s+Number of Teams,(\d+),*\s+((.|\s)*)$/;
|
13 | var BRIAN_LEES_SCHEDULER_DELIMITER = ',';
|
14 |
|
15 | $scope.parseData = function() {
|
16 | if(!$scope.importRaw) {
|
17 | $scope.importLines = [];
|
18 | $scope.importNumberExample = '';
|
19 | $scope.importNameExample = '';
|
20 | return;
|
21 | }
|
22 |
|
23 |
|
24 | var lines = $scope.importRaw.match(/[^\r\n]+/g);
|
25 | var headerLength = $scope.headerLength ? $scope.headerLength : 0;
|
26 | lines.splice(0, headerLength);
|
27 | lines = lines.map(function (line) {
|
28 | if ($scope.useCustomDelimiter) {
|
29 | return line.split($scope.delimiter);
|
30 | }
|
31 |
|
32 | return line.split(/\t/);
|
33 | });
|
34 |
|
35 | $scope.importNumberColumn = 1;
|
36 | $scope.importNameColumn = 2;
|
37 |
|
38 | lines = lines.filter((parsedLine) => parsedLine[$scope.importNumberColumn - 1] !== "");
|
39 |
|
40 | if (lines[0]) {
|
41 | $scope.importNumberExample = lines[0][$scope.importNumberColumn - 1];
|
42 | $scope.importNameExample = lines[0][$scope.importNameColumn - 1];
|
43 | } else {
|
44 | $scope.importNumberExample = '';
|
45 | $scope.importNameExample = '';
|
46 | }
|
47 |
|
48 | $scope.importLines = lines;
|
49 | }
|
50 |
|
51 | $scope.upload = function (files, file) {
|
52 | if (file === null) {
|
53 |
|
54 | return;
|
55 | }
|
56 | var reader = new FileReader();
|
57 | reader.onload = (event) => {
|
58 |
|
59 | var data = event.target.result;
|
60 | var brianLeesFormattedData = data.match(BRIAN_LEES_SCHEDULER_FORMAT);
|
61 | if (brianLeesFormattedData) {
|
62 | $scope.useCustomDelimiter = true;
|
63 | $scope.delimiter = BRIAN_LEES_SCHEDULER_DELIMITER;
|
64 | $scope.headerLength = 0;
|
65 | var teamCount = parseInt(brianLeesFormattedData[1]);
|
66 | var lines = brianLeesFormattedData[2].split("\n");
|
67 | $scope.importRaw = lines.splice(0, teamCount).join("\n");
|
68 | } else {
|
69 | $scope.useCustomDelimiter = true;
|
70 | $scope.delimiter = ",";
|
71 | $scope.importRaw = data;
|
72 | }
|
73 | $scope.parseData();
|
74 | };
|
75 | reader.readAsText(file);
|
76 |
|
77 | };
|
78 |
|
79 | $scope.save = function () {
|
80 | var closeDialog = true;
|
81 | var teams = $scope.importLines.map(function (line) {
|
82 | return {
|
83 | number: line[$scope.importNumberColumn - 1],
|
84 | name: line[$scope.importNameColumn - 1]
|
85 | };
|
86 | });
|
87 |
|
88 | if (teams) {
|
89 | try {
|
90 | $teams.clear();
|
91 | teams.forEach(function(team) {
|
92 | $teams.add({
|
93 | number: team.number,
|
94 | name: team.name
|
95 | });
|
96 | });
|
97 | $teams.save();
|
98 | } catch(e) {
|
99 | alert(`An error acoured trying to save the teams: ${e.message}`);
|
100 | closeDialog = false;
|
101 | }
|
102 | }
|
103 |
|
104 | if(closeDialog) {
|
105 | $scope.dialog.show = false;
|
106 | $scope.dialog.onClose ? $scope.dialog.onClose() : undefined;
|
107 | }
|
108 | };
|
109 |
|
110 | $scope.dialog = $dialogs.teamsImport;
|
111 |
|
112 | $scope.cancel = function () {
|
113 | $scope.importRaw = '';
|
114 | $scope.parseData();
|
115 | $scope.dialog.show = false;
|
116 | };
|
117 | }
|
118 | ]);
|
119 | });
|