Browse Source

Fixed old javascript

pull/10/head
crunch 6 months ago
parent
commit
cc6e545fad
9 changed files with 2327 additions and 66 deletions
  1. +1
    -0
      Consulta_project/landing/static/js/bundle.js
  2. +7
    -3
      Consulta_project/landing/static/js/stats.js
  3. +210
    -0
      Consulta_project/landing/static/js/stats_compiled.js
  4. +33
    -0
      Consulta_project/landing/static/js/stats_test_compiled.js
  5. +3
    -2
      Consulta_project/polls/templates/polls/stats.html
  6. +12
    -13
      babel.config.json
  7. +2033
    -47
      package-lock.json
  8. +27
    -0
      package.json
  9. +1
    -1
      utils/compile_es_to_js.sh

+ 1
- 0
Consulta_project/landing/static/js/bundle.js
File diff suppressed because it is too large
View File


+ 7
- 3
Consulta_project/landing/static/js/stats.js View File

@@ -1,9 +1,12 @@
import "core-js/stable";
import "regenerator-runtime/runtime";

// Selects all Divs that have Questions with answers
question_selectors = document.querySelectorAll("[data-id]");
const question_selectors = document.querySelectorAll("[data-id]");

// API URLs
question_url = "/api/questions/";
answer_url = "/api/answers/";
const question_url = "/api/questions/";
const answer_url = "/api/answers/";

// Pulls QuestionIDs from all Question Divs with answers
function getQuestionIds() {
@@ -111,6 +114,7 @@ async function createCharts() {
createChartForQuestion(q_id)
});
}

$(document).ready(function() {
createCharts()
});

+ 210
- 0
Consulta_project/landing/static/js/stats_compiled.js View File

@@ -0,0 +1,210 @@
"use strict";

require("core-js/stable");

require("regenerator-runtime/runtime");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

// Selects all Divs that have Questions with answers
var question_selectors = document.querySelectorAll("[data-id]"); // API URLs

var question_url = "/api/questions/";
var answer_url = "/api/answers/"; // Pulls QuestionIDs from all Question Divs with answers

function getQuestionIds() {
var question_ids = [];
question_selectors.forEach(function (data_id) {
question_ids.push(data_id.dataset.id);
});
return question_ids;
} // Pulls Question Object from Backend API via given Question ID - this is a middleware function for getting Answer IDs from the returned Question Object


function getQuestion(_x) {
return _getQuestion.apply(this, arguments);
}

function _getQuestion() {
_getQuestion = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(q_id) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return fetch(question_url + q_id).then(function (response) {
return response.json();
});

case 2:
return _context.abrupt("return", _context.sent);

case 3:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _getQuestion.apply(this, arguments);
}

function createChartForQuestion(_x2) {
return _createChartForQuestion.apply(this, arguments);
}

function _createChartForQuestion() {
_createChartForQuestion = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(q_id) {
var labels, series, answers, index;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
labels = [];
series = [];
_context3.next = 4;
return getQuestion(q_id);

case 4:
answers = _context3.sent.answers;
index = 0;
_context3.next = 8;
return answers.forEach( /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(answerId) {
var answer, base_target_id, bar_target_id, pie_target_id;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return fetch(answer_url + answerId);

case 2:
_context2.next = 4;
return _context2.sent.json();

case 4:
answer = _context2.sent;

/* console.log("Labels: " + answer.answer_string + " Series: " + answer.count); */
labels.push(answer.answer_string);
series.push(answer.count);

if (index == answers.length - 1) {
base_target_id = "chart-";
bar_target_id = base_target_id + q_id + "-bar";
pie_target_id = base_target_id + q_id + "-pie";
createBarChart(series, labels, bar_target_id);
createPieChart(series, labels, pie_target_id);
} else {
index++;
}

case 8:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));

return function (_x3) {
return _ref.apply(this, arguments);
};
}());

case 8:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return _createChartForQuestion.apply(this, arguments);
}

