1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-14 14:12:29 +01:00
polr/public/js/AdminCtrl.js

317 lines
10 KiB
JavaScript
Raw Normal View History

2016-08-02 01:05:22 +02:00
polr.controller('AdminCtrl', function($scope, $compile) {
$scope.state = {
showNewUserWell: false
};
$scope.datatables = {};
2016-08-02 01:05:22 +02:00
$scope.syncHash = function() {
var url = document.location.toString();
if (url.match('#')) {
$('.admin-nav a[href=#' + url.split('#')[1] + ']').tab('show');
2016-08-02 01:05:22 +02:00
}
};
// Initialise Datatables elements
2016-10-01 04:10:58 +02:00
$scope.initTables = function () {
var datatables_config = {
'autoWidth': false,
'processing': true,
'serverSide': true,
'drawCallback': function () {
// Compile Angular bindings on each draw
$compile($(this))($scope);
}
};
if ($('#admin_users_table').length) {
$scope.datatables['admin_users_table'] = $('#admin_users_table').DataTable($.extend({
2016-10-01 04:10:58 +02:00
"ajax": BASE_API_PATH + 'admin/get_admin_users',
"columns": [
2016-10-03 23:30:17 +02:00
{className: 'wrap-text', data: 'username', name: 'username'},
{className: 'wrap-text', data: 'email', name: 'email'},
2016-10-01 04:10:58 +02:00
{data: 'created_at', name: 'created_at'},
{data: 'toggle_active', name: 'toggle_active'},
{data: 'api_action', name: 'api_action', orderable: false, searchable: false},
{data: 'change_role', name: 'change_role'},
{data: 'delete', name: 'delete', orderable: false, searchable: false}
2016-10-01 04:10:58 +02:00
]
}, datatables_config));
}
if ($('#admin_links_table').length) {
$scope.datatables['admin_links_table'] = $('#admin_links_table').DataTable($.extend({
2016-10-01 04:10:58 +02:00
"ajax": BASE_API_PATH + 'admin/get_admin_links',
"columns": [
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
{data: 'clicks', name: 'clicks'},
{data: 'created_at', name: 'created_at'},
{data: 'creator', name: 'creator'},
2016-10-01 04:10:58 +02:00
{data: 'disable', name: 'disable', orderable: false, searchable: false},
{data: 'delete', name: 'delete', orderable: false, searchable: false}
]
}, datatables_config));
}
$scope.datatables['user_links_table'] = $('#user_links_table').DataTable($.extend({
2016-10-02 02:30:06 +02:00
"ajax": BASE_API_PATH + 'admin/get_user_links',
"columns": [
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
{data: 'clicks', name: 'clicks'},
{data: 'created_at', name: 'created_at'}
]
}, datatables_config));
2016-10-01 04:10:58 +02:00
};
// Append modals to Angular root
$scope.appendModal = function(html, id) {
2016-08-02 01:05:22 +02:00
id = esc_selector(id);
$(".ng-root").append(html);
var modal_ele = $("#" + id);
modal_ele.append(html);
modal_ele.modal();
$compile(modal_ele)($scope);
$("body").delegate("#" + id, "hidden.bs.modal", function() {
2016-08-02 01:05:22 +02:00
modal_ele.remove();
});
};
// Hide table rows
$scope.hideRow = function(el, msg) {
var row = el.parent().parent();
toastr.success(msg, "Success");
row.fadeOut('slow');
2016-08-02 01:05:22 +02:00
};
2016-10-13 21:19:01 +02:00
/*
User Management
*/
$scope.toggleUserActiveStatus = function($event) {
var el = $($event.target);
var user_id = el.data('user-id');
apiCall('admin/toggle_user_active', {
'user_id': user_id,
}, function(new_status) {
var text = (new_status == 1) ? 'Active' : 'Inactive';
el.text(text);
if (el.hasClass('btn-success')) {
el.removeClass('btn-success').addClass('btn-danger');
}
else {
el.removeClass('btn-danger').addClass('btn-success');
}
});
}
2016-10-13 21:19:01 +02:00
$scope.checkNewUserFields = function() {
var response = true;
2016-10-01 23:54:22 +02:00
2016-10-13 21:19:01 +02:00
$('.new-user-fields input').each(function () {
console.log($(this).attr('id'));
if ($(this).val().trim() == '' || response == false) {
response = false;
}
});
2016-10-01 23:54:22 +02:00
2016-10-13 21:19:01 +02:00
return response;
2016-10-01 23:54:22 +02:00
}
$scope.addNewUser = function($event) {
// Create a new user
2016-10-01 23:54:22 +02:00
var username = $('#new-username').val();
var user_password = $('#new-user-password').val();
var user_email = $('#new-user-email').val();
var user_role = $('#new-user-role').val();
2016-10-01 23:54:22 +02:00
2016-10-13 21:19:01 +02:00
if (!$scope.checkNewUserFields()) {
$('#new-user-status').text('Fields cannot be empty.');
return false;
2016-10-01 23:54:22 +02:00
}
apiCall('admin/add_new_user', {
'username': username,
2016-10-01 23:54:22 +02:00
'user_password': user_password,
'user_email': user_email,
'user_role': user_role,
}, function(result) {
toastr.success("User " + username + " successfully created.", "Success");
$('#new-user-form').clearForm();
$scope.datatables['admin_users_table'].ajax.reload();
}, function () {
$('#new-user-status').text('An error occurred. Try again later.').show();
2016-10-01 23:54:22 +02:00
});
}
// Delete user
$scope.deleteUser = function($event, user_id) {
2016-08-02 01:05:22 +02:00
var el = $($event.target);
apiCall('admin/delete_user', {
'user_id': user_id,
}, function(new_status) {
$scope.hideRow(el, 'User successfully deleted.');
2016-08-02 01:05:22 +02:00
});
};
$scope.changeUserRole = function(role, user_id) {
apiCall('admin/change_user_role', {
'user_id': user_id,
'role': role,
}, function(result) {
toastr.success("User role successfully changed.", "Success");
});
};
// Generate new API key for user_id
2016-08-02 01:05:22 +02:00
$scope.generateNewAPIKey = function($event, user_id, is_dev_tab) {
var el = $($event.target);
var status_display_elem = el.prevAll('.status-display');
if (is_dev_tab) {
status_display_elem = el.parent().prev().children();
}
apiCall('admin/generate_new_api_key', {
'user_id': user_id,
}, function(new_status) {
2016-08-02 01:05:22 +02:00
if (status_display_elem.is('input')) {
status_display_elem.val(new_status);
} else {
2016-08-02 01:05:22 +02:00
status_display_elem.text(new_status);
}
2016-10-03 23:48:51 +02:00
$('a#api_info_btn_' + user_id).attr('data-api-key', new_status);
2016-08-02 01:05:22 +02:00
});
};
// Toggle API access status
2016-08-02 01:05:22 +02:00
$scope.toggleAPIStatus = function($event, user_id) {
var el = $($event.target);
var status_display_elem = el.prevAll('.status-display');
apiCall('admin/toggle_api_active', {
'user_id': user_id,
}, function(new_status) {
$('a#api_info_btn_' + user_id).attr('data-api-active', new_status);
2016-08-02 01:05:22 +02:00
new_status = res_value_to_text(new_status);
status_display_elem.text(new_status);
});
};
// Update user API quotas
$scope.updateAPIQuota = function($event, user_id) {
var el = $($event.target);
var new_quota = el.prevAll('.api-quota').val();
apiCall('admin/edit_api_quota', {
'user_id': user_id,
'new_quota': parseInt(new_quota)
}, function(next_action) {
2016-10-03 23:48:51 +02:00
$('a#api_info_btn_' + user_id).attr('data-api-quota', new_quota);
toastr.success("Quota successfully changed.", "Success");
});
2016-10-01 04:10:58 +02:00
};
// Open user API settings menu
2016-10-03 23:48:51 +02:00
$scope.openAPIModal = function($event, username, user_id) {
2016-08-02 01:05:22 +02:00
var el = $($event.target);
2016-10-13 21:19:01 +02:00
api_active = $('a#api_info_btn_' + user_id).attr('data-api-active');
2016-10-03 23:48:51 +02:00
api_key = $('a#api_info_btn_' + user_id).attr('data-api-key');
api_quota = $('a#api_info_btn_' + user_id).attr('data-api-quota');
2016-08-02 01:05:22 +02:00
var markup = $('#api-modal-template').html();
2016-08-02 01:05:22 +02:00
var modal_id = "api-modal-" + username;
var modal_context = {
id: modal_id,
api_key: api_key,
api_active: parseInt(api_active),
api_quota: api_quota,
user_id: user_id,
title: "API Information for " + username,
body: markup
};
var mt_html = $scope.modal_template(modal_context);
var compiled_mt = Handlebars.compile(mt_html);
mt_html = compiled_mt(modal_context);
$scope.appendModal(mt_html, modal_id);
2016-10-01 04:10:58 +02:00
};
2016-08-02 01:05:22 +02:00
2016-10-13 21:19:01 +02:00
/*
Link Management
*/
// Delete link
$scope.deleteLink = function($event, link_ending) {
var el = $($event.target);
apiCall('admin/delete_link', {
'link_ending': link_ending,
}, function(new_status) {
$scope.hideRow(el, 'Link successfully deleted.');
2016-10-13 21:19:01 +02:00
});
};
// Disable and enable links
$scope.toggleLink = function($event, link_ending) {
var el = $($event.target);
var curr_action = el.text();
apiCall('admin/toggle_link', {
'link_ending': link_ending,
}, function(next_action) {
toastr.success(curr_action + " was successful.", "Success");
if (next_action == 'Disable') {
el.removeClass('btn-success');
el.addClass('btn-danger');
} else {
el.removeClass('btn-danger');
el.addClass('btn-success');
}
el.text(next_action);
});
};
/*
Initialisation
*/
// Initialise AdminCtrl
$scope.init = function() {
var modal_source = $("#modal-template").html();
2016-08-02 01:05:22 +02:00
$scope.modal_template = Handlebars.compile(modal_source);
$('.admin-nav a').click(function(e) {
2016-08-02 01:05:22 +02:00
e.preventDefault();
$(this).tab('show');
});
$scope.syncHash();
$(window).on('hashchange', function() {
2016-08-02 01:05:22 +02:00
$scope.syncHash();
});
$("a[href^=#]").on("click", function(e) {
history.pushState({}, '', this.href);
});
2016-10-01 04:10:58 +02:00
$scope.initTables();
};
2016-08-02 01:05:22 +02:00
$scope.init();
});