From fc5fff570323722a8c7cc7cf0157279bbe0a9f5d Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Fri, 18 Sep 2020 15:27:21 +0200 Subject: [PATCH] Added first files --- ajax_list.js | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++ posting2.js | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 417 insertions(+) create mode 100644 ajax_list.js create mode 100644 posting2.js diff --git a/ajax_list.js b/ajax_list.js new file mode 100644 index 0000000..0f1508c --- /dev/null +++ b/ajax_list.js @@ -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=' \ +
'; + + // \ + // + + $(this).html(table_base); + + //Connect with ajax. + if(!options.hasOwnProperty("url")) { + + + list_tr=' \ + No ajax url defined \ + '; + + + + $(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=['', '']; + + 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=''; + + for(i in data.fields) + { + + if(data.fields[i][1]) + { + + list_tr+=''+data.fields[i][0]+''; + + } + else + { + + list_tr+=''+data.fields[i][0]+''; + + } + + } + + list_tr+=''; + + $('#'+name_list).append(list_tr); + + list_tr=''; + + for(i in data.rows) + { + + list_tr+=''; + + for(n in data.rows[i]) { + + + if(exclude_list.indexOf(n)==-1) + { + + list_tr+=''+data.rows[i][n]+''; + + } + } + + list_tr+=''; + + } + $('#'+name_list).append(list_tr); + + block_name.prepend('

'+data.html_pages+'

'); + block_name.append('

'+data.html_pages+'

'); + + // 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 ); diff --git a/posting2.js b/posting2.js new file mode 100644 index 0000000..7d5c89f --- /dev/null +++ b/posting2.js @@ -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 );