function createBarChart(series, labels, target_id) {
/* console.log(series + " " + labels + " " + target_id); */
var ctx = document.getElementById(target_id).getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'bar',
// The data for our dataset
data: {
labels: labels,
datasets: [{
label: 'Number of votes',
backgroundColor: 'rgb(0,188,140)',
borderColor: '#282d32',
data: series
}]
},
// Configuration options go here
options: {
events: null,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}

function createPieChart(series, labels, target_id) {
/* console.log(series + " " + labels + " " + target_id); */
var ctx = document.getElementById(target_id).getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'pie',
// The data for our dataset
data: {
labels: labels,
datasets: [{
backgroundColor: 'rgb(0,188,140)',
borderColor: '#282d32',
data: series
}]
},
// Configuration options go here
options: {
events: null,
tooltips: {
mode: 'point'
}
}
});
} // This function loops through all Divs that have Answers to their question and creates a chart


function createCharts() {
return _createCharts.apply(this, arguments);
}

function _createCharts() {
_createCharts = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
getQuestionIds().forEach(function (q_id) {
createChartForQuestion(q_id);
});

case 1:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return _createCharts.apply(this, arguments);
}

$(document).ready(function () {
createCharts();
});

+ 33
- 0
Consulta_project/landing/static/js/stats_test_compiled.js View File

@@ -0,0 +1,33 @@
"use strict";

window.onload = function () {
function createBarChart(series, labels, target_id) {
/* console.log(series + " " + labels + " " + target_id); */
var ctx = document.getElementById("stats").getContext("2d");
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "bar",
// The data for our dataset
data: {
labels: ["test", "kek", "lel"],
datasets: [{
label: "Number of votes",
backgroundColor: "rgb(0,188,140)",
borderColor: "#282d32",
data: [1, 2, 3]
}]
},
// Configuration options go here
options: {
events: null,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
};

+ 3
- 2
Consulta_project/polls/templates/polls/stats.html View File

@@ -77,6 +77,7 @@
{% endblock content %}

{% block bottom_scripts %}
<script type="text/javascript" src="{% static 'js/stats.js' %}"></script>
<script type="text/javascript" src="{% static 'js/stats_test.js' %}"></script>
<!-- <script type="text/javascript" src="{% static 'js/stats_compiled.js' %}"></script> -->
<script type="text/javascript" src="{% static 'js/bundle.js' %}"></script>
<script type="text/javascript" src="{% static 'js/stats_test_compiled.js' %}"></script>
{% endblock %}

+ 12
- 13
babel.config.json View File

@@ -1,16 +1,15 @@
{
"presets": [
[
"@babel/env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1",
},
"useBuiltIns": "usage",
"presets": [
[
"@babel/env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "43",
"safari": "11.1"
}
]
}
]
}
]
}

+ 2033
- 47
package-lock.json
File diff suppressed because it is too large
View File


+ 27
- 0
package.json View File

@@ -0,0 +1,27 @@
{
"name": "consulta",
"version": "1.0.0",
"description": "[![Build Status](https://drone.reinkober-it.de/api/badges/crunch/Consulta/status.svg)](https://drone.reinkober-it.de/crunch/Consulta)",
"main": "index.js",
"directories": {
"lib": "lib"
},
"dependencies": {
"@babel/polyfill": "^7.8.7",
"core-js": "^3.6.4",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7"
},
"scripts": {},
"repository": {
"type": "git",
"url": "git@git.reinkober-it.de:crunch/Consulta.git"
},
"author": "",
"license": "ISC"
}

+ 1
- 1
utils/compile_es_to_js.sh View File

@@ -1 +1 @@
npx babel ../Consulta_project/landing/static/js --out-dir ../lib --presets=@babel/env
npx babel ../Consulta_project/landing/static/js/stats.js --out-file ../Consulta_project/landing/static/js/stats_compiled.js --presets=@babel/env

Loading…
Cancel
Save