From cb58acda2c6db04c14bffc25d41ff55e3629f1ee Mon Sep 17 00:00:00 2001 From: Findeton Date: Wed, 8 Feb 2023 11:10:09 -0500 Subject: [PATCH 1/8] wip --- .../admin-sidebar-directive/admin-sidebar-directive.html | 8 +++++++- .../admin-sidebar-directive/admin-sidebar-directive.js | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html index 16e6b398..132c6fac 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html @@ -33,7 +33,13 @@ - + + diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js index 548aace0..9d2ac0db 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js @@ -48,6 +48,13 @@ angular.module('avAdmin') scope.showImport = !!$window.showOpenFilePicker; scope.isEditingDraft = DraftElection.isEditingDraft; scope.globalPerms = { val: '' }; + scope.selectFile = function () { + document.querySelector("#side-import-file").click(); + }; + scope.uploadFile = function (element) { + var f = element.files[0]; + console.log(f); + }; // update perms ElectionsApi From 8b0ab74011c49c53befd772fd07463da8fd687f1 Mon Sep 17 00:00:00 2001 From: Findeton Date: Wed, 8 Feb 2023 15:14:34 -0500 Subject: [PATCH 2/8] wip --- avAdmin/admin-controller/admin-controller.js | 42 ++++++++++--------- .../admin-sidebar-directive.html | 12 +++--- .../admin-sidebar-directive.js | 1 + 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index d4b3a722..fe64e157 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -127,15 +127,31 @@ angular return deferred.promise; } + function adminImportFile(fileHandle) + { + fileHandle + .getFile() + .then( + function (file) + { + file + .text() + .then( + function (fileText) + { + ElectionsApi.currentElections = JSON.parse(fileText); + $state.go("admin.create"); + } + ); + } + ); + } function adminImport() { if (!$window.showOpenFilePicker) { - console.log( - "please use a browser that supports window.showOpenFilePicker" - ); - return; + document.querySelector("#side-import-file").click(); } $window @@ -144,22 +160,7 @@ angular function (data) { var fileHandle = data[0]; - fileHandle - .getFile() - .then( - function (file) - { - file - .text() - .then( - function (fileText) - { - ElectionsApi.currentElections = JSON.parse(fileText); - $state.go("admin.create"); - } - ); - } - ); + adminImportFile(fileHandle); } ); @@ -352,6 +353,7 @@ angular getUpdateDraft(); $scope.adminImport = adminImport; + $scope.adminImportFile = adminImportFile; $scope.loadDraft = function () { // show a warning dialog before loading draft $modal diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html index 132c6fac..f9e110f5 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html @@ -33,13 +33,13 @@ + - diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js index 9d2ac0db..17ab4b28 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js @@ -54,6 +54,7 @@ angular.module('avAdmin') scope.uploadFile = function (element) { var f = element.files[0]; console.log(f); + scope.adminImportFile(f); }; // update perms From 33ce807019c324054c5e1a4e981522c6fa07a08a Mon Sep 17 00:00:00 2001 From: Findeton Date: Wed, 8 Feb 2023 15:39:43 -0500 Subject: [PATCH 3/8] wip --- avAdmin/admin-controller/admin-controller.js | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index fe64e157..dad16a21 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -127,22 +127,15 @@ angular return deferred.promise; } - function adminImportFile(fileHandle) + function adminImportFile(file) { - fileHandle - .getFile() + file + .text() .then( - function (file) + function (fileText) { - file - .text() - .then( - function (fileText) - { - ElectionsApi.currentElections = JSON.parse(fileText); - $state.go("admin.create"); - } - ); + ElectionsApi.currentElections = JSON.parse(fileText); + $state.go("admin.create"); } ); } @@ -160,7 +153,10 @@ angular function (data) { var fileHandle = data[0]; - adminImportFile(fileHandle); + + fileHandle + .getFile() + .then(adminImportFile); } ); From ebc0c2347c2cb08bb66da7f809cebb69ac83a5cc Mon Sep 17 00:00:00 2001 From: Findeton Date: Wed, 8 Feb 2023 15:47:47 -0500 Subject: [PATCH 4/8] wip --- avAdmin/admin-controller/admin-controller.js | 19 +------------------ .../admin-sidebar-directive.js | 1 - 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index dad16a21..dedbc570 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -142,24 +142,7 @@ angular function adminImport() { - if (!$window.showOpenFilePicker) - { - document.querySelector("#side-import-file").click(); - } - - $window - .showOpenFilePicker() - .then( - function (data) - { - var fileHandle = data[0]; - - fileHandle - .getFile() - .then(adminImportFile); - } - ); - + document.querySelector("#side-import-file").click(); var el = ElectionsApi.templateEl(); $scope.current = el; diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js index 17ab4b28..dfaea277 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js @@ -45,7 +45,6 @@ angular.module('avAdmin') scope.admin = admin; scope.isAdmin = $cookies.get("isAdmin" + postfix); scope.active = attrs.active; - scope.showImport = !!$window.showOpenFilePicker; scope.isEditingDraft = DraftElection.isEditingDraft; scope.globalPerms = { val: '' }; scope.selectFile = function () { From a4afc2f70140a09f3e919205bdcf46a2a39b49fc Mon Sep 17 00:00:00 2001 From: Findeton Date: Wed, 8 Feb 2023 16:00:09 -0500 Subject: [PATCH 5/8] fix --- avAdmin/admin-controller/admin-controller.js | 4 +++- .../admin-sidebar-directive/admin-sidebar-directive.html | 2 +- .../admin-sidebar-directive/admin-sidebar-directive.js | 8 -------- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index dedbc570..9544ea8b 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -127,8 +127,10 @@ angular return deferred.promise; } - function adminImportFile(file) + function adminImportFile(element) { + var file = element.files[0]; + file .text() .then( diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html index f9e110f5..81a81207 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.html @@ -38,7 +38,7 @@ id="side-import-file" class="hidden" aria-hidden="true" - onchange="angular.element(this).scope().uploadFile(this)"/> + onchange="angular.element(this).scope().adminImportFile(this)"/> diff --git a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js index dfaea277..5cd03c87 100644 --- a/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js +++ b/avAdmin/admin-sidebar-directive/admin-sidebar-directive.js @@ -47,14 +47,6 @@ angular.module('avAdmin') scope.active = attrs.active; scope.isEditingDraft = DraftElection.isEditingDraft; scope.globalPerms = { val: '' }; - scope.selectFile = function () { - document.querySelector("#side-import-file").click(); - }; - scope.uploadFile = function (element) { - var f = element.files[0]; - console.log(f); - scope.adminImportFile(f); - }; // update perms ElectionsApi From 32946920beadc0f346cb3ff84e22314bc3f67b4c Mon Sep 17 00:00:00 2001 From: Findeton Date: Thu, 9 Feb 2023 16:06:15 -0500 Subject: [PATCH 6/8] add service --- avAdmin/admin-controller/admin-controller.js | 6 +- avAdmin/admin-directives/create/create.js | 93 +------------- avAdmin/automatic-ids-service.js | 120 +++++++++++++++++++ index.html | 1 + 4 files changed, 128 insertions(+), 92 deletions(-) create mode 100644 avAdmin/automatic-ids-service.js diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index 9544ea8b..ec63c64f 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -32,7 +32,8 @@ angular $timeout, $q, $window, - $modal + $modal, + AutomaticIds ) { var id = $stateParams.id; $scope.electionId = id; @@ -136,7 +137,8 @@ angular .then( function (fileText) { - ElectionsApi.currentElections = JSON.parse(fileText); + var elections = JSON.parse(fileText); + ElectionsApi.currentElections = AutomaticIds.fillInElectionIds(elections); $state.go("admin.create"); } ); diff --git a/avAdmin/admin-directives/create/create.js b/avAdmin/admin-directives/create/create.js index 1042388d..ac2c3660 100644 --- a/avAdmin/admin-directives/create/create.js +++ b/avAdmin/admin-directives/create/create.js @@ -35,7 +35,8 @@ angular.module('avAdmin') CheckerService, ElectionCreation, CsvLoad, - MustExtraFieldsService) + MustExtraFieldsService, + AutomaticIds) { /** * @returns true if the url with the specific title and url appears in the @@ -1227,94 +1228,6 @@ angular.module('avAdmin') deferred.resolve(scope.elections[electionIndex]); } - /** - * If the elections are using negative numbers, find the existing election - * with the highest election id and replace the negative numbers with - * higher election ids. - */ - function fillInElectionIds(elections) { - var deferred = $q.defer(); - var promise = deferred.promise; - - // check if there are negative election ids - var hasNegativeIds = undefined !== elections.find(function (el) { - return _.isNumber(el.id) && el.id <= 0; - }); - - /* jshint ignore:start */ - - if (elections.length > 1 || hasNegativeIds) { - - // Find highest election id - Authmethod.highestEvent() - .then( - function onSuccess(response) { - var highestId = response.data.highest_id; - var newIdsMap = {}; - - // map negative ids to new election ids - for (var idx = 0; idx < elections.length; idx++) { - var electionId = elections[idx].id; - if (_.isNumber(electionId) && electionId > 0) { - newIdsMap[electionId] = electionId; - } else { - newIdsMap[electionId] = highestId + idx + 1; - } - } - - // replace election ids with new ids - for (var index = 0; index < elections.length; index++) { - var election = elections[index]; - // replace the election id - election.id = newIdsMap[election.id]; - - // replace the parent id - if (election.parent_id) { - election.parent_id = newIdsMap[election.parent_id] || election.parent_id; - } - - // replace ids for virtualSubelections - if (election.virtualSubelections) { - election.virtualSubelections = election.virtualSubelections.map(function (e) { - return newIdsMap[e] || e; - }); - } - - // replace ids in the children elections structure - if (election.children_election_info && - election.children_election_info.natural_order) { - election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (e) { - return newIdsMap[e] || e; - }); - } - if (election.children_election_info && - election.children_election_info.presentation && - election.children_election_info.presentation.categories) { - election.children_election_info.presentation.categories = - election.children_election_info.presentation.categories.map(function (category) { - if (category.events) { - category.events = category.events.map(function (event) { - if (event.event_id) { - event.event_id = newIdsMap[event.event_id] || event.event_id; - } - return event; - }); - } - return category; - }); - } - } - deferred.resolve(elections); - }, - deferred.reject - ); - } else { - deferred.resolve(elections); - } - /* jshint ignore:end */ - return promise; - } - function addElection(electionIndex) { var deferred = $q.defer(); @@ -1378,7 +1291,7 @@ angular.module('avAdmin') function (data) { var elections = angular.fromJson(data.electionJson); - return fillInElectionIds(elections); + return AutomaticIds.fillInElectionIds(elections); } ).then( function (data) diff --git a/avAdmin/automatic-ids-service.js b/avAdmin/automatic-ids-service.js new file mode 100644 index 00000000..267dbe57 --- /dev/null +++ b/avAdmin/automatic-ids-service.js @@ -0,0 +1,120 @@ +/** + * This file is part of admin-console. + * Copyright (C) 2022 Sequent Tech Inc + + * admin-console is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License. + + * admin-console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with admin-console. If not, see . +**/ + +/** + * Service to manage the keys ceremony modal steps. + */ +angular +.module('avAdmin') +.factory('AutomaticIds', function( + $q, + Authmethod, +) +{ + var service = { + }; + + /** + * If the elections are using negative numbers, find the existing election + * with the highest election id and replace the negative numbers with + * higher election ids. + */ + service.fillInElectionIds = function (elections) { + var deferred = $q.defer(); + var promise = deferred.promise; + + // check if there are negative election ids + var hasNegativeIds = undefined !== elections.find(function (el) { + return _.isNumber(el.id) && el.id <= 0; + }); + + /* jshint ignore:start */ + + if (elections.length > 1 || hasNegativeIds) { + + // Find highest election id + Authmethod.highestEvent() + .then( + function onSuccess(response) { + var highestId = response.data.highest_id; + var newIdsMap = {}; + + // map negative ids to new election ids + for (var idx = 0; idx < elections.length; idx++) { + var electionId = elections[idx].id; + if (_.isNumber(electionId) && electionId > 0) { + newIdsMap[electionId] = electionId; + } else { + newIdsMap[electionId] = highestId + idx + 1; + } + } + + // replace election ids with new ids + for (var index = 0; index < elections.length; index++) { + var election = elections[index]; + // replace the election id + election.id = newIdsMap[election.id]; + + // replace the parent id + if (election.parent_id) { + election.parent_id = newIdsMap[election.parent_id] || election.parent_id; + } + + // replace ids for virtualSubelections + if (election.virtualSubelections) { + election.virtualSubelections = election.virtualSubelections.map(function (e) { + return newIdsMap[e] || e; + }); + } + + // replace ids in the children elections structure + if (election.children_election_info && + election.children_election_info.natural_order) { + election.children_election_info.natural_order = election.children_election_info.natural_order.map(function (e) { + return newIdsMap[e] || e; + }); + } + if (election.children_election_info && + election.children_election_info.presentation && + election.children_election_info.presentation.categories) { + election.children_election_info.presentation.categories = + election.children_election_info.presentation.categories.map(function (category) { + if (category.events) { + category.events = category.events.map(function (event) { + if (event.event_id) { + event.event_id = newIdsMap[event.event_id] || event.event_id; + } + return event; + }); + } + return category; + }); + } + } + deferred.resolve(elections); + }, + deferred.reject + ); + } else { + deferred.resolve(elections); + } + /* jshint ignore:end */ + return promise; + }; + + return service; +}); \ No newline at end of file diff --git a/index.html b/index.html index 13bd517d..88d726f7 100755 --- a/index.html +++ b/index.html @@ -135,6 +135,7 @@ + From daa1bb43a9cc60dcdf04e5a35a19d003d39e2aff Mon Sep 17 00:00:00 2001 From: Findeton Date: Thu, 9 Feb 2023 17:06:57 -0500 Subject: [PATCH 7/8] wip --- avAdmin/admin-controller/admin-controller.js | 10 ++++++++-- avAdmin/automatic-ids-service.js | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/avAdmin/admin-controller/admin-controller.js b/avAdmin/admin-controller/admin-controller.js index ec63c64f..80fa0f26 100644 --- a/avAdmin/admin-controller/admin-controller.js +++ b/avAdmin/admin-controller/admin-controller.js @@ -138,8 +138,14 @@ angular function (fileText) { var elections = JSON.parse(fileText); - ElectionsApi.currentElections = AutomaticIds.fillInElectionIds(elections); - $state.go("admin.create"); + AutomaticIds.fillInElectionIds(elections) + .then( + function (fixedElections) + { + ElectionsApi.currentElections = fixedElections; + $state.go("admin.create"); + } + ); } ); } diff --git a/avAdmin/automatic-ids-service.js b/avAdmin/automatic-ids-service.js index 267dbe57..1deeb70a 100644 --- a/avAdmin/automatic-ids-service.js +++ b/avAdmin/automatic-ids-service.js @@ -22,7 +22,7 @@ angular .module('avAdmin') .factory('AutomaticIds', function( $q, - Authmethod, + Authmethod ) { var service = { From c7a6f623dde51cec3a8e00f71cf142bc6f6492aa Mon Sep 17 00:00:00 2001 From: Findeton Date: Mon, 13 Feb 2023 08:24:30 -0500 Subject: [PATCH 8/8] fix tests --- avAdmin/admin-controller/admin-controller-spec.js | 1 + 1 file changed, 1 insertion(+) diff --git a/avAdmin/admin-controller/admin-controller-spec.js b/avAdmin/admin-controller/admin-controller-spec.js index 1b96eba9..4117d22a 100644 --- a/avAdmin/admin-controller/admin-controller-spec.js +++ b/avAdmin/admin-controller/admin-controller-spec.js @@ -39,6 +39,7 @@ describe("Admin Controler tests", function () { $i18next: undefined, $cookies: undefined, ConfigService: { helpUrl: 'http://sequentech.io', showSuccessAction: true }, + AutomaticIds: undefined, ElectionsApi: { autoreloadStats: function() {}, getElection: function() {