-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
120 lines (107 loc) · 3.85 KB
/
scripts.js
File metadata and controls
120 lines (107 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$(document).ready(function(){
// set the height of cards to equal lengths
function equalizeCardHeights() {
var maxHeight = 0;
var cardContents = $(".card .card-content");
cardContents.each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
cardContents.height(maxHeight);
}
equalizeCardHeights();
// function to get a csrfTokenVal to be passed into the headers
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
// opens the modal pop up window
function openModal(modalSelector) {
$(modalSelector).openModal({
dismissible: false, // Modal can be dismissed by clicking outside of the modal
opacity: .7, // Opacity of modal background
in_duration: 400, // Transition in duration
out_duration: 300, // Transition out duration
});
}
function makeAjaxRequest(url, type, data, dataType, async, csrftoken, success_callback) {
$.ajax({
url: url,
type: type,
data: data || null,
dataType: dataType,
async: async,
success: function (response) {
success_callback(response);
},
headers: {
'X-CSRFToken': csrftoken
}
});
}
function doCommonPostWork(response, selector) {
var msg = JSON.parse(response);
if (msg.error) {
var errorArea = $(selector);
errorArea.html('')
$.each(msg.error, function(errName, description) {
errorArea.append(
'<p style="color: #b71c1c" class="news-title">\
<i class="ion-close-circled"></i> ' + errName + ': ' + description[0] + '</p>'
);
});
} else {
window.location.href = msg.redirect_to;
}
}
// load add to-do form to modal dynamically
$("a.fixed-element").click(function (event) {
event.preventDefault();
openModal('#modal1');
makeAjaxRequest('/add_todo/', 'get', {}, 'html', true, null, function(response) {
$('.modal-content').html(response);
post_todo_form();
});
});
// post the form data
function post_todo_form() {
$("#add_todo_form").submit(function (event) {
event.preventDefault();
var data = { title: $("#id_title").val(), desc: $("#id_desc").val() }
makeAjaxRequest('/add_todo/', 'post', data, null, true, csrftoken, function(response) {
doCommonPostWork(response, '.error-area');
});
});
}
// load edit to-do form to modal dynamically
$(".edit-todo").click(function (event) {
event.preventDefault();
var todo_id = $(this).data("id");
openModal('#modal1');
makeAjaxRequest('/edit_todo/', 'get', {id: todo_id}, 'html', true, null, function(response) {
$('.modal-content').html(response);
edit_todo_form(todo_id);
});
});
// post the form data
function edit_todo_form(todo_id) {
$("#edit_todo_form").submit(function (event) {
event.preventDefault();
var data = { id: todo_id, title: $("#id_title").val(), desc: $("#id_desc").val() }
makeAjaxRequest('/edit_todo/', 'post', data, null, true, csrftoken, function(response) {
doCommonPostWork(response, '.error-area');
});
});
}
});