Added first files
This commit is contained in:
commit
fc5fff5703
2 changed files with 417 additions and 0 deletions
209
ajax_list.js
Normal file
209
ajax_list.js
Normal file
|
|
@ -0,0 +1,209 @@
|
||||||
|
$=jQuery;
|
||||||
|
|
||||||
|
(function( $ ){
|
||||||
|
|
||||||
|
/* Add csrf token */
|
||||||
|
|
||||||
|
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean */
|
||||||
|
|
||||||
|
$.fn.ajaxList = function (name_list, options)
|
||||||
|
{
|
||||||
|
table_base='<table class="table_list" id="'+name_list+'"> \
|
||||||
|
</table>';
|
||||||
|
|
||||||
|
// <tr id="'+name_list+'_row_list" class="row_list" style="display: none;"> \
|
||||||
|
// </tr>
|
||||||
|
|
||||||
|
$(this).html(table_base);
|
||||||
|
|
||||||
|
//Connect with ajax.
|
||||||
|
if(!options.hasOwnProperty("url")) {
|
||||||
|
|
||||||
|
|
||||||
|
list_tr='<tr class="title_list"> \
|
||||||
|
<td>No ajax url defined</td> \
|
||||||
|
</tr>';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$(this).children('#'+name_list).append(list_tr);
|
||||||
|
|
||||||
|
//alert(new_title_list.attr('id'));
|
||||||
|
/*
|
||||||
|
$(new_title_list).html('');*/
|
||||||
|
|
||||||
|
//new_title_list.show();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exclude_list=[];
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("exclude_list")) {
|
||||||
|
|
||||||
|
exclude_list=options.exclude_list;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
this.order_field=0;
|
||||||
|
this.order=0;
|
||||||
|
|
||||||
|
arr_icon=['<i class="fa fa-arrow-down asc_icon" aria-hidden="true"></i>', '<i class="fa fa-arrow-up desc_icon" aria-hidden="true"></i>'];
|
||||||
|
|
||||||
|
var func=this;
|
||||||
|
|
||||||
|
this.updateAjax=function (name_list, options, position) {
|
||||||
|
|
||||||
|
// Get ajax, the ajax request need give a json request with name titles and fields value.
|
||||||
|
|
||||||
|
$('#'+name_list).html('');
|
||||||
|
|
||||||
|
$('.pages').remove();
|
||||||
|
|
||||||
|
$('.link_pages').off();
|
||||||
|
$('.change_order').off();
|
||||||
|
|
||||||
|
extra_data={}
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("extra_data")) {
|
||||||
|
|
||||||
|
extra_data=options.extra_data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
data={position: position, order_field: func.order_field, order: func.order};
|
||||||
|
|
||||||
|
for(i in extra_data) {
|
||||||
|
|
||||||
|
data[i]=extra_data[i];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var block_name=$('#'+name_list).parent();
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: options.url,
|
||||||
|
data: data,
|
||||||
|
success: function (data) {
|
||||||
|
|
||||||
|
order_field='';
|
||||||
|
|
||||||
|
list_tr='<tr class="title_list">';
|
||||||
|
|
||||||
|
for(i in data.fields)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(data.fields[i][1])
|
||||||
|
{
|
||||||
|
|
||||||
|
list_tr+='<td><a href="#'+i+'" class="change_order" id="field_list_'+i+'">'+data.fields[i][0]+'</a></td>';
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
list_tr+='<td>'+data.fields[i][0]+'</td>';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
list_tr+='</tr>';
|
||||||
|
|
||||||
|
$('#'+name_list).append(list_tr);
|
||||||
|
|
||||||
|
list_tr='';
|
||||||
|
|
||||||
|
for(i in data.rows)
|
||||||
|
{
|
||||||
|
|
||||||
|
list_tr+='<tr class="row_list">';
|
||||||
|
|
||||||
|
for(n in data.rows[i]) {
|
||||||
|
|
||||||
|
|
||||||
|
if(exclude_list.indexOf(n)==-1)
|
||||||
|
{
|
||||||
|
|
||||||
|
list_tr+='<td class="'+n+'_td">'+data.rows[i][n]+'</td>';
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
list_tr+='</tr>';
|
||||||
|
|
||||||
|
}
|
||||||
|
$('#'+name_list).append(list_tr);
|
||||||
|
|
||||||
|
block_name.prepend('<p class="pages">'+data.html_pages+'</p>');
|
||||||
|
block_name.append('<p class="pages">'+data.html_pages+'</p>');
|
||||||
|
|
||||||
|
// Prepare defaults of fields orders
|
||||||
|
|
||||||
|
|
||||||
|
$('#field_list_'+func.order_field).append(' '+arr_icon[func.order]);
|
||||||
|
|
||||||
|
// Prepare button events
|
||||||
|
|
||||||
|
$('.link_pages').click(function (e) {
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
position=parseInt($(this).attr('href').replace('#?begin_page=', ''));
|
||||||
|
|
||||||
|
if(isNaN(position))
|
||||||
|
{
|
||||||
|
|
||||||
|
position=0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func.updateAjax(name_list, options, position);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.change_order').click(function (e) {
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
func.order_field=$(this).attr('href').replace('#', '');
|
||||||
|
|
||||||
|
//alert($(this).children('i').attr('class'));
|
||||||
|
|
||||||
|
if($(this).children('i').hasClass('asc_icon'))
|
||||||
|
{
|
||||||
|
|
||||||
|
func.order=1;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
func.order=0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func.updateAjax(name_list, options, position);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
},
|
||||||
|
error: function (data) {
|
||||||
|
console.log('Cannot connect to list source');
|
||||||
|
},
|
||||||
|
dataType: 'json'
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
this.updateAjax(name_list, options, 0, '');
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
})( jQuery );
|
||||||
208
posting2.js
Normal file
208
posting2.js
Normal file
|
|
@ -0,0 +1,208 @@
|
||||||
|
|
||||||
|
|
||||||
|
(function( $ ){
|
||||||
|
|
||||||
|
/* Add csrf token */
|
||||||
|
|
||||||
|
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean */
|
||||||
|
|
||||||
|
$.fn.sendPost = function (options)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(!options.hasOwnProperty("url")) {
|
||||||
|
|
||||||
|
throw 'Error: you need an url for use sendPost';
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!options.hasOwnProperty("loading")) {
|
||||||
|
|
||||||
|
options['loading']='#loading';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$(this).on('submit', function (e) {
|
||||||
|
|
||||||
|
//Get data of form
|
||||||
|
|
||||||
|
form=this;
|
||||||
|
|
||||||
|
/*if($(this).attr('enctype'))
|
||||||
|
{*/
|
||||||
|
if(options.separated_data)
|
||||||
|
{
|
||||||
|
|
||||||
|
data[options.separated_data]=new FormData($(this)[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
data=new FormData($(this)[0]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
xhrFields={
|
||||||
|
withCredentials: true
|
||||||
|
};
|
||||||
|
|
||||||
|
/*}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
if(options.separated_data)
|
||||||
|
{
|
||||||
|
|
||||||
|
data[options.separated_data]=$(this).serializeArray();
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
data=$(this).serializeArray().reduce(function(obj, item) {
|
||||||
|
obj[item.name] = item.value;
|
||||||
|
return obj;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
xhrFields={
|
||||||
|
};
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if(!data.hasOwnProperty("csrf_token")) {
|
||||||
|
|
||||||
|
data['csrf_token']=$('#csrf_token_generic').val();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Hide form and show the time icon
|
||||||
|
|
||||||
|
$(this).find('.error').hide();
|
||||||
|
//$(this).hide();
|
||||||
|
$(options.loading).show();
|
||||||
|
|
||||||
|
/*$(this).find('input').prop("disabled", true);
|
||||||
|
$(this).find('select').prop("disabled", true);*/
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("pre_callback")) {
|
||||||
|
|
||||||
|
options.pre_callback(data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Ajax
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: "POST",
|
||||||
|
url: options.url,
|
||||||
|
data: data,
|
||||||
|
encoding: "UTF-8",
|
||||||
|
xhrFields: xhrFields,
|
||||||
|
cache:false,
|
||||||
|
contentType: false,
|
||||||
|
processData: false,
|
||||||
|
success: function (data) {
|
||||||
|
|
||||||
|
$(options.loading).hide();
|
||||||
|
|
||||||
|
|
||||||
|
if(data.error) {
|
||||||
|
|
||||||
|
if(!data.hasOwnProperty("error_csrf"))
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
if(data.hasOwnProperty("csrf_token"))
|
||||||
|
{
|
||||||
|
|
||||||
|
$('.csrf_token').attr('value', data.csrf_token);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
console.log('Error');
|
||||||
|
|
||||||
|
if(data.hasOwnProperty('error_form'))
|
||||||
|
{
|
||||||
|
for(i in data.error_form) {
|
||||||
|
|
||||||
|
$(i).html(data.error_form[i]);
|
||||||
|
$(i).show();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if(data.hasOwnProperty('form'))
|
||||||
|
{
|
||||||
|
|
||||||
|
for(i in data.form) {
|
||||||
|
|
||||||
|
$('#'+i+'_error').html(data.form[i]);
|
||||||
|
$('#'+i+'_error').show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("error_data")) {
|
||||||
|
|
||||||
|
options.error_data(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
$(form).show();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
alert('You session expired, the page was reload by security');
|
||||||
|
location.reload();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// If ok then post successful task.
|
||||||
|
|
||||||
|
if(options.hasOwnProperty("success")) {
|
||||||
|
|
||||||
|
options.success(data);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function (data) {
|
||||||
|
|
||||||
|
//Show the form again
|
||||||
|
|
||||||
|
if(data.hasOwnProperty("csrf_token"))
|
||||||
|
{
|
||||||
|
|
||||||
|
$('.csrf_token').attr('value', data.csrf_token);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$(options.loading).hide();
|
||||||
|
alert('Error: '+data.status+' '+data.statusText);
|
||||||
|
|
||||||
|
|
||||||
|
},
|
||||||
|
dataType: 'json'
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
})( jQuery );
|
||||||
Loading…
Add table
Add a link
Reference in a new issue