You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

160 lines
5.7 KiB

$(document).ready(function () {
$("#modal_create_news").on('click', function() {
$("#create_news_modal").modal('toggle')
});
$("#create_news").on("click", function() {
let news = {
title: $("#title_news").val(),
text: $("#text_news").val(),
image: '',
file: ''
}
//img = $('#image_news_file')[0].files[0]
//if (img) {
//
// var reader = new FileReader();
// reader.readAsDataURL(img);
// reader.onload = function() {
// let url=this.result.substring(this.result.indexOf(',')+1);
// news.file = 'data:image/png;base64,' + url
// console.log(news)
// }
//}
var stav = true
if ($("#image_news").val() != '') {
var RegExp = /^((ftp|http|https):\/\/)?(www\.)?([A-Za-zА-Яа-я0-9]{1}[A-Za-zА-Яа-я0-9\-]*\.?)*\.{1}[A-Za-zА-Яа-я0-9-]{2,8}(\/([\w#!:.?+=&%@!\-\/])*)?/;
if(RegExp.test($("#image_news").val())){
news.image = $("#image_news").val()
stav = true
} else {
stav = false
$('#exception').slideDown(500);
$('#exception').delay(2000).slideUp();
}
} else {
stav = true
}
if (news.title != '' && news.text != '') {
if (stav == true) {
$.ajax({
type: 'POST',
url: '/add_news',
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
data: JSON.stringify(news),
success: (data ) => {
if (data['success'] == true) {
$("#create_news_modal").modal('hide');
location.reload();
}
}
})
}
} else {
$('#exception_data').slideDown(500);
$('#exception_data').delay(2000).slideUp();
}
});
$('.remove_news').on('click', function() {
//console.log($(this)[0].attributes);
//console.log($(this)[0].attributes['id-data_news']['value']);
var ind = $(this)[0].attributes['id-data_news']['value'];
data = {
'ind': ind
}
$.ajax({
type: "POST",
url: "/delete_news",
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
data: JSON.stringify(data),
success: (data ) => {
if (data['success'] == true) {
location.reload()
}
}
})
});
$(".edit_news").on('click', function() {
var ind = $(this)[0].attributes['id-data_news']['value'];
$.ajax({
type: 'GET',
url: '/get_news/'+ind,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: (data) => {
$('#title_news_edit').val(data['title'])
$('#text_news_edit').val(data['text'])
$('#image_news_edit').val(data['url'])
$('#save_edit_news').attr("id-data_card", data['id'])
}
})
$("#edit_news_modal").modal('toggle')
});
$('#save_edit_news').on('click', function() {
let news = {
title: $("#title_news_edit").val(),
text: $("#text_news_edit").val(),
image: '',
file: ''
}
//img = $('#image_news_file')[0].files[0]
//if (img) {
//
// var reader = new FileReader();
// reader.readAsDataURL(img);
// reader.onload = function() {
// let url=this.result.substring(this.result.indexOf(',')+1);
// news.file = 'data:image/png;base64,' + url
// console.log(news)
// }
//}
var stav = true
if ($("#image_news_edit").val() != '') {
var RegExp = /^((ftp|http|https):\/\/)?(www\.)?([A-Za-zА-Яа-я0-9]{1}[A-Za-zА-Яа-я0-9\-]*\.?)*\.{1}[A-Za-zА-Яа-я0-9-]{2,8}(\/([\w#!:.?+=&%@!\-\/])*)?/;
if(RegExp.test($("#image_news_edit").val())){
news.image = $("#image_news_edit").val()
stav = true
} else {
stav = false
$('#exception').slideDown(500);
$('#exception').delay(2000).slideUp();
}
} else {
stav = true
}
var ind = $(this)[0].attributes[2]['value'];
console.log( $(this)[0].attributes[2]['value'])
if (news.title != '' && news.text != '') {
if (stav == true) {
$.ajax({
type: 'POST',
url: '/edit_news/' + ind,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
data: JSON.stringify(news),
success: (data ) => {
if (data['success'] == true) {
$("#edit_news_modal").modal('hide');
location.reload();
}
}
})
}
} else {
$('#exception_data').slideDown(500);
$('#exception_data').delay(2000).slideUp();
}
})
})