2019-01-28 21:58:55 +00:00
|
|
|
var app = angular.module('pinTableApp', []);
|
2019-01-30 22:23:14 +00:00
|
|
|
app.controller('pinTableController', function($scope, $http) {
|
2019-02-01 20:57:20 +00:00
|
|
|
$scope.refresh_list = function(){
|
2019-01-30 22:23:14 +00:00
|
|
|
var new_pinlist = [];
|
|
|
|
var temp_pinlist = [];
|
|
|
|
$scope.pinlist.forEach(function (item, index) {
|
|
|
|
if(index % 2 == 0){
|
|
|
|
temp_pinlist.push(item);
|
|
|
|
}else{
|
|
|
|
temp_pinlist.push(item);
|
|
|
|
new_pinlist.push(temp_pinlist);
|
|
|
|
temp_pinlist = []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return new_pinlist;
|
|
|
|
};
|
|
|
|
|
2019-01-30 23:18:37 +00:00
|
|
|
var token = document.getElementsByName("csrfmiddlewaretoken")[0].value;
|
2019-01-30 22:23:14 +00:00
|
|
|
$http.get("/pins/api/")
|
|
|
|
.then(function(response) {
|
|
|
|
$scope.pinlist = response.data.results;
|
2019-02-01 20:57:20 +00:00
|
|
|
$scope.pins = $scope.refresh_list();
|
2019-01-30 22:23:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$scope.change_mode = function (physical, mode_code) {
|
|
|
|
var url = "/pins/api/" + physical;
|
2019-01-30 23:18:37 +00:00
|
|
|
$http.post(url,
|
|
|
|
{mode: mode_code},
|
|
|
|
{headers: {'Content-Type': 'application/json', 'X-CSRFToken': token}})
|
2019-01-30 22:50:27 +00:00
|
|
|
.then(function (response) {
|
2019-02-01 20:57:20 +00:00
|
|
|
if(response.data.operation){
|
2019-01-30 22:50:27 +00:00
|
|
|
$scope.pinlist[physical-1] = response.data.pin;
|
2019-02-01 20:57:20 +00:00
|
|
|
$scope.pins = $scope.refresh_list();
|
2019-01-30 22:50:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2019-02-01 20:57:20 +00:00
|
|
|
var error_msg, footer_msg = '';
|
|
|
|
console.log(err);
|
|
|
|
if(err.status == 403){
|
|
|
|
error_msg = err.data.detail;
|
|
|
|
footer_msg = '<a href="/login">Login</a>';
|
|
|
|
}else{
|
|
|
|
error_msg = err.data.mode
|
|
|
|
}
|
|
|
|
Swal.fire({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Oops...',
|
|
|
|
text: error_msg,
|
|
|
|
footer: footer_msg
|
|
|
|
});
|
|
|
|
$scope.pins = $scope.refresh_list();
|
2019-01-30 22:50:27 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.change_value = function (physical) {
|
|
|
|
var url = "/pins/api/" + physical;
|
|
|
|
var new_value = ($scope.pinlist[physical-1].value) ? 0 : 1
|
2019-01-30 23:18:37 +00:00
|
|
|
$http.post(url,
|
|
|
|
{value: new_value},
|
|
|
|
{headers: {'Content-Type': 'application/json', 'X-CSRFToken': token}})
|
2019-01-30 22:23:14 +00:00
|
|
|
.then(function (response) {
|
2019-02-01 20:57:20 +00:00
|
|
|
if(response.data.operation){
|
2019-01-30 22:23:14 +00:00
|
|
|
$scope.pinlist[physical-1] = response.data.pin;
|
2019-02-01 20:57:20 +00:00
|
|
|
$scope.pins = $scope.refresh_list();
|
2019-01-30 22:23:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2019-02-01 20:57:20 +00:00
|
|
|
var error_msg, footer_msg = '';
|
|
|
|
if(err.status == 403){
|
|
|
|
error_msg = err.data.detail;
|
|
|
|
footer_msg = '<a href="/login">Login</a>';
|
|
|
|
}else{
|
|
|
|
error_msg = err.data.value
|
|
|
|
}
|
|
|
|
Swal.fire({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Oops...',
|
|
|
|
text: error_msg,
|
|
|
|
footer: footer_msg
|
|
|
|
});
|
|
|
|
$scope.pins = $scope.refresh_list();
|
2019-01-30 22:23:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|